Skip to content

Commit f3bf01a

Browse files
committed
escaping is mandatory in HtmlAttributeUnquoted & HtmlComment
1 parent 249d43d commit f3bf01a

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

src/Latte/Compiler/Escaper.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,17 +230,19 @@ public function escape(string $str): string
230230
}
231231

232232

233-
public function escapeMandatory(string $str): string
233+
public function escapeMandatory(string $str, ?Position $position = null): string
234234
{
235235
$quote = var_export($this->quote, true);
236236
return match ($this->contentType) {
237237
ContentType::Html => match ($this->state) {
238238
self::HtmlAttributeQuoted => "LR\\Filters::escapeHtmlChar($str, $quote)",
239239
self::HtmlRawText => "LR\\Filters::convertJSToHtmlRawText((string) $str)",
240+
self::HtmlAttributeUnquoted, self::HtmlComment => throw new Latte\CompileException('Using |noescape is not allowed in this context.', $position),
240241
default => $str,
241242
},
242243
ContentType::Xml => match ($this->state) {
243244
self::HtmlAttributeQuoted => "LR\\Filters::escapeHtmlChar($str, $quote)",
245+
self::HtmlAttributeUnquoted, self::HtmlComment => throw new Latte\CompileException('Using |noescape is not allowed in this context.', $position),
244246
default => $str,
245247
},
246248
default => $str,

src/Latte/Compiler/Nodes/Php/ModifierNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function printSimple(PrintContext $context, string $expr): string
7070

7171
$expr = $escape
7272
? $escaper->escape($expr)
73-
: $escaper->escapeMandatory($expr);
73+
: $escaper->escapeMandatory($expr, $this->position);
7474

7575
return $expr;
7676
}

tests/common/Compiler.noescape.phpt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ Assert::match(
3333
);
3434

3535
// attribute unquoted values
36-
Assert::match(
37-
'<p title=foo a=\'a\' b="b">></p>',
38-
$latte->renderToString('<p title={="foo a=\'a\' b=\"b\">"|noescape}></p>'),
36+
Assert::exception(
37+
fn() => $latte->renderToString('<p title={="foo a=\'a\' b=\"b\">"|noescape}></p>'),
38+
Latte\CompileException::class,
39+
'Using |noescape is not allowed in this context (on line 1 at column 32)',
3940
);
4041

4142
// attribute quoted values
@@ -58,3 +59,10 @@ Assert::match(
5859
'<p onclick="foo a=\'a\' b=&quot;b&quot;>"></p>',
5960
$latte->renderToString('<p onclick="{="foo a=\'a\' b=\"b\">"|noescape}"></p>'),
6061
);
62+
63+
// comment
64+
Assert::exception(
65+
fn() => $latte->renderToString('<!-- {="-->"|noescape} -->'),
66+
Latte\CompileException::class,
67+
'Using |noescape is not allowed in this context (on line 1 at column 13)',
68+
);

0 commit comments

Comments
 (0)