Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.45% covered (success)
97.45%
268 / 275
72.00% covered (warning)
72.00%
18 / 25
CRAP
0.00% covered (danger)
0.00%
0 / 1
TreeBuilder
97.45% covered (success)
97.45%
268 / 275
72.00% covered (warning)
72.00%
18 / 25
102
0.00% covered (danger)
0.00%
0 / 1
 __construct
93.75% covered (success)
93.75%
15 / 16
0.00% covered (danger)
0.00%
0 / 1
2.00
 startDocument
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
3
 adjustedCurrentNode
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
4
 appropriatePlace
94.12% covered (success)
94.12%
16 / 17
0.00% covered (danger)
0.00%
0 / 1
9.02
 insertCharacters
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 insertElement
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 insertForeign
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 pop
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 doctype
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 comment
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 error
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 mergeAttributes
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
3
 closePInButtonScope
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 checkUnclosed
90.91% covered (success)
90.91%
10 / 11
0.00% covered (danger)
0.00%
0 / 1
5.02
 reconstructAFE
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
1 / 1
8
 adoptionAgency
100.00% covered (success)
100.00%
90 / 90
100.00% covered (success)
100.00%
1 / 1
22
 anyOtherEndTag
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
6
 generateImpliedEndTags
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 generateImpliedEndTagsThoroughly
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 generateImpliedEndTagsAndPop
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 popAllUpToElement
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
4.03
 popAllUpToName
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
4.03
 popAllUpToNames
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
4.03
 clearStackBack
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
4.13
 stopParsing
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2declare( strict_types = 1 );
3
4namespace Wikimedia\RemexHtml\TreeBuilder;
5
6use Wikimedia\RemexHtml\HTMLData;
7use Wikimedia\RemexHtml\PropGuard;
8use Wikimedia\RemexHtml\Tokenizer\Attributes;
9use Wikimedia\RemexHtml\Tokenizer\PlainAttributes;
10use Wikimedia\RemexHtml\Tokenizer\Tokenizer;
11
12/**
13 * TreeBuilder is the receiver of events from the InsertionMode subclasses,
14 * and is responsible for forwarding events on to the TreeHandler, which is
15 * responsible for constructing a DOM.
16 *
17 * TreeBuilder contains most of the state referred to by the "tree construction"
18 * part of the HTML spec which is not handled elsewhere, such as the stack of
19 * open elements and the list of active formatting elements.
20 *
21 * Miscellaneous helpers for InsertionMode subclasses are also in this class.
22 *
23 * https://www.w3.org/TR/2016/REC-html51-20161101/syntax.html
24 */
25class TreeBuilder {
26    use PropGuard;
27
28    // Quirks
29    public const NO_QUIRKS = 0;
30    public const LIMITED_QUIRKS = 1;
31    public const QUIRKS = 2;
32
33    // Insertion placement
34    public const BEFORE = 0;
35    public const UNDER = 1;
36    public const ROOT = 2;
37
38    // Configuration
39    /** @var bool */
40    public $isIframeSrcdoc;
41    /** @var bool */
42    public $scriptingFlag;
43    /** @var bool */
44    public $ignoreErrors;
45    /** @var bool */
46    public $ignoreNulls;
47
48    // Objects
49
50    public TreeHandler $handler;
51
52    /** @var Stack */
53    public $stack;
54
55    public ActiveFormattingElements $afe;
56
57    /** @var Tokenizer */
58    public $tokenizer;
59
60    // State
61    /** @var bool */
62    public $isFragment = false;
63    /** @var Element|null */
64    public $fragmentContext;
65    /** @var Element|null */
66    public $headElement;
67    /** @var Element|null */
68    public $formElement;
69    /** @var bool */
70    public $framesetOK = true;
71    /** @var int */
72    public $quirks = self::NO_QUIRKS;
73    /** @var bool */
74    public $fosterParenting = false;
75    /** @var array[] */
76    public $pendingTableCharacters = [];
77
78    private const FOSTER_TRIGGERS = [
79        'table' => true,
80        'tbody' => true,
81        'tfoot' => true,
82        'thead' => true,
83        'tr' => true
84    ];
85
86    private const IMPLIED_END_TAGS = [
87        'dd' => true,
88        'dt' => true,
89        'li' => true,
90        'option' => true,
91        'optgroup' => true,
92        'p' => true,
93        'rb' => true,
94        'rp' => true,
95        'rt' => true,
96        'rtc' => true,
97    ];
98
99    private const THOROUGHLY_IMPLIED_END_TAGS = [
100        'caption' => true,
101        'colgroup' => true,
102        'dd' => true,
103        'dt' => true,
104        'li' => true,
105        'optgroup' => true,
106        'option' => true,
107        'p' => true,
108        'rb' => true,
109        'rp' => true,
110        'rt' => true,
111        'rtc' => true,
112        'tbody' => true,
113        'td' => true,
114        'tfoot' => true,
115        'th' => true,
116        'thead' => true,
117        'tr' => true
118    ];
119
120    /**
121     * @param TreeHandler $handler The handler which receives tree mutation events
122     * @param array $options Associative array of options. The options are:
123     *   - isIframeSrcdoc: True if the document is an iframe srcdoc document.
124     *     Default false.
125     *   - scriptingFlag: True if the scripting flag is enabled. Default true.
126     *     Setting this to false cause the contents of <noscript> elements to be
127     *     processed as normal content. The scriptingFlag option in the
128     *     Tokenizer should be set to the same value.
129     *   - ignoreErrors: True to ignore errors. This improves performance.
130     *     Default false.
131     *   - ignoreNulls: True to ignore null characters in the input, instead of
132     *     following the specified behaviour. Default false.
133     *   - scopeCache: True to use an implementation of the stack of open elements
134     *     which caches "in scope" predicates. False to use a simpler
135     *     implementation. Default true. Setting this to false may improve
136     *     performance for simple documents, although at the expense of O(N^2)
137     *     instead of O(N) worst case time order.
138     */
139    public function __construct( TreeHandler $handler, $options = [] ) {
140        $this->handler = $handler;
141        $this->afe = new ActiveFormattingElements;
142        $options += [
143            'isIframeSrcdoc' => false,
144            'scriptingFlag' => true,