1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sulao\HtmlQuery; |
4
|
|
|
|
5
|
|
|
use DOMDocument; |
6
|
|
|
use DOMNode; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class HtmlNode |
10
|
|
|
* |
11
|
|
|
* @package Sulao\HtmlQuery |
12
|
|
|
*/ |
13
|
|
|
class HtmlNode |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var DOMNode |
17
|
|
|
*/ |
18
|
|
|
protected $node; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* HtmlNode constructor. |
22
|
|
|
* |
23
|
|
|
* @param DOMNode $node |
24
|
54 |
|
*/ |
25
|
|
|
public function __construct(DOMNode $node) |
26
|
54 |
|
{ |
27
|
54 |
|
$this->node = $node; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Get the outer HTML content. |
32
|
|
|
* |
33
|
|
|
* @return string|null |
34
|
29 |
|
*/ |
35
|
|
|
public function outerHtml() |
36
|
29 |
|
{ |
37
|
|
|
return $this->getDoc()->saveHTML($this->node); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get the inner HTML content. |
42
|
|
|
* |
43
|
|
|
* @return string|null |
44
|
26 |
|
*/ |
45
|
|
|
public function getHtml() |
46
|
26 |
|
{ |
47
|
26 |
|
$content = ''; |
48
|
26 |
|
foreach (iterator_to_array($this->node->childNodes) as $childNode) { |
49
|
|
|
$content .= $this->getDoc()->saveHTML($childNode); |
50
|
|
|
} |
51
|
26 |
|
|
52
|
|
|
return $content; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get the combined text contents, including it's descendants. |
57
|
|
|
* |
58
|
|
|
* @return string|null |
59
|
4 |
|
*/ |
60
|
|
|
public function getText() |
61
|
4 |
|
{ |
62
|
|
|
return $this->node->textContent; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Set the text contents. |
67
|
|
|
* |
68
|
|
|
* @param string $text |
69
|
2 |
|
*/ |
70
|
|
|
public function setText(string $text) |
71
|
2 |
|
{ |
72
|
2 |
|
$this->node->nodeValue = $text; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Remove all child nodes from the DOM. |
77
|
3 |
|
*/ |
78
|
|
|
public function empty() |
79
|
3 |
|
{ |
80
|
3 |
|
$this->node->nodeValue = ''; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Remove the node from the DOM. |
85
|
4 |
|
*/ |
86
|
|
|
public function remove() |
87
|
4 |
|
{ |
88
|
4 |
|
if ($this->node->parentNode) { |
89
|
|
|
$this->node->parentNode->removeChild($this->node); |
90
|
4 |
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Insert a node before the node. |
95
|
|
|
* |
96
|
|
|
* @param DOMNode $newNode |
97
|
5 |
|
*/ |
98
|
|
|
public function before(DOMNode $newNode) |
99
|
5 |
|
{ |
100
|
5 |
|
if ($this->node->parentNode) { |
101
|
|
|
$this->node->parentNode->insertBefore($newNode, $this->node); |
102
|
5 |
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Insert new node after the node. |
107
|
|
|
* |
108
|
|
|
* @param DOMNode $newNode |
109
|
5 |
|
*/ |
110
|
|
|
public function after(DOMNode $newNode) |
111
|
5 |
|
{ |
112
|
|
|
$nextSibling = $this->node->nextSibling; |
113
|
5 |
|
|
114
|
5 |
|
if ($nextSibling && $nextSibling->parentNode) { |
115
|
1 |
|
$nextSibling->parentNode->insertBefore($newNode, $nextSibling); |
116
|
1 |
|
} elseif ($this->node->parentNode) { |
117
|
|
|
$this->node->parentNode->appendChild($newNode); |
118
|
5 |
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Insert a node to the end of the node. |
123
|
|
|
* |
124
|
|
|
* @param DOMNode $newNode |
125
|
6 |
|
*/ |
126
|
|
|
public function append(DOMNode $newNode) |
127
|
6 |
|
{ |
128
|
6 |
|
$this->node->appendChild($newNode); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Insert content or node(s) to the beginning of each matched node. |
133
|
|
|
* |
134
|
|
|
* @param DOMNode $newNode |
135
|
3 |
|
*/ |
136
|
|
|
public function prepend(DOMNode $newNode) |
137
|
3 |
|
{ |
138
|
3 |
|
if ($this->node->firstChild) { |
139
|
|
|
$this->node->insertBefore($newNode, $this->node->firstChild); |
140
|
1 |
|
} else { |
141
|
|
|
$this->node->appendChild($newNode); |
142
|
3 |
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Replace the node with the provided node |
147
|
|
|
* |
148
|
|
|
* @param DOMNode $newNode |
149
|
1 |
|
*/ |
150
|
|
|
public function replaceWith(DOMNode $newNode) |
151
|
1 |
|
{ |
152
|
1 |
|
if ($this->node->parentNode) { |
153
|
|
|
$this->node->parentNode->replaceChild($newNode, $this->node); |
154
|
1 |
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Remove the HTML tag of the node from the DOM. |
159
|
|
|
* Leaving the child nodes in their place. |
160
|
3 |
|
*/ |
161
|
|
|
public function unwrapSelf() |
162
|
3 |
|
{ |
163
|
3 |
|
foreach (iterator_to_array($this->node->childNodes) as $childNode) { |
164
|
|
|
$this->before($childNode); |
165
|
|
|
} |
166
|
3 |
|
|
167
|
3 |
|
$this->remove(); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Get DOMDocument of the node |
172
|
|
|
* |
173
|
|
|
* @return DOMDocument |
174
|
51 |
|
*/ |
175
|
|
|
protected function getDoc(): DOMDocument |
176
|
51 |
|
{ |
177
|
2 |
|
return $this->node instanceof DOMDocument |
|
|
|
|
178
|
51 |
|
? $this->node |
179
|
|
|
: $this->node->ownerDocument; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|