Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
57 / 57
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
InHeadNoscript
100.00% covered (success)
100.00%
57 / 57
100.00% covered (success)
100.00%
4 / 4
19
100.00% covered (success)
100.00%
1 / 1
 characters
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
3
 startTag
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
11
 endTag
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
4
 endDocument
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Wikimedia\RemexHtml\TreeBuilder;
4
5use Wikimedia\RemexHtml\Tokenizer\Attributes;
6
7/**
8 * The "in head noscript" insertion mode
9 */
10class InHeadNoscript extends InsertionMode {
11    public function characters( $text, $start, $length, $sourceStart, $sourceLength ) {
12        $builder = $this->builder;
13        $dispatcher = $this->dispatcher;
14
15        // Insert whitespace
16        [ $part1, $part2 ] = $this->splitInitialMatch( true, "\t\n\f\r ",
17            $text, $start, $length, $sourceStart, $sourceLength );
18        [ $start, $length, $sourceStart, $sourceLength ] = $part1;
19        if ( $length ) {
20            $dispatcher->inHead->characters(
21                $text, $start, $length, $sourceStart, $sourceLength );
22        }
23
24        // Switch mode on non-whitespace
25        [ $start, $length, $sourceStart, $sourceLength ] = $part2;
26        if ( $length ) {
27            $builder->error( "unexpected non-whitespace character in head in noscript, " .
28                "closing noscript", $sourceStart );
29            $builder->pop( $sourceStart, 0 );
30            $dispatcher->switchMode( Dispatcher::IN_HEAD )
31                ->characters( $text, $start, $length, $sourceStart, $sourceLength );
32        }
33    }
34
35    public function startTag( $name, Attributes $attrs, $selfClose, $sourceStart, $sourceLength ) {
36        $builder = $this->builder;
37        $dispatcher = $this->dispatcher;
38
39        switch ( $name ) {
40            case 'html':
41                $dispatcher->inBody->startTag( $name, $attrs, $selfClose, $sourceStart, $sourceLength );
42                break;
43
44            case 'basefont':
45            case 'bgsound':
46            case 'link':
47            case 'meta':
48            case 'noframes':
49            case 'style':
50                $dispatcher->inHead->startTag( $name, $attrs, $selfClose, $sourceStart, $sourceLength );
51                break;
52
53            case 'head':
54            case 'noscript':
55                $builder->error( "unexpected <$name> in head in noscript, ignoring", $sourceStart );
56                return;
57
58            default:
59                $builder->error( "unexpected <$name> in head in noscript, closing noscript",
60                    $sourceStart );
61                $builder->pop( $sourceStart, 0 );
62                $dispatcher->switchMode( Dispatcher::IN_HEAD )
63                    ->startTag( $name, $attrs, $selfClose, $sourceStart, $sourceLength );
64        }
65    }
66
67    public function endTag( $name, $sourceStart, $sourceLength ) {
68        $builder = $this->builder;
69        $dispatcher = $this->dispatcher;
70
71        switch ( $name ) {
72            case 'noscript':
73                $builder->pop( $sourceStart, $sourceLength );
74                $dispatcher->switchMode( Dispatcher::IN_HEAD );
75                break;
76
77            case 'br':
78                $builder->error( "unexpected </br> in head in noscript, closing noscript",
79                    $sourceStart );
80                $builder->pop( $sourceStart, 0 );
81                $dispatcher->switchMode( Dispatcher::IN_HEAD )
82                    ->endTag( $name, $sourceStart, $sourceLength );
83                break;
84
85            default:
86                $builder->error( "unexpected </$name> in head in noscript, ignoring",
87                    $sourceStart );
88                return;
89        }
90    }
91
92    public function endDocument( $pos ) {
93        $this->builder->error( "unexpected end-of-file in head in noscript", $pos );
94        $this->builder->pop( $pos, 0 );
95        $this->dispatcher->switchMode( Dispatcher::IN_HEAD )
96            ->endDocument( $pos );
97    }
98}