Skip to content

Commit

Permalink
FormNNameNode: $form is created before tag itself [Closes #291]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Dec 27, 2022
1 parent 397e38b commit a116e54
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 22 deletions.
24 changes: 14 additions & 10 deletions src/Bridges/FormsLatte/Nodes/FormNNameNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,18 @@ public static function create(Tag $tag): \Generator

public function print(PrintContext $context): string
{
return $this->content->print($context);
return $context->format(
'$form = $this->global->formsStack[] = '
. ($this->name instanceof StringNode
? '$this->global->uiControl[%node]'
: 'is_object($ʟ_tmp = %node) ? $ʟ_tmp : $this->global->uiControl[$ʟ_tmp]')
. ' %line;'
. '%node '
. 'array_pop($this->global->formsStack);',
$this->name,
$this->position,
$this->content,
);
}


Expand All @@ -50,22 +61,15 @@ private function init(Tag $tag)
$el = $tag->htmlElement;

$tag->replaceNAttribute(new AuxiliaryNode(fn(PrintContext $context) => $context->format(
'$form = $this->global->formsStack[] = '
. ($this->name instanceof StringNode
? '$this->global->uiControl[%node]'
: 'is_object($ʟ_tmp = %node) ? $ʟ_tmp : $this->global->uiControl[$ʟ_tmp]')
. ' %line;'
. 'echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin(end($this->global->formsStack), %dump, false) %line;',
$this->name,
$this->position,
'echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin(end($this->global->formsStack), %dump, false) %line;',
array_fill_keys(FieldNNameNode::findUsedAttributes($el), null),
$this->position,
)));

$el->content = new Latte\Compiler\Nodes\FragmentNode([
$el->content,
new AuxiliaryNode(fn(PrintContext $context) => $context->format(
'echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack), false) %line;',
'echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(end($this->global->formsStack), false) %line;',
$this->position,
)),
]);
Expand Down
5 changes: 3 additions & 2 deletions tests/Forms.Latte3/expected/forms.button.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
%A%
echo '<form';
$form = $this->global->formsStack[] = $this->global->uiControl['myForm'] /* line %d% */;
echo '<form';
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin(end($this->global->formsStack), [], false) /* line %d% */;
echo '>
<button';
Expand All @@ -20,7 +20,8 @@
echo LR\Filters::escapeHtmlText($ʟ_input->getCaption()) /* line %d% */;
echo '</button>
';
echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack), false) /* line %d% */;
echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(end($this->global->formsStack), false) /* line %d% */;
echo '</form>
';
array_pop($this->global->formsStack);
%A%
6 changes: 4 additions & 2 deletions tests/Forms.Latte3/expected/forms.get.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@

echo '
<form';
';
$form = $this->global->formsStack[] = $this->global->uiControl['myForm'] /* line %d% */;
echo '<form';
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin(end($this->global->formsStack), [], false) /* line %d% */;
echo '>
';
echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack), false) /* line %d% */;
echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(end($this->global->formsStack), false) /* line %d% */;
echo '</form>
';
array_pop($this->global->formsStack);
%A%
23 changes: 15 additions & 8 deletions tests/Forms.Latte3/expected/forms.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,22 +174,24 @@
';
if (1) /* line %d% */ {
echo '<form';
$form = $this->global->formsStack[] = $this->global->uiControl['myForm'] /* line %d% */;
echo '<form';
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin(end($this->global->formsStack), ['id' => null, 'class' => null], false) /* line %d% */;
echo ' id="myForm" class="ajax">
<input';
echo ($ʟ_input = Nette\Bridges\FormsLatte\Runtime::item('username', $this->global))->getControlPart()->attributes() /* line %d% */;
echo '>
';
echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack), false) /* line %d% */;
echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(end($this->global->formsStack), false) /* line %d% */;
echo '</form>
';
array_pop($this->global->formsStack);
}
echo '
<form';
';
$form = $this->global->formsStack[] = $this->global->uiControl['myForm'] /* line %d% */;
echo '<form';
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin(end($this->global->formsStack), ['class' => null], false) /* line %d% */;
echo ($ʟ_tmp = array_filter(['nclass'])) ? ' class="' . LR\Filters::escapeHtmlAttr(implode(" ", array_unique($ʟ_tmp))) . '"' : "" /* line %d% */;
echo '>
Expand All @@ -198,21 +200,26 @@
echo ($ʟ_tmp = array_filter(['nclass'])) ? ' class="' . LR\Filters::escapeHtmlAttr(implode(" ", array_unique($ʟ_tmp))) . '"' : "" /* line %d% */;
echo '>
';
echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack), false) /* line %d% */;
echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(end($this->global->formsStack), false) /* line %d% */;
echo '</form>
';
array_pop($this->global->formsStack);
echo '
<FORM';
';
$form = $this->global->formsStack[] = is_object($ʟ_tmp = $this->global->uiControl['myForm']) ? $ʟ_tmp : $this->global->uiControl[$ʟ_tmp] /* line %d% */;
echo '<FORM';
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin(end($this->global->formsStack), [], false) /* line %d% */;
echo '>
<input';
echo ($ʟ_input = Nette\Bridges\FormsLatte\Runtime::item('username', $this->global))->getControlPart()->attributes() /* line %d% */;
echo '>
';
echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack), false) /* line %d% */;
echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(end($this->global->formsStack), false) /* line %d% */;
echo '</FORM>
';
array_pop($this->global->formsStack);
echo '
<select';
echo ($ʟ_input = Nette\Bridges\FormsLatte\Runtime::item('select', $this->global))->getControlPart()->attributes() /* line %d% */;
Expand Down
55 changes: 55 additions & 0 deletions tests/Forms.Latte3/n-name.form.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/** @phpVersion 8.0 */

declare(strict_types=1);

use Nette\Bridges\FormsLatte\FormsExtension;
use Tester\Assert;

require __DIR__ . '/../bootstrap.php';

if (version_compare(Latte\Engine::VERSION, '3', '<')) {
Tester\Environment::skip('Test for Latte 3');
}


$latte = new Latte\Engine;
$latte->setLoader(new Latte\Loaders\StringLoader);
$latte->addExtension(new FormsExtension);

Assert::match(
<<<'XX'
%A%
$form = $this->global->formsStack[] = $this->global->uiControl['foo'] /* line 1 */;
echo '<form';
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin(end($this->global->formsStack), [], false) /* line 1 */;
echo '>';
echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(end($this->global->formsStack), false) /* line 1 */;
echo '</form>';
array_pop($this->global->formsStack);
%A%
XX,
$latte->compile('<form n:name="foo"></form>'),
);


Assert::match(
<<<'XX'
%A%
$form = $this->global->formsStack[] = $this->global->uiControl['foo'] /* line 1 */;
$ʟ_tag[0] = '';
if (0) /* line 1 */ {
echo '<';
echo $ʟ_tmp = ('form');
$ʟ_tag[0] = '</' . $ʟ_tmp . '>' . $ʟ_tag[0];
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin(end($this->global->formsStack), [], false) /* line 1 */;
echo '>';
}
echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(end($this->global->formsStack), false) /* line 1 */;
echo $ʟ_tag[0];
array_pop($this->global->formsStack);
%A%
XX,
$latte->compile('<form n:tag-if=0 n:name="foo"></form>'),
);

0 comments on commit a116e54

Please sign in to comment.