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