Skip to content

Commit

Permalink
[application] UIMacros: {control} can be used in HTML attribute [Closes
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jan 6, 2021
1 parent 158240b commit 15825a5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
7 changes: 5 additions & 2 deletions application/src/Bridges/ApplicationLatte/UIMacros.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function macroControl(MacroNode $node, PhpWriter $writer)
if ($node->modifiers) {
trigger_error('Modifiers are deprecated in ' . $node->getNotation(), E_USER_DEPRECATED);
} elseif ($node->context !== [Latte\Compiler::CONTENT_HTML, Latte\Compiler::CONTEXT_HTML_TEXT]) {
trigger_error('Tag {control} must be used in HTML text.', E_USER_WARNING);
$node->modifiers .= '|escape';
}

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

Expand Down
16 changes: 11 additions & 5 deletions application/tests/Bridges.Latte/UIMacros.control.3.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ $latte = new Latte\Engine;
$latte->setLoader(new Latte\Loaders\StringLoader);
UIMacros::install($latte->getCompiler());
$latte->addProvider('uiControl', new class {
public function render()
{
echo '<>&amp;';
}

public function __call($name, $args)
{
return new self;
Expand All @@ -25,12 +30,13 @@ $latte->addProvider('uiControl', new class {

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

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

Assert::error(function () use ($latte) {
$latte->renderToString('<style> {control x} </style>');
}, E_USER_WARNING, 'Tag {control} must be used in HTML text.');
}, E_USER_WARNING, 'Filters: unable to convert content type HTML to HTMLCSS');
4 changes: 2 additions & 2 deletions application/tests/Bridges.Latte/UIMacros.control.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ UIMacros::install($compiler);

// {control ...}
Assert::match('<?php %a% $this->global->uiControl->getComponent("form"); %a%->render(); ?>', $compiler->expandMacro('control', 'form', '')->openingCode);
@Assert::match('<?php %a% $this->global->uiControl->getComponent("form"); %a%->render(); echo ($this->filters->filter)(%a%); ?>', $compiler->expandMacro('control', 'form', 'filter')->openingCode); // @ deprecated
@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
Assert::match('<?php %a% if (is_object($form)) %a% else %a% $this->global->uiControl->getComponent($form); %a%->render(); ?>', $compiler->expandMacro('control', '$form', '')->openingCode);
Assert::match('<?php %a% $this->global->uiControl->getComponent("form"); %a%->renderType(); ?>', $compiler->expandMacro('control', 'form:type', '')->openingCode);
Assert::match('<?php %a% $this->global->uiControl->getComponent("form"); %a%->{"render$type"}(); ?>', $compiler->expandMacro('control', 'form:$type', '')->openingCode);
Assert::match('<?php %a% $this->global->uiControl->getComponent("form"); %a%->renderType(\'param\'); ?>', $compiler->expandMacro('control', 'form:type param', '')->openingCode);
Assert::match('<?php %a% $this->global->uiControl->getComponent("form"); %a%->render(array_merge([], $params, [])); ?>', $compiler->expandMacro('control', 'form (expand) $params', '')->openingCode);
Assert::match('<?php %a% $this->global->uiControl->getComponent("form"); %a%->renderType([\'param\' => 123]); ?>', $compiler->expandMacro('control', 'form:type param => 123', '')->openingCode);
Assert::match('<?php %a% $this->global->uiControl->getComponent("form"); %a%->renderType([\'param\' => 123]); ?>', $compiler->expandMacro('control', 'form:type, param => 123', '')->openingCode);
@Assert::match('<?php %a% $this->global->uiControl->getComponent("form"); %a%->render(); echo ($this->filters->striptags)(%a%); ?>', $compiler->expandMacro('control', 'form', 'striptags')->openingCode); // @ deprecated
@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
Assert::match('<?php %a% $this->global->uiControl->getComponent("form"); %a%->renderType(param: 123); ?>', $compiler->expandMacro('control', 'form:type, param: 123', '')->openingCode);

0 comments on commit 15825a5

Please sign in to comment.