Source for file CurlyBracketsFilter.php
Documentation is available at CurlyBracketsFilter.php
- 1: <?php
- 3: /**
- 4: * Nette Framework
- 5: *
- 6: * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
- 7: *
- 8: * This source file is subject to the "Nette license" that is bundled
- 9: * with this package in the file license.txt.
- 10: *
- 11: * For more information please see http://nettephp.com
- 12: *
- 18: */
- 26: /**
- 27: * Compile-time filter CurlyBrackets.
- 28: *
- 32: */
- 34: {
- 35: /** single & double quoted PHP string */
- 38: /** PHP identifier */
- 41: /** curly bracket tag */
- 43: (?P<indent>\n[ \t]*)?
- 44: \\{(?P<macro>[^\\s\'"{}](?>
- 45: \'(?:\\\\.|[^\'\\\\])*\'|"(?:\\\\.|[^"\\\\])*"| # RE_STRING
- 46: [^\'"}]+)*)\\}
- 47: (?P<newline>[\ \t]*(?=\r|\n))?
- 50: /** spcial HTML tag or attribute prefix */
- 71: /**#@+ Context-aware escaping states */
- 78: /**#@-*/
- 82: /**
- 83: * Sets a macro handler.
- 86: */
- 88: {
- 90: }
- 94: /**
- 95: * Returns macro handler.
- 97: */
- 99: {
- 102: }
- 104: }
- 108: /**
- 109: * Invokes filter.
- 112: */
- 114: {
- 115: // context-aware escaping
- 119: // initialize handlers
- 122: // process all {tags} and <tags/>
- 128: }
- 132: /**
- 133: * Searches for curly brackets, HTML tags and attributes.
- 136: */
- 138: {
- 152: preg_match('#^(/?[a-z]+)?(.*?)(\\|[a-z](?:'.self::RE_STRING.'|[^\'"\s]+)*)?$()#is', $matches['macro'], $m2);
- 157: }
- 163: }
- 167: }
- 168: }
- 172: throw new InvalidStateException("Missing end tag </$tag->name> for macro-attribute " . self::HTML_PREFIX . implode(' and ' . self::HTML_PREFIX, array_keys($tag->attrs)) . ".");
- 173: }
- 174: }
- 177: }
- 181: /**
- 182: * Handles CONTEXT_TEXT.
- 183: */
- 185: {
- 187: (?:\n[ \t]*)?<(?P<closing>/?)(?P<tag>[a-z0-9:]+)| ## begin of HTML tag <tag </tag - ignores <!DOCTYPE
- 188: <(?P<comment>!--)| ## begin of HTML comment <!--
- 212: //throw new InvalidStateException("End tag for element '$matches[tag]' which is not open on line $this->line.");
- 216: }
- 223: }
- 225: }
- 229: /**
- 230: * Handles CONTEXT_CDATA.
- 231: */
- 233: {
- 245: }
- 247: }
- 251: /**
- 252: * Handles CONTEXT_TAG.
- 253: */
- 255: {
- 257: (?P<end>>)(?P<tagnewline>[\ \t]*(?=\r|\n))?| ## end of HTML tag
- 259: \s*(?P<attr>[^\s/>={]+)(?:\s*=\s*(?P<value>["\']|[^\s/>{]+))? ## begin of HTML attribute
- 271: }
- 276: throw new InvalidStateException("Unknown macro-attribute " . self::HTML_PREFIX . implode(' or ' . self::HTML_PREFIX, array_keys($tag->attrs)) . " on line $this->line.");
- 277: }
- 278: }
- 281: }
- 283: if (!$tag->closing && (strcasecmp($tag->name, 'script') === 0 || strcasecmp($tag->name, 'style') === 0)) {
- 285: $this->escape = strcasecmp($tag->name, 'style') ? 'TemplateHelpers::escapeJs' : 'TemplateHelpers::escapeCss';
- 290: }
- 296: // special attribute?
- 299: }
- 305: }
- 306: }
- 314: ? (strcasecmp($name, 'style') ? 'TemplateHelpers::escapeHtml' : 'TemplateHelpers::escapeHtmlCss')
- 316: }
- 317: }
- 319: }
- 323: /**
- 324: * Handles CONTEXT_ATTRIBUTE.
- 325: */
- 327: {
- 336: }
- 338: }
- 342: /**
- 343: * Handles CONTEXT_COMMENT.
- 344: */
- 346: {
- 348: (--\s*>)| ## 1) end of HTML comment
- 355: }
- 357: }
- 361: /**
- 362: * Handles CONTEXT_NONE.
- 363: */
- 365: {
- 370: }
- 374: /**
- 375: * Matches next token.
- 378: */
- 380: {
- 385: }
- 387: }
- 391: /**
- 392: * Returns current line number.
- 394: */
- 396: {
- 398: }
- 402: /********************* compile-time helpers ****************d*g**/
- 406: /**
- 407: * Applies modifiers.
- 411: */
- 413: {
- 416: '~
- 418: [^\'"|:,]+| ## symbol
- 419: [|:,] ## separator
- 422: $tokens
- 433: }
- 441: }
- 446: }
- 449: }
- 450: }
- 452: }
- 456: /**
- 457: * Reads single token (optionally delimited by comma) from string.
- 460: */
- 462: {
- 463: if (preg_match('#^((?>'.self::RE_STRING.'|[^\'"\s,]+)+)\s*,?\s*(.*)$#', $s, $matches)) { // token [,] tail
- 466: }
- 468: }
- 472: /**
- 473: * Formats parameters to PHP array.
- 477: */
- 479: {
- 481: '~
- 483: (?<=[,=(]|=>|^)\s*([a-z\d_]+)(?=\s*[,=)]|$) ## 1) symbol
- 489: }
- 493: /**
- 494: * Callback for formatArgs().
- 495: */
- 497: {
- 498: // [1] => symbol
- 502: static $keywords = array('true'=>1, 'false'=>1, 'null'=>1, 'and'=>1, 'or'=>1, 'xor'=>1, 'clone'=>1, 'new'=>1);
- 503: return is_numeric($symbol) || isset($keywords[strtolower($symbol)]) ? $matches[0] : "'$symbol'";
- 507: }
- 508: }
- 512: /**
- 513: * Formats parameter to PHP string.
- 516: */
- 518: {
- 520: }
- 524: /**
- 525: * Invokes filter.
- 527: */
- 529: {
- 532: }
- 534: }