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