-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathHTML5DOMDocument.php
More file actions
128 lines (106 loc) · 3.38 KB
/
HTML5DOMDocument.php
File metadata and controls
128 lines (106 loc) · 3.38 KB
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
<?php
namespace Masterminds\HTML5;
use Masterminds\HTML5\Serializer\OutputRules;
use Masterminds\HTML5\Serializer\Traverser;
/**
* Shared serializer logic for template-aware DOMDocument implementations.
*/
class HTML5DOMDocumentBase extends \DOMDocument
{
/**
* @var \SplObjectStorage|null
*/
protected $templateContents;
/**
* @param bool $create
*
* @return \SplObjectStorage|null
*/
public function html5PhpTemplateContentsStorage($create = true)
{
if (null === $this->templateContents && $create) {
$this->templateContents = new \SplObjectStorage();
}
return $this->templateContents;
}
protected function html5PhpSaveHTML($node = null)
{
$target = null === $node ? $this : $node;
if (!$this->containsDetachedTemplateContents($target)) {
return parent::saveHTML($node);
}
$stream = fopen('php://temp', 'wb');
$rules = new OutputRules($stream);
$traverser = new Traverser($target, $stream, $rules);
$traverser->walk();
$rules->unsetTraverser();
$html = stream_get_contents($stream, -1, 0);
fclose($stream);
return $html;
}
protected function html5PhpImportNode($node, $deep = false)
{
$imported = parent::importNode($node, $deep);
TemplateContents::copySubtree($node, $imported, $deep);
return $imported;
}
protected function html5PhpCloneNode($deep = false)
{
$class = get_class($this);
$clone = new $class($this->xmlVersion, $this->encoding);
TemplateContents::registerNodeClasses($clone);
$clone->encoding = $this->encoding;
$clone->formatOutput = $this->formatOutput;
$clone->preserveWhiteSpace = $this->preserveWhiteSpace;
$clone->recover = $this->recover;
$clone->resolveExternals = $this->resolveExternals;
$clone->strictErrorChecking = $this->strictErrorChecking;
$clone->substituteEntities = $this->substituteEntities;
$clone->validateOnParse = $this->validateOnParse;
if (!$deep) {
return $clone;
}
foreach ($this->childNodes as $child) {
$clone->appendChild($clone->importNode($child, true));
}
return $clone;
}
/**
* @param \DOMNode $node
*
* @return bool
*/
protected function containsDetachedTemplateContents(\DOMNode $node)
{
if ($node instanceof \DOMElement && 'template' === strtolower($node->tagName)) {
return null !== TemplateContents::find($node);
}
if ($node->hasChildNodes()) {
foreach ($node->childNodes as $child) {
if ($this->containsDetachedTemplateContents($child)) {
return true;
}
}
}
return false;
}
}
if (PHP_VERSION_ID >= 80100) {
require __DIR__ . '/HTML5DOMDocument81.php';
} else {
class HTML5DOMDocument extends HTML5DOMDocumentBase
{
public function saveHTML($node = null)
{
return $this->html5PhpSaveHTML($node);
}
public function importNode($node, $deep = false)
{
return $this->html5PhpImportNode($node, $deep);
}
public function cloneNode($deep = false)
{
return $this->html5PhpCloneNode($deep);
}
}
}