| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\RemexHtml\TreeBuilder; |
| 4 |
|
| 5 |
use Wikimedia\RemexHtml\Tokenizer\Attributes; |
| 6 |
|
| 7 |
/** |
| 8 |
* The "in column group" insertion mode |
| 9 |
*/ |
| 10 |
class InColumnGroup 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 |
|
| 16 |
[ $start, $length, $sourceStart, $sourceLength ] = $part1; |
| 17 |
if ( $length !== 0 ) { |
| 18 |
$this->builder->insertCharacters( $text, $start, $length, |
| 19 |
$sourceStart, $sourceLength ); |
| 20 |
} |
| 21 |
|
| 22 |
[ $start, $length, $sourceStart, $sourceLength ] = $part2; |
| 23 |
|
| 24 |
if ( $length === 0 ) { |
| 25 |
// All done with this sequence |
| 26 |
return; |
| 27 |
} |
| 28 |
|
| 29 |
// Handle non-whitespace as "anything else" |
| 30 |
$builder = $this->builder; |
| 31 |
$stack = $builder->stack; |
| 32 |
if ( $stack->current->htmlName !== 'colgroup' ) { |
| 33 |
$builder->error( 'text should close the colgroup but another element is open', |
| 34 |
$sourceStart ); |
| 35 |
// Ignore |
| 36 |
return; |
| 37 |
} else { |
| 38 |
$builder->pop( $sourceStart, 0 ); |
| 39 |
} |
| 40 |
$this->dispatcher->switchMode( Dispatcher::IN_TABLE ) |
| 41 |
->characters( $text, $start, $length, $sourceStart, $sourceLength ); |
| 42 |
} |
| 43 |
|
| 44 |
/** @inheritDoc */ |
| 45 |
public function startTag( $name, Attributes $attrs, $selfClose, $sourceStart, $sourceLength ) { |
| 46 |
$dispatcher = $this->dispatcher; |
| 47 |
$builder = $this->builder; |
| 48 |
$stack = $builder->stack; |
| 49 |
|
| 50 |
switch ( $name ) { |
| 51 |
case 'html': |
| 52 |
$dispatcher->inBody->startTag( $name, $attrs, $selfClose, $sourceStart, $sourceLength ); |
| 53 |
break; |
| 54 |
|
| 55 |
case 'col': |
| 56 |
$dispatcher->ack = true; |
| 57 |
$builder->insertElement( $name, $attrs, true, $sourceStart, $sourceLength ); |
| 58 |
break; |
| 59 |
|
| 60 |
case 'template': |
| 61 |
$dispatcher->inHead->startTag( $name, $attrs, $selfClose, $sourceStart, $sourceLength ); |
| 62 |
break; |
| 63 |
|
| 64 |
default: |
| 65 |
if ( $stack->current->htmlName !== 'colgroup' ) { |
| 66 |
$builder->error( 'start tag should close the colgroup but another element is open', |
| 67 |
$sourceStart ); |
| 68 |
// Ignore |
| 69 |
return; |
| 70 |
} else { |
| 71 |
$builder->pop( $sourceStart, 0 ); |
| 72 |
} |
| 73 |
$this->dispatcher->switchMode( Dispatcher::IN_TABLE ) |
| 74 |
->startTag( $name, $attrs, $selfClose, $sourceStart, $sourceLength ); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
/** @inheritDoc */ |
| 79 |
public function endTag( $name, $sourceStart, $sourceLength ) { |
| 80 |
$dispatcher = $this->dispatcher; |
| 81 |
$builder = $this->builder; |
| 82 |
$stack = $builder->stack; |
| 83 |
|
| 84 |
switch ( $name ) { |
| 85 |
case 'colgroup': |
| 86 |
if ( $stack->current->htmlName !== 'colgroup' ) { |
| 87 |
$builder->error( '</colgroup> found but another element is open, ignoring', |
| 88 |
$sourceStart ); |
| 89 |
return; |
| 90 |
} |
| 91 |
$builder->pop( $sourceStart, $sourceLength ); |
| 92 |
$dispatcher->switchMode( Dispatcher::IN_TABLE ); |
| 93 |
break; |
| 94 |
|
| 95 |
case 'col': |
| 96 |
$builder->error( '</col> found in column group mode, ignoring', $sourceStart ); |
| 97 |
break; |
| 98 |
|
| 99 |
case 'template': |
| 100 |
$dispatcher->inHead->endTag( $name, $sourceStart, $sourceLength ); |
| 101 |
break; |
| 102 |
|
| 103 |
default: |
| 104 |
if ( $stack->current->htmlName !== 'colgroup' ) { |
| 105 |
$builder->error( 'non-matching end tag should close the colgroup ' . |
| 106 |
' but another element is open', $sourceStart ); |
| 107 |
// Ignore |
| 108 |
return; |
| 109 |
} else { |
| 110 |
$builder->pop( $sourceStart, 0 ); |
| 111 |
} |
| 112 |
$dispatcher->switchMode( Dispatcher::IN_TABLE ) |
| 113 |
->endTag( $name, $sourceStart, $sourceLength ); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
/** @inheritDoc */ |
| 118 |
public function endDocument( $pos ) { |
| 119 |
$this->dispatcher->inBody->endDocument( $pos ); |
| 120 |
} |
| 121 |
} |