-
-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathFieldNNameNode.php
More file actions
125 lines (103 loc) · 3.12 KB
/
FieldNNameNode.php
File metadata and controls
125 lines (103 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?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\Compiler\Nodes\AreaNode;
use Latte\Compiler\Nodes\AuxiliaryNode;
use Latte\Compiler\Nodes\Html\AttributeNode;
use Latte\Compiler\Nodes\Html\ElementNode;
use Latte\Compiler\Nodes\NopNode;
use Latte\Compiler\Nodes\Php\ExpressionNode;
use Latte\Compiler\Nodes\Php\Scalar\StringNode;
use Latte\Compiler\Nodes\StatementNode;
use Latte\Compiler\Nodes\TextNode;
use Latte\Compiler\PrintContext;
use Latte\Compiler\Tag;
/**
* <input n:name>, <select n:name>, <textarea n:name>, <label n:name> and <button n:name>
*/
final class FieldNNameNode extends StatementNode
{
public ExpressionNode $name;
public ?ExpressionNode $part = null;
public AreaNode $content;
public static function create(Tag $tag): \Generator
{
$tag->expectArguments();
$node = new static;
$node->name = $tag->parser->parseUnquotedStringOrExpression(colon: false);
if ($tag->parser->stream->tryConsume(':')) {
$node->part = $tag->parser->isEnd()
? new StringNode('')
: $tag->parser->parseUnquotedStringOrExpression();
}
[$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;
$usedAttributes = self::findUsedAttributes($el);
$elName = strtolower($el->name);
$tag->replaceNAttribute(new AuxiliaryNode(fn(PrintContext $context) => $context->format(
'echo ($ʟ_input = $this->global->forms->item(%node))'
. ($elName === 'label' ? '->getLabelPart(%node)' : '->getControlPart(%node)')
. ($usedAttributes ? '->addAttributes(%dump)' : '')
. '->attributes() %3.line;',
$this->name,
$this->part,
array_fill_keys($usedAttributes, null),
$this->position,
)));
if ($elName === 'label') {
if ($el->content instanceof NopNode) {
$el->content = new AuxiliaryNode(fn(PrintContext $context) => $context->format(
'echo $ʟ_input->getLabelPart()->getHtml() %line;',
$this->position,
));
}
} elseif ($elName === 'button') {
if ($el->content instanceof NopNode) {
$el->content = new AuxiliaryNode(fn(PrintContext $context) => $context->format(
'echo %escape($ʟ_input->getCaption()) %line;',
$this->position,
));
}
} elseif ($el->content) { // select, textarea
$el->content = new AuxiliaryNode(fn(PrintContext $context) => $context->format(
'echo $ʟ_input->getControl()->getHtml() %line;',
$this->position,
));
}
}
/** @internal */
public static function findUsedAttributes(ElementNode $el): array
{
$res = [];
foreach ($el->attributes?->children as $child) {
if ($child instanceof AttributeNode && $child->name instanceof TextNode) {
$res[] = $child->name->content;
}
}
if (isset($el->nAttributes['class'])) {
$res[] = 'class';
}
return $res;
}
public function &getIterator(): \Generator
{
yield $this->name;
if ($this->part) {
yield $this->part;
}
yield $this->content;
}
}