1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PHPHtmlParser; |
6
|
|
|
|
7
|
|
|
class Options |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* The whitespaceTextNode, by default true, option tells the parser to save textnodes even if the content of the |
11
|
|
|
* node is empty (only whitespace). Setting it to false will ignore all whitespace only text node found in the document. |
12
|
|
|
* |
13
|
|
|
* @var bool |
14
|
|
|
*/ |
15
|
|
|
private $whitespaceTextNode = true; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Strict, by default false, will throw a StrictException if it finds that the html is not strictly compliant |
19
|
|
|
* (all tags must have a closing tag, no attribute with out a value, etc.). |
20
|
|
|
* |
21
|
|
|
* @var bool |
22
|
|
|
*/ |
23
|
|
|
private $strict = false; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The enforceEncoding, by default null, option will enforce an character set to be used for reading the content |
27
|
|
|
* and returning the content in that encoding. Setting it to null will trigger an attempt to figure out |
28
|
|
|
* the encoding from within the content of the string given instead. |
29
|
|
|
* |
30
|
|
|
* @var ?string |
31
|
|
|
*/ |
32
|
|
|
private $enforceEncoding; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Set this to false to skip the entire clean up phase of the parser. Defaults to true. |
36
|
|
|
* |
37
|
|
|
* @var bool |
38
|
|
|
*/ |
39
|
|
|
private $cleanupInput = true; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Set this to false to skip removing the script tags from the document body. This might have adverse effects. |
43
|
|
|
* Defaults to true. |
44
|
|
|
* |
45
|
|
|
* NOTE: Ignored if cleanupInit is true. |
46
|
|
|
* |
47
|
|
|
* @var bool |
48
|
|
|
*/ |
49
|
|
|
private $removeScripts = true; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Set this to false to skip removing of style tags from the document body. This might have adverse effects. Defaults to true. |
53
|
|
|
* |
54
|
|
|
* NOTE: Ignored if cleanupInit is true. |
55
|
|
|
* |
56
|
|
|
* @var bool |
57
|
|
|
*/ |
58
|
|
|
private $removeStyles = true; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Preserves Line Breaks if set to true. If set to false line breaks are cleaned up |
62
|
|
|
* as part of the input clean up process. Defaults to false. |
63
|
|
|
* |
64
|
|
|
* NOTE: Ignored if cleanupInit is true. |
65
|
|
|
* |
66
|
|
|
* @var bool |
67
|
|
|
*/ |
68
|
|
|
private $preserveLineBreaks = false; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Set this to false if you want to preserve whitespace inside of text nodes. It is set to true by default. |
72
|
|
|
* |
73
|
|
|
* @var bool |
74
|
|
|
*/ |
75
|
|
|
private $removeDoubleSpace = true; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Set this to false if you want to preserve smarty script found in the html content. It is set to true by default. |
79
|
|
|
* |
80
|
|
|
* @var bool |
81
|
|
|
*/ |
82
|
|
|
private $removeSmartyScripts = true; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* By default this is set to false. Setting this to true will apply the php function htmlspecialchars_decode too all attribute values and text nodes. |
86
|
|
|
* |
87
|
|
|
* @var bool |
88
|
|
|
*/ |
89
|
|
|
private $htmlSpecialCharsDecode = false; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* A list of tags which will always be self closing. |
93
|
|
|
* |
94
|
|
|
* @var string[] |
95
|
|
|
*/ |
96
|
|
|
private $selfClosing = [ |
97
|
|
|
'area', |
98
|
|
|
'base', |
99
|
|
|
'basefont', |
100
|
|
|
'br', |
101
|
|
|
'col', |
102
|
|
|
'embed', |
103
|
|
|
'hr', |
104
|
|
|
'img', |
105
|
|
|
'input', |
106
|
|
|
'keygen', |
107
|
|
|
'link', |
108
|
|
|
'meta', |
109
|
|
|
'param', |
110
|
|
|
'source', |
111
|
|
|
'spacer', |
112
|
|
|
'track', |
113
|
|
|
'wbr', |
114
|
|
|
]; |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* A list of tags where there should be no /> at the end (html5 style). |
118
|
|
|
* |
119
|
|
|
* @var string[] |
120
|
|
|
*/ |
121
|
|
|
private $noSlash = []; |
122
|
|
|
|
123
|
273 |
|
public function isWhitespaceTextNode(): bool |
124
|
|
|
{ |
125
|
273 |
|
return $this->whitespaceTextNode; |
126
|
|
|
} |
127
|
|
|
|
128
|
90 |
|
public function setWhitespaceTextNode(bool $whitespaceTextNode): Options |
129
|
|
|
{ |
130
|
90 |
|
$this->whitespaceTextNode = $whitespaceTextNode; |
131
|
|
|
|
132
|
90 |
|
return clone $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
225 |
|
public function isStrict(): bool |
136
|
|
|
{ |
137
|
225 |
|
return $this->strict; |
138
|
|
|
} |
139
|
|
|
|
140
|
93 |
|
public function setStrict(bool $strict): Options |
141
|
|
|
{ |
142
|
93 |
|
$this->strict = $strict; |
143
|
|
|
|
144
|
93 |
|
return clone $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
303 |
|
public function getEnforceEncoding(): ?string |
148
|
|
|
{ |
149
|
303 |
|
return $this->enforceEncoding; |
150
|
|
|
} |
151
|
|
|
|
152
|
90 |
|
public function setEnforceEncoding(?string $enforceEncoding): Options |
153
|
|
|
{ |
154
|
90 |
|
$this->enforceEncoding = $enforceEncoding; |
155
|
|
|
|
156
|
90 |
|
return clone $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
303 |
|
public function isCleanupInput(): bool |
160
|
|
|
{ |
161
|
303 |
|
return $this->cleanupInput; |
162
|
|
|
} |
163
|
|
|
|
164
|
90 |
|
public function setCleanupInput(bool $cleanupInput): Options |
165
|
|
|
{ |
166
|
90 |
|
$this->cleanupInput = $cleanupInput; |
167
|
|
|
|
168
|
90 |
|
return clone $this; |
169
|
|
|
} |
170
|
|
|
|
171
|
303 |
|
public function isRemoveScripts(): bool |
172
|
|
|
{ |
173
|
303 |
|
return $this->removeScripts; |
174
|
|
|
} |
175
|
|
|
|
176
|
90 |
|
public function setRemoveScripts(bool $removeScripts): Options |
177
|
|
|
{ |
178
|
90 |
|
$this->removeScripts = $removeScripts; |
179
|
|
|
|
180
|
90 |
|
return clone $this; |
181
|
|
|
} |
182
|
|
|
|
183
|
303 |
|
public function isRemoveStyles(): bool |
184
|
|
|
{ |
185
|
303 |
|
return $this->removeStyles; |
186
|
|
|
} |
187
|
|
|
|
188
|
90 |
|
public function setRemoveStyles(bool $removeStyles): Options |
189
|
|
|
{ |
190
|
90 |
|
$this->removeStyles = $removeStyles; |
191
|
|
|
|
192
|
90 |
|
return clone $this; |
193
|
|
|
} |
194
|
|
|
|
195
|
303 |
|
public function isPreserveLineBreaks(): bool |
196
|
|
|
{ |
197
|
303 |
|
return $this->preserveLineBreaks; |
198
|
|
|
} |
199
|
|
|
|
200
|
90 |
|
public function setPreserveLineBreaks(bool $preserveLineBreaks): Options |
201
|
|
|
{ |
202
|
90 |
|
$this->preserveLineBreaks = $preserveLineBreaks; |
203
|
|
|
|
204
|
90 |
|
return clone $this; |
205
|
|
|
} |
206
|
|
|
|
207
|
270 |
|
public function isRemoveDoubleSpace(): bool |
208
|
|
|
{ |
209
|
270 |
|
return $this->removeDoubleSpace; |
210
|
|
|
} |
211
|
|
|
|
212
|
90 |
|
public function setRemoveDoubleSpace(bool $removeDoubleSpace): Options |
213
|
|
|
{ |
214
|
90 |
|
$this->removeDoubleSpace = $removeDoubleSpace; |
215
|
|
|
|
216
|
90 |
|
return clone $this; |
217
|
|
|
} |
218
|
|
|
|
219
|
303 |
|
public function isRemoveSmartyScripts(): bool |
220
|
|
|
{ |
221
|
303 |
|
return $this->removeSmartyScripts; |
222
|
|
|
} |
223
|
|
|
|
224
|
90 |
|
public function setRemoveSmartyScripts(bool $removeSmartyScripts): Options |
225
|
|
|
{ |
226
|
90 |
|
$this->removeSmartyScripts = $removeSmartyScripts; |
227
|
|
|
|
228
|
90 |
|
return clone $this; |
229
|
|
|
} |
230
|
|
|
|
231
|
300 |
|
public function isHtmlSpecialCharsDecode(): bool |
232
|
|
|
{ |
233
|
300 |
|
return $this->htmlSpecialCharsDecode; |
234
|
|
|
} |
235
|
|
|
|
236
|
90 |
|
public function setHtmlSpecialCharsDecode(bool $htmlSpecialCharsDecode): Options |
237
|
|
|
{ |
238
|
90 |
|
$this->htmlSpecialCharsDecode = $htmlSpecialCharsDecode; |
239
|
|
|
|
240
|
90 |
|
return clone $this; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @return string[] |
245
|
|
|
*/ |
246
|
294 |
|
public function getSelfClosing(): array |
247
|
|
|
{ |
248
|
294 |
|
return $this->selfClosing; |
249
|
|
|
} |
250
|
|
|
|
251
|
87 |
|
public function setSelfClosing(array $selfClosing): Options |
252
|
|
|
{ |
253
|
87 |
|
$this->selfClosing = $selfClosing; |
254
|
|
|
|
255
|
87 |
|
return clone $this; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Adds the tag to the list of tags that will always be self closing. |
260
|
|
|
*/ |
261
|
3 |
|
public function addSelfClosingTag(string $tag): Options |
262
|
|
|
{ |
263
|
3 |
|
$this->selfClosing[] = $tag; |
264
|
|
|
|
265
|
3 |
|
return clone $this; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Adds the tags to the list of tags that will always be self closing. |
270
|
|
|
* |
271
|
|
|
* @param string[] $tags |
272
|
|
|
*/ |
273
|
3 |
|
public function addSelfClosingTags(array $tags): Options |
274
|
|
|
{ |
275
|
3 |
|
foreach ($tags as $tag) { |
276
|
3 |
|
$this->selfClosing[] = $tag; |
277
|
|
|
} |
278
|
|
|
|
279
|
3 |
|
return clone $this; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Removes the tag from the list of tags that will always be self closing. |
284
|
|
|
*/ |
285
|
3 |
|
public function removeSelfClosingTag(string $tag): Options |
286
|
|
|
{ |
287
|
3 |
|
$tags = [$tag]; |
288
|
3 |
|
$this->selfClosing = \array_diff($this->selfClosing, $tags); |
289
|
|
|
|
290
|
3 |
|
return clone $this; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Sets the list of self closing tags to empty. |
295
|
|
|
*/ |
296
|
3 |
|
public function clearSelfClosingTags(): Options |
297
|
|
|
{ |
298
|
3 |
|
$this->selfClosing = []; |
299
|
|
|
|
300
|
3 |
|
return clone $this; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* @return string[] |
305
|
|
|
*/ |
306
|
195 |
|
public function getNoSlash(): array |
307
|
|
|
{ |
308
|
195 |
|
return $this->noSlash; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* @param string[] $noSlash |
313
|
|
|
*/ |
314
|
87 |
|
public function setNoSlash(array $noSlash): Options |
315
|
|
|
{ |
316
|
87 |
|
$this->noSlash = $noSlash; |
317
|
|
|
|
318
|
87 |
|
return clone $this; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Adds a tag to the list of self closing tags that should not have a trailing slash. |
323
|
|
|
*/ |
324
|
9 |
|
public function addNoSlashTag(string $tag): Options |
325
|
|
|
{ |
326
|
9 |
|
$this->noSlash[] = $tag; |
327
|
|
|
|
328
|
9 |
|
return clone $this; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* Removes a tag from the list of no-slash tags. |
333
|
|
|
*/ |
334
|
3 |
|
public function removeNoSlashTag(string $tag): Options |
335
|
|
|
{ |
336
|
3 |
|
$tags = [$tag]; |
337
|
3 |
|
$this->noSlash = \array_diff($this->noSlash, $tags); |
338
|
|
|
|
339
|
3 |
|
return clone $this; |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* Empties the list of no-slash tags. |
344
|
|
|
*/ |
345
|
3 |
|
public function clearNoSlashTags(): Options |
346
|
|
|
{ |
347
|
3 |
|
$this->noSlash = []; |
348
|
|
|
|
349
|
3 |
|
return clone $this; |
350
|
|
|
} |
351
|
|
|
|
352
|
87 |
|
public function setFromOptions(Options $options): Options |
353
|
|
|
{ |
354
|
87 |
|
return $this->setCleanupInput($options->isCleanupInput()) |
355
|
87 |
|
->setEnforceEncoding($options->getEnforceEncoding()) |
356
|
87 |
|
->setHtmlSpecialCharsDecode($options->isHtmlSpecialCharsDecode()) |
357
|
87 |
|
->setPreserveLineBreaks($options->isPreserveLineBreaks()) |
358
|
87 |
|
->setRemoveDoubleSpace($options->isRemoveDoubleSpace()) |
359
|
87 |
|
->setRemoveScripts($options->isRemoveScripts()) |
360
|
87 |
|
->setRemoveSmartyScripts($options->isRemoveSmartyScripts()) |
361
|
87 |
|
->setRemoveStyles($options->isRemoveStyles()) |
362
|
87 |
|
->setStrict($options->isStrict()) |
363
|
87 |
|
->setWhitespaceTextNode($options->isWhitespaceTextNode()) |
364
|
87 |
|
->setSelfClosing($options->getSelfClosing()) |
365
|
87 |
|
->setNoSlash($options->getNoSlash()); |
366
|
|
|
} |
367
|
|
|
} |
368
|
|
|
|