1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PHPHtmlParser\Dom; |
6
|
|
|
|
7
|
|
|
use PHPHtmlParser\DTO\Tag\AttributeDTO; |
8
|
|
|
use PHPHtmlParser\Exceptions\Tag\AttributeNotFoundException; |
9
|
|
|
use stringEncode\Encode; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Tag. |
13
|
|
|
*/ |
14
|
|
|
class Tag |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* The name of the tag. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $name; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The attributes of the tag. |
25
|
|
|
* |
26
|
|
|
* @var AttributeDTO[] |
27
|
|
|
*/ |
28
|
|
|
protected $attr = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Is this tag self closing. |
32
|
|
|
* |
33
|
|
|
* @var bool |
34
|
|
|
*/ |
35
|
|
|
protected $selfClosing = false; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* If self-closing, will this use a trailing slash. />. |
39
|
|
|
* |
40
|
|
|
* @var bool |
41
|
|
|
*/ |
42
|
|
|
protected $trailingSlash = true; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Tag noise. |
46
|
|
|
*/ |
47
|
|
|
protected $noise = ''; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* The encoding class to... encode the tags. |
51
|
|
|
* |
52
|
|
|
* @var Encode|null |
53
|
|
|
*/ |
54
|
|
|
protected $encode; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var bool |
58
|
|
|
*/ |
59
|
|
|
private $HtmlSpecialCharsDecode = false; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* What the opening of this tag will be. |
63
|
|
|
* |
64
|
|
|
* @var string |
65
|
|
|
*/ |
66
|
|
|
private $opening = '<'; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* What the closing tag for self-closing elements should be. |
70
|
|
|
* |
71
|
|
|
* @var string |
72
|
|
|
*/ |
73
|
|
|
private $closing = ' />'; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Sets up the tag with a name. |
77
|
|
|
* |
78
|
|
|
* @param $name |
79
|
|
|
*/ |
80
|
501 |
|
public function __construct(string $name) |
81
|
|
|
{ |
82
|
501 |
|
$this->name = $name; |
83
|
501 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Returns the name of this tag. |
87
|
|
|
*/ |
88
|
378 |
|
public function name(): string |
89
|
|
|
{ |
90
|
378 |
|
return $this->name; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Sets the tag to be self closing. |
95
|
|
|
*/ |
96
|
216 |
|
public function selfClosing(): Tag |
97
|
|
|
{ |
98
|
216 |
|
$this->selfClosing = true; |
99
|
|
|
|
100
|
216 |
|
return clone $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
9 |
|
public function setOpening(string $opening): Tag |
104
|
|
|
{ |
105
|
9 |
|
$this->opening = $opening; |
106
|
|
|
|
107
|
9 |
|
return clone $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
9 |
|
public function setClosing(string $closing): Tag |
111
|
|
|
{ |
112
|
9 |
|
$this->closing = $closing; |
113
|
|
|
|
114
|
9 |
|
return clone $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Sets the tag to not use a trailing slash. |
119
|
|
|
*/ |
120
|
3 |
|
public function noTrailingSlash(): Tag |
121
|
|
|
{ |
122
|
3 |
|
$this->trailingSlash = false; |
123
|
|
|
|
124
|
3 |
|
return clone $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Checks if the tag is self closing. |
129
|
|
|
*/ |
130
|
330 |
|
public function isSelfClosing(): bool |
131
|
|
|
{ |
132
|
330 |
|
return $this->selfClosing; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Sets the encoding type to be used. |
137
|
|
|
*/ |
138
|
291 |
|
public function setEncoding(Encode $encode): void |
139
|
|
|
{ |
140
|
291 |
|
$this->encode = $encode; |
141
|
291 |
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param bool $htmlSpecialCharsDecode |
145
|
|
|
*/ |
146
|
294 |
|
public function setHtmlSpecialCharsDecode($htmlSpecialCharsDecode = false): void |
147
|
|
|
{ |
148
|
294 |
|
$this->HtmlSpecialCharsDecode = $htmlSpecialCharsDecode; |
149
|
294 |
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Sets the noise for this tag (if any). |
153
|
|
|
*/ |
154
|
3 |
|
public function noise(string $noise): Tag |
155
|
|
|
{ |
156
|
3 |
|
$this->noise = $noise; |
157
|
|
|
|
158
|
3 |
|
return clone $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Set an attribute for this tag. |
163
|
|
|
*/ |
164
|
375 |
|
public function setAttribute(string $key, ?string $attributeValue, bool $doubleQuote = true): Tag |
165
|
|
|
{ |
166
|
375 |
|
$attributeDTO = AttributeDTO::makeFromPrimitives( |
167
|
375 |
|
$attributeValue, |
168
|
250 |
|
$doubleQuote |
169
|
|
|
); |
170
|
375 |
|
if ($this->HtmlSpecialCharsDecode) { |
171
|
3 |
|
$attributeDTO->htmlspecialcharsDecode(); |
172
|
|
|
} |
173
|
375 |
|
$this->attr[\strtolower($key)] = $attributeDTO; |
174
|
|
|
|
175
|
375 |
|
return clone $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Set inline style attribute value. |
180
|
|
|
* |
181
|
|
|
* @param mixed $attr_key |
182
|
|
|
* @param mixed $attr_value |
183
|
|
|
*/ |
184
|
6 |
|
public function setStyleAttributeValue($attr_key, $attr_value): void |
185
|
|
|
{ |
186
|
6 |
|
$style_array = $this->getStyleAttributeArray(); |
187
|
6 |
|
$style_array[$attr_key] = $attr_value; |
188
|
|
|
|
189
|
6 |
|
$style_string = ''; |
190
|
6 |
|
foreach ($style_array as $key => $value) { |
191
|
6 |
|
$style_string .= $key . ':' . $value . ';'; |
192
|
|
|
} |
193
|
|
|
|
194
|
6 |
|
$this->setAttribute('style', $style_string); |
195
|
6 |
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Get style attribute in array. |
199
|
|
|
*/ |
200
|
6 |
|
public function getStyleAttributeArray(): array |
201
|
|
|
{ |
202
|
|
|
try { |
203
|
6 |
|
$value = $this->getAttribute('style')->getValue(); |
204
|
3 |
|
if (\is_null($value)) { |
205
|
|
|
return []; |
206
|
|
|
} |
207
|
3 |
|
$value = \explode(';', \substr(\trim($value), 0, -1)); |
208
|
3 |
|
$result = []; |
209
|
3 |
|
foreach ($value as $attr) { |
210
|
3 |
|
$attr = \explode(':', $attr); |
211
|
3 |
|
$result[$attr[0]] = $attr[1]; |
212
|
|
|
} |
213
|
|
|
|
214
|
3 |
|
return $result; |
215
|
6 |
|
} catch (AttributeNotFoundException $e) { |
216
|
6 |
|
unset($e); |
217
|
|
|
|
218
|
6 |
|
return []; |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Removes an attribute from this tag. |
224
|
|
|
* |
225
|
|
|
* @param mixed $key |
226
|
|
|
* |
227
|
|
|
* @return void |
228
|
|
|
*/ |
229
|
6 |
|
public function removeAttribute($key) |
230
|
|
|
{ |
231
|
6 |
|
$key = \strtolower($key); |
232
|
6 |
|
unset($this->attr[$key]); |
233
|
6 |
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Removes all attributes on this tag. |
237
|
|
|
* |
238
|
|
|
* @return void |
239
|
|
|
*/ |
240
|
6 |
|
public function removeAllAttributes() |
241
|
|
|
{ |
242
|
6 |
|
$this->attr = []; |
243
|
6 |
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Sets the attributes for this tag. |
247
|
|
|
* |
248
|
|
|
* @return $this |
249
|
|
|
*/ |
250
|
75 |
|
public function setAttributes(array $attr) |
251
|
|
|
{ |
252
|
75 |
|
foreach ($attr as $key => $info) { |
253
|
75 |
|
if (\is_array($info)) { |
254
|
72 |
|
$this->setAttribute($key, $info['value'], $info['doubleQuote']); |
255
|
|
|
} else { |
256
|
6 |
|
$this->setAttribute($key, $info); |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|
260
|
75 |
|
return $this; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Returns all attributes of this tag. |
265
|
|
|
* |
266
|
|
|
* @throws \stringEncode\Exception |
267
|
|
|
* |
268
|
|
|
* @return AttributeDTO[] |
269
|
|
|
*/ |
270
|
9 |
|
public function getAttributes(): array |
271
|
|
|
{ |
272
|
9 |
|
$return = []; |
273
|
9 |
|
foreach (\array_keys($this->attr) as $attr) { |
274
|
|
|
try { |
275
|
3 |
|
$return[$attr] = $this->getAttribute($attr); |
276
|
|
|
} catch (AttributeNotFoundException $e) { |
277
|
|
|
// attribute that was in the array was not found in the array.... |
278
|
|
|
unset($e); |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
|
282
|
9 |
|
return $return; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Returns an attribute by the key. |
287
|
|
|
* |
288
|
|
|
* @throws AttributeNotFoundException |
289
|
|
|
* @throws \stringEncode\Exception |
290
|
|
|
*/ |
291
|
411 |
|
public function getAttribute(string $key): AttributeDTO |
292
|
|
|
{ |
293
|
411 |
|
$key = \strtolower($key); |
294
|
411 |
|
if (!isset($this->attr[$key])) { |
295
|
339 |
|
throw new AttributeNotFoundException('Attribute with key "' . $key . '" not found.'); |
296
|
|
|
} |
297
|
309 |
|
$attributeDTO = $this->attr[$key]; |
298
|
309 |
|
if (!\is_null($this->encode)) { |
299
|
|
|
// convert charset |
300
|
204 |
|
$attributeDTO->encodeValue($this->encode); |
301
|
|
|
} |
302
|
|
|
|
303
|
309 |
|
return $attributeDTO; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Returns TRUE if node has attribute. |
308
|
|
|
* |
309
|
|
|
* @return bool |
310
|
|
|
*/ |
311
|
123 |
|
public function hasAttribute(string $key) |
312
|
|
|
{ |
313
|
123 |
|
return isset($this->attr[$key]); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* Generates the opening tag for this object. |
318
|
|
|
* |
319
|
|
|
* @return string |
320
|
|
|
*/ |
321
|
177 |
|
public function makeOpeningTag() |
322
|
|
|
{ |
323
|
177 |
|
$return = $this->opening . $this->name; |
324
|
|
|
|
325
|
|
|
// add the attributes |
326
|
177 |
|
foreach (\array_keys($this->attr) as $key) { |
327
|
|
|
try { |
328
|
150 |
|
$attributeDTO = $this->getAttribute($key); |
329
|
|
|
} catch (AttributeNotFoundException $e) { |
330
|
|
|
// attribute that was in the array not found in the array... let's continue. |
331
|
|
|
continue; |
332
|
|
|
} catch (\TypeError $e) { |
333
|
|
|
$val = null; |
|
|
|
|
334
|
|
|
} |
335
|
150 |
|
$val = $attributeDTO->getValue(); |
336
|
150 |
|
if (\is_null($val)) { |
337
|
27 |
|
$return .= ' ' . $key; |
338
|
138 |
|
} elseif ($attributeDTO->isDoubleQuote()) { |
339
|
123 |
|
$return .= ' ' . $key . '="' . $val . '"'; |
340
|
|
|
} else { |
341
|
30 |
|
$return .= ' ' . $key . '=\'' . $val . '\''; |
342
|
|
|
} |
343
|
|
|
} |
344
|
|
|
|
345
|
177 |
|
if ($this->selfClosing && $this->trailingSlash) { |
346
|
93 |
|
return $return . $this->closing; |
347
|
|
|
} |
348
|
|
|
|
349
|
156 |
|
return $return . '>'; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Generates the closing tag for this object. |
354
|
|
|
* |
355
|
|
|
* @return string |
356
|
|
|
*/ |
357
|
156 |
|
public function makeClosingTag() |
358
|
|
|
{ |
359
|
156 |
|
if ($this->selfClosing) { |
360
|
3 |
|
return ''; |
361
|
|
|
} |
362
|
|
|
|
363
|
153 |
|
return '</' . $this->name . '>'; |
364
|
|
|
} |
365
|
|
|
} |
366
|
|
|
|