From d61f221a6734ba46fe8c256a411813bc709a02cd Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 28 Oct 2026 18:50:58 +0100 Subject: [PATCH 01/14] TemplateParserHtml: fixed newline lost after indented end tag extractIndentation() moved the indentation into the element but left TemplateParser::$lastIndentation set, so a line like ' {/if}' was taken for a line holding only a Latte tag and lost its newline. --- src/Latte/Compiler/TemplateParserHtml.php | 1 + tests/common/Compiler.whitespace.phpt | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/Latte/Compiler/TemplateParserHtml.php b/src/Latte/Compiler/TemplateParserHtml.php index 5654cf8d6..d01151317 100644 --- a/src/Latte/Compiler/TemplateParserHtml.php +++ b/src/Latte/Compiler/TemplateParserHtml.php @@ -178,6 +178,7 @@ private function extractIndentation(): AreaNode if ($this->parser->lastIndentation) { $dolly = clone $this->parser->lastIndentation; $this->parser->lastIndentation->clear(); + $this->parser->lastIndentation = null; return $dolly; } else { return new Nodes\NopNode; diff --git a/tests/common/Compiler.whitespace.phpt b/tests/common/Compiler.whitespace.phpt index 6113c9d93..0ebbdc769 100644 --- a/tests/common/Compiler.whitespace.phpt +++ b/tests/common/Compiler.whitespace.phpt @@ -138,6 +138,21 @@ Assert::match(<<<'EOD' )); +// indented end tag followed by a Latte tag keeps the newline +Assert::match(<<<'EOD' + + World + EOD, $latte->renderToString( + <<<'EOD' + {if true} + {/if} + World + EOD, +)); + + // empty comment Assert::match('ab', $latte->renderToString('a{**}b')); Assert::match('ab', $latte->renderToString('a{* *}b')); From c98a726f59d92d356a0c1fcf15b98f6e4409f1d4 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 28 Oct 2026 22:12:46 +0100 Subject: [PATCH 02/14] Cache: skips including a template whose class is already declared A coroutine (Swoole) or a fiber can pass the class_exists() check in Engine::loadTemplate() and then wait for the cache lock while another one declares the class. After getting the lock it included the file again and failed with "Cannot redeclare class". --- src/Latte/Runtime/Cache.php | 8 +++++--- tests/common/Cache.loadOrCreate.phpt | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 tests/common/Cache.loadOrCreate.phpt diff --git a/src/Latte/Runtime/Cache.php b/src/Latte/Runtime/Cache.php index 137d57409..7c7324496 100644 --- a/src/Latte/Runtime/Cache.php +++ b/src/Latte/Runtime/Cache.php @@ -32,6 +32,7 @@ public function loadOrCreate(Engine $engine, string $name): void // so on Linux we include the file directly without shared lock, therefore, the file must be created atomically by renaming. // 2) On Windows file cannot be renamed-to while is open (ie by include), so we have to acquire a lock. $file = $this->generateFilePath($engine, $name); + $class = $engine->getTemplateClass($name); $signature = $this->autoRefresh ? hash('xxh128', serialize($this->generateRefreshSignature($engine, $name))) : null; @@ -40,8 +41,9 @@ public function loadOrCreate(Engine $engine, string $name): void : null; if ( - !($signature && $lock && $signature !== stream_get_contents($lock)) - && (@include $file) !== false // @ - file may not exist + class_exists($class, false) // another coroutine or fiber may have declared it while we waited for the lock + || (!($signature && $lock && $signature !== stream_get_contents($lock)) + && (@include $file) !== false) // @ - file may not exist ) { return; } @@ -73,7 +75,7 @@ public function loadOrCreate(Engine $engine, string $name): void } } - if ((include $file) === false) { + if (!class_exists($class, false) && (include $file) === false) { throw new RuntimeException("Unable to load '$file'."); } diff --git a/tests/common/Cache.loadOrCreate.phpt b/tests/common/Cache.loadOrCreate.phpt new file mode 100644 index 000000000..c71684be9 --- /dev/null +++ b/tests/common/Cache.loadOrCreate.phpt @@ -0,0 +1,24 @@ +setCacheDirectory($dir); + $latte->warmupCache($template); + Assert::true(class_exists($latte->getTemplateClass($template), false)); + + $cache = new Cache; + $cache->directory = $dir; + Assert::noError(fn() => $cache->loadOrCreate($latte, $template)); +}); From 5de91e0a15e0f1b687ecbaca24088444e376aaef Mon Sep 17 00:00:00 2001 From: Jakub Vrana Date: Mon, 28 Sep 2026 19:05:02 +0200 Subject: [PATCH 03/14] Escaper: srcdoc attribute is escaped as an HTML document [security] (BC break) The value of srcdoc is an HTML document: the browser attribute-decodes it and then parses the result as HTML. Latte escaped it like any other attribute, so ', + $latte->renderToString('', $params), +); + +Assert::same( + '', + $latte->renderToString("", $params), +); + +Assert::same( + '', + $latte->renderToString('', $params), +); + +// HTML is escaped only for the attribute +Assert::same( + '', + $latte->renderToString('', $params), +); + +// |noescape escapes only quotes +Assert::same( + '', + $latte->renderToString('', $params), +); + +// dynamic attribute +Assert::same( + '', + $latte->renderToString('', $params), +); + +Assert::same( + '', + $latte->renderToString('', $params), +); + +Assert::same( + '', + $latte->renderToString('', ['none' => null]), +); + +// n:attr +Assert::same( + '', + $latte->renderToString('', $params), +); + +Assert::same( + '', + $latte->renderToString('', $params), +); + +// blocks +Assert::same( + '', + $latte->renderToString('{define doc}{$x}{/define}', $params), +); + +$latte->setLoader(new Latte\Loaders\StringLoader([ + 'main' => ' ', + 'html.latte' => ' & ', + 'text.latte' => '{contentType text} & "', + 'js.latte' => '{contentType javascript}a < b', +])); + +Assert::same( + ' ', + $latte->renderToString('main'), +); + +// XML has no srcdoc context +$latte->setLoader(new Latte\Loaders\StringLoader); +Assert::same( + '', + $latte->renderToString('{contentType xml}', $params), +); diff --git a/tests/runtime/HtmlHelpers.escapeHtmlAttr.phpt b/tests/runtime/HtmlHelpers.escapeHtmlAttr.phpt new file mode 100644 index 000000000..6f24c005d --- /dev/null +++ b/tests/runtime/HtmlHelpers.escapeHtmlAttr.phpt @@ -0,0 +1,16 @@ + & " \'')); +Assert::same('&#123; {<!-- -->{', HtmlHelpers::escapeHtmlAttr('{ {{')); +Assert::same('&amp;amp;', HtmlHelpers::escapeHtmlAttr('&')); +Assert::same('<b title="x">a &amp; b</b>', HtmlHelpers::escapeHtmlAttr(new Html('a & b'))); +Assert::same('{', HtmlHelpers::escapeHtmlAttr(new Html('{'))); diff --git a/tests/runtime/HtmlHelpers.formatSrcdocAttribute.phpt b/tests/runtime/HtmlHelpers.formatSrcdocAttribute.phpt new file mode 100644 index 000000000..8f3ff0494 --- /dev/null +++ b/tests/runtime/HtmlHelpers.formatSrcdocAttribute.phpt @@ -0,0 +1,22 @@ +')); +Assert::same(' srcdoc="<b>"', HtmlHelpers::formatSrcdocAttribute(' srcdoc', new Html(''))); + +// null & invalid +Assert::same('', HtmlHelpers::formatSrcdocAttribute('srcdoc', null)); +Assert::error( + fn() => Assert::same('', HtmlHelpers::formatSrcdocAttribute('srcdoc', [])), + E_USER_WARNING, + "Invalid value for attribute 'srcdoc': array is not allowed.", +); From 7ce36eb93a5cf1f60741c6b6485f5c8757844830 Mon Sep 17 00:00:00 2001 From: Jakub Vrana Date: Mon, 28 Sep 2026 19:18:53 +0200 Subject: [PATCH 04/14] HtmlHelpers: srcdoc triggers a migration warning for a value previously rendered as HTML With Feature::MigrationWarnings, a plain srcdoc value that renders as a different document now than before (it contains '<' or a character reference) triggers a warning suggesting to wrap trusted HTML in Latte\Runtime\Html. Text such as 'a & b' renders the same document as before and stays quiet; so does an Html object, whose tags were stripped before and are kept now. |accept silences it. It covers dynamic attributes (srcdoc={$x}, srcdoc="{$x}") and n:attr. A mixed value like srcdoc="

{$x}

" is escaped by code the Escaper emits, which has no access to feature flags, so it does not warn. Co-Authored-By: Claude Opus 5.5 --- docs/internals/escaping.md | 6 +++++ src/Latte/Runtime/HtmlHelpers.php | 19 ++++++++++++---- tests/common/Engine.migrationWarnings.phpt | 6 +++++ .../HtmlHelpers.formatSrcdocAttribute.phpt | 22 +++++++++++++++++++ 4 files changed, 49 insertions(+), 4 deletions(-) diff --git a/docs/internals/escaping.md b/docs/internals/escaping.md index c9cb88aa8..6b2f2bb32 100644 --- a/docs/internals/escaping.md +++ b/docs/internals/escaping.md @@ -47,6 +47,12 @@ and is encoded only once, so trusted HTML keeps its tags. All three places route `srcdoc` through it: the `html/attr/html` state in `escape()` and in the `Convertors`, and `formatSrcdocAttribute` for dynamic attributes and `n:attr`. +With `Feature::MigrationWarnings`, `formatSrcdocAttribute` warns about a plain value +whose document differs from its text (it contains `<` or a character reference), +i.e. one that used to render as markup. Only the formatter paths warn: a mixed value +like `srcdoc="

{$x}

"` is escaped by code `Escaper::escape()` emits, and +`Escaper` has no access to feature flags. + ## How nodes obtain the context: the `PrintContext` escaper stack At print time the current escaping context lives in a **stack of `Escaper`s inside diff --git a/src/Latte/Runtime/HtmlHelpers.php b/src/Latte/Runtime/HtmlHelpers.php index 7ffe7027b..6f8c5bfa1 100644 --- a/src/Latte/Runtime/HtmlHelpers.php +++ b/src/Latte/Runtime/HtmlHelpers.php @@ -290,10 +290,21 @@ public static function formatStyleAttribute(string $namePart, mixed $value): str */ public static function formatSrcdocAttribute(string $namePart, mixed $value, bool $migrationWarnings = false): string { - return match (true) { - is_string($value), is_int($value), is_float($value), $value instanceof \Stringable => $namePart . '="' . self::escapeHtmlAttr($value) . '"', - default => self::formatAttribute($namePart, $value, $migrationWarnings), - }; + if (!is_string($value) && !is_int($value) && !is_float($value) && !$value instanceof \Stringable) { + return self::formatAttribute($namePart, $value, $migrationWarnings); + } + + if ($migrationWarnings + && !$value instanceof HtmlStringable + && !$value instanceof Nette\HtmlStringable + && (str_contains($s = (string) $value, '<') || html_entity_decode($s, ENT_QUOTES | ENT_HTML5, 'UTF-8') !== $s) // the document differs from the text + ) { + self::triggerMigrationWarning( + trim($namePart), + 'string value: previously it was rendered as HTML, now it is escaped as text (wrap trusted HTML in Latte\Runtime\Html)', + ); + } + return $namePart . '="' . self::escapeHtmlAttr($value) . '"'; } diff --git a/tests/common/Engine.migrationWarnings.phpt b/tests/common/Engine.migrationWarnings.phpt index 28b096480..109806868 100644 --- a/tests/common/Engine.migrationWarnings.phpt +++ b/tests/common/Engine.migrationWarnings.phpt @@ -39,6 +39,12 @@ Assert::error( 'Behavior change for attribute \'contenteditable\' with value false: previously it rendered as contenteditable="", now it renders as contenteditable="false" (on line 1 at column 25)', ); +Assert::error( + fn() => $latte->renderToString('', + '', $latte->renderToString('', $params), ); Assert::same( - '', + '', $latte->renderToString("", $params), ); Assert::same( - '', + '', $latte->renderToString('', $params), ); @@ -46,7 +46,7 @@ Assert::same( // dynamic attribute Assert::same( - '', + '', $latte->renderToString('', $params), ); @@ -62,7 +62,7 @@ Assert::same( // n:attr Assert::same( - '', + '', $latte->renderToString('', $params), ); @@ -73,7 +73,7 @@ Assert::same( // blocks Assert::same( - '', + '', $latte->renderToString('{define doc}{$x}{/define}', $params), ); diff --git a/tests/runtime/HtmlHelpers.escapeHtmlAttr.phpt b/tests/runtime/HtmlHelpers.escapeHtmlAttr.phpt index 6f24c005d..97968a8ce 100644 --- a/tests/runtime/HtmlHelpers.escapeHtmlAttr.phpt +++ b/tests/runtime/HtmlHelpers.escapeHtmlAttr.phpt @@ -10,7 +10,7 @@ Assert::same('', HtmlHelpers::escapeHtmlAttr(null)); Assert::same('', HtmlHelpers::escapeHtmlAttr('')); Assert::same('1', HtmlHelpers::escapeHtmlAttr(1)); Assert::same('&lt;b&gt; &amp; " '', HtmlHelpers::escapeHtmlAttr(' & " \'')); -Assert::same('&#123; {<!-- -->{', HtmlHelpers::escapeHtmlAttr('{ {{')); +Assert::same('<!-- -->{ {<!-- -->{<!-- -->', HtmlHelpers::escapeHtmlAttr('{ {{')); Assert::same('&amp;amp;', HtmlHelpers::escapeHtmlAttr('&')); Assert::same('<b title="x">a &amp; b</b>', HtmlHelpers::escapeHtmlAttr(new Html('a & b'))); Assert::same('{', HtmlHelpers::escapeHtmlAttr(new Html('{'))); diff --git a/tests/runtime/HtmlHelpers.escapeRawHtml.phpt b/tests/runtime/HtmlHelpers.escapeRawHtml.phpt index c271f01f4..28a143f4e 100644 --- a/tests/runtime/HtmlHelpers.escapeRawHtml.phpt +++ b/tests/runtime/HtmlHelpers.escapeRawHtml.phpt @@ -30,6 +30,11 @@ Assert::same('
', HtmlHelpers::escapeRawHtml(new Test)); Assert::same('

', HtmlHelpers::escapeRawHtml(new Latte\Runtime\Html('

'))); Assert::same('', HtmlHelpers::escapeRawHtml(new Latte\Runtime\Html(''))); +// JS +Assert::same('hello {{ worlds }}', HtmlHelpers::escapeRawHtml('hello {{ worlds }}')); +Assert::same('a{', HtmlHelpers::escapeRawHtml('a{')); +Assert::same('{a', HtmlHelpers::escapeRawHtml('{a')); + // invalid UTF-8 Assert::same("foo \u{FFFD} bar", HtmlHelpers::escapeRawHtml("foo \u{D800} bar")); // invalid codepoint high surrogates Assert::same("foo \u{FFFD}" bar", HtmlHelpers::escapeRawHtml("foo \xE3\x80\x22 bar")); // stripped UTF diff --git a/tests/runtime/HtmlHelpers.escapeText.phpt b/tests/runtime/HtmlHelpers.escapeText.phpt index 6b5cbec04..19aaa26ca 100644 --- a/tests/runtime/HtmlHelpers.escapeText.phpt +++ b/tests/runtime/HtmlHelpers.escapeText.phpt @@ -35,4 +35,11 @@ Assert::same("foo \u{FFFD}\" bar", HtmlHelpers::escapeText("foo \xE3\x80\x22 bar // JS Assert::same('hello {{ worlds }}', HtmlHelpers::escapeText('hello {{ worlds }}')); -Assert::same('hello { worlds }', HtmlHelpers::escapeText('hello { worlds }')); +Assert::same('hello { worlds }', HtmlHelpers::escapeText('hello { worlds }')); +Assert::same('{{{ x }}}', HtmlHelpers::escapeText('{{{ x }}}')); + +// a brace at the edge can form {{ with the adjacent output +Assert::same('{', HtmlHelpers::escapeText('{')); +Assert::same('a{', HtmlHelpers::escapeText('a{')); +Assert::same('{a', HtmlHelpers::escapeText('{a')); +Assert::same('{"a":1}', HtmlHelpers::escapeText('{"a":1}')); From b3aeb57a8cb04f4d3e4b76a9a16d74672241075f Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 29 Oct 2026 00:50:58 +0100 Subject: [PATCH 11/14] Escaper: ', $latte->renderToString('', $params)); +Assert::same('a {{b}} <c>', $latte->renderToString('{$v}', $params)); +Assert::same('', $latte->renderToString('', $params)); +Assert::same('', $latte->renderToString('', ['v' => new Html('{{x}}')])); + +// elsewhere braces are separated +Assert::same('

a {{b}} <c>

', $latte->renderToString('

{$v}

', $params)); +Assert::same('a {{b}} <c>', $latte->renderToString('{$v}', $params)); + + +test('filters and blocks', function () use ($latte, $params) { + Assert::same( + 'a {{b}} <c>', + $latte->renderToString('{block|stripHtml}<b>{$v}</b>{/block}', $params), + ); + Assert::same( + 'a {{b}} <c>

a {{b}} <c>

', + $latte->renderToString('{block bar}{$v}{/block}

{include bar}

', $params), + ); +}); + + +test('overridden block', function () use ($params) { + $latte = createLatte(); + $latte->setLoader(new Latte\Loaders\StringLoader([ + 'main' => '{layout lay}{block title}{$v}{/block}', + 'lay' => '{block title}{/block}', + ])); + Assert::same('a {<!-- -->{b}} <c>', $latte->renderToString('main', $params)); +}); diff --git a/tests/tags/expected/inheritance.1.parent.php b/tests/tags/expected/inheritance.1.parent.php index f445d4693..44436a73c 100644 --- a/tests/tags/expected/inheritance.1.parent.php +++ b/tests/tags/expected/inheritance.1.parent.php @@ -3,7 +3,7 @@ final class Template%a% extends Latte\Runtime\Template { public const Blocks = [ - ['title' => 'blockTitle', 'sidebar' => 'blockSidebar'], + ['title' => ['blockTitle', 'html/rcdata'], 'sidebar' => 'blockSidebar'], ]; @@ -17,7 +17,7 @@ public function main(array $ʟ_args): void '; $this->renderBlock('title', get_defined_vars(), function ($s, $type) { $ʟ_fi = new LR\FilterInfo($type); - return LR\Helpers::convertTo($ʟ_fi, 'html', $this->filters->filterContent('upper', $ʟ_fi, $this->filters->filterContent('stripHtml', $ʟ_fi, $s))); + return LR\Helpers::convertTo($ʟ_fi, 'html/rcdata', $this->filters->filterContent('upper', $ʟ_fi, $this->filters->filterContent('stripHtml', $ʟ_fi, $s))); }) /* pos %d%:%d% */; echo ' From 9fcfa2e3f1c31d47e28889a0ee7481b9f1718935 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 29 Oct 2026 02:37:07 +0100 Subject: [PATCH 12/14] HtmlHelpers: srcdoc and text/template escape as attributes [security] Latte does not parse the document in srcdoc nor the template in such a script, so a value may land in an attribute there. srcdoc escaped the inner layer as HTML text without quotes, so ', + '', + $latte->renderToString('', ['x' => '" onmouseover="alert(1)']), +); + +Assert::same( + '', $latte->renderToString('', $params), ); Assert::same( - '', + '', $latte->renderToString("", $params), ); Assert::same( - '', + '', $latte->renderToString('', $params), ); @@ -46,7 +51,7 @@ Assert::same( // dynamic attribute Assert::same( - '', + '', $latte->renderToString('', $params), ); @@ -62,7 +67,7 @@ Assert::same( // n:attr Assert::same( - '', + '', $latte->renderToString('', $params), ); @@ -85,7 +90,7 @@ $latte->setLoader(new Latte\Loaders\StringLoader([ ])); Assert::same( - ' ', + ' ', $latte->renderToString('main'), ); diff --git a/tests/runtime/HtmlHelpers.convertHtmlAttrToHtml.phpt b/tests/runtime/HtmlHelpers.convertHtmlAttrToHtml.phpt index fb5f6cd11..44161d973 100644 --- a/tests/runtime/HtmlHelpers.convertHtmlAttrToHtml.phpt +++ b/tests/runtime/HtmlHelpers.convertHtmlAttrToHtml.phpt @@ -12,13 +12,14 @@ require __DIR__ . '/../bootstrap.php'; Assert::same('', HtmlHelpers::convertAttrToHtml('')); Assert::same('string', HtmlHelpers::convertAttrToHtml('string')); -Assert::same('< & ' " >', HtmlHelpers::convertAttrToHtml('< & \' " >')); +Assert::same('< & \' " >', HtmlHelpers::convertAttrToHtml('< & \' " >')); Assert::same('<span>hello</span>', HtmlHelpers::convertAttrToHtml('hello')); -Assert::same('"', HtmlHelpers::convertAttrToHtml('"')); +Assert::same('"', HtmlHelpers::convertAttrToHtml('"')); // invalid UTF-8 Assert::same("foo \u{FFFD} bar", HtmlHelpers::convertAttrToHtml("foo \u{D800} bar")); // invalid codepoint high surrogates -Assert::same("foo \u{FFFD}" bar", HtmlHelpers::convertAttrToHtml("foo \xE3\x80\x22 bar")); // stripped UTF +Assert::same("foo \u{FFFD}\" bar", HtmlHelpers::convertAttrToHtml("foo \xE3\x80\x22 bar")); // stripped UTF // JS -Assert::same('hello { worlds }', HtmlHelpers::convertAttrToHtml('hello { worlds }')); +Assert::same('hello { worlds }', HtmlHelpers::convertAttrToHtml('hello { worlds }')); +Assert::same('hello {{ worlds }}', HtmlHelpers::convertAttrToHtml('hello {{ worlds }}')); diff --git a/tests/runtime/HtmlHelpers.escapeHtmlAttr.phpt b/tests/runtime/HtmlHelpers.escapeHtmlAttr.phpt index 97968a8ce..5353b9f24 100644 --- a/tests/runtime/HtmlHelpers.escapeHtmlAttr.phpt +++ b/tests/runtime/HtmlHelpers.escapeHtmlAttr.phpt @@ -9,8 +9,8 @@ require __DIR__ . '/../bootstrap.php'; Assert::same('', HtmlHelpers::escapeHtmlAttr(null)); Assert::same('', HtmlHelpers::escapeHtmlAttr('')); Assert::same('1', HtmlHelpers::escapeHtmlAttr(1)); -Assert::same('&lt;b&gt; &amp; " '', HtmlHelpers::escapeHtmlAttr(' & " \'')); -Assert::same('<!-- -->{ {<!-- -->{<!-- -->', HtmlHelpers::escapeHtmlAttr('{ {{')); +Assert::same('&lt;b&gt; &amp; &quot; &apos;', HtmlHelpers::escapeHtmlAttr(' & " \'')); +Assert::same('&#123; &#123;&#123;', HtmlHelpers::escapeHtmlAttr('{ {{')); Assert::same('&amp;amp;', HtmlHelpers::escapeHtmlAttr('&')); Assert::same('<b title="x">a &amp; b</b>', HtmlHelpers::escapeHtmlAttr(new Html('a & b'))); Assert::same('{', HtmlHelpers::escapeHtmlAttr(new Html('{'))); diff --git a/tests/runtime/HtmlHelpers.escapeRawHtml.phpt b/tests/runtime/HtmlHelpers.escapeRawHtml.phpt index 28a143f4e..657555bbb 100644 --- a/tests/runtime/HtmlHelpers.escapeRawHtml.phpt +++ b/tests/runtime/HtmlHelpers.escapeRawHtml.phpt @@ -31,9 +31,7 @@ Assert::same('

', HtmlHelpers::escapeRawHtml(new Latte\Runtime\Html('

', HtmlHelpers::escapeRawHtml(new Latte\Runtime\Html(''))); // JS -Assert::same('hello {{ worlds }}', HtmlHelpers::escapeRawHtml('hello {{ worlds }}')); -Assert::same('a{', HtmlHelpers::escapeRawHtml('a{')); -Assert::same('{a', HtmlHelpers::escapeRawHtml('{a')); +Assert::same('hello {{ worlds }}', HtmlHelpers::escapeRawHtml('hello {{ worlds }}')); // invalid UTF-8 Assert::same("foo \u{FFFD} bar", HtmlHelpers::escapeRawHtml("foo \u{D800} bar")); // invalid codepoint high surrogates From d427922d79f4ec0dbe625afd6d11f93feb2dcac2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Kapic=CC=8Cka?= Date: Thu, 12 Nov 2026 20:35:09 +0100 Subject: [PATCH 13/14] Linter: add --fail-on-warnings flag to treat DEPRECATED/WARNING as errors --- bin/latte-lint | 7 +++++-- src/Tools/Linter.php | 9 +++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/bin/latte-lint b/bin/latte-lint index 5884e95e8..b71d2d7bd 100755 --- a/bin/latte-lint +++ b/bin/latte-lint @@ -19,7 +19,7 @@ Latte linter $argv = $_SERVER['argv'] ?? []; if (count($argv) < 2) { - echo "Usage: latte-lint \n"; + echo "Usage: latte-lint [--strict] [--fail-on-warnings] [--debug]\n"; exit(1); } @@ -29,11 +29,14 @@ if ($debug = in_array('--debug', $argv, true)) { if ($strict = in_array('--strict', $argv, true)) { echo "Strict mode\n"; } +if ($failOnWarnings = in_array('--fail-on-warnings', $argv, true)) { + echo "Fail on warnings mode\n"; +} $path = $argv[1]; try { - $linter = new Latte\Tools\Linter(debug: $debug, strict: $strict); + $linter = new Latte\Tools\Linter(debug: $debug, strict: $strict, failOnWarnings: $failOnWarnings); $ok = $linter->scanDirectory($path); exit($ok ? 0 : 1); diff --git a/src/Tools/Linter.php b/src/Tools/Linter.php index ff15e501d..018d1a5de 100644 --- a/src/Tools/Linter.php +++ b/src/Tools/Linter.php @@ -25,6 +25,7 @@ public function __construct( private ?Latte\Engine $engine = null, private readonly bool $debug = false, private readonly bool $strict = false, + private readonly bool $failOnWarnings = false, ) { } @@ -104,15 +105,19 @@ public function lintLatte(string $file): bool $this->writeError('ERROR', $file, 'unable to read file'); return false; } + $hasWarning = false; + if (str_starts_with($s, "\xEF\xBB\xBF")) { $this->writeError('WARNING', $file, 'contains BOM'); + $hasWarning = true; } - set_error_handler(function (int $severity, string $message, string $errFile = '', int $errLine = 0) use ($file): bool { + set_error_handler(function (int $severity, string $message, string $errFile = '', int $errLine = 0) use ($file, &$hasWarning): bool { if (in_array($severity, [E_USER_DEPRECATED, E_USER_WARNING, E_USER_NOTICE], true)) { $pos = preg_match('~on line (\d+)~', $message, $m) ? ':' . $m[1] : ''; $label = $severity === E_USER_DEPRECATED ? 'DEPRECATED' : 'WARNING'; $this->writeError($label, $file . $pos, $message); + $hasWarning = true; return true; } return false; @@ -136,7 +141,7 @@ public function lintLatte(string $file): bool restore_error_handler(); } - return true; + return !($this->failOnWarnings && $hasWarning); } From 78229ba745f65ac584f74927b0641639b64ee2a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Kapic=CC=8Cka?= Date: Thu, 12 Nov 2026 20:41:21 +0100 Subject: [PATCH 14/14] Tests: add Linter.failOnWarnings tests --- tests/linter/Linter.failOnWarnings.phpt | 36 +++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 tests/linter/Linter.failOnWarnings.phpt diff --git a/tests/linter/Linter.failOnWarnings.phpt b/tests/linter/Linter.failOnWarnings.phpt new file mode 100644 index 000000000..965ffb600 --- /dev/null +++ b/tests/linter/Linter.failOnWarnings.phpt @@ -0,0 +1,36 @@ +lintLatte(__DIR__ . '/templates/unknown.latte')); +}); + + +test('warnings fail with failOnWarnings', function () { + $linter = new Linter(failOnWarnings: true); + Assert::false($linter->lintLatte(__DIR__ . '/templates/unknown.latte')); +}); + + +test('no warnings pass with failOnWarnings', function () { + $linter = new Linter(failOnWarnings: true); + Assert::true($linter->lintLatte(__DIR__ . '/templates/known.latte')); +}); + + +test('BOM fails with failOnWarnings', function () { + $file = tempnam(sys_get_temp_dir(), 'latte-'); + file_put_contents($file, "\xEF\xBB\xBF" . '{$text}'); + try { + $linter = new Linter(failOnWarnings: true); + Assert::false($linter->lintLatte($file)); + } finally { + unlink($file); + } +});