• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nette / forms / 19616945056

22 Nov 2025 07:36PM UTC coverage: 93.247%. First build
19616945056

push

github

dg
compatible with Latte 3.1

1 of 2 new or added lines in 1 file covered. (50.0%)

2085 of 2236 relevant lines covered (93.25%)

0.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.33
/src/Bridges/FormsLatte/Nodes/FieldNNameNode.php
1
<?php
2

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

8
declare(strict_types=1);
9

10
namespace Nette\Bridges\FormsLatte\Nodes;
11

12
use Latte\Compiler\Nodes\AreaNode;
13
use Latte\Compiler\Nodes\AuxiliaryNode;
14
use Latte\Compiler\Nodes\Html\AttributeNode;
15
use Latte\Compiler\Nodes\Html\ElementNode;
16
use Latte\Compiler\Nodes\Html\ExpressionAttributeNode;
17
use Latte\Compiler\Nodes\NopNode;
18
use Latte\Compiler\Nodes\Php\ExpressionNode;
19
use Latte\Compiler\Nodes\Php\Scalar\StringNode;
20
use Latte\Compiler\Nodes\StatementNode;
21
use Latte\Compiler\Nodes\TextNode;
22
use Latte\Compiler\PrintContext;
23
use Latte\Compiler\Tag;
24

25

26
/**
27
 * <input n:name>, <select n:name>, <textarea n:name>, <label n:name> and <button n:name>
28
 */
29
final class FieldNNameNode extends StatementNode
30
{
31
        public ExpressionNode $name;
32
        public ?ExpressionNode $part = null;
33
        public AreaNode $content;
34

35

36
        public static function create(Tag $tag): \Generator
1✔
37
        {
38
                $tag->expectArguments();
1✔
39
                $node = $tag->node = new static;
1✔
40
                $node->name = $tag->parser->parseUnquotedStringOrExpression(colon: false);
1✔
41
                if ($tag->parser->stream->tryConsume(':')) {
1✔
42
                        $node->part = $tag->parser->isEnd()
1✔
43
                                ? new StringNode('')
1✔
44
                                : $tag->parser->parseUnquotedStringOrExpression();
1✔
45
                }
46

47
                [$node->content] = yield;
1✔
48

49
                $node->init($tag);
1✔
50

51
                return $node;
1✔
52
        }
53

54

55
        public function print(PrintContext $context): string
1✔
56
        {
57
                return $this->content->print($context);
1✔
58
        }
59

60

61
        private function init(Tag $tag): void
1✔
62
        {
63
                $el = $tag->htmlElement;
1✔
64
                $usedAttributes = self::findUsedAttributes($el);
1✔
65
                $elName = strtolower($el->name);
1✔
66

67
                $tag->replaceNAttribute(new AuxiliaryNode(fn(PrintContext $context) => $context->format(
1✔
68
                        'echo ($ʟ_elem = Nette\Bridges\FormsLatte\Runtime::item(%node, $this->global)'
69
                        . ($elName === 'label' ? '->getLabelPart(%node))' : '->getControlPart(%node))')
1✔
70
                        . ($usedAttributes ? '->addAttributes(%dump)' : '')
1✔
71
                        . '->attributes() %3.line;',
1✔
72
                        $this->name,
1✔
73
                        $this->part,
1✔
74
                        array_fill_keys($usedAttributes, null),
1✔
75
                        $this->position,
1✔
76
                )));
1✔
77

78
                if ($elName === 'label') {
1✔
79
                        if ($el->content instanceof NopNode) {
1✔
80
                                $el->content = new AuxiliaryNode(fn(PrintContext $context) => $context->format(
1✔
81
                                        'echo $ʟ_elem->getHtml() %line;',
1✔
82
                                        $this->position,
1✔
83
                                ));
1✔
84
                        }
85
                } elseif ($elName === 'button') {
1✔
86
                        if ($el->content instanceof NopNode) {
1✔
87
                                $el->content = new AuxiliaryNode(fn(PrintContext $context) => $context->format(
1✔
88
                                        'echo %escape($ʟ_elem->value) %line;',
1✔
89
                                        $this->position,
1✔
90
                                ));
1✔
91
                        }
92
                } elseif ($el->content) { // select, textarea
1✔
93
                        $el->content = new AuxiliaryNode(fn(PrintContext $context) => $context->format(
1✔
94
                                'echo $ʟ_elem->getHtml() %line;',
1✔
95
                                $this->position,
1✔
96
                        ));
1✔
97
                }
98
        }
1✔
99

100

101
        /** @internal */
102
        public static function findUsedAttributes(ElementNode $el): array
1✔
103
        {
104
                $res = [];
1✔
105
                foreach ($el->attributes?->children as $child) {
1✔
106
                        if ($child instanceof AttributeNode && $child->name instanceof TextNode) {
1✔
107
                                $res[] = $child->name->content;
1✔
108
                        } elseif ($child instanceof ExpressionAttributeNode) {
1✔
NEW
109
                                $res[] = $child->name;
×
110
                        }
111
                }
112

113
                if (isset($el->nAttributes['class'])) {
1✔
114
                        $res[] = 'class';
1✔
115
                }
116
                return $res;
1✔
117
        }
118

119

120
        public function &getIterator(): \Generator
1✔
121
        {
122
                yield $this->name;
1✔
123
                if ($this->part) {
1✔
124
                        yield $this->part;
1✔
125
                }
126
                yield $this->content;
1✔
127
        }
1✔
128
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc