|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Spiral\Stempler\Parser\Syntax; |
|
6
|
|
|
|
|
7
|
|
|
use Spiral\Stempler\Exception\SyntaxException; |
|
8
|
|
|
use Spiral\Stempler\Lexer\Grammar\HTMLGrammar; |
|
9
|
|
|
use Spiral\Stempler\Lexer\Token; |
|
10
|
|
|
use Spiral\Stempler\Node\HTML\Attr; |
|
11
|
|
|
use Spiral\Stempler\Node\HTML\Nil; |
|
12
|
|
|
use Spiral\Stempler\Node\HTML\Tag; |
|
13
|
|
|
use Spiral\Stempler\Node\HTML\Verbatim; |
|
14
|
|
|
use Spiral\Stempler\Node\Mixin; |
|
15
|
|
|
use Spiral\Stempler\Node\Raw; |
|
16
|
|
|
use Spiral\Stempler\Parser; |
|
17
|
|
|
use Spiral\Stempler\Parser\Assembler; |
|
18
|
|
|
use Spiral\Stempler\Parser\SyntaxInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* HTML tags and attributes. |
|
22
|
|
|
*/ |
|
23
|
|
|
final class HTMLSyntax implements SyntaxInterface |
|
24
|
|
|
{ |
|
25
|
|
|
use Parser\Syntax\Traits\MixinTrait; |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
// list of tags which are closed automatically (http://xahlee.info/js/html5_non-closing_tag.html) |
|
28
|
|
|
private const VOID_TAGS = [ |
|
29
|
|
|
'area', |
|
30
|
|
|
'base', |
|
31
|
|
|
'br', |
|
32
|
|
|
'col', |
|
33
|
|
|
'embed', |
|
34
|
|
|
'hr', |
|
35
|
|
|
'img', |
|
36
|
|
|
'input', |
|
37
|
|
|
'link', |
|
38
|
|
|
'meta', |
|
39
|
|
|
'param', |
|
40
|
|
|
'source', |
|
41
|
|
|
'track', |
|
42
|
|
|
'wbr', |
|
43
|
|
|
]; |
|
44
|
|
|
|
|
45
|
|
|
// list of attributes which define verbatim blocks (https://www.w3schools.com/tags/ref_eventattributes.asp) |
|
46
|
|
|
private const VERBATIM_ATTRIBUTES = [ |
|
47
|
|
|
'style', |
|
48
|
|
|
'on*', |
|
49
|
|
|
]; |
|
50
|
|
|
|
|
51
|
|
|
private ?Tag $node = null; |
|
52
|
|
|
private ?Token $token = null; |
|
53
|
|
|
private ?Attr $attr = null; |
|
54
|
|
|
|
|
55
|
108 |
|
public function handle(Parser $parser, Assembler $asm, Token $token): void |
|
56
|
|
|
{ |
|
57
|
108 |
|
switch ($token->type) { |
|
58
|
|
|
case HTMLGrammar::TYPE_OPEN: |
|
59
|
|
|
case HTMLGrammar::TYPE_OPEN_SHORT: |
|
60
|
108 |
|
$this->node = new Tag(new Parser\Context($token, $parser->getPath())); |
|
61
|
108 |
|
$this->token = $token; |
|
62
|
|
|
|
|
63
|
108 |
|
break; |
|
64
|
|
|
|
|
65
|
|
|
case HTMLGrammar::TYPE_KEYWORD: |
|
66
|
108 |
|
if ($this->node->name === null) { |
|
67
|
108 |
|
$this->node->name = $this->parseToken($parser, $token); |
|
68
|
108 |
|
return; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
85 |
|
if ($this->attr !== null && !$this->attr->value instanceof Nil) { |
|
72
|
1 |
|
$this->attr->value = $this->parseToken($parser, $token); |
|
73
|
1 |
|
$this->attr = null; |
|
74
|
1 |
|
break; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
85 |
|
$this->attr = new Attr( |
|
78
|
85 |
|
$this->parseToken($parser, $token), |
|
|
|
|
|
|
79
|
85 |
|
new Nil(), |
|
80
|
85 |
|
new Parser\Context($token, $parser->getPath()), |
|
81
|
85 |
|
); |
|
82
|
|
|
|
|
83
|
85 |
|
$this->node->attrs[] = $this->attr; |
|
84
|
85 |
|
break; |
|
85
|
|
|
|
|
86
|
|
|
case HTMLGrammar::TYPE_EQUAL: |
|
87
|
84 |
|
if ($this->attr === null) { |
|
88
|
|
|
throw new SyntaxException('unexpected attribute token', $token); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
// expect the value |
|
92
|
84 |
|
$this->attr->value = null; |
|
93
|
84 |
|
break; |
|
94
|
|
|
|
|
95
|
|
|
case HTMLGrammar::TYPE_ATTRIBUTE: |
|
96
|
84 |
|
if ($this->attr === null) { |
|
97
|
|
|
throw new SyntaxException('unexpected attribute token', $token); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
if ( |
|
101
|
84 |
|
\is_string($this->attr->name) |
|
102
|
|
|
&& ( |
|
103
|
84 |
|
\str_starts_with($this->attr->name, 'on') |
|
104
|
84 |
|
|| \in_array($this->attr->name, self::VERBATIM_ATTRIBUTES, true) |
|
105
|
|
|
) |
|
106
|
|
|
) { |
|
107
|
5 |
|
$this->attr->value = $this->parseVerbatim($parser, $token); |
|
108
|
|
|
} else { |
|
109
|
81 |
|
$this->attr->value = $this->parseToken($parser, $token); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
84 |
|
$this->attr = null; |
|
113
|
84 |
|
break; |
|
114
|
|
|
|
|
115
|
|
|
case HTMLGrammar::TYPE_CLOSE_SHORT: |
|
116
|
67 |
|
$this->node->void = true; |
|
117
|
67 |
|
$asm->push($this->node); |
|
|
|
|
|
|
118
|
67 |
|
$this->flush(); |
|
119
|
67 |
|
break; |
|
120
|
|
|
|
|
121
|
|
|
case HTMLGrammar::TYPE_CLOSE: |
|
122
|
99 |
|
if ($this->token->type == HTMLGrammar::TYPE_OPEN_SHORT) { |
|
123
|
95 |
|
if (!$asm->getNode() instanceof Tag || $asm->getNode()->name !== $this->node->name) { |
|
124
|
|
|
/** |
|
125
|
|
|
* TODO issue #767 |
|
126
|
|
|
* @link https://github.com/spiral/framework/issues/767 |
|
127
|
|
|
* @psalm-suppress NoInterfaceProperties |
|
128
|
|
|
*/ |
|
129
|
4 |
|
throw new SyntaxException( |
|
130
|
4 |
|
"Invalid closing tag `{$this->node->name}`, expected `{$asm->getNode()->name}`", |
|
131
|
4 |
|
$this->token, |
|
|
|
|
|
|
132
|
4 |
|
); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
93 |
|
$asm->close(); |
|
136
|
99 |
|
} elseif (\in_array($this->node->name, self::VOID_TAGS)) { |
|
137
|
2 |
|
$this->node->void = true; |
|
138
|
2 |
|
$asm->push($this->node); |
|
139
|
|
|
} else { |
|
140
|
97 |
|
$asm->open($this->node, 'nodes'); |
|
|
|
|
|
|
141
|
|
|
} |
|
142
|
99 |
|
$this->flush(); |
|
143
|
|
|
|
|
144
|
99 |
|
break; |
|
145
|
|
|
|
|
146
|
|
|
case HTMLGrammar::TYPE_VERBATIM: |
|
147
|
3 |
|
$asm->push($this->parseVerbatim($parser, $token)); |
|
148
|
3 |
|
break; |
|
149
|
|
|
|
|
150
|
|
|
default: |
|
151
|
87 |
|
if ($asm->getNode() instanceof Mixin || $asm->getNode() instanceof Verbatim) { |
|
152
|
45 |
|
$node = $this->parseToken($parser, $token); |
|
153
|
45 |
|
if (\is_string($node)) { |
|
154
|
|
|
$node = new Raw($node, new Parser\Context($token, $parser->getPath())); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
45 |
|
$asm->push($node); |
|
|
|
|
|
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Flush open nodes and tokens. |
|
164
|
|
|
*/ |
|
165
|
108 |
|
private function flush(): void |
|
166
|
|
|
{ |
|
167
|
108 |
|
$this->node = null; |
|
168
|
108 |
|
$this->token = null; |
|
169
|
108 |
|
$this->attr = null; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
8 |
|
private function parseVerbatim(Parser $parser, Token $token): Verbatim |
|
173
|
|
|
{ |
|
174
|
8 |
|
$verbatim = new Verbatim(new Parser\Context($token, $parser->getPath())); |
|
175
|
|
|
|
|
176
|
8 |
|
if ($token->tokens === []) { |
|
177
|
3 |
|
if ($token->content) { |
|
178
|
3 |
|
$verbatim->nodes[] = $token->content; |
|
179
|
|
|
} |
|
180
|
|
|
} else { |
|
181
|
|
|
/** |
|
182
|
|
|
* TODO issue #767 |
|
183
|
|
|
* @link https://github.com/spiral/framework/issues/767 |
|
184
|
|
|
* @psalm-suppress InvalidArgument |
|
185
|
|
|
*/ |
|
186
|
5 |
|
$parser->parseTokens( |
|
187
|
5 |
|
new Assembler($verbatim, 'nodes'), |
|
188
|
5 |
|
$token->tokens, |
|
189
|
5 |
|
); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
8 |
|
return $verbatim; |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|