| 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 head" insertion mode |
| 10 |
*/ |
| 11 |
class BeforeHead 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 |
// Handle non-whitespace |
| 22 |
$this->builder->headElement = $this->builder->insertElement( |
| 23 |
'head', new PlainAttributes, false, $sourceStart, 0 ); |
| 24 |
$this->dispatcher->switchMode( Dispatcher::IN_HEAD ) |
| 25 |
->characters( $text, $start, $length, $sourceStart, $sourceLength ); |
| 26 |
} |
| 27 |
|
| 28 |
/** @inheritDoc */ |
| 29 |
public function startTag( $name, Attributes $attrs, $selfClose, $sourceStart, $sourceLength ) { |
| 30 |
if ( $name === 'html' ) { |
| 31 |
$this->dispatcher->inBody->startTag( $name, $attrs, $selfClose, |
| 32 |
$sourceStart, $sourceLength ); |
| 33 |
} elseif ( $name === 'head' ) { |
| 34 |
$this->builder->headElement = $this->builder->insertElement( |
| 35 |
$name, $attrs, false, $sourceStart, $sourceLength ); |
| 36 |
$this->dispatcher->switchMode( Dispatcher::IN_HEAD ); |
| 37 |
} else { |
| 38 |
$this->builder->headElement = $this->builder->insertElement( |
| 39 |
'head', new PlainAttributes, false, $sourceStart, 0 ); |
| 40 |
$this->dispatcher->switchMode( Dispatcher::IN_HEAD ) |
| 41 |
->startTag( $name, $attrs, $selfClose, $sourceStart, $sourceLength ); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
/** @inheritDoc */ |
| 46 |
public function endTag( $name, $sourceStart, $sourceLength ) { |
| 47 |
$allowed = [ "head" => true, "body" => true, "html" => true, "br" => true ]; |
| 48 |
if ( !isset( $allowed[$name] ) ) { |
| 49 |
$this->builder->error( 'end tag not allowed before head', $sourceStart ); |
| 50 |
return; |
| 51 |
} |
| 52 |
$this->builder->headElement = $this->builder->insertElement( |
| 53 |
'head', new PlainAttributes, false, $sourceStart, 0 ); |
| 54 |
$this->dispatcher->switchMode( Dispatcher::IN_HEAD ) |
| 55 |
->endTag( $name, $sourceStart, $sourceLength ); |
| 56 |
} |
| 57 |
|
| 58 |
/** @inheritDoc */ |
| 59 |
public function endDocument( $pos ) { |
| 60 |
$this->builder->headElement = $this->builder->insertElement( |
| 61 |
'head', new PlainAttributes, false, $pos, 0 ); |
| 62 |
$this->dispatcher->switchMode( Dispatcher::IN_HEAD ) |
| 63 |
->endDocument( $pos ); |
| 64 |
} |
| 65 |
} |