From 480e76dee8dd047c6594ba9437b9ebdd0b0cba5c Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 5 Oct 2023 05:12:42 +0200 Subject: [PATCH] Latte: split initialization and form rendering allows
--- src/Bridges/FormsLatte/FormMacros.php | 9 ++++++--- src/Bridges/FormsLatte/Nodes/FormNNameNode.php | 1 + src/Bridges/FormsLatte/Nodes/FormNode.php | 1 + src/Bridges/FormsLatte/Runtime.php | 12 ++++++++---- tests/Forms.Latte2/expected/FormMacros.button.php | 1 + .../expected/FormMacros.formContainer.php | 4 +++- tests/Forms.Latte2/expected/FormMacros.forms.php | 15 ++++++++++++--- tests/Forms.Latte2/expected/FormMacros.get.php | 5 ++++- tests/Forms.Latte3/expected/forms.button.php | 1 + .../Forms.Latte3/expected/forms.formContainer.php | 1 + tests/Forms.Latte3/expected/forms.get.php | 2 ++ tests/Forms.Latte3/expected/forms.php | 7 +++++++ tests/Forms.Latte3/n-name.form.phpt | 2 ++ 13 files changed, 49 insertions(+), 12 deletions(-) diff --git a/src/Bridges/FormsLatte/FormMacros.php b/src/Bridges/FormsLatte/FormMacros.php index 736ca3349..6db574965 100644 --- a/src/Bridges/FormsLatte/FormMacros.php +++ b/src/Bridges/FormsLatte/FormMacros.php @@ -67,11 +67,13 @@ public function macroForm(MacroNode $node, PhpWriter $writer) $node->replaced = true; $node->tokenizer->reset(); return $writer->write( - 'echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form = $this->global->formsStack[] = ' + '$form = $this->global->formsStack[] = ' . ($name[0] === '$' ? 'is_object($ʟ_tmp = %node.word) ? $ʟ_tmp : $this->global->uiControl[$ʟ_tmp]' : '$this->global->uiControl[%node.word]') - . ', %node.array)' + . ';' + . 'Nette\Bridges\FormsLatte\Runtime::initializeForm($form);' + . 'echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form, %node.array)' . " /* line $node->startLine */;" ); } @@ -226,7 +228,8 @@ public function macroNameAttr(MacroNode $node, PhpWriter $writer) . ($name[0] === '$' ? 'is_object($ʟ_tmp = %0.word) ? $ʟ_tmp : $this->global->uiControl[$ʟ_tmp]' : '$this->global->uiControl[%0.word]') - . " /* line $node->startLine */; ?>", + . " /* line $node->startLine */;" + . 'Nette\Bridges\FormsLatte\Runtime::initializeForm($form); ?>', $name ); return $writer->write( diff --git a/src/Bridges/FormsLatte/Nodes/FormNNameNode.php b/src/Bridges/FormsLatte/Nodes/FormNNameNode.php index 2de161e4a..75119298e 100644 --- a/src/Bridges/FormsLatte/Nodes/FormNNameNode.php +++ b/src/Bridges/FormsLatte/Nodes/FormNNameNode.php @@ -47,6 +47,7 @@ public function print(PrintContext $context): string ? '$this->global->uiControl[%node]' : 'is_object($ʟ_tmp = %node) ? $ʟ_tmp : $this->global->uiControl[$ʟ_tmp]') . ' %line;' + . 'Nette\Bridges\FormsLatte\Runtime::initializeForm($form);' . '%node ' . 'array_pop($this->global->formsStack);', $this->name, diff --git a/src/Bridges/FormsLatte/Nodes/FormNode.php b/src/Bridges/FormsLatte/Nodes/FormNode.php index c2902d78d..2146a18a3 100644 --- a/src/Bridges/FormsLatte/Nodes/FormNode.php +++ b/src/Bridges/FormsLatte/Nodes/FormNode.php @@ -66,6 +66,7 @@ public function print(PrintContext $context): string ? '$this->global->uiControl[%node]' : 'is_object($ʟ_tmp = %node) ? $ʟ_tmp : $this->global->uiControl[$ʟ_tmp]') . ' %line;' + . 'Nette\Bridges\FormsLatte\Runtime::initializeForm($form);' . ($this->print ? 'echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form, %node) %1.line;' : '') diff --git a/src/Bridges/FormsLatte/Runtime.php b/src/Bridges/FormsLatte/Runtime.php index 1e1eb3605..4eee9413f 100644 --- a/src/Bridges/FormsLatte/Runtime.php +++ b/src/Bridges/FormsLatte/Runtime.php @@ -23,16 +23,20 @@ class Runtime { use Nette\StaticClass; - /** - * Renders form begin. - */ - public static function renderFormBegin(Form $form, array $attrs, bool $withTags = true): string + public static function initializeForm(Form $form): void { $form->fireRenderEvents(); foreach ($form->getControls() as $control) { $control->setOption('rendered', false); } + } + + /** + * Renders form begin. + */ + public static function renderFormBegin(Form $form, array $attrs, bool $withTags = true): string + { $el = $form->getElementPrototype(); $el->action = (string) $el->action; $el = clone $el; diff --git a/tests/Forms.Latte2/expected/FormMacros.button.php b/tests/Forms.Latte2/expected/FormMacros.button.php index eee0f8d0f..984e031a8 100644 --- a/tests/Forms.Latte2/expected/FormMacros.button.php +++ b/tests/Forms.Latte2/expected/FormMacros.button.php @@ -1,6 +1,7 @@ global->formsStack[] = $this->global->uiControl["myForm"] /* line 1 */; + Nette\Bridges\FormsLatte\Runtime::initializeForm($form); echo 'global->formsStack), [], false); echo '> diff --git a/tests/Forms.Latte2/expected/FormMacros.formContainer.php b/tests/Forms.Latte2/expected/FormMacros.formContainer.php index c0e393ec6..8b33447ec 100644 --- a/tests/Forms.Latte2/expected/FormMacros.formContainer.php +++ b/tests/Forms.Latte2/expected/FormMacros.formContainer.php @@ -1,6 +1,8 @@ global->formsStack[] = $this->global->uiControl["myForm"], []) /* line 1 */; + $form = $this->global->formsStack[] = $this->global->uiControl["myForm"]; + Nette\Bridges\FormsLatte\Runtime::initializeForm($form); + echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form, []) /* line 1 */; echo ' diff --git a/tests/Forms.Latte2/expected/FormMacros.forms.php b/tests/Forms.Latte2/expected/FormMacros.forms.php index ffb93af75..dda078042 100644 --- a/tests/Forms.Latte2/expected/FormMacros.forms.php +++ b/tests/Forms.Latte2/expected/FormMacros.forms.php @@ -1,6 +1,8 @@ global->formsStack[] = $this->global->uiControl["myForm"], ['id' => 'myForm', 'class'=>"ajax"]) /* line 1 */; + $form = $this->global->formsStack[] = $this->global->uiControl["myForm"]; + Nette\Bridges\FormsLatte\Runtime::initializeForm($form); + echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form, ['id' => 'myForm', 'class'=>"ajax"]) /* line 1 */; echo "\n"; $iterations = 0; foreach (['id', 'username', 'select', 'area', 'send'] as $name) /* line 2 */ { @@ -76,12 +78,16 @@ '; - echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form = $this->global->formsStack[] = $this->global->uiControl["myForm"], []) /* line 27 */; + $form = $this->global->formsStack[] = $this->global->uiControl["myForm"]; + Nette\Bridges\FormsLatte\Runtime::initializeForm($form); + echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form, []) /* line 27 */; echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack)); echo ' '; - echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form = $this->global->formsStack[] = $this->global->uiControl["myForm"], []) /* line 29 */; + $form = $this->global->formsStack[] = $this->global->uiControl["myForm"]; + Nette\Bridges\FormsLatte\Runtime::initializeForm($form); + echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form, []) /* line 29 */; echo "\n"; $iterations = 0; foreach ($form['sex']->items as $key => $label) /* line 31 */ { @@ -197,6 +203,7 @@ '; $form = $this->global->formsStack[] = $this->global->uiControl["myForm"] /* line 58 */; + Nette\Bridges\FormsLatte\Runtime::initializeForm($form); if (1) /* line 58 */ { echo 'global->formsStack), ['id' => null, 'class' => null], false); @@ -214,6 +221,7 @@ '; $form = $this->global->formsStack[] = $this->global->uiControl["myForm"] /* line 63 */; + Nette\Bridges\FormsLatte\Runtime::initializeForm($form); echo 'global->formsStack), ['class' => null], false); @@ -230,6 +238,7 @@ '; $form = $this->global->formsStack[] = is_object($ʟ_tmp = $this->global->uiControl['myForm']) ? $ʟ_tmp : $this->global->uiControl[$ʟ_tmp] /* line 68 */; + Nette\Bridges\FormsLatte\Runtime::initializeForm($form); echo 'global->formsStack), [], false); echo '> diff --git a/tests/Forms.Latte2/expected/FormMacros.get.php b/tests/Forms.Latte2/expected/FormMacros.get.php index 65fc816d8..ae17372cd 100644 --- a/tests/Forms.Latte2/expected/FormMacros.get.php +++ b/tests/Forms.Latte2/expected/FormMacros.get.php @@ -1,11 +1,14 @@ global->formsStack[] = $this->global->uiControl["myForm"], []) /* line 1 */; + $form = $this->global->formsStack[] = $this->global->uiControl["myForm"]; + Nette\Bridges\FormsLatte\Runtime::initializeForm($form); + echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form, []) /* line 1 */; echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack)); echo ' '; $form = $this->global->formsStack[] = $this->global->uiControl["myForm"] /* line 3 */; + Nette\Bridges\FormsLatte\Runtime::initializeForm($form); echo 'global->formsStack), [], false); echo '> diff --git a/tests/Forms.Latte3/expected/forms.button.php b/tests/Forms.Latte3/expected/forms.button.php index ab94e396a..39b37e7dd 100644 --- a/tests/Forms.Latte3/expected/forms.button.php +++ b/tests/Forms.Latte3/expected/forms.button.php @@ -1,6 +1,7 @@ global->formsStack[] = $this->global->uiControl['myForm'] /* line %d% */; + Nette\Bridges\FormsLatte\Runtime::initializeForm($form); echo 'global->formsStack), [], false) /* line %d% */; echo '> diff --git a/tests/Forms.Latte3/expected/forms.formContainer.php b/tests/Forms.Latte3/expected/forms.formContainer.php index cbe9ad366..893cc8e3a 100644 --- a/tests/Forms.Latte3/expected/forms.formContainer.php +++ b/tests/Forms.Latte3/expected/forms.formContainer.php @@ -1,6 +1,7 @@ global->formsStack[] = $this->global->uiControl['myForm'] /* line %d% */; + Nette\Bridges\FormsLatte\Runtime::initializeForm($form); echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form, []) /* line %d% */; echo '
diff --git a/tests/Forms.Latte3/expected/forms.get.php b/tests/Forms.Latte3/expected/forms.get.php index 06530d61b..25af74d41 100644 --- a/tests/Forms.Latte3/expected/forms.get.php +++ b/tests/Forms.Latte3/expected/forms.get.php @@ -1,6 +1,7 @@ global->formsStack[] = $this->global->uiControl['myForm'] /* line %d% */; + Nette\Bridges\FormsLatte\Runtime::initializeForm($form); echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form, []) /* line %d% */; echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack)) /* line %d% */; @@ -8,6 +9,7 @@ '; $form = $this->global->formsStack[] = $this->global->uiControl['myForm'] /* line %d% */; + Nette\Bridges\FormsLatte\Runtime::initializeForm($form); echo 'global->formsStack), [], false) /* line %d% */; echo '> diff --git a/tests/Forms.Latte3/expected/forms.php b/tests/Forms.Latte3/expected/forms.php index c9551ec81..ee2a1a269 100644 --- a/tests/Forms.Latte3/expected/forms.php +++ b/tests/Forms.Latte3/expected/forms.php @@ -1,6 +1,7 @@ global->formsStack[] = $this->global->uiControl['myForm'] /* line %d% */; + Nette\Bridges\FormsLatte\Runtime::initializeForm($form); echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form, ['id' => 'myForm', 'class' => 'ajax']) /* line %d% */; echo "\n"; foreach (['id', 'username', 'select', 'area', 'send'] as $name) /* line %d% */ { @@ -64,6 +65,7 @@ '; $form = $this->global->formsStack[] = $this->global->uiControl['myForm'] /* line %d% */; + Nette\Bridges\FormsLatte\Runtime::initializeForm($form); echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form, []) /* line %d% */; echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack)) /* line %d% */; @@ -71,6 +73,7 @@ '; $form = $this->global->formsStack[] = $this->global->uiControl['myForm'] /* line %d% */; + Nette\Bridges\FormsLatte\Runtime::initializeForm($form); echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form, []) /* line %d% */; echo "\n"; foreach ($form['sex']->items as $key => $label) /* line %d% */ { @@ -175,6 +178,7 @@ '; if (1) /* line %d% */ { $form = $this->global->formsStack[] = $this->global->uiControl['myForm'] /* line %d% */; + Nette\Bridges\FormsLatte\Runtime::initializeForm($form); echo 'global->formsStack), ['id' => null, 'class' => null], false) /* line %d% */; echo ' id="myForm" class="ajax"> @@ -191,6 +195,7 @@ '; $form = $this->global->formsStack[] = $this->global->uiControl['myForm'] /* line %d% */; + Nette\Bridges\FormsLatte\Runtime::initializeForm($form); echo 'global->formsStack), ['class' => null], false) /* line %d% */; echo ($ʟ_tmp = array_filter(['nclass'])) ? ' class="' . LR\Filters::escapeHtmlAttr(implode(" ", array_unique($ʟ_tmp))) . '"' : "" /* line %d% */; @@ -208,6 +213,7 @@ '; $form = $this->global->formsStack[] = is_object($ʟ_tmp = $this->global->uiControl['myForm']) ? $ʟ_tmp : $this->global->uiControl[$ʟ_tmp] /* line %d% */; + Nette\Bridges\FormsLatte\Runtime::initializeForm($form); echo 'global->formsStack), [], false) /* line %d% */; echo '> @@ -250,6 +256,7 @@ '; $form = $this->global->formsStack[] = $this->global->uiControl['myForm'] /* line %d% */; + Nette\Bridges\FormsLatte\Runtime::initializeForm($form); echo ' global))->getLabelPart()->attributes() /* line %d% */; diff --git a/tests/Forms.Latte3/n-name.form.phpt b/tests/Forms.Latte3/n-name.form.phpt index 4201a796b..501110a59 100644 --- a/tests/Forms.Latte3/n-name.form.phpt +++ b/tests/Forms.Latte3/n-name.form.phpt @@ -22,6 +22,7 @@ Assert::match( <<<'XX' %A% $form = $this->global->formsStack[] = $this->global->uiControl['foo'] /* line 1 */; + Nette\Bridges\FormsLatte\Runtime::initializeForm($form); echo 'global->formsStack), [], false) /* line 1 */; echo '>'; @@ -38,6 +39,7 @@ Assert::match( <<<'XX' %A% $form = $this->global->formsStack[] = $this->global->uiControl['foo'] /* line 1 */; + Nette\Bridges\FormsLatte\Runtime::initializeForm($form); $ʟ_tag[0] = ''; if (0) /* line 1 */ { echo '<';