This repository has been archived by the owner on May 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathDomVisitor.php
96 lines (81 loc) · 2.83 KB
/
DomVisitor.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
<?php
/*
* This file is part of the HTML sanitizer project.
*
* (c) Titouan Galopin <galopintitouan@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace HtmlSanitizer;
use HtmlSanitizer\Model\Cursor;
use HtmlSanitizer\Node\DocumentNode;
use HtmlSanitizer\Node\TextNode;
use HtmlSanitizer\Visitor\NamedNodeVisitorInterface;
use HtmlSanitizer\Visitor\NodeVisitorInterface;
/**
* The DomVisitor iterate over the parsed DOM tree and visit nodes using NodeVisitorInterface objects.
* For performance reasons, these objects are split in 2 groups: generic ones and node-specific ones.
*
* @author Titouan Galopin <galopintitouan@gmail.com>
*
* @final
*/
class DomVisitor implements DomVisitorInterface
{
/**
* @var NodeVisitorInterface[]
*/
private $genericNodeVisitors = [];
/**
* @var NamedNodeVisitorInterface[]
*/
private $namedNodeVisitors = [];
/**
* @param NodeVisitorInterface[] $visitors
*/
public function __construct(array $visitors = [])
{
foreach ($visitors as $visitor) {
if ($visitor instanceof NamedNodeVisitorInterface) {
foreach ($visitor->getSupportedNodeNames() as $nodeName) {
$this->namedNodeVisitors[$nodeName][] = $visitor;
}
continue;
}
$this->genericNodeVisitors[] = $visitor;
}
}
public function visit(\DOMNode $node): DocumentNode
{
$cursor = new Cursor();
$cursor->node = new DocumentNode();
$this->visitNode($node, $cursor);
return $cursor->node;
}
private function visitNode(\DOMNode $node, Cursor $cursor)
{
/** @var NodeVisitorInterface[] $supportedVisitors */
$supportedVisitors = array_merge($this->namedNodeVisitors[$node->nodeName] ?? [], $this->genericNodeVisitors);
foreach ($supportedVisitors as $visitor) {
if ($visitor->supports($node, $cursor)) {
$visitor->enterNode($node, $cursor);
}
}
/** @var \DOMNode $child */
foreach ($node->childNodes ?? [] as $k => $child) {
if ('#text' === $child->nodeName) {
// Add text in the safe tree without a visitor for performance
$cursor->node->addChild(new TextNode($cursor->node, $child->nodeValue));
} elseif (!$child instanceof \DOMText) {
// Ignore comments for security reasons (interpreted differently by browsers)
$this->visitNode($child, $cursor);
}
}
foreach (array_reverse($supportedVisitors) as $visitor) {
if ($visitor->supports($node, $cursor)) {
$visitor->leaveNode($node, $cursor);
}
}
}
}