Skip to content

Commit

Permalink
escaping is mandatory in HtmlComment
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Oct 11, 2023
1 parent e1105ad commit f23c2cf
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/Latte/Compiler/Escaper.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public function escape(string $str): string
}


public function escapeMandatory(string $str): string
public function escapeMandatory(string $str, ?Position $position = null): string
{
$quote = var_export($this->quote, true); // TODO
return match ($this->contentType) {
Expand All @@ -232,10 +232,12 @@ public function escapeMandatory(string $str): string
self::HtmlText => 'LR\Filters::convertHtmlToHtmlRawText(' . $str . ')',
default => "LR\\Filters::convertJSToHtmlRawText($str)",
},
self::HtmlComment => throw new Latte\CompileException('Using |noescape is not allowed in this context.', $position),
default => $str,
},
ContentType::Xml => match ($this->state) {
self::HtmlAttribute => "LR\\Filters::escapeHtmlChar($str, $quote)",
self::HtmlComment => throw new Latte\CompileException('Using |noescape is not allowed in this context.', $position),
default => $str,
},
default => $str,
Expand Down
2 changes: 1 addition & 1 deletion src/Latte/Compiler/Nodes/Php/ModifierNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function printSimple(PrintContext $context, string $expr): string

$expr = $escape
? $escaper->escape($expr)
: $escaper->escapeMandatory($expr);
: $escaper->escapeMandatory($expr, $this->position);

return $expr;
}
Expand Down
7 changes: 7 additions & 0 deletions tests/common/Compiler.noescape.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,10 @@ Assert::match(
'<p onclick="foo a=\'a\' b=&quot;b&quot;>"></p>',
$latte->renderToString('<p onclick="{="foo a=\'a\' b=\"b\">"|noescape}"></p>'),
);

// comment
Assert::exception(
fn() => $latte->renderToString('<!-- {="-->"|noescape} -->'),
Latte\CompileException::class,
'Using |noescape is not allowed in this context (on line 1 at column 13)',
);

0 comments on commit f23c2cf

Please sign in to comment.