Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 55
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
InsertionMode
0.00% covered (danger)
0.00%
0 / 55
0.00% covered (danger)
0.00%
0 / 7
272
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 doctype
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 comment
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 error
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 splitInitialMatch
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
20
 handleFramesetWhitespace
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
20
 stripNulls
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
20
 characters
n/a
0 / 0
n/a
0 / 0
0
 startTag
n/a
0 / 0
n/a
0 / 0
0
 endTag
n/a
0 / 0
n/a
0 / 0
0
 endDocument
n/a
0 / 0
n/a
0 / 0
0
1<?php
2declare( strict_types = 1 );
3
4namespace Wikimedia\RemexHtml\TreeBuilder;
5
6use Wikimedia\RemexHtml\PropGuard;
7use Wikimedia\RemexHtml\Tokenizer\Attributes;
8
9abstract class InsertionMode {
10    use PropGuard;
11
12    protected TreeBuilder $builder;
13    protected Dispatcher $dispatcher;
14
15    public function __construct( TreeBuilder $builder, Dispatcher $dispatcher ) {
16        $this->builder = $builder;
17        $this->dispatcher = $dispatcher;
18    }
19
20    /**
21     * A valid DOCTYPE token was found.
22     *
23     * @param string $name The doctype name, usually "html"
24     * @param string $public The PUBLIC identifier
25     * @param string $system The SYSTEM identifier
26     * @param bool $quirks What the spec calls the "force-quirks flag"
27     * @param int $sourceStart The input position
28     * @param int $sourceLength The length of the input which is consumed
29     */
30    public function doctype( $name, $public, $system, $quirks, $sourceStart, $sourceLength ) {
31        $this->builder->error( "unexpected doctype", $sourceStart );
32    }
33
34    /**
35     * @param string $text The text of the comment
36     * @param int $sourceStart The input position
37     * @param int $sourceLength The length of the input which is consumed
38     */
39    public function comment( $text, $sourceStart, $sourceLength ) {
40        $this->builder->comment( null, $text, $sourceStart, $sourceLength );
41    }
42
43    /**
44     * A parse error
45     *
46     * @param string $text An error message explaining in English what the
47     *   author did wrong, and what the parser intends to do about the
48     *   situation.
49     * @param int $pos The input position at which the error occurred
50     */
51    public function error( $text, $pos ) {
52        $this->builder->error( $text, $pos );
53    }
54
55    /**
56     * @param bool $isStartOfToken
57     * @param string $mask Match mask
58     * @param string $text The text to insert is a substring of this string,
59     *   with the start and length of the substring given by $start and
60     *   $length. We do it this way to avoid unnecessary copying.
61     * @param int $start The start of the substring
62     * @param int $length The length of the substring
63     * @param int $sourceStart The input position
64     * @param int $sourceLength The length of the input which is consumed
65     * @return array
66     */
67    protected function splitInitialMatch( $isStartOfToken, $mask, $text, $start, $length,
68        $sourceStart, $sourceLength
69    ) {
70        $matchLength = strspn( $text, $mask, $start, $length );
71        if ( $isStartOfToken && $matchLength ) {
72            // Do some extra work to figure out a plausible start position if
73            // the text node started with <![CDATA[
74            // FIXME: make this optional?
75            $sourceText = $this->builder->tokenizer->getPreprocessedText();
76            $isCdata = substr_compare( $sourceText, '<![CDATA[', $sourceStart, $sourceLength ) === 0;
77            $cdataLength = $isCdata ? strlen( '<![CDATA[' ) : 0;
78        } else {
79            $cdataLength = 0;
80        }
81
82        return [
83            [
84                $start,
85                $matchLength,
86                $sourceStart,
87                $matchLength + $cdataLength,
88            ], [
89                $start + $matchLength,
90                $length - $matchLength,
91                $sourceStart + $matchLength + $cdataLength,
92                $sourceLength - $matchLength - $cdataLength
93            ]
94        ];
95    }
96
97    /**
98     * @param bool $inBody
99     * @param string $text The text to insert is a substring of this string,
100     *   with the start and length of the substring given by $start and
101     *   $length. We do it this way to avoid unnecessary copying.
102     * @param int $start The start of the substring
103     * @param int $length The length of the substring
104     * @param int $sourceStart The input position
105     * @param int $sourceLength The length of the input which is consumed
106     */
107    protected function handleFramesetWhitespace( $inBody, $text, $start, $length,
108        $sourceStart, $sourceLength
109    ) {
110        $isStartOfToken = true;
111        $builder = $this->builder;
112
113        do {
114            [ $part1, $part2 ] = $this->splitInitialMatch(
115                $isStartOfToken, "\t\n\f\r ", $text, $start, $length, $sourceStart, $sourceLength );
116            $isStartOfToken = false;
117
118            [ $start, $length, $sourceStart, $sourceLength ] = $part1;
119            if ( $length ) {
120                if ( $inBody ) {
121                    $this->dispatcher->inBody->characters( $text, $start, $length,
122                        $sourceStart, $sourceLength );
123                } else {
124                    $builder->insertCharacters( $text, $start, $length, $sourceStart, $sourceLength );
125                }
126            }
127
128            [ $start, $length, $sourceStart, $sourceLength ] = $part2;
129            if ( $length ) {
130                $builder->error( "unexpected non-whitespace character", $sourceStart );
131                $start++;
132                $length--;
133                $sourceStart++;
134                $sourceLength--;
135            }
136        } while ( $length > 0 );
137    }
138
139    /**
140     * @param callable $callback
141     * @param string $text The text to insert is a substring of this string,
142     *   with the start and length of the substring given by $start and
143     *   $length. We do it this way to avoid unnecessary copying.
144     * @param int $start The start of the substring
145     * @param int $length The length of the substring
146     * @param int $sourceStart The input position
147     * @param int $sourceLength The length of the input which is consumed
148     */
149    protected function stripNulls( $callback, $text, $start, $length, $sourceStart, $sourceLength ) {
150        $errorOffset = $sourceStart - $start;
151        while ( $length > 0 ) {
152            $validLength = strcspn( $text, "\0", $start, $length );
153            if ( $validLength ) {
154                $callback( $text, $start, $validLength, $sourceStart, $sourceLength );
155                $start += $validLength;
156                $length -= $validLength;
157            }
158            if ( $length <= 0 ) {
159                break;
160            }
161            $this->error( 'unexpected null character', $start + $errorOffset );
162            $start++;
163            $length--;
164        }
165    }
166
167    /**
168     * Insert characters.
169     *
170     * @param string $text The text to insert is a substring of this string,
171     *   with the start and length of the substring given by $start and
172     *   $length. We do it this way to avoid unnecessary copying.
173     * @param int $start The start of the substring
174     * @param int $length The length of the substring
175     * @param int $sourceStart The input position
176     * @param int $sourceLength The length of the input which is consumed
177     */
178    abstract public function characters( $text, $start, $length, $sourceStart, $sourceLength );
179
180    /**
181     * @param string $name The tag name being ended
182     * @param Attributes $attrs
183     * @param bool $selfClose True if this is a self closing tag
184     * @param int $sourceStart The input position
185     * @param int $sourceLength The input position
186     */
187    abstract public function startTag( $name, Attributes $attrs, $selfClose,
188        $sourceStart, $sourceLength );
189
190    /**
191     * @param string $name The tag name being ended
192     * @param int $sourceStart The input position
193     * @param int $sourceLength The input position
194     */
195    abstract public function endTag( $name, $sourceStart, $sourceLength );
196
197    /**
198     * Called when parsing stops.
199     *
200     * @param int $pos The input string length, i.e. the past-the-end position.
201     */
202    abstract public function endDocument( $pos );
203}