3 namespace RemexHtml\TreeBuilder;
4 use RemexHtml\Tokenizer\Attributes;
7 * The "in cell" insertion mode
9 class InCell extends InsertionMode {
10 public function characters( $text, $start, $length, $sourceStart, $sourceLength ) {
11 $this->dispatcher->inBody->characters( $text, $start, $length,
12 $sourceStart, $sourceLength );
15 public function startTag( $name, Attributes $attrs, $selfClose, $sourceStart, $sourceLength ) {
26 if ( !$this->builder->stack->isInTableScope( 'td' )
27 && !$this->builder->stack->isInTableScope( 'th' )
29 $this->builder->error( "<$name> tag should close the cell but none is in scope",
33 $this->closeTheCell( $sourceStart )
34 ->startTag( $name, $attrs, $selfClose, $sourceStart, $sourceLength );
38 $this->dispatcher->inBody->startTag(
39 $name, $attrs, $selfClose, $sourceStart, $sourceLength );
43 public function endTag( $name, $sourceStart, $sourceLength ) {
44 $builder = $this->builder;
45 $stack = $builder->stack;
46 $dispatcher = $this->dispatcher;
51 if ( !$stack->isInTableScope( $name ) ) {
52 $builder->error( "</$name> encountered but there is no $name in scope, ignoring",
56 $builder->generateImpliedEndTags( null, $sourceStart );
57 if ( $stack->current->htmlName !== $name ) {
58 $builder->error( "</$name> encountered when there are tags open " .
59 "which can't be closed automatically", $sourceStart );
61 $builder->popAllUpToName( $name, $sourceStart, $sourceLength );
62 $builder->afe->clearToMarker();
63 $dispatcher->switchMode( Dispatcher::IN_ROW );
71 $builder->error( "unexpected </$name> in cell, ignoring", $sourceStart );
79 if ( !$stack->isInTableScope( $name ) ) {
80 $builder->error( "</$name> encountered but there is no $name in scope, ignoring",
84 $this->closeTheCell( $sourceStart )
85 ->endTag( $name, $sourceStart, $sourceLength );
89 $dispatcher->inBody->endTag( $name, $sourceStart, $sourceLength );
93 public function endDocument( $pos ) {
94 $this->dispatcher->inBody->endDocument( $pos );
97 private function closeTheCell( $sourceStart ) {
98 $tdth = [ 'td' => true, 'th' => true ];
99 $builder = $this->builder;
100 $stack = $builder->stack;
101 $builder->generateImpliedEndTags( false, $sourceStart );
102 if ( !isset( $tdth[$stack->current->htmlName] ) ) {
103 $builder->error( "closing the cell but there are tags open " .
104 "which can't be closed automatically", $sourceStart );
106 $builder->afe->clearToMarker();
107 return $this->dispatcher->switchMode( Dispatcher::IN_ROW );