Skip to content

Commit

Permalink
Latte: NNameNode split to two classes
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Dec 27, 2022
1 parent 9399a8d commit 397e38b
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 38 deletions.
4 changes: 3 additions & 1 deletion src/Bridges/FormsLatte/FormsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ public function getTags(): array
'inputError' => [Nodes\InputErrorNode::class, 'create'],
'formPrint' => [Nodes\FormPrintNode::class, 'create'],
'formClassPrint' => [Nodes\FormPrintNode::class, 'create'],
'n:name' => [Nodes\NNameNode::class, 'create'],
'n:name' => fn(Latte\Compiler\Tag $tag) => yield from strtolower($tag->htmlElement->name) === 'form'
? Nodes\FormNNameNode::create($tag)
: Nodes\FieldNNameNode::create($tag),
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace Nette\Bridges\FormsLatte\Nodes;

use Latte;
use Latte\Compiler\Nodes\AreaNode;
use Latte\Compiler\Nodes\AuxiliaryNode;
use Latte\Compiler\Nodes\Html\AttributeNode;
Expand All @@ -24,9 +23,9 @@


/**
* <form n:name>, <input n:name>, <select n:name>, <textarea n:name>, <label n:name> and <button n:name>
* <input n:name>, <select n:name>, <textarea n:name>, <label n:name> and <button n:name>
*/
final class NNameNode extends StatementNode
final class FieldNNameNode extends StatementNode
{
public ExpressionNode $name;
public ?ExpressionNode $part = null;
Expand All @@ -46,11 +45,7 @@ public static function create(Tag $tag): \Generator

[$node->content] = yield;

if (strtolower($tag->htmlElement->name) === 'form') {
$node->initForm($tag);
} else {
$node->initElement($tag);
}
$node->init($tag);

return $node;
}
Expand All @@ -62,34 +57,7 @@ public function print(PrintContext $context): string
}


private function initForm(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,
array_fill_keys(self::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;',
$this->position,
)),
]);
}


private function initElement(Tag $tag)
private function init(Tag $tag)
{
$el = $tag->htmlElement;
$usedAttributes = self::findUsedAttributes($el);
Expand Down Expand Up @@ -129,7 +97,8 @@ private function initElement(Tag $tag)
}


private static function findUsedAttributes(ElementNode $el): array
/** @internal */
public static function findUsedAttributes(ElementNode $el): array
{
$res = [];
foreach ($el->attributes?->children as $child) {
Expand Down
80 changes: 80 additions & 0 deletions src/Bridges/FormsLatte/Nodes/FormNNameNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

/**
* This file is part of the Latte (https://latte.nette.org)
* Copyright (c) 2008 David Grudl (https://davidgrudl.com)
*/

declare(strict_types=1);

namespace Nette\Bridges\FormsLatte\Nodes;

use Latte;
use Latte\Compiler\Nodes\AreaNode;
use Latte\Compiler\Nodes\AuxiliaryNode;
use Latte\Compiler\Nodes\Php\ExpressionNode;
use Latte\Compiler\Nodes\Php\Scalar\StringNode;
use Latte\Compiler\Nodes\StatementNode;
use Latte\Compiler\PrintContext;
use Latte\Compiler\Tag;


/**
* <form n:name>
*/
final class FormNNameNode extends StatementNode
{
public ExpressionNode $name;
public AreaNode $content;


public static function create(Tag $tag): \Generator
{
$tag->expectArguments();
$node = new static;
$node->name = $tag->parser->parseUnquotedStringOrExpression(colon: false);
[$node->content] = yield;
$node->init($tag);
return $node;
}


public function print(PrintContext $context): string
{
return $this->content->print($context);
}


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,
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;',
$this->position,
)),
]);
}


public function &getIterator(): \Generator
{
yield $this->name;
yield $this->content;
}
}

0 comments on commit 397e38b

Please sign in to comment.