-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathChildNode.php
108 lines (98 loc) · 2.92 KB
/
ChildNode.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
namespace Gt\Dom;
/**
* The ChildNode mixin contains methods and properties that are common to all
* types of Node objects that can have a parent. It's implemented by Element,
* DocumentType, and CharacterData objects.
*
* @link https://developer.mozilla.org/en-US/docs/Web/API/ChildNode
*
* @property-read null|Element $parentElement
* @property-read null|Node|Element $parentNode
*/
trait ChildNode {
/** @link https://developer.mozilla.org/en-US/docs/Web/API/Node/parentElement */
protected function __prop_get_parentElement():null|Element {
$element = $this->parentNode;
if($element instanceof Element) {
return $element;
}
return null;
}
/**
* Removes the object from the tree it belongs to.
*
* @link https://developer.mozilla.org/en-US/docs/Web/API/ChildNode/remove
*/
public function remove():void {
if($parentNode = $this->parentNode) {
$parentNode->removeChild($this);
}
}
/**
* The ChildNode.before() method inserts a set of Node or DOMString
* objects in the children list of this ChildNode's parent, just before
* this ChildNode. DOMString objects are inserted as equivalent Text
* nodes.
*
* @param Element|Node|string...$nodes A set of Node or DOMString
* objects to insert.
* @link https://developer.mozilla.org/en-US/docs/Web/API/ChildNode/before
*/
public function before(...$nodes):void {
$parent = $this->parentNode;
if(!$parent) {
return;
}
foreach($nodes as $node) {
if(is_string($node)) {
$node = $this->ownerDocument->createTextNode($node);
}
$parent->insertBefore($node, $this);
}
}
/**
* The ChildNode.after() method inserts a set of Node or DOMString
* objects in the children list of this ChildNode's parent, just after
* this ChildNode. DOMString objects are inserted as equivalent Text
* nodes.
*
* @param Element|Node|string...$nodes
* @link https://developer.mozilla.org/en-US/docs/Web/API/ChildNode/after
*/
public function after(...$nodes):void {
$parent = $this->parentNode;
$nextSibling = $this->nextSibling;
if(!$parent) {
return;
}
foreach($nodes as $node) {
if(is_string($node)) {
$node = $this->ownerDocument->createTextNode($node);
}
$parent->insertBefore($node, $nextSibling);
}
}
/**
* The ChildNode.replaceWith() method replaces this ChildNode in the
* children list of its parent with a set of Node or DOMString objects.
* DOMString objects are inserted as equivalent Text nodes.
*
* @param Node|Element|string...$nodes
* @link https://developer.mozilla.org/en-US/docs/Web/API/ChildNode/replaceWith
*/
public function replaceWith(...$nodes):void {
$parent = $this->parentNode;
if(!$parent) {
return;
}
$fragment = $this->ownerDocument->createDocumentFragment();
foreach($nodes as $node) {
if(is_string($node)) {
$node = $this->ownerDocument->createTextNode($node);
}
$fragment->appendChild($node);
}
$parent->replaceChild($fragment, $this);
}
}