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/docs/internals/escaping.md b/docs/internals/escaping.md index 25802a9f0..ef07145f6 100644 --- a/docs/internals/escaping.md +++ b/docs/internals/escaping.md @@ -12,9 +12,10 @@ layers that must stay in sync**. `(contentType, state, subType)`, emits a string that **calls** a runtime escaping function (e.g. `LR\HtmlHelpers::escapeText(...)`, `LR\Helpers::escapeCss(...)`; plain-text output goes through the `escape` filter instead). The subType is not - purely state-derived: `enterHtmlAttribute` infers `js` for `on*` and `css` for - `style` attributes, and `enterHtmlText` classifies a `"}\'> {=""} '), ); +// braces as entities for client-side template engines, which decode them also in attributes +Assert::match( + '', + $latte->renderToString('', ['x' => '{{7*7}}', 'y' => '{"a":1}']), +); + // escaping of Html object Assert::match( '', diff --git a/tests/common/contentType.html.srcdoc.phpt b/tests/common/contentType.html.srcdoc.phpt new file mode 100644 index 000000000..6c7511184 --- /dev/null +++ b/tests/common/contentType.html.srcdoc.phpt @@ -0,0 +1,102 @@ + ' & {', + 'html' => new Html('bold'), +]; + +// text is escaped for an attribute of the document and then for the srcdoc attribute +Assert::same( + '', + $latte->renderToString('', ['x' => '" onmouseover="alert(1)']), +); + +Assert::same( + '', + $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/filters/bytes.phpt b/tests/filters/bytes.phpt index c4fd3719d..bc8bdef18 100644 --- a/tests/filters/bytes.phpt +++ b/tests/filters/bytes.phpt @@ -16,6 +16,8 @@ test('no locale', function () { Assert::same('0 B', $filters->bytes(0.1)); Assert::same('-1.03 GB', $filters->bytes(-1024 * 1024 * 1050)); Assert::same('8881.78 PB', $filters->bytes(1e19)); + Assert::same('1.13 kB', $filters->bytes(1152)); + Assert::same('-1.13 kB', $filters->bytes(-1152)); }); @@ -26,4 +28,6 @@ test('with locale', function () { Assert::same('0 B', $filters->bytes(0.1)); Assert::same('-1,03 GB', $filters->bytes(-1024 * 1024 * 1050)); Assert::same("8\u{00A0}881,78 PB", $filters->bytes(1e19)); + Assert::same('1,13 kB', $filters->bytes(1152)); + Assert::same('-1,13 kB', $filters->bytes(-1152)); }); diff --git a/tests/filters/number.phpt b/tests/filters/number.phpt index 4fb5324da..99566ae00 100644 --- a/tests/filters/number.phpt +++ b/tests/filters/number.phpt @@ -44,6 +44,9 @@ test('with locale', function () { Assert::same("-1\u{00A0}234", $filters->number(-1234)); Assert::same("-1\u{00A0}234,57", $filters->number(-1234.5678, 2)); Assert::same('NaN', $filters->number(NAN, 2)); + Assert::same('9,11', $filters->number(9.105, 2)); + Assert::same('-9,11', $filters->number(-9.105, 2)); + Assert::same('0,29', $filters->number(0.285, 2)); }); @@ -54,6 +57,7 @@ test('pattern', function () { Assert::same("00\u{00A0}123,4560", $filters->number(123.456, '00,000.0000')); Assert::same('3,14__', $filters->number(3.14, '##0.00*_')); Assert::same('Kč 3,14', $filters->number(3.14, '¤ #,##0.00')); + Assert::same('9,11', $filters->number(9.105, '#,##0.00')); }); @@ -64,3 +68,15 @@ test('disabled locale', function () { Assert::same('1 234.56', $filters->number(1234.56, 2, '.', ' ')); Assert::same('1.234,56', $filters->number(1234.56, 2, ',', '.')); }); + + +test('changed locale', function () { + $filters = new Filters; + $filters->locale = 'cs_CZ'; + Assert::same('1,50', $filters->number(1.5, 2)); + Assert::same('2', $filters->number(1.5, '0')); + + $filters->locale = 'en_US'; + Assert::same('1.50', $filters->number(1.5, 2)); + Assert::same('1.50', $filters->number(1.5, '0.00')); +}); 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); + } +}); 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 new file mode 100644 index 000000000..5353b9f24 --- /dev/null +++ b/tests/runtime/HtmlHelpers.escapeHtmlAttr.phpt @@ -0,0 +1,16 @@ + & " \'')); +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 c271f01f4..657555bbb 100644 --- a/tests/runtime/HtmlHelpers.escapeRawHtml.phpt +++ b/tests/runtime/HtmlHelpers.escapeRawHtml.phpt @@ -30,6 +30,9 @@ 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 }}')); + // 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}')); diff --git a/tests/runtime/HtmlHelpers.formatSrcdocAttribute.phpt b/tests/runtime/HtmlHelpers.formatSrcdocAttribute.phpt new file mode 100644 index 000000000..dbfce7f30 --- /dev/null +++ b/tests/runtime/HtmlHelpers.formatSrcdocAttribute.phpt @@ -0,0 +1,44 @@ +'; + } +} + + +Assert::same('srcdoc', HtmlHelpers::classifyAttributeType('srcdoc')); +Assert::same('srcdoc', HtmlHelpers::classifyAttributeType('SrcDoc')); + +// escaped by escapeHtmlAttr +Assert::same('srcdoc="&lt;b&gt;"', HtmlHelpers::formatSrcdocAttribute('srcdoc', '')); +Assert::same(' srcdoc="<b>"', HtmlHelpers::formatSrcdocAttribute(' srcdoc', new Html(''))); + +// migration warnings +foreach (['x', 'a & b', new StringObject] as $value) { + Assert::error( + fn() => HtmlHelpers::formatSrcdocAttribute('srcdoc', $value, migrationWarnings: true), + E_USER_WARNING, + "Behavior change for attribute 'srcdoc' with string value: previously it was rendered as HTML, now it is escaped as text (wrap trusted HTML in Latte\\Runtime\\Html).", + ); +} + +foreach (['a & b > " {', 1, new Html('x')] as $value) { // renders the same document as before + Assert::noError(fn() => HtmlHelpers::formatSrcdocAttribute('srcdoc', $value, migrationWarnings: true)); +} + +// 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.", +); diff --git a/tests/tags/define.args.phpt b/tests/tags/define.args.phpt index fa5144203..55c9e5d9f 100644 --- a/tests/tags/define.args.phpt +++ b/tests/tags/define.args.phpt @@ -71,14 +71,19 @@ $template = <<<'XX' d) {include test, var2 => 1, 2} // invalid XX; -Assert::matchFile( - __DIR__ . '/expected/define.args2.php', - $latte->compile($template), -); -Assert::matchFile( - __DIR__ . '/expected/define.args2.html', - $latte->renderToString($template, ['var3' => 'outer']), -); +Assert::error(function () use ($latte, $template) { + Assert::matchFile( + __DIR__ . '/expected/define.args2.php', + $latte->compile($template), + ); + Assert::matchFile( + __DIR__ . '/expected/define.args2.html', + $latte->renderToString($template, ['var3' => 'outer']), + ); +}, [ + [E_USER_DEPRECATED, 'Positional argument after a named argument is deprecated (on line 13 at column 30)'], + [E_USER_DEPRECATED, 'Positional argument after a named argument is deprecated (on line 13 at column 30)'], +]); // named arguments (order dependent) 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 ' diff --git a/tests/tags/include.block.phpt b/tests/tags/include.block.phpt index f7a9e54b5..895c4ee8c 100644 --- a/tests/tags/include.block.phpt +++ b/tests/tags/include.block.phpt @@ -62,3 +62,14 @@ Assert::exception( InvalidArgumentException::class, 'Block name must be a string, null given.', ); + +Assert::error( + fn() => $latte->renderToString('{define block, $name, $value}{/define} {include block, name: 1, 2}'), + E_USER_DEPRECATED, + 'Positional argument after a named argument is deprecated (on line 1 at column 65)', +); + +Assert::match( + ' [1|2]', + $latte->renderToString('{define block, $name, $value}[{$name}|{$value}]{/define} {include block, 1, value: 2}'), +);