Debian Codegraph @

vendor/wikimedia/remex-html/src/TreeBuilder/BeforeHtml.php

raw
1 <?php
2
3 namespace Wikimedia\RemexHtml\TreeBuilder;
4
5 use Wikimedia\RemexHtml\Tokenizer\Attributes;
6 use Wikimedia\RemexHtml\Tokenizer\PlainAttributes;
7
8 /**
9 * The "before html" insertion mode
10 */
11 class BeforeHtml extends InsertionMode {
12 /** @inheritDoc */
13 public function characters( $text, $start, $length, $sourceStart, $sourceLength ) {
14 // Ignore whitespace
15 [ $part1, $part2 ] = $this->splitInitialMatch(
16 true, "\t\n\f\r ", $text, $start, $length, $sourceStart, $sourceLength );
17 [ $start, $length, $sourceStart, $sourceLength ] = $part2;
18 if ( !$length ) {
19 return;
20 }
21 // Generate missing <html> tag
22 $this->builder->insertElement( 'html', new PlainAttributes, false, $sourceStart, 0 );
23 $this->dispatcher->switchMode( Dispatcher::BEFORE_HEAD )
24 ->characters( $text, $start, $length, $sourceStart, $sourceLength );
25 }
26
27 /** @inheritDoc */
28 public function startTag( $name, Attributes $attrs, $selfClose, $sourceStart, $sourceLength ) {
29 if ( $name === 'html' ) {
30 $this->builder->insertElement( $name, $attrs, false,
31 $sourceStart, $sourceLength );
32 $this->dispatcher->switchMode( Dispatcher::BEFORE_HEAD );
33 } else {
34 $this->builder->insertElement( 'html', new PlainAttributes, false,
35 $sourceStart, 0 );
36 $this->dispatcher->switchMode( Dispatcher::BEFORE_HEAD )
37 ->startTag( $name, $attrs, $selfClose, $sourceStart, $sourceLength );
38 }
39 }
40
41 /** @inheritDoc */
42 public function endTag( $name, $sourceStart, $sourceLength ) {
43 $allowed = [ "head" => true, "body" => true, "html" => true, "br" => true ];
44 if ( !isset( $allowed[$name] ) ) {
45 $this->builder->error( 'end tag not allowed before html', $sourceStart );
46 return;
47 }
48 $this->builder->insertElement( 'html', new PlainAttributes, false, $sourceStart, 0 );
49 $this->dispatcher->switchMode( Dispatcher::BEFORE_HEAD )
50 ->endTag( $name, $sourceStart, $sourceLength );
51 }
52
53 /** @inheritDoc */
54 public function endDocument( $pos ) {
55 $this->builder->insertElement( 'html', new PlainAttributes, false, $pos, 0 );
56 $this->dispatcher->switchMode( Dispatcher::BEFORE_HEAD )
57 ->endDocument( $pos );
58 }
59 }