Completed
Pull Request — master (#160)
by
unknown
01:39
created

DOMTreeBuilder::__construct()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 5.0018

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 23
cts 24
cp 0.9583
rs 8.9848
c 0
b 0
f 0
cc 5
nc 12
nop 2
crap 5.0018
1
<?php
2
3
namespace Masterminds\HTML5\Parser;
4
5
use Masterminds\HTML5\Elements;
6
use Masterminds\HTML5\InstructionProcessor;
7
8
/**
9
 * Create an HTML5 DOM tree from events.
10
 *
11
 * This attempts to create a DOM from events emitted by a parser. This
12
 * attempts (but does not guarantee) to up-convert older HTML documents
13
 * to HTML5. It does this by applying HTML5's rules, but it will not
14
 * change the architecture of the document itself.
15
 *
16
 * Many of the error correction and quirks features suggested in the specification
17
 * are implemented herein; however, not all of them are. Since we do not
18
 * assume a graphical user agent, no presentation-specific logic is conducted
19
 * during tree building.
20
 *
21
 * FIXME: The present tree builder does not exactly follow the state machine rules
22
 * for insert modes as outlined in the HTML5 spec. The processor needs to be
23
 * re-written to accomodate this. See, for example, the Go language HTML5
24
 * parser.
25
 */
26
class DOMTreeBuilder implements EventHandler
27
{
28
    /**
29
     * Defined in http://www.w3.org/TR/html51/infrastructure.html#html-namespace-0.
30
     */
31
    const NAMESPACE_HTML = 'http://www.w3.org/1999/xhtml';
32
33
    const NAMESPACE_MATHML = 'http://www.w3.org/1998/Math/MathML';
34
35
    const NAMESPACE_SVG = 'http://www.w3.org/2000/svg';
36
37
    const NAMESPACE_XLINK = 'http://www.w3.org/1999/xlink';
38
39
    const NAMESPACE_XML = 'http://www.w3.org/XML/1998/namespace';
40
41
    const NAMESPACE_XMLNS = 'http://www.w3.org/2000/xmlns/';
42
43
    const OPT_DISABLE_HTML_NS = 'disable_html_ns';
44
45
    const OPT_TARGET_DOC = 'target_document';
46
47
    const OPT_IMPLICIT_NS = 'implicit_namespaces';
48
49
    /**
50
     * Holds the HTML5 element names that causes a namespace switch.
51
     *
52
     * @var array
53
     */
54
    protected $nsRoots = array(
55
        'html' => self::NAMESPACE_HTML,
56
        'svg' => self::NAMESPACE_SVG,
57
        'math' => self::NAMESPACE_MATHML,
58
    );
59
60
    /**
61
     * Holds the always available namespaces (which does not require the XMLNS declaration).
62
     *
63
     * @var array
64
     */
65
    protected $implicitNamespaces = array(
66
        'xml' => self::NAMESPACE_XML,
67
        'xmlns' => self::NAMESPACE_XMLNS,
68
        'xlink' => self::NAMESPACE_XLINK,
69
    );
70
71
    /**
72
     * Holds a stack of currently active namespaces.
73
     *
74
     * @var array
75
     */
76
    protected $nsStack = array();
77
78
    /**
79
     * Holds the number of namespaces declared by a node.
80
     *
81
     * @var array
82
     */
83
    protected $pushes = array();
84
85
    /**
86
     * Defined in 8.2.5.
87
     */
88
    const IM_INITIAL = 0;
89
90
    const IM_BEFORE_HTML = 1;
91
92
    const IM_BEFORE_HEAD = 2;
93
94
    const IM_IN_HEAD = 3;
95
96
    const IM_IN_HEAD_NOSCRIPT = 4;
97
98
    const IM_AFTER_HEAD = 5;
99
100
    const IM_IN_BODY = 6;
101
102
    const IM_TEXT = 7;
103
104
    const IM_IN_TABLE = 8;
105
106
    const IM_IN_TABLE_TEXT = 9;
107
108
    const IM_IN_CAPTION = 10;
109
110
    const IM_IN_COLUMN_GROUP = 11;
111
112
    const IM_IN_TABLE_BODY = 12;
113
114
    const IM_IN_ROW = 13;
115
116
    const IM_IN_CELL = 14;
117
118
    const IM_IN_SELECT = 15;
119
120
    const IM_IN_SELECT_IN_TABLE = 16;
121
122
    const IM_AFTER_BODY = 17;
123
124
    const IM_IN_FRAMESET = 18;
125
126
    const IM_AFTER_FRAMESET = 19;
127
128
    const IM_AFTER_AFTER_BODY = 20;
129
130
    const IM_AFTER_AFTER_FRAMESET = 21;
131
132
    const IM_IN_SVG = 22;
133
134
    const IM_IN_MATHML = 23;
135
136
    protected $options = array();
137
138
    protected $stack = array();
139
140
    protected $current; // Pointer in the tag hierarchy.
141
    protected $rules;
142
    protected $doc;
143
144
    protected $frag;
145
146
    protected $processor;
147
148
    protected $insertMode = 0;
149
150
    /**
151
     * Track if we are in an element that allows only inline child nodes.
152
     *
153
     * @var string|null
154
     */
155
    protected $onlyInline;
156
157
    /**
158
     * Quirks mode is enabled by default.
159
     * Any document that is missing the DT will be considered to be in quirks mode.
160
     */
161
    protected $quirks = true;
162
163
    protected $errors = array();
164
165 110
    public function __construct($isFragment = false, array $options = array())
166
    {
167 110
        $this->options = $options;
168
169 110
        if (isset($options[self::OPT_TARGET_DOC])) {
170 1
            $this->doc = $options[self::OPT_TARGET_DOC];
171 1
        } else {
172 109
            $impl = new \DOMImplementation();
173
            // XXX:
174
            // Create the doctype. For now, we are always creating HTML5
175
            // documents, and attempting to up-convert any older DTDs to HTML5.
176 109
            $dt = $impl->createDocumentType('html');
177
            // $this->doc = \DOMImplementation::createDocument(NULL, 'html', $dt);
178 109
            $this->doc = $impl->createDocument(null, null, $dt);
179
        }
180
181 110
        $this->errors = array();
182
183 110
        $this->current = $this->doc; // ->documentElement;
184
185
        // Create a rules engine for tags.
186 110
        $this->rules = new TreeBuildingRules();
187
188 110
        $implicitNS = array();
189 110
        if (isset($this->options[self::OPT_IMPLICIT_NS])) {
190
            $implicitNS = $this->options[self::OPT_IMPLICIT_NS];
191 110
        } elseif (isset($this->options['implicitNamespaces'])) {
192 2
            $implicitNS = $this->options['implicitNamespaces'];
193 2
        }
194
195
        // Fill $nsStack with the defalut HTML5 namespaces, plus the "implicitNamespaces" array taken form $defaultOptions
196 110
        array_unshift($this->nsStack, $implicitNS + array('' => self::NAMESPACE_HTML) + $this->implicitNamespaces);
197
198 110
        if ($isFragment) {
199 18
            $this->insertMode = static::IM_IN_BODY;
200 18
            $this->frag = $this->doc->createDocumentFragment();
201 18
            $this->current = $this->frag;
202 18
        }
203 110
    }
204
205
    /**
206
     * Get the document.
207
     */
208 100
    public function document()
209
    {
210 100
        return $this->doc;
211
    }
212
213
    /**
214
     * Get the DOM fragment for the body.
215
     *
216
     * This returns a DOMNodeList because a fragment may have zero or more
217
     * DOMNodes at its root.
218
     *
219
     * @see http://www.w3.org/TR/2012/CR-html5-20121217/syntax.html#concept-frag-parse-context
220
     *
221
     * @return \DOMDocumentFragment
222
     */
223 18
    public function fragment()
224
    {
225 18
        return $this->frag;
226
    }
227
228
    /**
229
     * Provide an instruction processor.
230
     *
231
     * This is used for handling Processor Instructions as they are
232
     * inserted. If omitted, PI's are inserted directly into the DOM tree.
233
     *
234
     * @param InstructionProcessor $proc
235
     */
236 1
    public function setInstructionProcessor(InstructionProcessor $proc)
237
    {
238 1
        $this->processor = $proc;
239 1
    }
240
241 94
    public function doctype($name, $idType = 0, $id = null, $quirks = false)
242
    {
243
        // This is used solely for setting quirks mode. Currently we don't
244
        // try to preserve the inbound DT. We convert it to HTML5.
245 94
        $this->quirks = $quirks;
246
247 94
        if ($this->insertMode > static::IM_INITIAL) {
248
            $this->parseError('Illegal placement of DOCTYPE tag. Ignoring: '.$name);
249
250
            return;
251
        }
252
253 94
        $this->insertMode = static::IM_BEFORE_HTML;
254 94
    }
255
256
    /**
257
     * Process the start tag.
258
     *
259
     * @todo - XMLNS namespace handling (we need to parse, even if it's not valid)
260
     *       - XLink, MathML and SVG namespace handling
261
     *       - Omission rules: 8.1.2.4 Optional tags
262
     *
263
     * @param string $name
264
     * @param array $attributes
265
     * @param bool $selfClosing
266
     *
267
     * @return int
268
     */
269 106
    public function startTag($name, $attributes = array(), $selfClosing = false)
270
    {
271 106
        $lname = $this->normalizeTagName($name);
272
273
        // Make sure we have an html element.
274 106
        if (!$this->doc->documentElement && 'html' !== $name && !$this->frag) {
275 3
            $this->startTag('html');
276 3
        }
277
278
        // Set quirks mode if we're at IM_INITIAL with no doctype.
279 106
        if ($this->insertMode === static::IM_INITIAL) {
280 5
            $this->quirks = true;
281 5
            $this->parseError('No DOCTYPE specified.');
282 5
        }
283
284
        // SPECIAL TAG HANDLING:
285
        // Spec says do this, and "don't ask."
286
        // find the spec where this is defined... looks problematic
287 106
        if ('image' === $name && !($this->insertMode === static::IM_IN_SVG || $this->insertMode === static::IM_IN_MATHML)) {
288
            $name = 'img';
289
        }
290
291
        // Autoclose p tags where appropriate.
292 106
        if ($this->insertMode >= static::IM_IN_BODY && Elements::isA($name, Elements::AUTOCLOSE_P)) {
293 54
            $this->autoclose('p');
294 54
        }
295
296
        // Set insert mode:
297
        switch ($name) {
298 106
            case 'html':
299 99
                $this->insertMode = static::IM_BEFORE_HEAD;
300 99
                break;
301 100
            case 'head':
302 41
                if ($this->insertMode > static::IM_BEFORE_HEAD) {
303
                    $this->parseError('Unexpected head tag outside of head context.');
304
                } else {
305 41
                    $this->insertMode = static::IM_IN_HEAD;
306
                }
307 41
                break;
308 99
            case 'body':
309 84
                $this->insertMode = static::IM_IN_BODY;
310 84
                break;
311 93
            case 'svg':
312 8
                $this->insertMode = static::IM_IN_SVG;
313 8
                break;
314 93
            case 'math':
315 7
                $this->insertMode = static::IM_IN_MATHML;
316 7
                break;
317 90
            case 'noscript':
318 1
                if ($this->insertMode === static::IM_IN_HEAD) {
319 1
                    $this->insertMode = static::IM_IN_HEAD_NOSCRIPT;
320 1
                }
321 1
                break;
322
        }
323
324
        // Special case handling for SVG.
325 106
        if ($this->insertMode === static::IM_IN_SVG) {
326 8
            $lname = Elements::normalizeSvgElement($lname);
327 8
        }
328
329 106
        $pushes = 0;
330
        // when we found a tag thats appears inside $nsRoots, we have to switch the defalut namespace
331 106
        if (isset($this->nsRoots[$lname]) && $this->nsStack[0][''] !== $this->nsRoots[$lname]) {
332 15
            array_unshift($this->nsStack, array(
333 15
                '' => $this->nsRoots[$lname],
334 15
            ) + $this->nsStack[0]);
335 15
            ++$pushes;
336 15
        }
337 106
        $needsWorkaround = false;
338 106
        if (isset($this->options['xmlNamespaces']) && $this->options['xmlNamespaces']) {
339
            // when xmlNamespaces is true a and we found a 'xmlns' or 'xmlns:*' attribute, we should add a new item to the $nsStack
340 6
            foreach ($attributes as $aName => $aVal) {
341 5
                if ('xmlns' === $aName) {
342 3
                    $needsWorkaround = $aVal;
343 3
                    array_unshift($this->nsStack, array(
344 3
                        '' => $aVal,
345 3
                    ) + $this->nsStack[0]);
346 3
                    ++$pushes;
347 5
                } elseif ('xmlns' === (($pos = strpos($aName, ':')) ? substr($aName, 0, $pos) : '')) {
348 3
                    array_unshift($this->nsStack, array(
349 3
                        substr($aName, $pos + 1) => $aVal,
350 3
                    ) + $this->nsStack[0]);
351 3
                    ++$pushes;
352 3
                }
353 6
            }
354 6
        }
355
356 106
        if ($this->onlyInline && Elements::isA($lname, Elements::BLOCK_TAG)) {
357 2
            $this->autoclose($this->onlyInline);
358 2
            $this->onlyInline = null;
359 2
        }
360
361
        try {
362 106
            $prefix = ($pos = strpos($lname, ':')) ? substr($lname, 0, $pos) : '';
363
364 106
            if (false !== $needsWorkaround) {
365 3
                $xml = "<$lname xmlns=\"$needsWorkaround\" ".(strlen($prefix) && isset($this->nsStack[0][$prefix]) ? ("xmlns:$prefix=\"".$this->nsStack[0][$prefix].'"') : '').'/>';
366
367 3
                $frag = new \DOMDocument('1.0', 'UTF-8');
368 3
                $frag->loadXML($xml);
369
370 3
                $ele = $this->doc->importNode($frag->documentElement, true);
371 3
            } else {
372 106
                if (!isset($this->nsStack[0][$prefix]) || ('' === $prefix && isset($this->options[self::OPT_DISABLE_HTML_NS]) && $this->options[self::OPT_DISABLE_HTML_NS])) {
373 2
                    $ele = $this->doc->createElement($lname);
374 2
                } else {
375 105
                    $ele = $this->doc->createElementNS($this->nsStack[0][$prefix], $lname);
376
                }
377
            }
378 106
        } catch (\DOMException $e) {
379
            $this->parseError("Illegal tag name: <$lname>. Replaced with <invalid>.");
380
            $ele = $this->doc->createElement('invalid');
381
        }
382
383 106
        if (Elements::isA($lname, Elements::BLOCK_ONLY_INLINE)) {
384 27
            $this->onlyInline = $lname;
385 27
        }
386
387
        // When we add some namespacess, we have to track them. Later, when "endElement" is invoked, we have to remove them.
388
        // When we are on a void tag, we do not need to care about namesapce nesting.
389 106
        if ($pushes > 0 && !Elements::isA($name, Elements::VOID_TAG)) {
390
            // PHP tends to free the memory used by DOM,
391
            // to avoid spl_object_hash collisions whe have to avoid garbage collection of $ele storing it into $pushes
392
            // see https://bugs.php.net/bug.php?id=67459
393 17
            $this->pushes[spl_object_hash($ele)] = array($pushes, $ele);
394
395
            // SEE https://github.com/facebook/hhvm/issues/2962
396 17
            if (defined('HHVM_VERSION')) {
397
                $ele->setAttribute('html5-php-fake-id-attribute', spl_object_hash($ele));
398
            }
399 17
        }
400
401 106
        foreach ($attributes as $aName => $aVal) {
402
            // xmlns attributes can't be set
403 78
            if ('xmlns' === $aName) {
404 5
                continue;
405
            }
406
407 77
            if ($this->insertMode === static::IM_IN_SVG) {
408 8
                $aName = Elements::normalizeSvgAttribute($aName);
409 77
            } elseif ($this->insertMode === static::IM_IN_MATHML) {
410 4
                $aName = Elements::normalizeMathMlAttribute($aName);
411 4
            }
412
413
            try {
414 77
                $prefix = ($pos = strpos($aName, ':')) ? substr($aName, 0, $pos) : false;
415
416 77
                if ('xmlns' === $prefix) {
417 4
                    $ele->setAttributeNS(self::NAMESPACE_XMLNS, $aName, $aVal);
418 77
                } elseif (false !== $prefix && isset($this->nsStack[0][$prefix])) {
419 6
                    $ele->setAttributeNS($this->nsStack[0][$prefix], $aName, $aVal);
420 6
                } else {
421 74
                    $ele->setAttribute($aName, $aVal);
422
                }
423 77
            } catch (\DOMException $e) {
424
                $this->parseError("Illegal attribute name for tag $name. Ignoring: $aName");
425
                continue;
426
            }
427
428
            // This is necessary on a non-DTD schema, like HTML5.
429 77
            if ('id' === $aName) {
430 24
                $ele->setIdAttribute('id', true);
431 24
            }
432 106
        }
433
434 106
        if ($this->frag !== $this->current && $this->rules->hasRules($name)) {
435
            // Some elements have special processing rules. Handle those separately.
436 6
            $this->current = $this->rules->evaluate($ele, $this->current);
437 6
        } else {
438
            // Otherwise, it's a standard element.
439 106
            $this->current->appendChild($ele);
440
441 106
            if (!Elements::isA($name, Elements::VOID_TAG)) {
442 106
                $this->current = $ele;
443 106
            }
444
445
            // Self-closing tags should only be respected on foreign elements
446
            // (and are implied on void elements)
447
            // See: https://www.w3.org/TR/html5/syntax.html#start-tags
448 106
            if (Elements::isHtml5Element($name)) {
449 105
                $selfClosing = false;
450 105
            }
451
        }
452
453
        // This is sort of a last-ditch attempt to correct for cases where no head/body
454
        // elements are provided.
455 106
        if ($this->insertMode <= static::IM_BEFORE_HEAD && 'head' !== $name && 'html' !== $name) {
456 5
            $this->insertMode = static::IM_IN_BODY;
457 5
        }
458
459
        // When we are on a void tag, we do not need to care about namesapce nesting,
460
        // but we have to remove the namespaces pushed to $nsStack.
461 106
        if ($pushes > 0 && Elements::isA($name, Elements::VOID_TAG)) {
462
            // remove the namespaced definded by current node
463
            for ($i = 0; $i < $pushes; ++$i) {
464
                array_shift($this->nsStack);
465
            }
466
        }
467
468 106
        if ($selfClosing) {
469 7
            $this->endTag($name);
470 7
        }
471
472
        // Return the element mask, which the tokenizer can then use to set
473
        // various processing rules.
474 106
        return Elements::element($name);
475
    }
476
477 104
    public function endTag($name)
478
    {
479 104
        $lname = $this->normalizeTagName($name);
480
481
        // Ignore closing tags for unary elements.
482 104
        if (Elements::isA($name, Elements::VOID_TAG)) {
483
            return;
484
        }
485
486 104
        if ($this->insertMode <= static::IM_BEFORE_HTML) {
487
            // 8.2.5.4.2
488
            if (in_array($name, array(
489
                'html',
490
                'br',
491
                'head',
492
                'title',
493
            ))) {
494
                $this->startTag('html');
495
                $this->endTag($name);
496
                $this->insertMode = static::IM_BEFORE_HEAD;
497
498
                return;
499
            }
500
501
            // Ignore the tag.
502
            $this->parseError('Illegal closing tag at global scope.');
503
504
            return;
505
        }
506
507
        // Special case handling for SVG.
508 104
        if ($this->insertMode === static::IM_IN_SVG) {
509 8
            $lname = Elements::normalizeSvgElement($lname);
510 8
        }
511
512
        // See https://github.com/facebook/hhvm/issues/2962
513 104
        if (defined('HHVM_VERSION') && ($cid = $this->current->getAttribute('html5-php-fake-id-attribute'))) {
514
            $this->current->removeAttribute('html5-php-fake-id-attribute');
515
        } else {
516 104
            $cid = spl_object_hash($this->current);
517
        }
518
519
        // XXX: HTML has no parent. What do we do, though,
520
        // if this element appears in the wrong place?
521 104
        if ('html' === $lname) {
522 95
            return;
523
        }
524
525
        // remove the namespaced definded by current node
526 98
        if (isset($this->pushes[$cid])) {
527 15
            for ($i = 0; $i < $this->pushes[$cid][0]; ++$i) {
528 15
                array_shift($this->nsStack);
529 15
            }
530 15
            unset($this->pushes[$cid]);
531 15
        }
532
533 98
        if (!$this->autoclose($lname)) {
534 2
            $this->parseError('Could not find closing tag for '.$lname);
535 2
        }
536
537
        switch ($lname) {
538 98
            case 'head':
539 41
                $this->insertMode = static::IM_AFTER_HEAD;
540 41
                break;
541 97
            case 'body':
542 85
                $this->insertMode = static::IM_AFTER_BODY;
543 85
                break;
544 82
            case 'svg':
545 82
            case 'mathml':
546 8
                $this->insertMode = static::IM_IN_BODY;
547 8
                break;
548
        }
549 98
    }
550
551 5
    public function comment($cdata)
552
    {
553
        // TODO: Need to handle case where comment appears outside of the HTML tag.
554 5
        $node = $this->doc->createComment($cdata);
555 5
        $this->current->appendChild($node);
556 5
    }
557
558 87
    public function text($data)
559
    {
560
        // XXX: Hmmm.... should we really be this strict?
561 87
        if ($this->insertMode < static::IM_IN_HEAD) {
562
            // Per '8.2.5.4.3 The "before head" insertion mode' the characters
563
            // " \t\n\r\f" should be ignored but no mention of a parse error. This is
564
            // practical as most documents contain these characters. Other text is not
565
            // expected here so recording a parse error is necessary.
566 56
            $dataTmp = trim($data, " \t\n\r\f");
567 56
            if (!empty($dataTmp)) {
568
                // fprintf(STDOUT, "Unexpected insert mode: %d", $this->insertMode);
569 1
                $this->parseError('Unexpected text. Ignoring: '.$dataTmp);
570 1
            }
571
572 56
            return;
573
        }
574
        // fprintf(STDOUT, "Appending text %s.", $data);
575 86
        $node = $this->doc->createTextNode($data);
576 86
        $this->current->appendChild($node);
577 86
    }
578
579 110
    public function eof()
580
    {
581
        // If the $current isn't the $root, do we need to do anything?
582 110
    }
583
584 11
    public function parseError($msg, $line = 0, $col = 0)
585
    {
586 11
        $this->errors[] = sprintf('Line %d, Col %d: %s', $line, $col, $msg);
587 11
    }
588
589 104
    public function getErrors()
590
    {
591 104
        return $this->errors;
592
    }
593
594 3
    public function cdata($data)
595
    {
596 3
        $node = $this->doc->createCDATASection($data);
597 3
        $this->current->appendChild($node);
598 3
    }
599
600 5
    public function processingInstruction($name, $data = null)
601
    {
602
        // XXX: Ignore initial XML declaration, per the spec.
603 5
        if ($this->insertMode === static::IM_INITIAL && 'xml' === strtolower($name)) {
604 1
            return;
605
        }
606
607
        // Important: The processor may modify the current DOM tree however it sees fit.
608 5
        if ($this->processor instanceof InstructionProcessor) {
609 1
            $res = $this->processor->process($this->current, $name, $data);
0 ignored issues
show
Bug introduced by
It seems like $this->current can also be of type object<DOMNode>; however, Masterminds\HTML5\InstructionProcessor::process() does only seem to accept object<DOMElement>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
610 1
            if (!empty($res)) {
611 1
                $this->current = $res;
612 1
            }
613
614 1
            return;
615
        }
616
617
        // Otherwise, this is just a dumb PI element.
618 4
        $node = $this->doc->createProcessingInstruction($name, $data);
619
620 4
        $this->current->appendChild($node);
621 4
    }
622
623
    // ==========================================================================
624
    // UTILITIES
625
    // ==========================================================================
626
627
    /**
628
     * Apply normalization rules to a tag name.
629
     *
630
     * See sections 2.9 and 8.1.2.
631
     *
632
     * @param string $name The tag name
633
     *
634
     * @return string the normalized tag name
635
     */
636 106
    protected function normalizeTagName($name)
637
    {
638
        /*
639
         * Section 2.9 suggests that we should not do this. if (strpos($name, ':') !== false) { // We know from the grammar that there must be at least one other // char besides :, since : is not a legal tag start. $parts = explode(':', $name); return array_pop($parts); }
640
         */
641 106
        return $name;
642
    }
643
644
    protected function quirksTreeResolver($name)
645
    {
646
        throw new \Exception('Not implemented.');
647
    }
648
649
    /**
650
     * Automatically climb the tree and close the closest node with the matching $tag.
651
     *
652
     * @param string $tagName
653
     *
654
     * @return bool
655
     */
656 98
    protected function autoclose($tagName)
657
    {
658 98
        $working = $this->current;
659
        do {
660 98
            if (XML_ELEMENT_NODE !== $working->nodeType) {
661 54
                return false;
662
            }
663 98
            if ($working->tagName === $tagName) {
664 98
                $this->current = $working->parentNode;
665
666 98
                return true;
667
            }
668 51
        } while ($working = $working->parentNode);
669
670
        return false;
671
    }
672
673
    /**
674
     * Checks if the given tagname is an ancestor of the present candidate.
675
     *
676
     * If $this->current or anything above $this->current matches the given tag
677
     * name, this returns true.
678
     *
679
     * @param string $tagName
680
     *
681
     * @return bool
682
     */
683
    protected function isAncestor($tagName)
684
    {
685
        $candidate = $this->current;
686
        while (XML_ELEMENT_NODE === $candidate->nodeType) {
687
            if ($candidate->tagName === $tagName) {
688
                return true;
689
            }
690
            $candidate = $candidate->parentNode;
691
        }
692
693
        return false;
694
    }
695
696
    /**
697
     * Returns true if the immediate parent element is of the given tagname.
698
     *
699
     * @param string $tagName
700
     *
701
     * @return bool
702
     */
703
    protected function isParent($tagName)
704
    {
705
        return $this->current->tagName === $tagName;
706
    }
707
}
708