Skip to content

Commit 6d81a0e

Browse files
committed
UIMacros: {control} can be used in HTML attribute [Closes nette/latte#250]
1 parent fcb2578 commit 6d81a0e

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

src/Bridges/ApplicationLatte/UIMacros.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function macroControl(MacroNode $node, PhpWriter $writer)
8484
if ($node->modifiers) {
8585
trigger_error('Modifiers are deprecated in ' . $node->getNotation(), E_USER_DEPRECATED);
8686
} elseif ($node->context !== [Latte\Compiler::CONTENT_HTML, Latte\Compiler::CONTEXT_HTML_TEXT]) {
87-
trigger_error('Tag {control} must be used in HTML text.', E_USER_WARNING);
87+
$node->modifiers .= '|escape';
8888
}
8989

9090
$words = $node->tokenizer->fetchWords();
@@ -115,7 +115,10 @@ public function macroControl(MacroNode $node, PhpWriter $writer)
115115
. 'if ($_tmp instanceof Nette\Application\UI\Renderable) $_tmp->redrawControl(null, false); '
116116
. ($node->modifiers === ''
117117
? "\$_tmp->$method($param);"
118-
: $writer->write("ob_start(function () {}); \$_tmp->$method($param); echo %modify(ob_get_clean());")
118+
: $writer->write(
119+
"ob_start(function () {}); \$_tmp->$method($param); \$ʟ_fi = new LR\\FilterInfo(%var); echo %modifyContent(ob_get_clean());",
120+
Latte\Engine::CONTENT_HTML
121+
)
119122
);
120123
}
121124

tests/Bridges.Latte/UIMacros.control.3.phpt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ $latte = new Latte\Engine;
1717
$latte->setLoader(new Latte\Loaders\StringLoader);
1818
UIMacros::install($latte->getCompiler());
1919
$latte->addProvider('uiControl', new class {
20+
public function render()
21+
{
22+
echo '<>&amp;';
23+
}
24+
2025
public function __call($name, $args)
2126
{
2227
return new self;
@@ -25,12 +30,13 @@ $latte->addProvider('uiControl', new class {
2530

2631
Assert::error(function () use ($latte) {
2732
$latte->renderToString('<div {control x}');
28-
}, E_USER_WARNING, 'Tag {control} must be used in HTML text.');
33+
}, E_USER_WARNING, 'Filters: unable to convert content type HTML to HTMLTAG');
2934

30-
Assert::error(function () use ($latte) {
31-
$latte->renderToString('<div title="{control x}"');
32-
}, E_USER_WARNING, 'Tag {control} must be used in HTML text.');
35+
Assert::same(
36+
'<div title="&lt;&gt;&amp;">',
37+
$latte->renderToString('<div title="{control x}">')
38+
);
3339

3440
Assert::error(function () use ($latte) {
3541
$latte->renderToString('<style> {control x} </style>');
36-
}, E_USER_WARNING, 'Tag {control} must be used in HTML text.');
42+
}, E_USER_WARNING, 'Filters: unable to convert content type HTML to HTMLCSS');

tests/Bridges.Latte/UIMacros.control.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ UIMacros::install($compiler);
1818

1919
// {control ...}
2020
Assert::match('<?php %a% $this->global->uiControl->getComponent("form"); %a%->render(); ?>', $compiler->expandMacro('control', 'form', '')->openingCode);
21-
@Assert::match('<?php %a% $this->global->uiControl->getComponent("form"); %a%->render(); echo ($this->filters->filter)(%a%); ?>', $compiler->expandMacro('control', 'form', 'filter')->openingCode); // @ deprecated
21+
@Assert::match('<?php %a% $this->global->uiControl->getComponent("form"); %a%->render(); $ʟ_fi = new LR\FilterInfo(\'html\'); echo $this->filters->filterContent(\'filter\', $ʟ_fi, ob_get_clean()); ?>', $compiler->expandMacro('control', 'form', 'filter')->openingCode); // @deprecated
2222
Assert::match('<?php %a% if (is_object($form)) %a% else %a% $this->global->uiControl->getComponent($form); %a%->render(); ?>', $compiler->expandMacro('control', '$form', '')->openingCode);
2323
Assert::match('<?php %a% $this->global->uiControl->getComponent("form"); %a%->renderType(); ?>', $compiler->expandMacro('control', 'form:type', '')->openingCode);
2424
Assert::match('<?php %a% $this->global->uiControl->getComponent("form"); %a%->{"render$type"}(); ?>', $compiler->expandMacro('control', 'form:$type', '')->openingCode);
2525
Assert::match('<?php %a% $this->global->uiControl->getComponent("form"); %a%->renderType(\'param\'); ?>', $compiler->expandMacro('control', 'form:type param', '')->openingCode);
2626
Assert::match('<?php %a% $this->global->uiControl->getComponent("form"); %a%->render(array_merge([], $params, [])); ?>', $compiler->expandMacro('control', 'form (expand) $params', '')->openingCode);
2727
Assert::match('<?php %a% $this->global->uiControl->getComponent("form"); %a%->renderType([\'param\' => 123]); ?>', $compiler->expandMacro('control', 'form:type param => 123', '')->openingCode);
2828
Assert::match('<?php %a% $this->global->uiControl->getComponent("form"); %a%->renderType([\'param\' => 123]); ?>', $compiler->expandMacro('control', 'form:type, param => 123', '')->openingCode);
29-
@Assert::match('<?php %a% $this->global->uiControl->getComponent("form"); %a%->render(); echo ($this->filters->striptags)(%a%); ?>', $compiler->expandMacro('control', 'form', 'striptags')->openingCode); // @ deprecated
29+
@Assert::match('<?php %a% $this->global->uiControl->getComponent("form"); %a%->render(); $ʟ_fi = new LR\FilterInfo(\'html\'); echo $this->filters->filterContent(\'striptags\', $ʟ_fi, ob_get_clean()); ?>', $compiler->expandMacro('control', 'form', 'striptags')->openingCode); // @ deprecated
3030
Assert::match('<?php %a% $this->global->uiControl->getComponent("form"); %a%->renderType(param: 123); ?>', $compiler->expandMacro('control', 'form:type, param: 123', '')->openingCode);

0 commit comments

Comments
 (0)