Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 31 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| BeforeHead | |
0.00% |
0 / 31 |
|
0.00% |
0 / 4 |
72 | |
0.00% |
0 / 1 |
| characters | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
| startTag | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
12 | |||
| endTag | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
| endDocument | |
0.00% |
0 / 4 |
|
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 head" insertion mode |
| 11 | */ |
| 12 | class BeforeHead 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 | // Handle non-whitespace |
| 30 | $this->builder->headElement = $this->builder->insertElement( |
| 31 | 'head', new PlainAttributes, false, $sourceStart, 0 ); |
| 32 | $this->dispatcher->switchMode( Dispatcher::IN_HEAD ) |
| 33 | ->characters( $text, $start, $length, $sourceStart, $sourceLength ); |
| 34 | } |
| 35 | |
| 36 | /** @inheritDoc */ |
| 37 | public function startTag( $name, Attributes $attrs, $selfClose, $sourceStart, $sourceLength ) { |
| 38 | if ( $name === 'html' ) { |
| 39 | $this->dispatcher->inBody->startTag( $name, $attrs, $selfClose, |
| 40 | $sourceStart, $sourceLength ); |
| 41 | } elseif ( $name === 'head' ) { |
| 42 | $this->builder->headElement = $this->builder->insertElement( |
| 43 | $name, $attrs, false, $sourceStart, $sourceLength ); |
| 44 | $this->dispatcher->switchMode( Dispatcher::IN_HEAD ); |
| 45 | } else { |
| 46 | $this->builder->headElement = $this->builder->insertElement( |
| 47 | 'head', new PlainAttributes, false, $sourceStart, 0 ); |
| 48 | $this->dispatcher->switchMode( Dispatcher::IN_HEAD ) |
| 49 | ->startTag( $name, $attrs, $selfClose, $sourceStart, $sourceLength ); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /** @inheritDoc */ |
| 54 | public function endTag( $name, $sourceStart, $sourceLength ) { |
| 55 | if ( !isset( self::TAG_ALLOWED[$name] ) ) { |
| 56 | $this->builder->error( 'end tag not allowed before head', $sourceStart ); |
| 57 | return; |
| 58 | } |
| 59 | $this->builder->headElement = $this->builder->insertElement( |
| 60 | 'head', new PlainAttributes, false, $sourceStart, 0 ); |
| 61 | $this->dispatcher->switchMode( Dispatcher::IN_HEAD ) |
| 62 | ->endTag( $name, $sourceStart, $sourceLength ); |
| 63 | } |
| 64 | |
| 65 | /** @inheritDoc */ |
| 66 | public function endDocument( $pos ) { |
| 67 | $this->builder->headElement = $this->builder->insertElement( |
| 68 | 'head', new PlainAttributes, false, $pos, 0 ); |
| 69 | $this->dispatcher->switchMode( Dispatcher::IN_HEAD ) |
| 70 | ->endDocument( $pos ); |
| 71 | } |
| 72 | } |