Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
node.php
1<?php
2namespace Bitrix\Main\Web\DOM;
3
4abstract class Node
5{
6 const ELEMENT_NODE = 1;
7 const ATTRIBUTE_NODE = 2;
8 const TEXT_NODE = 3;
11 const ENTITY_NODE = 6;
13 const COMMENT_NODE = 8;
15 const DOCUMENT_NODE = 9;
17 const NOTATION_NODE = 12;
18
19 public static $isNodeListAsArray = true;
20
21 /*@var Document $ownerDocument*/
22 protected $ownerDocument = null;
23
24 /*@var string $nodeType*/
25 protected $nodeType = null;
26
27 /*@var string $nodeName*/
28 protected $nodeName = null;
29
30 /*@var string $nodeValue*/
31 protected $nodeValue = null;
32
33 /*@var NodeList $childNodes*/
34 protected $childNodes = null;
35
36 /*@var Node $parentNode*/
37 protected $parentNode = null;
38
39 /*@var null|NamedNodeMap $attributes*/
40 protected $attributes = null;
41
42 /*@var string $textContent*/
43 protected $textContent = null;
44
45 /*@var bool $bxIsAlreadyClosed*/
46 public $bxIsAlreadyClosed = false;
47 /*@var bool $bxClosable*/
48 public $bxClosable = true;
49
50 public $bxNodeFoundCloseTag = false;
51
52 public function __construct()
53 {
54 $this->init();
55 }
56
57 protected function init()
58 {
59 if(self::$isNodeListAsArray)
60 {
61 $this->childNodes = array();
62 }
63 else
64 {
65 $this->childNodes = new NodeList;
66 }
67 }
68
69 public function getNodeType()
70 {
71 return $this->nodeType;
72 }
73
74 public function getParentNode()
75 {
76 return $this->parentNode;
77 }
78
79 public function setParentNode(Node $node = null)
80 {
81 $this->parentNode = $node;
82 }
83
84 public function hasAttributes()
85 {
86 if(!$this->attributes)
87 {
88 return false;
89 }
90
91 if(self::$isNodeListAsArray)
92 {
93 return !empty($this->attributes);
94 }
95 else
96 {
97 return $this->attributes->getLength() > 0;
98 }
99 }
100
101 public function getAttributes()
102 {
103 return $this->attributes;
104 }
105
106 public function getNodeName()
107 {
108 return $this->nodeName;
109 }
110
111 public function setNodeName($a)
112 {
113 $this->nodeName = $a;
114 }
115
116 public function getNodeValue()
117 {
118 return $this->nodeValue;
119 }
120
121 public function getTextContent()
122 {
123 return $this->nodeValue;
124 }
125
126 public function getOwnerDocument()
127 {
129 }
130
131 public function setOwnerDocument(Document $owner)
132 {
133 $this->ownerDocument = $owner;
134 }
135
136 public function getFirstChild()
137 {
138 $child = null;
139 foreach($this->getChildNodesArray() as $child)
140 {
141 break;
142 }
143
144 return $child;
145 }
146
147 public function getLastChild()
148 {
149 $child = null;
150 foreach($this->getChildNodesArray() as $child)
151 {
152
153 }
154
155 return $child;
156 }
157
158 public function getPreviousSibling()
159 {
160 $searchableSibling = null;
161 if($this->parentNode)
162 {
163 $previousSibling = null;
164 foreach($this->parentNode->getChildNodesArray() as $sibling)
165 {
166 if($this->isEqual($sibling))
167 {
168 $searchableSibling = $previousSibling;
169 break;
170 }
171
172 $previousSibling = $sibling;
173 }
174 }
175
176 return $searchableSibling;
177 }
178
179 public function getNextSibling()
180 {
181 $searchableSibling = null;
182 if($this->parentNode)
183 {
184 $previousSibling = null;
185 foreach($this->parentNode->getChildNodesArray() as $sibling)
186 {
187 if($this->isEqual($previousSibling))
188 {
189 $searchableSibling = $sibling;
190 break;
191 }
192
193 $previousSibling = $sibling;
194 }
195 }
196
197 return $searchableSibling;
198 }
199
200 /*
201 * @return NodeList|Node[]
202 */
203 public function getChildNodes()
204 {
205 return $this->childNodes;
206 }
207
208 /*
209 * @return Node[]
210 */
211 public function getChildNodesArray()
212 {
213 if(self::$isNodeListAsArray)
214 {
215 return $this->childNodes;
216 }
217 else
218 {
219 return $this->childNodes->get();
220 }
221 }
222
223 public function setChildNodesArray($childList)
224 {
225 if(self::$isNodeListAsArray)
226 {
227 $this->childNodes = $childList;
228 }
229 else
230 {
231 $this->childNodes->set($childList);
232 }
233 }
234
235 public function hasChildNodes()
236 {
237 if(self::$isNodeListAsArray)
238 {
239 return (!empty($this->getChildNodes()));
240 }
241 else
242 {
243 return ($this->getChildNodes()->getLength() > 0);
244 }
245 }
246
247 protected function haveChild($checkingChild)
248 {
249 foreach($this->childNodes as $child)
250 {
251 if($child === $checkingChild)
252 {
253 return true;
254 }
255 }
256
257 return false;
258 }
259
264 public function appendChild(Node $newChild)
265 {
266 $this->insertBefore($newChild);
267 }
268
269 public function insertBefore(Node $newChild, Node $refChild = null, $removeExist = true)
270 {
271 if($newChild->getOwnerDocument() !== $this->getOwnerDocument())
272 {
273 throw new DomException('Node newChild was created from a different document than the one that created this node', DomException::WRONG_DOCUMENT_ERR);
274 }
275
276 if($refChild && !$this->haveChild($refChild))
277 {
278 throw new DomException('Node refChild not found in childList', DomException::NOT_FOUND_ERR);
279 }
280
281 if ($removeExist && $this->haveChild($newChild))
282 {
283 $this->removeChild($newChild);
284 }
285
286
287
288 $childList = $this->getChildNodesArray();
289 if($refChild)
290 {
291 $childListNew = array();
292 foreach($childList as $child)
293 {
294 if($refChild === $child)
295 {
296 $childListNew[] = $newChild;
297 }
298 $childListNew[] = $child;
299 }
300 $childList = $childListNew;
301 }
302 else
303 {
304 $childList[] = $newChild;
305 }
306
307 $this->setChildNodesArray($childList);
308 $newChild->setParentNode($this);
309
310 return $newChild;
311 }
312
313 public function removeChild(Node $oldChild)
314 {
315 $childList = $this->getChildNodesArray();
316 $childListNew = array();
317 $isFound = false;
318 foreach($childList as $child)
319 {
320 if($oldChild !== $child)
321 {
322 $childListNew[] = $child;
323 }
324 else
325 {
326 $isFound = true;
327 }
328 }
329
330 if($isFound)
331 {
332 $this->setChildNodesArray($childListNew);
333 $oldChild->setParentNode(null);
334 }
335 else
336 {
337 throw new DomException('Node not found in childList', DomException::NOT_FOUND_ERR);
338 }
339
340 return $oldChild;
341 }
342
343 public function replaceChild(Node $newChild, Node $oldChild)
344 {
345 throw new DomException('Not implemented');
346 }
347
348 public function isEqual(Node $node = null)
349 {
350 if($node && $node === $this)
351 {
352 return true;
353 }
354
355 return false;
356 }
357
358 public function getOuterHTML()
359 {
360 return $this->getOwnerDocument()->getParser()->getSource($this);
361 }
362
363 public function getInnerHTML()
364 {
365 $html = '';
366 if(!$this->hasChildNodes())
367 {
368 return $html;
369 }
370
371 $parser = $this->getOwnerDocument()->getParser();
372 foreach($this->getChildNodesArray() as $child)
373 {
374 $html .= $parser->getSource($child);
375 }
376
377 return $html;
378 }
379
380 public function setInnerHTML($html)
381 {
382 foreach($this->getChildNodesArray() as $child)
383 {
384 $this->removeChild($child);
385 }
386 $this->getOwnerDocument()->getParser()->parse($html, $this);
387 }
388
394 public function querySelectorAll($queryString)
395 {
396 return QueryEngine::getQuerySelectorEngine()->query($queryString, $this);
397 }
398
403 public function querySelector($queryString)
404 {
405 $list = QueryEngine::getQuerySelectorEngine()->query($queryString, $this, 1);
406 return current($list);
407 }
408
409 public function closest($queryString)
410 {
411 $list = $this->getOwnerDocument()->getQueryEngine()->query($queryString, $this, 1, QueryEngine::DIR_UP);
412 return current($list);
413 }
414
415 public function toString()
416 {
417 throw new DomException('Not implemented');
418 }
419
420 public function toArray()
421 {
422 $childNodes = array();
423 if($this->hasChildNodes())
424 {
425 foreach($this->getChildNodesArray() as $child)
426 {
427 /* @var $child Node*/
428 $childNodes[] = $child->toArray();
429 }
430 }
431
432 $attributes = array();
433 if($this->hasAttributes())
434 {
435 foreach($this->getAttributes() as $attr)
436 {
437 /* @var $attr Attr*/
438 $attributes = array_merge($attributes, $attr->toArray());
439 }
440 }
441
442 return array(
443 'nodeType' => $this->nodeType,
444 'nodeName' => $this->nodeName,
445 'textContent' => $this->getTextContent(),
446 'attributesCount' => count($attributes),
447 'attributes' => $attributes,
448 'childNodesCount' => count($childNodes),
449 'childNodes' => $childNodes,
450 );
451 }
452}
removeChild(Node $oldChild)
Definition node.php:313
setChildNodesArray($childList)
Definition node.php:223
closest($queryString)
Definition node.php:409
isEqual(Node $node=null)
Definition node.php:348
querySelector($queryString)
Definition node.php:403
setOwnerDocument(Document $owner)
Definition node.php:131
insertBefore(Node $newChild, Node $refChild=null, $removeExist=true)
Definition node.php:269
setParentNode(Node $node=null)
Definition node.php:79
const ENTITY_REFERENCE_NODE
Definition node.php:10
querySelectorAll($queryString)
Definition node.php:394
haveChild($checkingChild)
Definition node.php:247
static $isNodeListAsArray
Definition node.php:19
const PROCESSING_INSTRUCTION_NODE
Definition node.php:12
const DOCUMENT_FRAGMENT_NODE
Definition node.php:14
appendChild(Node $newChild)
Definition node.php:264
replaceChild(Node $newChild, Node $oldChild)
Definition node.php:343