Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
element.php
1<?php
2namespace Bitrix\Main\Web\DOM;
3
4class Element extends Node
5{
9 protected $parentElement;
13 protected $attributes;
17 protected $tagName;
21 protected $classList;
25 protected $id;
29 protected $name;
33 protected $className;
37 protected $style;
38
39
40 public function __construct($name)
41 {
42 $this->init();
43
44 $this->nodeType = self::ELEMENT_NODE;
45 $this->nodeName = mb_strtoupper($name);
46 $this->tagName = $this->nodeName;
47
48 if(self::$isNodeListAsArray)
49 {
50 $this->attributes = array();
51 }
52 else
53 {
54 $this->attributes = new NamedNodeMap;
55 }
56 }
57
58 public function getTagName()
59 {
60 return $this->tagName;
61 }
62
63 public function getId()
64 {
65 return $this->getAttribute('id');
66 }
67
68 public function setId($id)
69 {
70 $this->setAttribute('id', $id);
71 }
72
73 public function getName()
74 {
75 return $this->getAttribute('name');
76 }
77
78 public function setName($id)
79 {
80 $this->setAttribute('name', $id);
81 }
82
83 public function getStyle()
84 {
85 return $this->getAttribute('style');
86 }
87
88 public function setStyle($style)
89 {
90 $this->setAttribute('style', $style);
91 }
92
93 public function getClassName()
94 {
95 return $this->getAttribute('class');
96 }
97
98 public function setClassName($className)
99 {
100 $this->setAttribute('class', $className);
101 }
102
103 public function getClassList()
104 {
105 return explode(' ', $this->getClassName());
106 }
107
108 public function setClassList(array $classList)
109 {
110 $this->setAttribute('class', implode(' ', $classList));
111 }
112
113 public function getParentElement()
114 {
115 return $this->parentElement;
116 }
117
118 public function getTextContent()
119 {
120 $content = '';
121 foreach($this->getChildNodesArray() as $child)
122 {
123 if($child->getNodeType() === self::COMMENT_NODE || $child->getNodeType() === self::PROCESSING_INSTRUCTION_NODE )
124 {
125 continue;
126 }
127
128 $content .= $child->getTextContent();
129 }
130
131 return $content;
132 }
133
134 protected function setParentElement(Element $parentElement)
135 {
136 $this->parentElement = $parentElement;
137 }
138
139 public function getAttributesArray()
140 {
141 if(self::$isNodeListAsArray)
142 {
143 return $this->attributes;
144 }
145 else
146 {
147 return $this->attributes->get();
148 }
149 }
150
151 public function setAttributeNode(Attr $attr)
152 {
153 if(self::$isNodeListAsArray)
154 {
155 $name = $attr->getName();
156 $this->attributes[$name] = $attr;
157 }
158 else
159 {
160 $this->getAttributes()->setNamedItem($attr);
161 }
162
163 $attr->setParentNode($this);
164 }
165
166 public function getAttributeNode($name)
167 {
168 $result = null;
169 if($this->hasAttributes())
170 {
171 if(self::$isNodeListAsArray)
172 {
173 $result = $this->attributes[$name] ?? null;
174 }
175 else
176 {
177 $result = $this->attributes->getNamedItem($name);
178 }
179 }
180
181 return $result;
182 }
183
184 public function removeAttributeNode(Attr $oldNode)
185 {
186 if($this->attributes)
187 {
188 if(self::$isNodeListAsArray)
189 {
190 $name = $oldNode->getName();
191 unset($this->attributes[$name]);
192 }
193 else
194 {
195 $this->attributes->removeNamedItem($oldNode->getName());
196 }
197
198 $oldNode->setParentNode(null);
199 }
200 }
201
202 public function setAttribute($name, $value)
203 {
204 $attr = $this->getAttributeNode($name);
205 if(!$attr)
206 {
207 $attr = $this->ownerDocument->createAttribute($name, $value);
208 }
209 else
210 {
211 $attr->setValue($value);
212 }
213
214 $this->setAttributeNode($attr);
215 }
216
221 public function hasAttribute($attributeName)
222 {
223 $result = false;
224 if($this->hasAttributes())
225 {
226 if(self::$isNodeListAsArray)
227 {
228 $result = array_key_exists($attributeName, $this->attributes);
229 }
230 else
231 {
232 $result = !is_null($this->attributes->getNamedItem($attributeName));
233 }
234 }
235 return $result;
236 }
237
238 public function getAttribute($name)
239 {
240 $attr = $this->getAttributeNode($name);
241 if($attr)
242 {
243 return $attr->getValue();
244 }
245
246 return null;
247 }
248
249 public function removeAttribute($name)
250 {
251 $attr = $this->getAttributeNode($name);
252 if($attr)
253 {
254 $this->removeAttributeNode($attr);
255 }
256 }
257}
hasAttribute($attributeName)
Definition element.php:221
setClassList(array $classList)
Definition element.php:108
setAttribute($name, $value)
Definition element.php:202
removeAttributeNode(Attr $oldNode)
Definition element.php:184
setParentElement(Element $parentElement)
Definition element.php:134
setParentNode(Node $node=null)
Definition node.php:79