3 namespace RemexHtml\TreeBuilder;
4 use RemexHtml\Tokenizer\Attributes;
7 * The "in select" insertion mode
9 class InSelect extends InsertionMode {
10 public function characters( $text, $start, $length, $sourceStart, $sourceLength ) {
12 function ( $text, $start, $length, $sourceStart, $sourceLength ) {
13 $this->builder->insertCharacters( $text, $start, $length,
14 $sourceStart, $sourceLength );
16 $text, $start, $length, $sourceStart, $sourceLength );
19 public function startTag( $name, Attributes $attrs, $selfClose, $sourceStart, $sourceLength ) {
20 $builder = $this->builder;
21 $stack = $builder->stack;
22 $dispatcher = $this->dispatcher;
26 $dispatcher->inBody->startTag( $name, $attrs, $selfClose, $sourceStart, $sourceLength );
30 if ( $stack->current->htmlName === 'option' ) {
31 $builder->pop( $sourceStart, 0 );
33 $builder->insertElement( 'option', $attrs, false, $sourceStart, $sourceLength );
37 if ( $stack->current->htmlName === 'option' ) {
38 $builder->pop( $sourceStart, 0 );
40 if ( $stack->current->htmlName === 'optgroup' ) {
41 $builder->pop( $sourceStart, 0 );
43 $builder->insertElement( 'optgroup', $attrs, false, $sourceStart, $sourceLength );
47 if ( !$stack->isInSelectScope( 'select' ) ) {
48 $builder->error( "<select> found in select mode but no select element is in " .
49 "scope, ignoring", $sourceStart );
53 $builder->error( "<select> found inside a select element", $sourceStart );
54 $builder->popAllUpToName( 'select', $sourceStart, $sourceLength );
61 $builder->error( "<$name> found inside a select element", $sourceStart );
62 if ( !$stack->isInSelectScope( 'select' ) ) {
66 $builder->popAllUpToName( 'select', $sourceStart, 0 );
68 ->startTag( $name, $attrs, $selfClose, $sourceStart, $sourceLength );
73 $dispatcher->inHead->startTag( $name, $attrs, $selfClose,
74 $sourceStart, $sourceLength );
78 $builder->error( "<$name> found inside a select element, ignoring", $sourceStart );
82 public function endTag( $name, $sourceStart, $sourceLength ) {
83 $builder = $this->builder;
84 $stack = $builder->stack;
85 $dispatcher = $this->dispatcher;
89 if ( $stack->current->htmlName === 'option' ) {
90 $penultimate = $stack->item( $stack->length() - 2 );
91 if ( $penultimate && $penultimate->htmlName === 'optgroup' ) {
92 $builder->pop( $sourceStart, 0 );
95 if ( $stack->current->htmlName !== 'optgroup' ) {
96 $builder->error( "unexpected </optgroup>, ignoring", $sourceStart );
99 $builder->pop( $sourceStart, $sourceLength );
103 if ( $stack->current->htmlName !== 'option' ) {
104 $builder->error( "unexpected </option>, ignoring", $sourceStart );
107 $builder->pop( $sourceStart, $sourceLength );
111 if ( !$stack->isInSelectScope( 'select' ) ) {
112 $builder->error( "</select> found but the select element is " .
113 "not in scope", $sourceStart );
116 $builder->popAllUpToName( 'select', $sourceStart, $sourceLength );
117 $dispatcher->reset();
121 $dispatcher->inHead->endTag( $name, $sourceStart, $sourceLength );
125 $builder->error( "unexpected </$name> in select, ignoring", $sourceStart );
130 public function endDocument( $pos ) {
131 $this->dispatcher->inBody->endDocument( $pos );