From 05187e99af7032a1debfef64270404ff37e41e0e Mon Sep 17 00:00:00 2001 From: David Grudl Date: Mon, 16 Jan 2017 06:09:01 +0100 Subject: [PATCH] BlockMacros: dynamic blocks with content-types html & htmlattr are compatible [Closes #146] --- src/Latte/Macros/BlockMacros.php | 5 ++++- tests/Latte/contentType.compatibility.phpt | 7 ++++++- tests/Latte/expected/BlockMacros.dynamicblock.phtml | 2 +- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/Latte/Macros/BlockMacros.php b/src/Latte/Macros/BlockMacros.php index 868a4133d..c20f61d53 100644 --- a/src/Latte/Macros/BlockMacros.php +++ b/src/Latte/Macros/BlockMacros.php @@ -249,7 +249,10 @@ public function macroBlock(MacroNode $node, PhpWriter $writer) if ($node->name === 'define') { $node->closingCode = ''; } else { - if ($node->modifiers) { + if (Helpers::startsWith((string) $node->context[1], Latte\Compiler::CONTEXT_HTML_ATTRIBUTE)) { + $node->context[1] = ''; + $node->modifiers .= '|escape'; + } elseif ($node->modifiers) { $node->modifiers .= '|escape'; } $node->closingCode = $writer->write('renderBlock(%raw, get_defined_vars()' diff --git a/tests/Latte/contentType.compatibility.phpt b/tests/Latte/contentType.compatibility.phpt index 3534a0517..6d8d9d8fd 100644 --- a/tests/Latte/contentType.compatibility.phpt +++ b/tests/Latte/contentType.compatibility.phpt @@ -53,6 +53,11 @@ test(function () { $latte->renderToString('{include main}{block main}
foo
{/block}') ); + Assert::same( + 'a"B

', + $latte->renderToString('{block main}a"B{/block}{var $name = main}

') + ); + Assert::same( '---', $latte->renderToString('{block main}---{/block}') @@ -340,7 +345,7 @@ Assert::error(function () use ($latte) { },E_USER_WARNING, 'Filter |upper is called with incompatible content type HTML, try to prepend |stripHtml.'); Assert::same('

" <

', $latte->renderToString('context1c')); -Assert::same('

', $latte->renderToString('context2')); +Assert::same('

', $latte->renderToString('context2')); Assert::same('

', $latte->renderToString('context2a')); Assert::same('', $latte->renderToString('context6')); diff --git a/tests/Latte/expected/BlockMacros.dynamicblock.phtml b/tests/Latte/expected/BlockMacros.dynamicblock.phtml index 8b48321dc..faa9705a8 100644 --- a/tests/Latte/expected/BlockMacros.dynamicblock.phtml +++ b/tests/Latte/expected/BlockMacros.dynamicblock.phtml @@ -63,7 +63,7 @@ class Template%a% extends Latte\Runtime\Template $this->blockQueue["strip$name"][] = [$this, 'blockStrip_name']; $this->renderBlock("strip$name", get_defined_vars(), function ($s, $type) { $_fi = new LR\FilterInfo($type); - return $this->filters->filterContent('striptags', $_fi, $s); + return LR\Filters::convertTo($_fi, 'html', $this->filters->filterContent('striptags', $_fi, $s)); }); ?>