| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\RemexHtml\TreeBuilder; |
| 4 |
|
| 5 |
use Wikimedia\RemexHtml\Tokenizer\Attributes; |
| 6 |
|
| 7 |
/** |
| 8 |
* The "after body" insertion mode |
| 9 |
*/ |
| 10 |
class AfterBody extends InsertionMode { |
| 11 |
/** @inheritDoc */ |
| 12 |
public function characters( $text, $start, $length, $sourceStart, $sourceLength ) { |
| 13 |
[ $part1, $part2 ] = $this->splitInitialMatch( |
| 14 |
true, "\t\n\f\r ", $text, $start, $length, $sourceStart, $sourceLength ); |
| 15 |
[ $start, $length, $sourceStart, $sourceLength ] = $part1; |
| 16 |
if ( $length ) { |
| 17 |
$this->dispatcher->inBody->characters( |
| 18 |
$text, $start, $length, $sourceStart, $sourceLength ); |
| 19 |
} |
| 20 |
[ $start, $length, $sourceStart, $sourceLength ] = $part2; |
| 21 |
$this->builder->error( "unexpected non-whitespace character after body", |
| 22 |
$sourceStart ); |
| 23 |
$this->dispatcher->switchMode( Dispatcher::IN_BODY ) |
| 24 |
->characters( $text, $start, $length, $sourceStart, $sourceLength ); |
| 25 |
} |
| 26 |
|
| 27 |
/** @inheritDoc */ |
| 28 |
public function startTag( $name, Attributes $attrs, $selfClose, $sourceStart, $sourceLength ) { |
| 29 |
$builder = $this->builder; |
| 30 |
$dispatcher = $this->dispatcher; |
| 31 |
|
| 32 |
switch ( $name ) { |
| 33 |
case 'html': |
| 34 |
$dispatcher->inBody->startTag( |
| 35 |
$name, $attrs, $selfClose, $sourceStart, $sourceLength ); |
| 36 |
break; |
| 37 |
|
| 38 |
default: |
| 39 |
$builder->error( "unexpected start tag after body", $sourceStart ); |
| 40 |
$dispatcher->switchMode( Dispatcher::IN_BODY ) |
| 41 |
->startTag( $name, $attrs, $selfClose, $sourceStart, $sourceLength ); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
/** @inheritDoc */ |
| 46 |
public function endTag( $name, $sourceStart, $sourceLength ) { |
| 47 |
$builder = $this->builder; |
| 48 |
$dispatcher = $this->dispatcher; |
| 49 |
|
| 50 |
switch ( $name ) { |
| 51 |
case 'html': |
| 52 |
if ( $builder->isFragment ) { |
| 53 |
$builder->error( "unexpected </html> in fragment", $sourceStart ); |
| 54 |
return; |
| 55 |
} |
| 56 |
$dispatcher->switchMode( Dispatcher::AFTER_AFTER_BODY ); |
| 57 |
break; |
| 58 |
|
| 59 |
default: |
| 60 |
$builder->error( "unexpected end tag after body", $sourceStart ); |
| 61 |
$dispatcher->switchMode( Dispatcher::IN_BODY ) |
| 62 |
->endTag( $name, $sourceStart, $sourceLength ); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
/** @inheritDoc */ |
| 67 |
public function endDocument( $pos ) { |
| 68 |
$this->builder->stopParsing( $pos ); |
| 69 |
} |
| 70 |
|
| 71 |
/** @inheritDoc */ |
| 72 |
public function comment( $text, $sourceStart, $sourceLength ) { |
| 73 |
$this->builder->comment( [ TreeBuilder::UNDER, $this->builder->stack->item( 0 ) ], |
| 74 |
$text, $sourceStart, $sourceLength ); |
| 75 |
} |
| 76 |
} |