Debian Codegraph @

vendor/wikimedia/remex-html/src/TreeBuilder/InHead.php

raw
1 <?php
2
3 namespace Wikimedia\RemexHtml\TreeBuilder;
4
5 use LogicException;
6 use Wikimedia\RemexHtml\Tokenizer\Attributes;
7 use Wikimedia\RemexHtml\Tokenizer\Tokenizer;
8
9 /**
10 * The "in head" insertion mode
11 */
12 class InHead extends InsertionMode {
13 /** @inheritDoc */
14 public function characters( $text, $start, $length, $sourceStart, $sourceLength ) {
15 // Split and insert whitespace
16 [ $part1, $part2 ] = $this->splitInitialMatch(
17 true, "\t\n\f\r ", $text, $start, $length, $sourceStart, $sourceLength );
18
19 [ $start, $length, $sourceStart, $sourceLength ] = $part1;
20 if ( $length ) {
21 $this->builder->insertCharacters( $text, $start, $length, $sourceStart,
22 $sourceLength );
23 }
24
25 // Handle non-whitespace specially
26 [ $start, $length, $sourceStart, $sourceLength ] = $part2;
27 if ( !$length ) {
28 return;
29 }
30
31 $elt = $this->builder->pop( $sourceStart, 0 );
32 if ( $elt->htmlName !== 'head' ) {
33 throw new LogicException( 'In head mode but current element is not <head>' );
34 }
35 $this->dispatcher->switchMode( Dispatcher::AFTER_HEAD )
36 ->characters( $text, $start, $length, $sourceStart, $sourceLength );
37 }
38
39 /** @inheritDoc */
40 public function startTag( $name, Attributes $attrs, $selfClose, $sourceStart, $sourceLength ) {
41 $void = false;
42 $tokenizerState = null;
43 $textMode = null;
44 $mode = null;
45 $dispatcher = $this->dispatcher;
46
47 switch ( $name ) {
48 case 'html':
49 $this->dispatcher->inBody->startTag( $name, $attrs, $selfClose,
50 $sourceStart, $sourceLength );
51 return;
52
53 case 'base':
54 case 'basefont':
55 case 'bgsound':
56 case 'link':
57 $void = true;
58 $dispatcher->ack = true;
59 break;
60
61 case 'meta':
62 $void = true;
63 $dispatcher->ack = true;
64 // charset handling omitted
65 break;
66
67 case 'title':
68 $tokenizerState = Tokenizer::STATE_RCDATA;
69 $textMode = Dispatcher::TEXT;
70 break;
71
72 case 'noscript':
73 if ( !$this->builder->scriptingFlag ) {
74 $mode = Dispatcher::IN_HEAD_NOSCRIPT;
75 break;
76 }
77 /*. missing_break; .*/
78 case 'noframes':
79 case 'style':
80 $tokenizerState = Tokenizer::STATE_RAWTEXT;
81 $textMode = Dispatcher::TEXT;
82 break;
83
84 case 'script':
85 $tokenizerState = Tokenizer::STATE_SCRIPT_DATA;
86 $textMode = Dispatcher::TEXT;
87 break;
88
89 case 'template':
90 $this->builder->afe->insertMarker();
91 $this->builder->framesetOK = false;
92 $mode = Dispatcher::IN_TEMPLATE;
93 $this->dispatcher->templateModeStack->push( Dispatcher::IN_TEMPLATE );
94 break;
95
96 case 'head':
97 $this->builder->error( 'unexpected head tag in head, ignoring', $sourceStart );
98 return;
99
100 default:
101 $elt = $this->builder->pop( $sourceStart, 0 );
102 if ( $elt->htmlName !== 'head' ) {
103 throw new LogicException( "In head mode but current element is not <head>" );
104 }
105 $this->dispatcher->switchMode( Dispatcher::AFTER_HEAD )
106 ->startTag( $name, $attrs, $selfClose, $sourceStart, $sourceLength );
107 return;
108 }
109
110 // Generic element insertion, for all cases that didn't return above
111 $this->builder->insertElement( $name, $attrs, $void,
112 $sourceStart, $sourceLength );
113 if ( $tokenizerState !== null ) {
114 $this->builder->tokenizer->switchState( $tokenizerState, $name );
115 }
116 if ( $textMode !== null ) {
117 $this->dispatcher->switchAndSave( $textMode );
118 } elseif ( $mode !== null ) {
119 $this->dispatcher->switchMode( $mode );
120 }
121 }
122
123 /** @inheritDoc */
124 public function endTag( $name, $sourceStart, $sourceLength ) {
125 $builder = $this->builder;
126 $stack = $builder->stack;
127
128 switch ( $name ) {
129 case 'head':
130 $builder->pop( $sourceStart, $sourceLength );
131 $this->dispatcher->switchMode( Dispatcher::AFTER_HEAD );
132 break;
133
134 case 'body':
135 case 'html':
136 case 'br':
137 $builder->pop( $sourceStart, 0 );
138 $this->dispatcher->switchMode( Dispatcher::AFTER_HEAD )
139 ->endTag( $name, $sourceStart, $sourceLength );
140 break;
141
142 case 'template':
143 if ( !$stack->hasTemplate() ) {
144 $builder->error( 'found </template> but there is no open template, ignoring',
145 $sourceStart );
146 return;
147 }
148 $builder->generateImpliedEndTagsThoroughly( $sourceStart );
149 if ( $stack->current->htmlName !== 'template' ) {
150 $builder->error( 'found </template> when other tags are still open', $sourceStart );
151 }
152 $builder->popAllUpToName( 'template', $sourceStart, $sourceLength );
153 $builder->afe->clearToMarker();
154 $this->dispatcher->templateModeStack->pop();
155 $this->dispatcher->reset();
156 break;
157
158 default:
159 $builder->error( "ignoring </$name> in head", $sourceStart );
160 }
161 }
162
163 /** @inheritDoc */
164 public function endDocument( $pos ) {
165 $this->builder->pop( $pos, 0 );
166 $this->dispatcher->switchMode( Dispatcher::AFTER_HEAD )
167 ->endDocument( $pos );
168 }
169 }