From a116e5482c2ae2268393f647438b38725f4a7cfd Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 27 Dec 2022 01:47:51 +0100 Subject: [PATCH] FormNNameNode: $form is created before tag itself [Closes #291] --- .../FormsLatte/Nodes/FormNNameNode.php | 24 ++++---- tests/Forms.Latte3/expected/forms.button.php | 5 +- tests/Forms.Latte3/expected/forms.get.php | 6 +- tests/Forms.Latte3/expected/forms.php | 23 +++++--- tests/Forms.Latte3/n-name.form.phpt | 55 +++++++++++++++++++ 5 files changed, 91 insertions(+), 22 deletions(-) create mode 100644 tests/Forms.Latte3/n-name.form.phpt diff --git a/src/Bridges/FormsLatte/Nodes/FormNNameNode.php b/src/Bridges/FormsLatte/Nodes/FormNNameNode.php index e6869467a..2de161e4a 100644 --- a/src/Bridges/FormsLatte/Nodes/FormNNameNode.php +++ b/src/Bridges/FormsLatte/Nodes/FormNNameNode.php @@ -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, + ); } @@ -50,14 +61,7 @@ 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, ))); @@ -65,7 +69,7 @@ private function init(Tag $tag) $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, )), ]); diff --git a/tests/Forms.Latte3/expected/forms.button.php b/tests/Forms.Latte3/expected/forms.button.php index a75a54684..ab94e396a 100644 --- a/tests/Forms.Latte3/expected/forms.button.php +++ b/tests/Forms.Latte3/expected/forms.button.php @@ -1,7 +1,7 @@ global->formsStack[] = $this->global->uiControl['myForm'] /* line %d% */; + echo 'global->formsStack), [], false) /* line %d% */; echo '> getCaption()) /* 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 ' '; + array_pop($this->global->formsStack); %A% diff --git a/tests/Forms.Latte3/expected/forms.get.php b/tests/Forms.Latte3/expected/forms.get.php index 358aef9c8..06530d61b 100644 --- a/tests/Forms.Latte3/expected/forms.get.php +++ b/tests/Forms.Latte3/expected/forms.get.php @@ -6,12 +6,14 @@ echo ' -global->formsStack[] = $this->global->uiControl['myForm'] /* line %d% */; + echo '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 ' '; + array_pop($this->global->formsStack); %A% diff --git a/tests/Forms.Latte3/expected/forms.php b/tests/Forms.Latte3/expected/forms.php index 38f271e98..c9551ec81 100644 --- a/tests/Forms.Latte3/expected/forms.php +++ b/tests/Forms.Latte3/expected/forms.php @@ -174,22 +174,24 @@ '; if (1) /* line %d% */ { - echo 'global->formsStack[] = $this->global->uiControl['myForm'] /* line %d% */; + echo 'global->formsStack), ['id' => null, 'class' => null], false) /* line %d% */; echo ' id="myForm" class="ajax"> 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 ' '; + array_pop($this->global->formsStack); } echo ' -global->formsStack[] = $this->global->uiControl['myForm'] /* line %d% */; + echo 'global->formsStack), ['class' => null], false) /* line %d% */; echo ($ʟ_tmp = array_filter(['nclass'])) ? ' class="' . LR\Filters::escapeHtmlAttr(implode(" ", array_unique($ʟ_tmp))) . '"' : "" /* line %d% */; echo '> @@ -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 ' +'; + array_pop($this->global->formsStack); + echo ' - -global->formsStack[] = is_object($ʟ_tmp = $this->global->uiControl['myForm']) ? $ʟ_tmp : $this->global->uiControl[$ʟ_tmp] /* line %d% */; + echo 'global->formsStack), [], false) /* line %d% */; echo '> 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 ' - +'; + array_pop($this->global->formsStack); + echo ' global))->getControlPart()->attributes() /* line %d% */; diff --git a/tests/Forms.Latte3/n-name.form.phpt b/tests/Forms.Latte3/n-name.form.phpt new file mode 100644 index 000000000..a97fc17db --- /dev/null +++ b/tests/Forms.Latte3/n-name.form.phpt @@ -0,0 +1,55 @@ +setLoader(new Latte\Loaders\StringLoader); +$latte->addExtension(new FormsExtension); + +Assert::match( + <<<'XX' + %A% + $form = $this->global->formsStack[] = $this->global->uiControl['foo'] /* line 1 */; + echo 'global->formsStack), [], false) /* line 1 */; + echo '>'; + echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(end($this->global->formsStack), false) /* line 1 */; + echo ''; + array_pop($this->global->formsStack); + %A% + XX, + $latte->compile('
'), +); + + +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] = '' . $ʟ_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('
'), +);