Skip to content

Commit

Permalink
Compiler, Parser: renamed CONTEXT constants
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jun 27, 2016
1 parent a7f1e3d commit 2ad6933
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 45 deletions.
53 changes: 30 additions & 23 deletions src/Latte/Compiler/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,28 @@ class Compiler
private $properties = [];

/** Context-aware escaping content types */
const CONTENT_HTML = Engine::CONTENT_HTML,
const
CONTENT_HTML = Engine::CONTENT_HTML,
CONTENT_XHTML = Engine::CONTENT_XHTML,
CONTENT_XML = Engine::CONTENT_XML,
CONTENT_JS = Engine::CONTENT_JS,
CONTENT_CSS = Engine::CONTENT_CSS,
CONTENT_URL = Engine::CONTENT_URL,
CONTENT_ICAL = Engine::CONTENT_ICAL,
CONTENT_TEXT = Engine::CONTENT_TEXT;

/** @internal Context-aware escaping HTML contexts */
const
CONTEXT_COMMENT = 'comment',
CONTEXT_BOGUS_COMMENT = 'bogus',
CONTEXT_QUOTED_ATTRIBUTE = 'attr',
CONTEXT_TAG = 'tag';
// for CONTENT_HTML, CONTENT_XHTML
CONTEXT_HTML_TAG = 'tag',
CONTEXT_HTML_ATTRIBUTE = 'attr',
CONTEXT_HTML_ATTRIBUTE_URL = 'attrurl',
CONTEXT_HTML_COMMENT = 'comment',
CONTEXT_HTML_BOGUS_COMMENT = 'bogus',
CONTEXT_HTML_CSS = 'css',
CONTEXT_HTML_JS = 'js',

CONTEXT_XML_TAG = self::CONTEXT_HTML_TAG,
CONTEXT_XML_COMMENT = self::CONTEXT_HTML_COMMENT;


/**
Expand Down Expand Up @@ -294,7 +301,7 @@ public function expandTokens($s)

private function processText(Token $token)
{
if ($this->context[0] === self::CONTEXT_QUOTED_ATTRIBUTE && $this->lastAttrValue === '') {
if ($this->context[0] === self::CONTEXT_HTML_ATTRIBUTE && $this->lastAttrValue === '') {
$this->lastAttrValue = $token->text;
}
$this->output .= $this->escape($token->text);
Expand All @@ -303,7 +310,7 @@ private function processText(Token $token)

private function processMacroTag(Token $token)
{
if (in_array($this->context[0], [self::CONTEXT_QUOTED_ATTRIBUTE, self::CONTEXT_TAG], TRUE)) {
if (in_array($this->context[0], [self::CONTEXT_HTML_ATTRIBUTE, self::CONTEXT_HTML_TAG], TRUE)) {
$this->lastAttrValue = TRUE;
}

Expand Down Expand Up @@ -351,17 +358,17 @@ private function processHtmlTagBegin(Token $token)
$this->setContext(NULL);

} elseif ($token->text === '<!--') {
$this->setContext(self::CONTEXT_COMMENT);
$this->setContext(self::CONTEXT_HTML_COMMENT);

} elseif ($token->text === '<?' || $token->text === '<!') {
$this->setContext(self::CONTEXT_BOGUS_COMMENT);
$this->setContext(self::CONTEXT_HTML_BOGUS_COMMENT);
$this->output .= $token->text === '<?' ? '<<?php ?>?' : '<!'; // bypass error in escape()
return;

} else {
$this->htmlNode = new HtmlNode($token->name, $this->htmlNode);
$this->htmlNode->startLine = $this->getLine();
$this->setContext(self::CONTEXT_TAG);
$this->setContext(self::CONTEXT_HTML_TAG);
}
$this->tagOffset = strlen($this->output);
$this->output .= $token->text;
Expand All @@ -370,7 +377,7 @@ private function processHtmlTagBegin(Token $token)

private function processHtmlTagEnd(Token $token)
{
if (in_array($this->context[0], [self::CONTEXT_COMMENT, self::CONTEXT_BOGUS_COMMENT], TRUE)) {
if (in_array($this->context[0], [self::CONTEXT_HTML_COMMENT, self::CONTEXT_HTML_BOGUS_COMMENT], TRUE)) {
$this->output .= $token->text;
$this->setContext(NULL);
return;
Expand Down Expand Up @@ -419,7 +426,7 @@ private function processHtmlTagEnd(Token $token)
} elseif ((($lower = strtolower($htmlNode->name)) === 'script' || $lower === 'style')
&& (!isset($htmlNode->attrs['type']) || preg_match('#(java|j|ecma|live)script|json|css#i', $htmlNode->attrs['type']))
) {
$this->setContext($lower === 'script' ? self::CONTENT_JS : self::CONTENT_CSS);
$this->setContext($lower === 'script' ? self::CONTEXT_HTML_JS : self::CONTEXT_HTML_CSS);
}
}

Expand All @@ -445,24 +452,24 @@ private function processHtmlAttributeBegin(Token $token)
$lower = strtolower($token->name);
if (in_array($token->value, ['"', "'"], TRUE)) {
$this->lastAttrValue = '';
$contextMain = self::CONTEXT_QUOTED_ATTRIBUTE;
$contextMain = self::CONTEXT_HTML_ATTRIBUTE;
if (in_array($this->contentType, [self::CONTENT_HTML, self::CONTENT_XHTML], TRUE)) {
if (Helpers::startsWith($lower, 'on')) {
$context = self::CONTENT_JS;
$context = self::CONTEXT_HTML_JS;
} elseif ($lower === 'style') {
$context = self::CONTENT_CSS;
$context = self::CONTEXT_HTML_CSS;
}
}
} else {
$this->lastAttrValue = $token->value;
$contextMain = self::CONTEXT_TAG;
$contextMain = self::CONTEXT_HTML_TAG;
}

if (in_array($this->contentType, [self::CONTENT_HTML, self::CONTENT_XHTML], TRUE)
&& (in_array($lower, ['href', 'src', 'action', 'formaction'], TRUE)
|| ($lower === 'data' && strtolower($this->htmlNode->name) === 'object'))
) {
$context = self::CONTENT_URL;
$context = self::CONTEXT_HTML_ATTRIBUTE_URL;
}

$this->setContext($contextMain, $context);
Expand All @@ -471,7 +478,7 @@ private function processHtmlAttributeBegin(Token $token)

private function processHtmlAttributeEnd(Token $token)
{
$this->setContext(self::CONTEXT_TAG);
$this->setContext(self::CONTEXT_HTML_TAG);
$this->output .= $token->text;
}

Expand Down Expand Up @@ -721,7 +728,7 @@ public function writeAttrsMacro($html)
*/
public function expandMacro($name, $args, $modifiers = NULL, $nPrefix = NULL)
{
$inScript = in_array($this->context[0], [self::CONTENT_JS, self::CONTENT_CSS], TRUE);
$inScript = in_array($this->context[0], [self::CONTEXT_HTML_JS, self::CONTEXT_HTML_CSS], TRUE);

if (empty($this->macros[$name])) {
$hint = ($t = Helpers::getSuggestion(array_keys($this->macros), $name)) ? ", did you mean {{$t}}?" : '';
Expand All @@ -735,7 +742,7 @@ public function expandMacro($name, $args, $modifiers = NULL, $nPrefix = NULL)
}

if (strpbrk($name, '=~%^&_')) {
if ($this->context[1] === self::CONTENT_URL) {
if ($this->context[1] === self::CONTEXT_HTML_ATTRIBUTE_URL) {
if (!Helpers::removeFilter($modifiers, 'nosafeurl|nocheck') && !preg_match('#\|datastream(?=\s|\||\z)#i', $modifiers)) {
$modifiers .= '|checkurl';
}
Expand All @@ -750,9 +757,9 @@ public function expandMacro($name, $args, $modifiers = NULL, $nPrefix = NULL)
}

if ($nPrefix === MacroNode::PREFIX_INNER && !strcasecmp($this->htmlNode->name, 'script')) {
$context = [$this->contentType, self::CONTENT_JS, NULL];
$context = [$this->contentType, self::CONTEXT_HTML_JS, NULL];
} elseif ($nPrefix === MacroNode::PREFIX_INNER && !strcasecmp($this->htmlNode->name, 'style')) {
$context = [$this->contentType, self::CONTENT_CSS, NULL];
$context = [$this->contentType, self::CONTEXT_HTML_CSS, NULL];
} elseif ($nPrefix) {
$context = [$this->contentType, NULL, NULL];
} else {
Expand Down
18 changes: 9 additions & 9 deletions src/Latte/Compiler/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ class Parser

/** @internal states */
const
CONTEXT_NONE = 'none',
CONTEXT_MACRO = 'macro',
CONTEXT_HTML_TEXT = 'htmlText',
CONTEXT_CDATA = 'cdata',
CONTEXT_HTML_TAG = 'htmlTag',
CONTEXT_HTML_ATTRIBUTE = 'htmlAttribute',
CONTEXT_RAW = 'raw',
CONTEXT_HTML_COMMENT = 'htmlComment',
CONTEXT_MACRO = 'macro';
CONTEXT_HTML_CDATA = 'htmlCData';


/**
Expand Down Expand Up @@ -153,9 +153,9 @@ private function contextHtmlText()


/**
* Handles CONTEXT_CDATA.
* Handles CONTEXT_HTML_CDATA.
*/
private function contextCData()
private function contextHtmlCData()
{
$matches = $this->match('~
</(?P<tag>' . $this->lastHtmlTag . ')(?![a-z0-9:])| ## end HTML tag </tag
Expand Down Expand Up @@ -187,7 +187,7 @@ private function contextHtmlTag()

if (!empty($matches['end'])) { // end of HTML tag />
$this->addToken(Token::HTML_TAG_END, $matches[0]);
$this->setContext(!$this->xmlMode && in_array($this->lastHtmlTag, ['script', 'style'], TRUE) ? self::CONTEXT_CDATA : self::CONTEXT_HTML_TEXT);
$this->setContext(!$this->xmlMode && in_array($this->lastHtmlTag, ['script', 'style'], TRUE) ? self::CONTEXT_HTML_CDATA : self::CONTEXT_HTML_TEXT);

} elseif (isset($matches['attr']) && $matches['attr'] !== '') { // HTML attribute
$token = $this->addToken(Token::HTML_ATTRIBUTE_BEGIN, $matches[0]);
Expand Down Expand Up @@ -250,9 +250,9 @@ private function contextHtmlComment()


/**
* Handles CONTEXT_RAW.
* Handles CONTEXT_NONE.
*/
private function contextRaw()
private function contextNone()
{
$matches = $this->match('~
(?P<macro>' . $this->delimiters[0] . ')
Expand Down Expand Up @@ -338,7 +338,7 @@ public function setContentType($type)
$this->setContext(self::CONTEXT_HTML_TEXT);
$this->xmlMode = $type === self::CONTENT_XML;
} else {
$this->setContext(self::CONTEXT_RAW);
$this->setContext(self::CONTEXT_NONE);
}
return $this;
}
Expand Down
22 changes: 11 additions & 11 deletions src/Latte/Compiler/PhpWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -474,35 +474,35 @@ public function escapePass(MacroTokens $tokens)
case Compiler::CONTENT_XHTML:
case Compiler::CONTENT_HTML:
switch ($context) {
case Compiler::CONTEXT_QUOTED_ATTRIBUTE:
case Compiler::CONTEXT_TAG:
if ($subContext === Compiler::CONTENT_JS) {
case Compiler::CONTEXT_HTML_ATTRIBUTE:
case Compiler::CONTEXT_HTML_TAG:
if ($subContext === Compiler::CONTEXT_HTML_JS) {
$tokens->prepend('LR\Filters::escapeJs(')->append(')');
} elseif ($subContext === Compiler::CONTENT_CSS) {
} elseif ($subContext === Compiler::CONTEXT_HTML_CSS) {
$tokens->prepend('LR\Filters::escapeCss(')->append(')');
}
if ($context === Compiler::CONTEXT_TAG) {
if ($context === Compiler::CONTEXT_HTML_TAG) {
$tokens->prepend('LR\Filters::escapeHtmlAttrUnquoted(')->append(')');
} else {
$tokens->prepend('LR\Filters::escapeHtmlAttr(')->append(')');
}
return $tokens;
case Compiler::CONTEXT_COMMENT:
case Compiler::CONTEXT_HTML_COMMENT:
return $tokens->prepend('LR\Filters::escapeHtmlComment(')->append(')');
case Compiler::CONTEXT_BOGUS_COMMENT:
case Compiler::CONTEXT_HTML_BOGUS_COMMENT:
return $tokens->prepend('LR\Filters::escapeHtml(')->append(')');
case Compiler::CONTENT_JS:
case Compiler::CONTENT_CSS:
case Compiler::CONTEXT_HTML_JS:
case Compiler::CONTEXT_HTML_CSS:
return $tokens->prepend('LR\Filters::escape' . ucfirst($context) . '(')->append(')');
default:
return $tokens->prepend('LR\Filters::escapeHtmlText(')->append(')');
}

case Compiler::CONTENT_XML:
switch ($context) {
case Compiler::CONTEXT_COMMENT:
case Compiler::CONTEXT_XML_COMMENT:
return $tokens->prepend('LR\Filters::escapeHtmlComment(')->append(')');
case Compiler::CONTEXT_TAG:
case Compiler::CONTEXT_XML_TAG:
return $tokens->prepend('LR\Filters::escapeXmlAttrUnquoted(')->append(')');
default:
return $tokens->prepend('LR\Filters::escapeXml(')->append(')');
Expand Down
1 change: 0 additions & 1 deletion src/Latte/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class Engine
CONTENT_XML = 'xml',
CONTENT_JS = 'js',
CONTENT_CSS = 'css',
CONTENT_URL = 'url',
CONTENT_ICAL = 'ical',
CONTENT_TEXT = 'text';

Expand Down
2 changes: 1 addition & 1 deletion src/Latte/Macros/MacroSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function nodeOpened(MacroNode $node)

if ($attr && $node->prefix === $node::PREFIX_NONE) {
$node->empty = TRUE;
$node->context[1] = Latte\Compiler::CONTEXT_QUOTED_ATTRIBUTE;
$node->context[1] = Latte\Compiler::CONTEXT_HTML_ATTRIBUTE;
$res = $this->compile($node, $attr);
if ($res === FALSE) {
return FALSE;
Expand Down

0 comments on commit 2ad6933

Please sign in to comment.