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