1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
document.php
См. документацию.
1<?php
2
3namespace Bitrix\Main\Web\DOM;
4
5class Document extends Node
6{
8 protected $parser;
9
11 protected $queryEngine;
12
16 public function __construct()
17 {
18 $this->init();
19 $this->nodeType = self::DOCUMENT_NODE;
20 $this->nodeName = '#document';
21
22 $this->ownerDocument = $this;
23
25 }
26
31 public function loadHTML($source)
32 {
33 $this->parser->parse($source, $this);
34 }
35
40 public function saveHTML(Node $node = null)
41 {
42 if($node === null)
43 {
44 $result = '';
45
46 if(self::$isNodeListAsArray)
47 {
49 foreach ($childNodes as $child)
50 {
52 $result .= $child->getOuterHTML();
53 }
54 }
55 else
56 {
57 for($i = 0; $i < $this->getChildNodes()->getLength(); $i++)
58 {
59 $child = $this->getChildNodes()->item($i);
61 if($child)
62 {
63 $result .= $child->getOuterHTML();
64 }
65 }
66 }
67
68 return $result;
69 }
70
71 return $node->getOuterHTML();
72 }
73
77 public function getQueryEngine()
78 {
79 if(!$this->queryEngine)
80 {
81 $this->queryEngine = QueryEngine::getQuerySelectorEngine();
82 }
83
84 return $this->queryEngine;
85 }
86
92 {
93 $this->queryEngine = $engine;
94 }
95
99 public function getParser()
100 {
101 return $this->parser;
102 }
103
108 public function setParser(Parser $parser)
109 {
110 $this->parser = $parser;
111 }
112
118 public function adoptNode(Node $source)
119 {
120 if($source->getParentNode())
121 {
122 $source->getParentNode()->removeChild($source);
123 }
124
125 $source->setOwnerDocument($this);
126
127 if($source->hasAttributes())
128 {
129 $attrList = $source->getAttributes()->get();
130 foreach($attrList as $attr)
131 {
133 $attr->setOwnerDocument($this);
134 }
135 }
136
137 if($source->hasChildNodes())
138 {
139 foreach($source->getChildNodesArray() as $child)
140 {
142 $child->setOwnerDocument($this);
143 }
144 }
145 }
146
151 public function createElement($tagName)
152 {
153 static $classByTag = [];
154
155 $tagName = mb_strtoupper($tagName);
156 $elementClass = "Bitrix\\Main\\Web\\DOM\\Element\\" . $tagName;
157
158 if(!isset($classByTag[$tagName]))
159 {
160 if(class_exists($elementClass))
161 {
162 $classByTag[$tagName] = $elementClass;
163 }
164 else
165 {
166 $classByTag[$tagName] = false;
167 }
168 }
169
170 if($classByTag[$tagName])
171 {
172 $elementClass = $classByTag[$tagName];
173 $node = new $elementClass($tagName);
174 }
175 else
176 {
177 $node = new Element($tagName);
178 }
179
180 $node->setOwnerDocument($this);
181
182 return $node;
183 }
184
190 public function createAttribute($name, $value)
191 {
192 $node = new Attr($name, $value);
193 $node->setOwnerDocument($this);
194
195 return $node;
196 }
197
202 public function createComment($comment)
203 {
204 $node = new Comment($comment);
205 $node->setOwnerDocument($this);
206
207 return $node;
208 }
209
214 public function createTextNode($text)
215 {
216 $node = new Text($text);
217 $node->setOwnerDocument($this);
218
219 return $node;
220 }
221
225 public function createDocumentFragment()
226 {
227 throw new DomException('Not implemented');
228 }
229
233 public function getElementById($id)
234 {
235 $resultList = $this->getElementsByAttr('id', $id, 1);
236
237 return (!empty($resultList)) ? current($resultList) : null;
238 }
239
243 public function getElementByClassName($className)
244 {
245 $resultList = $this->getElementsByClassName($className, 1);
246
247 return (!empty($resultList)) ? current($resultList) : null;
248 }
249
253 public function getElementsByName($name)
254 {
255 return $this->getElementsByAttr('name', $name);
256 }
257
258 public function getTextContent()
259 {
260 return null;
261 }
262
266 public function getElementsByAttr($attrName, $attrValue = null, $limit = 0)
267 {
268 $attrName = mb_strtolower($attrName);
269 $nodeList = $this->getQueryEngine()->walk(
270 [
271 [
273 [
274 'name' => $attrName,
275 'value' => $attrValue,
277 ],
278 ],
279 ],
280 ],
281 null, $this, $limit,
282 );
283
285 {
286 return $nodeList;
287 }
288
289 return new NodeList($nodeList);
290 }
291
295 public function getElementsByTagName($tagName)
296 {
297 $tagName = mb_strtoupper($tagName);
298 $nodeList = $this->getQueryEngine()->walk(
299 [
300 [QueryEngine::FILTER_NODE_NAME => $tagName],
301 ],
302 null, $this,
303 );
304
306 {
307 return $nodeList;
308 }
309
310 return new NodeList($nodeList);
311 }
312
316 public function getElementsByClassName($className, $limit = 0)
317 {
318 $nodeList = $this->getQueryEngine()->walk(
319 [
321 ],
322 null, $this, $limit,
323 );
324
326 {
327 return $nodeList;
328 }
329
330 return new NodeList($nodeList);
331 }
332
336 public function getDocumentElement()
337 {
338 foreach($this->getChildNodesArray() as $child)
339 {
341 if($child->getNodeName() === 'HTML')
342 {
343 return $child;
344 }
345 }
346
347 return null;
348 }
349
354 public function getHead()
355 {
356 if(!$this->getDocumentElement())
357 {
358 return null;
359 }
360
361 foreach($this->getDocumentElement()->getChildNodesArray() as $child)
362 {
364 if($child->getNodeName() === 'HEAD')
365 {
366 return $child;
367 }
368 }
369
370 return null;
371 }
372
377 public function getBody()
378 {
379 if(!$this->getDocumentElement())
380 {
381 return null;
382 }
383
384 foreach($this->getDocumentElement()->getChildNodesArray() as $child)
385 {
387 if($child->getNodeName() === 'BODY')
388 {
389 return $child;
390 }
391 }
392
393 return null;
394 }
395}
Определения attr.php:6
createComment($comment)
Определения document.php:202
createAttribute($name, $value)
Определения document.php:190
loadHTML($source)
Определения document.php:31
createElement($tagName)
Определения document.php:151
getQueryEngine()
Определения document.php:77
getElementByClassName($className)
Определения document.php:243
getElementsByClassName($className, $limit=0)
Определения document.php:316
getElementsByTagName($tagName)
Определения document.php:295
setQueryEngine(QueryEngine $engine)
Определения document.php:91
createTextNode($text)
Определения document.php:214
getElementsByAttr($attrName, $attrValue=null, $limit=0)
Определения document.php:266
setParser(Parser $parser)
Определения document.php:108
createDocumentFragment()
Определения document.php:225
getElementById($id)
Определения document.php:233
getElementsByName($name)
Определения document.php:253
Определения node.php:5
hasAttributes()
Определения node.php:85
init()
Определения node.php:58
setOwnerDocument(Document $owner)
Определения node.php:132
getChildNodesArray()
Определения node.php:212
$childNodes
Определения node.php:34
getParentNode()
Определения node.php:75
static $isNodeListAsArray
Определения node.php:19
hasChildNodes()
Определения node.php:236
getChildNodes()
Определения node.php:204
getAttributes()
Определения node.php:102
static getHtmlParser()
Определения parser.php:12
const FILTER_OPERATION_EQUAL
Определения queryengine.php:14
const FILTER_NODE_NAME
Определения queryengine.php:9
const FILTER_ATTR_CLASS_NAME
Определения queryengine.php:12
const FILTER_ATTR_VALUE
Определения queryengine.php:11
static getQuerySelectorEngine()
Определения queryengine.php:27
Определения text.php:5
$result
Определения get_property_values.php:14
$name
Определения menu_edit.php:35
$text
Определения template_pdf.php:79
$i
Определения factura.php:643
$comment
Определения template.php:15
$engine
Определения options.php:121