| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\RemexHtml\TreeBuilder; |
| 4 |
|
| 5 |
use Wikimedia\RemexHtml\Tokenizer\Attributes; |
| 6 |
|
| 7 |
/** |
| 8 |
* The "in caption" insertion mode |
| 9 |
*/ |
| 10 |
class InCaption extends InsertionMode { |
| 11 |
/** @inheritDoc */ |
| 12 |
public function characters( $text, $start, $length, $sourceStart, $sourceLength ) { |
| 13 |
$this->dispatcher->inBody->characters( $text, $start, $length, |
| 14 |
$sourceStart, $sourceLength ); |
| 15 |
} |
| 16 |
|
| 17 |
/** @inheritDoc */ |
| 18 |
public function startTag( $name, Attributes $attrs, $selfClose, $sourceStart, $sourceLength ) { |
| 19 |
$builder = $this->builder; |
| 20 |
$stack = $builder->stack; |
| 21 |
switch ( $name ) { |
| 22 |
case 'caption': |
| 23 |
case 'col': |
| 24 |
case 'colgroup': |
| 25 |
case 'tbody': |
| 26 |
case 'td': |
| 27 |
case 'tfoot': |
| 28 |
case 'th': |
| 29 |
case 'thead': |
| 30 |
case 'tr': |
| 31 |
$builder->error( "start tag <$name> not allowed in caption", $sourceStart ); |
| 32 |
if ( !$stack->isInTableScope( 'caption' ) ) { |
| 33 |
// Ignore |
| 34 |
return; |
| 35 |
} |
| 36 |
$builder->popAllUpToName( 'caption', $sourceStart, 0 ); |
| 37 |
$builder->afe->clearToMarker(); |
| 38 |
$this->dispatcher->switchMode( Dispatcher::IN_TABLE ) |
| 39 |
->startTag( $name, $attrs, $selfClose, $sourceStart, $sourceLength ); |
| 40 |
break; |
| 41 |
|
| 42 |
default: |
| 43 |
$this->dispatcher->inBody->startTag( $name, $attrs, $selfClose, |
| 44 |
$sourceStart, $sourceLength ); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
/** @inheritDoc */ |
| 49 |
public function endTag( $name, $sourceStart, $sourceLength ) { |
| 50 |
$dispatcher = $this->dispatcher; |
| 51 |
$builder = $this->builder; |
| 52 |
$stack = $builder->stack; |
| 53 |
|
| 54 |
switch ( $name ) { |
| 55 |
case 'caption': |
| 56 |
if ( !$stack->isInTableScope( 'caption' ) ) { |
| 57 |
$builder->error( "</caption> matches a start tag which is not in scope, ignoring", |
| 58 |
$sourceStart ); |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
$builder->generateImpliedEndTags( false, $sourceStart ); |
| 63 |
if ( $stack->current->htmlName !== 'caption' ) { |
| 64 |
$builder->error( "</caption> found but another element is open", $sourceStart ); |
| 65 |
} |
| 66 |
$builder->popAllUpToName( 'caption', $sourceStart, $sourceLength ); |
| 67 |
$builder->afe->clearToMarker(); |
| 68 |
$dispatcher->switchMode( Dispatcher::IN_TABLE ); |
| 69 |
break; |
| 70 |
|
| 71 |
case 'table': |
| 72 |
if ( !$stack->isInTableScope( 'caption' ) ) { |
| 73 |
$builder->error( '</table> found in caption, but there is no ' . |
| 74 |
'caption in scope, ignoring', $sourceStart ); |
| 75 |
return; |
| 76 |
} |
| 77 |
$builder->generateImpliedEndTags( false, $sourceStart ); |
| 78 |
if ( $stack->current->htmlName !== 'caption' ) { |
| 79 |
$builder->error( '</table> found in caption, closing caption', $sourceStart ); |
| 80 |
} |
| 81 |
$builder->popAllUpToName( 'caption', $sourceStart, 0 ); |
| 82 |
$builder->afe->clearToMarker(); |
| 83 |
$dispatcher->switchMode( Dispatcher::IN_TABLE ) |
| 84 |
->endTag( $name, $sourceStart, $sourceLength ); |
| 85 |
break; |
| 86 |
|
| 87 |
case 'body': |
| 88 |
case 'col': |
| 89 |
case 'colgroup': |
| 90 |
case 'html': |
| 91 |
case 'tbody': |
| 92 |
case 'td': |
| 93 |
case 'tfoot': |
| 94 |
case 'th': |
| 95 |
case 'thead': |
| 96 |
case 'tr': |
| 97 |
$this->builder->error( "end tag </$name> ignored in caption mode", $sourceStart ); |
| 98 |
break; |
| 99 |
|
| 100 |
default: |
| 101 |
$this->dispatcher->inBody->endTag( $name, $sourceStart, $sourceLength ); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
/** @inheritDoc */ |
| 106 |
public function endDocument( $pos ) { |
| 107 |
$this->dispatcher->inBody->endDocument( $pos ); |
| 108 |
} |
| 109 |
} |