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