Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
document.php
1<?php
2namespace Bitrix\Main\Web\DOM;
3
4class Document extends Node
5{
7 protected $parser;
8
10 protected $queryEngine;
11
12 /*
13 * @return void
14 */
15 public function __construct()
16 {
17 $this->init();
18 $this->nodeType = self::DOCUMENT_NODE;
19 $this->nodeName = '#document';
20
21 $this->ownerDocument = $this;
22
24 }
25
26 /*
27 * @param string $source
28 * @return void
29 */
30 public function loadHTML($source)
31 {
32 $this->parser->parse($source, $this);
33 }
34
35 /*
36 * @param null|Node $node
37 * @return string
38 */
39 public function saveHTML(Node $node = null)
40 {
41 if($node === null)
42 {
43 $result = '';
44
45 if(self::$isNodeListAsArray)
46 {
48 foreach ($childNodes as $child)
49 {
51 $result .= $child->getOuterHTML();
52 }
53 }
54 else
55 {
56 for($i = 0; $i < $this->getChildNodes()->getLength(); $i++)
57 {
58 $child = $this->getChildNodes()->item($i);
60 if($child)
61 {
62 $result .= $child->getOuterHTML();
63 }
64 }
65 }
66
67 return $result;
68 }
69 else
70 {
71 return $node->getOuterHTML();
72 }
73 }
74
75 /*
76 * @return QueryEngine
77 */
78 public function getQueryEngine()
79 {
80 if(!$this->queryEngine)
81 {
82 $this->queryEngine = QueryEngine::getQuerySelectorEngine();
83 }
84
85 return $this->queryEngine;
86 }
87
88 /*
89 * @param QueryEngine $engine
90 * @return void
91 */
92 public function setQueryEngine(QueryEngine $engine)
93 {
94 $this->queryEngine = $engine;
95 }
96
97 /*
98 * @return Parser
99 */
100 public function getParser()
101 {
102 return $this->parser;
103 }
104
105 /*
106 * @param Parser $parser
107 * @return void
108 */
109 public function setParser(Parser $parser)
110 {
111 $this->parser = $parser;
112 }
113
114 /*
115 * Changes the ownerDocument of a node, its children, as well as the attached attribute nodes if there are any.
116 * If the node has a parent it is first removed from its parent child list.
117 * This effectively allows moving a subtree from one document to another.
118 */
119 public function adoptNode(Node $source)
120 {
121 if($source->getParentNode())
122 {
123 $source->getParentNode()->removeChild($source);
124 }
125
126 $source->setOwnerDocument($this);
127
128 if($source->hasAttributes())
129 {
130 $attrList = $source->getAttributes()->get();
131 foreach($attrList as $attr)
132 {
134 $attr->setOwnerDocument($this);
135 }
136 }
137
138 if($source->hasChildNodes())
139 {
140 foreach($source->getChildNodesArray() as $child)
141 {
143 $child->setOwnerDocument($this);
144 }
145 }
146 }
147
148 /*
149 * @param string $tagName
150 * @return Element
151 */
152 public function createElement($tagName)
153 {
154 static $classByTag = array();
155
156 $tagName = mb_strtoupper($tagName);
157 $elementClass = "Bitrix\\Main\\Web\\DOM\\Element\\" . $tagName;
158
159 if(!isset($classByTag[$tagName]))
160 {
161 if(class_exists($elementClass))
162 {
163 $classByTag[$tagName] = $elementClass;
164 }
165 else
166 {
167 $classByTag[$tagName] = false;
168 }
169 }
170
171 if($classByTag[$tagName])
172 {
173 $elementClass = $classByTag[$tagName];
174 $node = new $elementClass($tagName);
175 }
176 else
177 {
178 $node = new Element($tagName);
179 }
180
181 $node->setOwnerDocument($this);
182 return $node;
183 }
184
185 /*
186 * @param string $name
187 * @param string $value
188 * @return Attr
189 */
190 public function createAttribute($name, $value)
191 {
192 $node = new Attr($name, $value);
193 $node->setOwnerDocument($this);
194 return $node;
195 }
196
197 /*
198 * @param string $comment
199 * @return Comment
200 */
201 public function createComment($comment)
202 {
203 $node = new Comment($comment);
204 $node->setOwnerDocument($this);
205 return $node;
206 }
207
208 /*
209 * @param string $text
210 * @return Text
211 */
212 public function createTextNode($text)
213 {
214 $node = new Text($text);
215 $node->setOwnerDocument($this);
216 return $node;
217 }
218
219 /*
220 * @return null
221 */
222 public function createDocumentFragment()
223 {
224 throw new DomException('Not implemented');
225 }
226
227 /*
228 * @return null|Node
229 */
230 public function getElementById($id)
231 {
232 $resultList = $this->getElementsByAttr('id', $id, 1);
233 return (!empty($resultList)) ? current($resultList) : null;
234 }
235
236 /*
237 * @return null|Node
238 */
239 public function getElementByClassName($className)
240 {
241 $resultList = $this->getElementsByClassName($className, 1);
242 return (!empty($resultList)) ? current($resultList) : null;
243 }
244
245 /*
246 * @return array|NodeList
247 */
248 public function getElementsByName($name)
249 {
250 return $this->getElementsByAttr('name', $name);
251 }
252
253 public function getTextContent()
254 {
255 return null;
256 }
257
258 /*
259 * @return array|NodeList
260 */
261 public function getElementsByAttr($attrName, $attrValue = null, $limit = 0)
262 {
263 $attrName = mb_strtolower($attrName);
264 $nodeList = $this->getQueryEngine()->walk(
265 array(
266 array(
268 array(
269 'name' => $attrName,
270 'value' => $attrValue,
272 )
273 )
274 )
275 ),
276 null, $this, $limit
277 );
278
280 {
281 return $nodeList;
282 }
283 else
284 {
285 return new NodeList($nodeList);
286 }
287 }
288
289 /*
290 * @return array|NodeList
291 */
292 public function getElementsByTagName($tagName)
293 {
294 $tagName = mb_strtoupper($tagName);
295 $nodeList = $this->getQueryEngine()->walk(
296 array(
297 array(QueryEngine::FILTER_NODE_NAME => $tagName)
298 ),
299 null, $this
300 );
301
303 {
304 return $nodeList;
305 }
306 else
307 {
308 return new NodeList($nodeList);
309 }
310 }
311
312 /*
313 * @return array|NodeList
314 */
315 public function getElementsByClassName($className, $limit = 0)
316 {
317 $nodeList = $this->getQueryEngine()->walk(
318 array(
319 array(QueryEngine::FILTER_ATTR_CLASS_NAME => $className)
320 ),
321 null, $this, $limit
322 );
323
325 {
326 return $nodeList;
327 }
328 else
329 {
330 return new NodeList($nodeList);
331 }
332 }
333
334 /*
335 * @return null|Element
336 */
337 public function getDocumentElement()
338 {
339 foreach($this->getChildNodesArray() as $child)
340 {
342 if($child->getNodeName() === 'HTML')
343 {
344 return $child;
345 }
346 }
347
348 return null;
349 }
350
351 /*
352 * Get HEAD element
353 * @return null|Element
354 */
355 public function getHead()
356 {
357 if(!$this->getDocumentElement())
358 {
359 return null;
360 }
361
362 foreach($this->getDocumentElement()->getChildNodesArray() as $child)
363 {
365 if($child->getNodeName() === 'HEAD')
366 {
367 return $child;
368 }
369 }
370
371 return null;
372 }
373
374 /*
375 * Get BODY element
376 * @return null|Element
377 */
378 public function getBody()
379 {
380 if(!$this->getDocumentElement())
381 {
382 return null;
383 }
384
385 foreach($this->getDocumentElement()->getChildNodesArray() as $child)
386 {
388 if($child->getNodeName() === 'BODY')
389 {
390 return $child;
391 }
392 }
393
394 return null;
395 }
396}
createAttribute($name, $value)
Definition document.php:190
getElementByClassName($className)
Definition document.php:239
getElementsByClassName($className, $limit=0)
Definition document.php:315
setQueryEngine(QueryEngine $engine)
Definition document.php:92
getElementsByAttr($attrName, $attrValue=null, $limit=0)
Definition document.php:261
setParser(Parser $parser)
Definition document.php:109
setOwnerDocument(Document $owner)
Definition node.php:131
static $isNodeListAsArray
Definition node.php:19