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