Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
70.84% covered (warning)
70.84%
464 / 655
24.14% covered (danger)
24.14%
7 / 29
CRAP
0.00% covered (danger)
0.00%
0 / 1
Tokenizer
70.84% covered (warning)
70.84%
464 / 655
24.14% covered (danger)
24.14%
7 / 29
2223.98
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
1
 setEnableCdataCallback
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 execute
83.33% covered (warning)
83.33%
10 / 12
0.00% covered (danger)
0.00%
0 / 1
3.04
 getPreprocessedText
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 switchState
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 setFragmentContext
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
210
 beginStepping
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 step
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 preprocess
28.57% covered (danger)
28.57%
16 / 56
0.00% covered (danger)
0.00%
0 / 1
38.52
 executeInternal
75.00% covered (warning)
75.00%
15 / 20
0.00% covered (danger)
0.00%
0 / 1
10.27
 dataState
71.56% covered (warning)
71.56%
78 / 109
0.00% covered (danger)
0.00%
0 / 1
58.05
 interpretCommentMatches
100.00% covered (success)
100.00%
32 / 32
100.00% covered (success)
100.00%
1 / 1
17
 interpretDoctypeMatches
100.00% covered (success)
100.00%
52 / 52
100.00% covered (success)
100.00%
1 / 1
36
 interpretDoctypeQuoted
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
7
 handleNulls
84.62% covered (warning)
84.62%
11 / 13
0.00% covered (danger)
0.00%
0 / 1
6.13
 handleAsciiErrors
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
5
 handleCharRefs
93.33% covered (success)
93.33%
70 / 75
0.00% covered (danger)
0.00%
0 / 1
45.60
 emitDataRange
92.31% covered (success)
92.31%
12 / 13
0.00% covered (danger)
0.00%
0 / 1
8.03
 emitCdataRange
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 emitRawTextRange
90.00% covered (success)
90.00%
9 / 10
0.00% covered (danger)
0.00%
0 / 1
6.04
 textElementState
95.45% covered (success)
95.45%
21 / 22
0.00% covered (danger)
0.00%
0 / 1
4
 consumeAttribs
82.35% covered (warning)
82.35%
14 / 17
0.00% covered (danger)
0.00%
0 / 1
5.14
 interpretAttribMatches
96.43% covered (success)
96.43%
54 / 56
0.00% covered (danger)
0.00%
0 / 1
31
 handleAttribsAndClose
71.43% covered (warning)
71.43%
25 / 35
0.00% covered (danger)
0.00%
0 / 1
16.94
 plaintextState
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 scriptDataState
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 1
42
 error
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 fatal
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 throwPregError
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
110
1<?php
2declare( strict_types = 1 );
3
4namespace Wikimedia\RemexHtml\Tokenizer;
5
6use Wikimedia\RemexHtml\HTMLData;
7use Wikimedia\RemexHtml\PropGuard;
8
9/**
10 * HTML 5 tokenizer
11 *
12 * Based on the W3C recommendation as published 01 November 2016:
13 * https://www.w3.org/TR/2016/REC-html51-20161101/
14 */
15class Tokenizer {
16    use PropGuard;
17
18    // States
19    public const STATE_START = 1;
20    public const STATE_DATA = 2;
21    public const STATE_RCDATA = 3;
22    public const STATE_RAWTEXT = 4;
23    public const STATE_SCRIPT_DATA = 5;
24    public const STATE_PLAINTEXT = 6;
25    public const STATE_EOF = 7;
26    public const STATE_CURRENT = 8;
27
28    // Match indices for the data state regex
29    private const MD_END_TAG_OPEN = 1;
30    private const MD_TAG_NAME = 2;
31    private const MD_TAG_AFTER_LOWERCASE = 3;
32    private const MD_COMMENT = 4;
33    private const MD_COMMENT_INNER = 5;
34    private const MD_COMMENT_END = 6;
35    private const MD_DOCTYPE = 7;
36    private const MD_DT_NAME_WS = 8;
37    private const MD_DT_NAME = 9;
38    private const MD_DT_PUBLIC_WS = 10;
39    private const MD_DT_PUBLIC_DQ = 11;
40    private const MD_DT_PUBLIC_SQ = 12;
41    private const MD_DT_PUBSYS_WS = 13;
42    private const MD_DT_PUBSYS_DQ = 14;
43    private const MD_DT_PUBSYS_SQ = 15;
44    private const MD_DT_SYSTEM_WS = 16;
45    private const MD_DT_SYSTEM_DQ = 17;
46    private const MD_DT_SYSTEM_SQ = 18;
47    private const MD_DT_BOGUS = 19;
48    private const MD_DT_END = 20;
49    private const MD_CDATA = 21;
50    private const MD_BOGUS_COMMENT = 22;
51
52    // Match indices for the character reference regex
53    private const MC_PREFIX = 1;
54    private const MC_DECIMAL = 2;
55    private const MC_HEXDEC = 3;
56    private const MC_SEMICOLON = 4;
57    private const MC_HASH = 5;
58    private const MC_NAMED = 6;
59    private const MC_SUFFIX = 7;
60    private const MC_INVALID = 8;
61
62    // Match indices for the attribute regex
63    private const MA_SLASH = 1;
64    private const MA_NAME = 2;
65    private const MA_SIMPLE_NAME = 3;
66    private const MA_DQUOTED = 4;
67    private const MA_DQUOTED_CHARREF = 5;
68    private const MA_DQUOTED_UNSIMPLE = 6;
69    private const MA_SQUOTED = 7;
70    private const MA_SQUOTED_CHARREF = 8;
71    private const MA_SQUOTED_UNSIMPLE = 9;
72    private const MA_UNQUOTED = 10;
73    private const MA_UNQUOTED_UNSIMPLE = 11;
74
75    // Characters
76    protected const REPLACEMENT_CHAR = "\xef\xbf\xbd";
77    protected const BYTE_ORDER_MARK = "\xef\xbb\xbf";
78
79    /**
80     * A list of "common well-behaved entities", used to optimize fast paths
81     */
82    private const COMMON_ENTITIES = [
83        '&amp;' => '&',
84        '&apos;' => "'",
85        '&lt;' => '<',
86        '&gt;' => '>',
87        '&quot;' => '"',
88        '&nbsp;' => "\u{00A0}",
89    ];
90
91    private const DISALLOWED_CODEPOINTS = [
92        0x000B => true,
93        0xFFFE => true, 0xFFFF => true,
94        0x1FFFE => true, 0x1FFFF => true,
95        0x2FFFE => true, 0x2FFFF => true,
96        0x3FFFE => true, 0x3FFFF => true,
97        0x4FFFE => true, 0x4FFFF => true,
98        0x5FFFE => true, 0x5FFFF => true,
99        0x6FFFE => true, 0x6FFFF => true,
100        0x7FFFE => true, 0x7FFFF => true,
101        0x8FFFE => true, 0x8FFFF => true,
102        0x9FFFE => true, 0x9FFFF => true,
103        0xAFFFE => true, 0xAFFFF => true,
104        0xBFFFE => true, 0xBFFFF => true,
105        0xCFFFE => true, 0xCFFFF => true,
106        0xDFFFE => true, 0xDFFFF => true,
107        0xEFFFE => true, 0xEFFFF => true,
108        0xFFFFE => true, 0xFFFFF => true,
109        0x10FFFE => true, 0x10FFFF => true,
110    ];
111
112    protected bool $ignoreErrors;
113    protected bool $ignoreCharRefs;
114    protected bool $ignoreNulls;
115    protected bool $skipPreprocess;
116    protected bool $lazyAttributes;
117    protected bool $scriptingFlag;
118    /** @var string|null */
119    protected $appropriateEndTag;
120    protected TokenHandler $listener;
121    /** @var int */
122    protected $state;
123    /** @var bool */
124    protected $preprocessed;
125    /** @var string */
126    protected $text;
127    /** @var int */
128    protected $pos;
129    /** @var int */
130    protected $length;
131    /** @var callable|null */
132    protected $enableCdataCallback;
133    /** @var string|null */
134    protected $fragmentNamespace;
135    /** @var string|null */
136    protected $fragmentName;
137
138    /**
139     * Constructor
140     *
141     * @param TokenHandler $listener The object which receives token events
142     * @param string $text The text to tokenize
143     * @param array $options Associative array of options, including:
144     *   - ignoreErrors: True to improve performance by ignoring errors. The
145     *     token stream should still be the same, except that error() won't be
146     *     called.
147     *   - ignoreCharRefs: True to ignore character references. Character tokens
148     *     will contain the unexpanded character references, and no errors
149     *     related to invalid character references will be raised. Performance
150     *     will be improved. This is not compliant behaviour.
151     *   - ignoreNulls: True to ignore NULL bytes in the input stream, instead
152     *     of raising errors and converting them to U+FFFD as is usually
153     *     required by the spec.
154     *   - skipPreprocess: True to skip the "preprocessing the input stream"
155     *     stage, which normalizes line endings and raises errors on certain
156     *     control characters. Advisable if the input stream is already
157     *     appropriately normalized.
158     *   - lazyAttributes: True to construct attributes lazily, saving
159     *     processing time (at the expense of memory usage) if most attributes
160     *     are not going to be accessed. Default false.
161     *   - scriptingFlag: True if the scripting flag is enabled. Default true.
162     *     Setting this to false cause the contents of <noscript> elements to be
163     *     processed as normal content. The scriptingFlag option in the
164     *     TreeBuilder should be set to the same value.
165     */
166    public function __construct( TokenHandler $listener, $text, $options = [] ) {
167        $this->listener = $listener;
168        $this->text = $text;