| 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 "after head" insertion mode |
| 10 |
*/ |
| 11 |
class AfterHead extends InsertionMode { |
| 12 |
/** @inheritDoc */ |
| 13 |
public function characters( $text, $start, $length, $sourceStart, $sourceLength ) { |
| 14 |
$builder = $this->builder; |
| 15 |
$dispatcher = $this->dispatcher; |
| 16 |
|
| 17 |
// Insert whitespace |
| 18 |
[ $part1, $part2 ] = $this->splitInitialMatch( |
| 19 |
true, "\t\n\f\r ", $text, $start, $length, $sourceStart, $sourceLength ); |
| 20 |
[ $start, $length, $sourceStart, $sourceLength ] = $part1; |
| 21 |
if ( $length ) { |
| 22 |
$builder->insertCharacters( $text, $start, $length, |
| 23 |
$sourceStart, $sourceLength ); |
| 24 |
} |
| 25 |
|
| 26 |
// Switch mode on non-whitespace |
| 27 |
[ $start, $length, $sourceStart, $sourceLength ] = $part2; |
| 28 |
if ( $length ) { |
| 29 |
$builder->insertElement( 'body', new PlainAttributes, false, $sourceStart, 0 ); |
| 30 |
$dispatcher->switchMode( Dispatcher::IN_BODY ) |
| 31 |
->characters( $text, $start, $length, $sourceStart, $sourceLength ); |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
/** @inheritDoc */ |
| 36 |
public function startTag( $name, Attributes $attrs, $selfClose, $sourceStart, $sourceLength ) { |
| 37 |
$builder = $this->builder; |
| 38 |
$stack = $builder->stack; |
| 39 |
$dispatcher = $this->dispatcher; |
| 40 |
|
| 41 |
switch ( $name ) { |
| 42 |
case 'html': |
| 43 |
$dispatcher->inBody->startTag( $name, $attrs, $selfClose, |
| 44 |
$sourceStart, $sourceLength ); |
| 45 |
break; |
| 46 |
|
| 47 |
case 'body': |
| 48 |
$builder->insertElement( $name, $attrs, false, $sourceStart, $sourceLength ); |
| 49 |
$builder->framesetOK = false; |
| 50 |
$dispatcher->switchMode( Dispatcher::IN_BODY ); |
| 51 |
break; |
| 52 |
|
| 53 |
case 'frameset': |
| 54 |
$builder->insertElement( $name, $attrs, false, $sourceStart, $sourceLength ); |
| 55 |
$dispatcher->switchMode( Dispatcher::IN_FRAMESET ); |
| 56 |
break; |
| 57 |
|
| 58 |
case 'base': |
| 59 |
case 'basefont': |
| 60 |
case 'bgsound': |
| 61 |
case 'link': |
| 62 |
case 'meta': |
| 63 |
case 'noframes': |
| 64 |
case 'script': |
| 65 |
case 'style': |
| 66 |
case 'template': |
| 67 |
case 'title': |
| 68 |
$builder->error( "unexpected <$name> after </head>, accepting", $sourceStart ); |
| 69 |
$stack->push( $builder->headElement ); |
| 70 |
$dispatcher->inHead->startTag( |
| 71 |
$name, $attrs, $selfClose, $sourceStart, $sourceLength ); |
| 72 |
$stack->remove( $builder->headElement ); |
| 73 |
break; |
| 74 |
|
| 75 |
case 'head': |
| 76 |
$builder->error( "unexpected <head> after </head>, ignoring", $sourceStart ); |
| 77 |
return; |
| 78 |
|
| 79 |
default: |
| 80 |
$builder->insertElement( 'body', new PlainAttributes, false, $sourceStart, 0 ); |
| 81 |
$dispatcher->switchMode( Dispatcher::IN_BODY ) |
| 82 |
->startTag( $name, $attrs, $selfClose, $sourceStart, $sourceLength ); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
/** @inheritDoc */ |
| 87 |
public function endTag( $name, $sourceStart, $sourceLength ) { |
| 88 |
$builder = $this->builder; |
| 89 |
$dispatcher = $this->dispatcher; |
| 90 |
|
| 91 |
switch ( $name ) { |
| 92 |
case 'template': |
| 93 |
$dispatcher->inHead->endTag( $name, $sourceStart, $sourceLength ); |
| 94 |
break; |
| 95 |
|
| 96 |
case 'body': |
| 97 |
case 'html': |
| 98 |
case 'br': |
| 99 |
$builder->insertElement( 'body', new PlainAttributes, false, $sourceStart, 0 ); |
| 100 |
$dispatcher->switchMode( Dispatcher::IN_BODY ) |
| 101 |
->endTag( $name, $sourceStart, $sourceLength ); |
| 102 |
break; |
| 103 |
|
| 104 |
default: |
| 105 |
$builder->error( "unexpected </$name> after head, ignoring", $sourceStart ); |
| 106 |
return; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
/** @inheritDoc */ |
| 111 |
public function endDocument( $pos ) { |
| 112 |
$this->builder->insertElement( 'body', new PlainAttributes, false, $pos, 0 ); |
| 113 |
$this->dispatcher->switchMode( Dispatcher::IN_BODY ) |
| 114 |
->endDocument( $pos ); |
| 115 |
} |
| 116 |
} |