1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bavix\AdvancedHtmlDom; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class AHTMLNode |
7
|
|
|
* |
8
|
|
|
* @package Bavix\AdvancedHtmlDom |
9
|
|
|
* |
10
|
|
|
* @property-read string $clean_text |
11
|
|
|
*/ |
12
|
|
|
class AHTMLNode extends AdvancedHtmlBase implements \ArrayAccess |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var |
17
|
|
|
*/ |
18
|
|
|
private $_path; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* AHTMLNode constructor. |
22
|
|
|
* |
23
|
|
|
* @param $node |
24
|
|
|
* @param $doc |
25
|
|
|
*/ |
26
|
4 |
|
public function __construct($node, $doc) |
27
|
|
|
{ |
28
|
4 |
|
$this->node = $node; |
29
|
4 |
|
$this->_path = $node->getNodePath(); |
30
|
4 |
|
$this->doc = $doc; |
31
|
4 |
|
$this->is_text = $node->nodeName === '#text'; |
32
|
4 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @inheritdoc |
36
|
|
|
*/ |
37
|
2 |
|
public function __destruct() |
38
|
|
|
{ |
39
|
2 |
|
$this->_path = null; |
40
|
2 |
|
unset($this->_path); |
41
|
2 |
|
parent::__destruct(); |
42
|
2 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param $html |
46
|
|
|
* |
47
|
|
|
* @return mixed |
48
|
|
|
*/ |
49
|
|
|
private function get_fragment($html) |
50
|
|
|
{ |
51
|
|
|
$dom = $this->doc->dom; |
52
|
|
|
$fragment = $dom->createDocumentFragment() or die('nope'); |
|
|
|
|
53
|
|
|
$fragment->appendXML($html); |
54
|
|
|
|
55
|
|
|
return $fragment; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param $html |
60
|
|
|
* |
61
|
|
|
* @return AHTMLNode|null |
62
|
|
|
*/ |
63
|
|
|
public function replace($html) |
64
|
|
|
{ |
65
|
|
|
$node = empty($html) ? null : $this->before($html); |
66
|
|
|
$this->remove(); |
67
|
|
|
|
68
|
|
|
return $node; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param $html |
73
|
|
|
* |
74
|
|
|
* @return AHTMLNode |
75
|
|
|
*/ |
76
|
|
|
public function before($html) |
77
|
|
|
{ |
78
|
|
|
$fragment = $this->get_fragment($html); |
79
|
|
|
$this->node->parentNode->insertBefore($fragment, $this->node); |
80
|
|
|
|
81
|
|
|
return new AHTMLNode($this->node->previousSibling, $this->doc); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param $html |
86
|
|
|
*/ |
87
|
|
|
public function after($html) |
88
|
|
|
{ |
89
|
|
|
$fragment = $this->get_fragment($html); |
90
|
|
|
if ($ref_node = $this->node->nextSibling) |
91
|
|
|
{ |
92
|
|
|
$this->node->parentNode->insertBefore($fragment, $ref_node); |
93
|
|
|
} else { |
94
|
|
|
$this->node->parentNode->appendChild($fragment); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param $str |
100
|
|
|
* |
101
|
|
|
* @return mixed |
102
|
|
|
*/ |
103
|
|
|
public function decamelize($str) |
104
|
|
|
{ |
105
|
|
|
$str = \preg_replace_callback( |
106
|
|
|
'/(^|[a-z])([A-Z])/', |
107
|
|
|
function($matches) { |
108
|
|
|
return |
109
|
|
|
\strtolower( |
110
|
|
|
\strlen($matches[1]) |
111
|
|
|
? $matches[1] . '_' . $matches[2] : $matches[2] |
112
|
|
|
); |
113
|
|
|
}, |
114
|
|
|
$str |
115
|
|
|
); |
116
|
|
|
|
117
|
|
|
return \preg_replace('/ /', '_', \strtolower($str)); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
|
|
public function attributes() |
124
|
|
|
{ |
125
|
|
|
$ret = array(); |
126
|
|
|
foreach ($this->node->attributes as $attr) { |
127
|
|
|
$ret[$attr->nodeName] = $attr->nodeValue; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $ret; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param null $key |
|
|
|
|
135
|
|
|
* @param int $level |
136
|
|
|
* |
137
|
|
|
* @return array |
138
|
|
|
*/ |
139
|
|
|
public function flatten($key = null, $level = 1) |
140
|
|
|
{ |
141
|
|
|
|
142
|
|
|
$children = $this->children; |
|
|
|
|
143
|
|
|
$ret = array(); |
144
|
|
|
$tag = $this->tag; |
|
|
|
|
145
|
|
|
|
146
|
|
|
if ($this->at('./preceding-sibling::' . $this->tag) || $this->at('./following-sibling::' . $this->tag) || ($key = $this->tag . 's')) |
|
|
|
|
147
|
|
|
{ |
148
|
|
|
$count = $this->search('./preceding-sibling::' . $this->tag)->length + 1; |
|
|
|
|
149
|
|
|
$tag .= '_' . $count; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
if ($children->length == 0) |
153
|
|
|
{ |
154
|
|
|
$ret[$this->decamelize(\implode(' ', \array_filter(array($key, $tag))))] = $this->text; |
|
|
|
|
155
|
|
|
} else { |
156
|
|
|
$flatten = []; |
157
|
|
|
foreach ($children as $child) |
158
|
|
|
{ |
159
|
|
|
$flatten[] = $child->flatten(\implode(' ', \array_filter(array($key, $level <= 0 ? $tag : null))), $level - 1); |
160
|
|
|
// $ret = array_merge($ret, $child->flatten(implode(' ', array_filter(array($key, $level <= 0 ? $tag : null))), $level - 1)); |
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$ret = \array_merge($ret, ...$flatten); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return $ret; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param $name |
171
|
|
|
* @param $value |
172
|
|
|
*/ |
173
|
|
|
public function __set($name, $value) |
174
|
|
|
{ |
175
|
|
|
switch ($name) |
176
|
|
|
{ |
177
|
|
|
case 'text': |
178
|
|
|
case 'innertext': |
179
|
|
|
case 'innerText': |
180
|
|
|
case 'plaintext': |
181
|
|
|
$this->node->nodeValue = $value; |
182
|
|
|
|
183
|
|
|
return; |
184
|
|
|
case 'outertext': |
185
|
|
|
$this->replace($value); |
186
|
|
|
|
187
|
|
|
return; |
188
|
|
|
case 'tag': |
189
|
|
|
$el = $this->replace('<' . $value . '>' . $this->innerhtml . '</' . $value . '>'); |
|
|
|
|
190
|
|
|
foreach ($this->node->attributes as $_name => $att) |
191
|
|
|
{ |
192
|
|
|
$el->$_name = $att->nodeValue; |
193
|
|
|
} |
194
|
|
|
$this->node = $el->node; |
195
|
|
|
|
196
|
|
|
return; |
197
|
|
|
|
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
$this->offsetSet($name, $value); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param mixed $offset |
205
|
|
|
* |
206
|
|
|
* @return bool |
207
|
|
|
*/ |
208
|
|
|
public function offsetExists($offset) |
209
|
|
|
{ |
210
|
|
|
return true; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @param mixed $offset |
215
|
|
|
* |
216
|
|
|
* @return mixed |
217
|
|
|
*/ |
218
|
|
|
public function offsetGet($offset) |
219
|
|
|
{ |
220
|
|
|
return $this->node->getAttribute($offset); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @param mixed $key |
225
|
|
|
* @param mixed $value |
226
|
|
|
*/ |
227
|
|
|
public function offsetSet($key, $value) |
228
|
|
|
{ |
229
|
|
|
if (\in_array($key, ['_path','dom','doc','node'])) |
230
|
|
|
{ |
231
|
|
|
return; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
if ($value) |
235
|
|
|
{ |
236
|
|
|
$this->node->setAttribute($key, $value); |
237
|
|
|
return; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
$this->node->removeAttribute($key); |
241
|
|
|
//trigger_error('offsetSet not implemented', E_USER_WARNING); |
|
|
|
|
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @param mixed $offset |
246
|
|
|
*/ |
247
|
|
|
public function offsetUnset($offset) |
248
|
|
|
{ |
249
|
|
|
\trigger_error('offsetUnset not implemented', E_USER_WARNING); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @return mixed |
254
|
|
|
*/ |
255
|
|
|
public function title() |
256
|
|
|
{ |
257
|
|
|
return $this->node->getAttribute('title'); |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.