Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 52
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Initial
0.00% covered (danger)
0.00%
0 / 52
0.00% covered (danger)
0.00%
0 / 5
812
0.00% covered (danger)
0.00%
0 / 1
 characters
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
12
 startTag
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 endTag
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 doctype
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 1
380
 endDocument
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2declare( strict_types = 1 );
3
4namespace Wikimedia\RemexHtml\TreeBuilder;
5
6use Wikimedia\RemexHtml\HTMLData;
7use Wikimedia\RemexHtml\Tokenizer\Attributes;
8
9/**
10 * The "initial" insertion mode
11 */
12class Initial extends InsertionMode {
13    /**
14     * The doctypes listed in the spec which are allowed without generating a
15     * parse error. A 2-d array where each row gives the doctype name, the
16     * public identifier and the system identifier.
17     */
18    private const ALLOWED_DOCTYPES = [
19        [ 'html', '-//W3C//DTD HTML 4.0//EN', null ],
20        [ 'html', '-//W3C//DTD HTML 4.0//EN', 'http://www.w3.org/TR/REC-html40/strict.dtd' ],
21        [ 'html', '-//W3C//DTD HTML 4.01//EN', null ],
22        [ 'html', '-//W3C//DTD HTML 4.01//EN', 'http://www.w3.org/TR/html4/strict.dtd' ],
23        [ 'html', '-//W3C//DTD XHTML 1.0 Strict//EN',
24            'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd' ],
25        [ 'html', '-//W3C//DTD XHTML 1.1//EN', 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd' ]
26    ];
27
28    /** @inheritDoc */
29    public function characters( $text, $start, $length, $sourceStart, $sourceLength ) {
30        // Ignore whitespace
31        [ $part1, $part2 ] = $this->splitInitialMatch(
32            true, "\t\n\f\r ", $text, $start, $length, $sourceStart, $sourceLength );
33        [ $start, $length, $sourceStart, $sourceLength ] = $part2;
34        if ( !$length ) {
35            return;
36        }
37        if ( !$this->builder->isIframeSrcdoc ) {
38            $this->error( 'missing doctype', $sourceStart );
39            $this->builder->quirks = TreeBuilder::QUIRKS;
40        }
41        $this->dispatcher->switchMode( Dispatcher::BEFORE_HTML )
42            ->characters( $text, $start, $length, $sourceStart, $sourceLength );
43    }
44
45    /** @inheritDoc */
46    public function startTag( $name, Attributes $attrs, $selfClose, $sourceStart, $sourceLength ) {
47        if ( !$this->builder->isIframeSrcdoc ) {
48            $this->error( 'missing doctype', $sourceStart );
49            $this->builder->quirks = TreeBuilder::QUIRKS;
50        }
51        $this->dispatcher->switchMode( Dispatcher::BEFORE_HTML )
52            ->startTag( $name, $attrs, $selfClose, $sourceStart, $sourceLength );
53    }
54
55    /** @inheritDoc */
56    public function endTag( $name, $sourceStart, $sourceLength ) {
57        if ( !$this->builder->isIframeSrcdoc ) {
58            $this->error( 'missing doctype', $sourceStart );
59            $this->builder->quirks = TreeBuilder::QUIRKS;
60        }
61        $this->dispatcher->switchMode( Dispatcher::BEFORE_HTML )
62            ->endTag( $name, $sourceStart, $sourceLength );
63    }
64
65    /** @inheritDoc */
66    public function doctype( $name, $public, $system, $quirks, $sourceStart, $sourceLength ) {
67        if ( ( $name !== 'html' || $public !== null
68                || ( $system !== null && $system !== 'about:legacy-compat' )
69            )
70            && !in_array( [ $name, $public, $system ], self::ALLOWED_DOCTYPES, true )
71        ) {
72            $this->error( 'invalid doctype', $sourceStart );
73        }
74
75        $quirks = $quirks ? TreeBuilder::QUIRKS : TreeBuilder::NO_QUIRKS;
76
77        $quirksIfNoSystem = '~-//W3C//DTD HTML 4\.01 Frameset//|' .
78            '-//W3C//DTD HTML 4\.01 Transitional//~Ai';
79        $limitedQuirks = '~-//W3C//DTD XHTML 1\.0 Frameset//|' .
80            '-//W3C//DTD XHTML 1\.0 Transitional//~Ai';
81
82        if ( $name !== 'html'
83            || $public === '-//W3O//DTD W3 HTML Strict 3.0//EN//'
84            || $public === '-/W3C/DTD HTML 4.0 Transitional/EN'
85            || $public === 'HTML'
86            || $system === 'http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd'
87            || ( $system === null && preg_match( $quirksIfNoSystem, $public ?? '' ) )
88            || preg_match( HTMLData::QUIRKY_PREFIX_REGEX, $public ?? '' )
89        ) {
90            $quirks = TreeBuilder::QUIRKS;
91        } elseif ( !$this->builder->isIframeSrcdoc
92            && (
93                preg_match( $limitedQuirks, $public ?? '' )
94                || ( $system !== null && preg_match( $quirksIfNoSystem, $public ?? '' ) )
95            )
96        ) {
97            $quirks = TreeBuilder::LIMITED_QUIRKS;
98        }
99
100        $name ??= '';
101        $public ??= '';
102        $system ??= '';
103        $this->builder->doctype( $name, $public, $system, $quirks,
104            $sourceStart, $sourceLength );
105        $this->dispatcher->switchMode( Dispatcher::BEFORE_HTML );
106    }
107
108    /** @inheritDoc */
109    public function endDocument( $pos ) {
110        if ( !$this->builder->isIframeSrcdoc ) {
111            $this->error( 'missing doctype', $pos );
112            $this->builder->quirks = TreeBuilder::QUIRKS;
113        }
114        $this->dispatcher->switchMode( Dispatcher::BEFORE_HTML )
115            ->endDocument( $pos );
116    }
117}