-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDom.php
648 lines (562 loc) · 17 KB
/
Dom.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
<?php
namespace PHPHtmlParser;
use PHPHtmlParser\Dom\AbstractNode;
use PHPHtmlParser\Dom\HtmlNode;
use PHPHtmlParser\Dom\TextNode;
use PHPHtmlParser\Exceptions\NotLoadedException;
use PHPHtmlParser\Exceptions\StrictException;
use stringEncode\Encode;
/**
* Class Dom
*
* @package PHPHtmlParser
*/
class Dom
{
/**
* The charset we would like the output to be in.
*
* @var string
*/
protected $defaultCharset = 'UTF-8';
/**
* Contains the root node of this dom tree.
*
* @var HtmlNode
*/
public $root;
/**
* The raw version of the document string.
*
* @var string
*/
protected $raw;
/**
* The document string.
*
* @var Content
*/
protected $content = null;
/**
* The original file size of the document.
*
* @var int
*/
protected $rawSize;
/**
* The size of the document after it is cleaned.
*
* @var int
*/
protected $size;
/**
* A global options array to be used by all load calls.
*
* @var array
*/
protected $globalOptions = [];
/**
* A persistent option object to be used for all options in the
* parsing of the file.
*
* @var Options
*/
protected $options;
/**
* A list of tags which will always be self closing
*
* @var array
*/
protected $selfClosing = [
'img',
'br',
'input',
'meta',
'link',
'hr',
'base',
'embed',
'spacer',
];
/**
* Returns the inner html of the root node.
*
* @return string
*/
public function __toString()
{
return $this->root->innerHtml();
}
/**
* A simple wrapper around the root node.
*
* @param string $name
* @return mixed
*/
public function __get($name)
{
return $this->root->$name;
}
/**
* Attempts to load the dom from any resource, string, file, or URL.
*
* @param string $str
* @param array $options
* @return $this
*/
public function load($str, $options = [])
{
// check if it's a file
if (strpos($str, "\n") === false && is_file($str)) {
return $this->loadFromFile($str, $options);
}
// check if it's a url
if (preg_match("/^https?:\/\//i", $str)) {
return $this->loadFromUrl($str, $options);
}
return $this->loadStr($str, $options);
}
/**
* Loads the dom from a document file/url
*
* @param string $file
* @param array $options
* @return $this
*/
public function loadFromFile($file, $options = [])
{
return $this->loadStr(file_get_contents($file), $options);
}
/**
* Use a curl interface implementation to attempt to load
* the content from a url.
*
* @param string $url
* @param array $options
* @param CurlInterface $curl
* @return $this
*/
public function loadFromUrl($url, $options = [], CurlInterface $curl = null)
{
if (is_null($curl)) {
// use the default curl interface
$curl = new Curl;
}
$content = $curl->get($url);
return $this->loadStr($content, $options);
}
/**
* Parsers the html of the given string. Used for load(), loadFromFile(),
* and loadFromUrl().
*
* @param string $str
* @param array $option
* @return $this
*/
public function loadStr($str, $option)
{
$this->options = new Options;
$this->options->setOptions($this->globalOptions)
->setOptions($option);
$this->rawSize = strlen($str);
$this->raw = $str;
$html = $this->clean($str);
$this->size = strlen($str);
$this->content = new Content($html);
$this->parse();
$this->detectCharset();
return $this;
}
/**
* Sets a global options array to be used by all load calls.
*
* @param array $options
* @return $this
*/
public function setOptions(array $options)
{
$this->globalOptions = $options;
return $this;
}
/**
* Find elements by css selector on the root node.
*
* @param string $selector
* @param int $nth
* @return array
*/
public function find($selector, $nth = null)
{
$this->isLoaded();
return $this->root->find($selector, $nth);
}
/**
* Adds the tag (or tags in an array) to the list of tags that will always
* be self closing.
*
* @param string|array $tag
* @return $this
*/
public function addSelfClosingTag($tag)
{
if ( ! is_array($tag)) {
$tag = [$tag];
}
foreach ($tag as $value) {
$this->selfClosing[] = $value;
}
return $this;
}
/**
* Removes the tag (or tags in an array) from the list of tags that will
* always be self closing.
*
* @param string|array $tag
* @return $this
*/
public function removeSelfClosingTag($tag)
{
if ( ! is_array($tag)) {
$tag = [$tag];
}
$this->selfClosing = array_diff($this->selfClosing, $tag);
return $this;
}
/**
* Sets the list of self closing tags to empty.
*
* @return $this
*/
public function clearSelfClosingTags()
{
$this->selfClosing = [];
return $this;
}
/**
* Simple wrapper function that returns the first child.
*
* @return \PHPHtmlParser\Dom\AbstractNode
*/
public function firstChild()
{
$this->isLoaded();
return $this->root->firstChild();
}
/**
* Simple wrapper function that returns the last child.
*
* @return \PHPHtmlParser\Dom\AbstractNode
*/
public function lastChild()
{
$this->isLoaded();
return $this->root->lastChild();
}
/**
* Simple wrapper function that returns an element by the
* id.
*
* @param string $id
* @return \PHPHtmlParser\Dom\AbstractNode
*/
public function getElementById($id)
{
$this->isLoaded();
return $this->find('#'.$id, 0);
}
/**
* Simple wrapper function that returns all elements by
* tag name.
*
* @param string $name
* @return array
*/
public function getElementsByTag($name)
{
$this->isLoaded();
return $this->find($name);
}
/**
* Simple wrapper function that returns all elements by
* class name.
*
* @param string $class
* @return array
*/
public function getElementsByClass($class)
{
$this->isLoaded();
return $this->find('.'.$class);
}
/**
* Checks if the load methods have been called.
*
* @throws NotLoadedException
*/
protected function isLoaded()
{
if (is_null($this->content)) {
throw new NotLoadedException('Content is not loaded!');
}
}
/**
* Cleans the html of any none-html information.
*
* @param string $str
* @return string
*/
protected function clean($str)
{
if ($this->options->get('cleanupInput') != true) {
// skip entire cleanup step
return $str;
}
// remove white space before closing tags
$str = mb_eregi_replace("'\s+>", "'>", $str);
$str = mb_eregi_replace('"\s+>', '">', $str);
// clean out the \n\r
$replace = ' ';
if ($this->options->get('preserveLineBreaks')) {
$replace = ' ';
}
$str = str_replace(["\r\n", "\r", "\n"], $replace, $str);
// strip the doctype
$str = mb_eregi_replace("<!doctype(.*?)>", '', $str);
// strip out comments
$str = mb_eregi_replace("<!--(.*?)-->", '', $str);
// strip out cdata
$str = mb_eregi_replace("<!\[CDATA\[(.*?)\]\]>", '', $str);
// strip out <script> tags
if ($this->options->get('removeScripts') == true) {
$str = mb_eregi_replace("<\s*script[^>]*[^/]>(.*?)<\s*/\s*script\s*>", '', $str);
$str = mb_eregi_replace("<\s*script\s*>(.*?)<\s*/\s*script\s*>", '', $str);
}
// strip out <style> tags
if ($this->options->get('removeStyles') == true) {
$str = mb_eregi_replace("<\s*style[^>]*[^/]>(.*?)<\s*/\s*style\s*>", '', $str);
$str = mb_eregi_replace("<\s*style\s*>(.*?)<\s*/\s*style\s*>", '', $str);
}
// strip out server side scripts
$str = mb_eregi_replace("(<\?)(.*?)(\?>)", '', $str);
// strip smarty scripts
$str = mb_eregi_replace("(\{\w)(.*?)(\})", '', $str);
return $str;
}
/**
* Attempts to parse the html in content.
*/
protected function parse()
{
// add the root node
$this->root = new HtmlNode('root');
$activeNode = $this->root;
while ( ! is_null($activeNode)) {
$str = $this->content->copyUntil('<');
if ($str == '') {
$info = $this->parseTag();
if ( ! $info['status']) {
// we are done here
$activeNode = null;
continue;
}
// check if it was a closing tag
if ($info['closing']) {
$originalNode = $activeNode;
while ($activeNode->getTag()->name() != $info['tag']) {
$activeNode = $activeNode->getParent();
if (is_null($activeNode)) {
// we could not find opening tag
$activeNode = $originalNode;
break;
}
}
if ( ! is_null($activeNode)) {
$activeNode = $activeNode->getParent();
}
continue;
}
if ( ! isset($info['node'])) {
continue;
}
/** @var AbstractNode $node */
$node = $info['node'];
$activeNode->addChild($node);
// check if node is self closing
if ( ! $node->getTag()->isSelfClosing()) {
$activeNode = $node;
}
} else if ($this->options->whitespaceTextNode ||
trim($str) != ''
) {
// we found text we care about
$textNode = new TextNode($str);
$activeNode->addChild($textNode);
}
}
}
/**
* Attempt to parse a tag out of the content.
*
* @return array
* @throws StrictException
*/
protected function parseTag()
{
$return = [
'status' => false,
'closing' => false,
'node' => null,
];
if ($this->content->char() != '<') {
// we are not at the beginning of a tag
return $return;
}
// check if this is a closing tag
if ($this->content->fastForward(1)->char() == '/') {
// end tag
$tag = $this->content->fastForward(1)
->copyByToken('slash', true);
// move to end of tag
$this->content->copyUntil('>');
$this->content->fastForward(1);
// check if this closing tag counts
$tag = strtolower($tag);
if (in_array($tag, $this->selfClosing)) {
$return['status'] = true;
return $return;
} else {
$return['status'] = true;
$return['closing'] = true;
$return['tag'] = strtolower($tag);
}
return $return;
}
$tag = strtolower($this->content->copyByToken('slash', true));
$node = new HtmlNode($tag);
// attributes
while ($this->content->char() != '>' &&
$this->content->char() != '/') {
$space = $this->content->skipByToken('blank', true);
if (empty($space)) {
$this->content->fastForward(1);
continue;
}
$name = $this->content->copyByToken('equal', true);
if ($name == '/') {
break;
}
if (empty($name)) {
$this->content->fastForward(1);
continue;
}
$this->content->skipByToken('blank');
if ($this->content->char() == '=') {
$attr = [];
$this->content->fastForward(1)
->skipByToken('blank');
switch ($this->content->char()) {
case '"':
$attr['doubleQuote'] = true;
$this->content->fastForward(1);
$string = $this->content->copyUntil('"', true, true);
do {
$moreString = $this->content->copyUntilUnless('"', '=>');
$string .= $moreString;
} while ( ! empty($moreString));
$attr['value'] = $string;
$this->content->fastForward(1);
$node->getTag()->$name = $attr;
break;
case "'":
$attr['doubleQuote'] = false;
$this->content->fastForward(1);
$string = $this->content->copyUntil("'", true, true);
do {
$moreString = $this->content->copyUntilUnless("'", '=>');
$string .= $moreString;
} while ( ! empty($moreString));
$attr['value'] = $string;
$this->content->fastForward(1);
$node->getTag()->$name = $attr;
break;
default:
$attr['doubleQuote'] = true;
$attr['value'] = $this->content->copyByToken('attr', true);
$node->getTag()->$name = $attr;
break;
}
} else {
// no value attribute
if ($this->options->strict) {
// can't have this in strict html
$character = $this->content->getPosition();
throw new StrictException("Tag '$tag' has an attribute '$name' with out a value! (character #$character)");
}
$node->getTag()->$name = [
'value' => null,
'doubleQuote' => true,
];
if ($this->content->char() != '>') {
$this->content->rewind(1);
}
}
}
$this->content->skipByToken('blank');
if ($this->content->char() == '/') {
// self closing tag
$node->getTag()->selfClosing();
$this->content->fastForward(1);
} elseif (in_array($tag, $this->selfClosing)) {
// Should be a self closing tag, check if we are strict
if ($this->options->strict) {
$character = $this->content->getPosition();
throw new StrictException("Tag '$tag' is not self closing! (character #$character)");
}
// We force self closing on this tag.
$node->getTag()->selfClosing();
}
$this->content->fastForward(1);
$return['status'] = true;
$return['node'] = $node;
return $return;
}
/**
* Attempts to detect the charset that the html was sent in.
*
* @return bool
*/
protected function detectCharset()
{
// set the default
$encode = new Encode;
$encode->from($this->defaultCharset);
$encode->to($this->defaultCharset);
if ( ! is_null($this->options->enforceEncoding)) {
// they want to enforce the given encoding
$encode->from($this->options->enforceEncoding);
$encode->to($this->options->enforceEncoding);
return false;
}
$meta = $this->root->find('meta[http-equiv=Content-Type]', 0);
if (is_null($meta)) {
// could not find meta tag
$this->root->propagateEncoding($encode);
return false;
}
$content = $meta->content;
if (empty($content)) {
// could not find content
$this->root->propagateEncoding($encode);
return false;
}
$matches = [];
if (preg_match('/charset=(.+)/', $content, $matches)) {
$encode->from(trim($matches[1]));
$this->root->propagateEncoding($encode);
return true;
}
// no charset found
$this->root->propagateEncoding($encode);
return false;
}
}