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