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