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