/[suikacvs]/markup/html/whatpm/Whatpm/HTML.pm.src
Suika

Diff of /markup/html/whatpm/Whatpm/HTML.pm.src

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.41 by wakaba, Sat Jul 21 06:04:07 2007 UTC revision 1.113 by wakaba, Sun Mar 16 07:07:59 2008 UTC
# Line 1  Line 1 
1  package Whatpm::HTML;  package Whatpm::HTML;
2  use strict;  use strict;
3  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
4    use Error qw(:try);
5    
6  ## ISSUE:  ## ISSUE:
7  ## var doc = implementation.createDocument (null, null, null);  ## var doc = implementation.createDocument (null, null, null);
8  ## doc.write ('');  ## doc.write ('');
9  ## alert (doc.compatMode);  ## alert (doc.compatMode);
10    
11  ## ISSUE: HTML5 revision 967 says that the encoding layer MUST NOT  ## TODO: Control charcters and noncharacters are not allowed (HTML5 revision 1263)
12  ## strip BOM and the HTML layer MUST ignore it.  Whether we can do it  ## TODO: 1252 parse error (revision 1264)
13  ## is not yet clear.  ## TODO: 8859-11 = 874 (revision 1271)
 ## "{U+FEFF}..." in UTF-16BE/UTF-16LE is three or four characters?  
 ## "{U+FEFF}..." in GB18030?  
14    
15  my $permitted_slash_tag_name = {  my $permitted_slash_tag_name = {
16    base => 1,    base => 1,
# Line 19  my $permitted_slash_tag_name = { Line 18  my $permitted_slash_tag_name = {
18    meta => 1,    meta => 1,
19    hr => 1,    hr => 1,
20    br => 1,    br => 1,
21    img=> 1,    img => 1,
22    embed => 1,    embed => 1,
23    param => 1,    param => 1,
24    area => 1,    area => 1,
# Line 75  my $special_category = { Line 74  my $special_category = {
74    textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,    textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,
75  };  };
76  my $scoping_category = {  my $scoping_category = {
77    button => 1, caption => 1, html => 1, marquee => 1, object => 1,    applet => 1, button => 1, caption => 1, html => 1, marquee => 1, object => 1,
78    table => 1, td => 1, th => 1,    table => 1, td => 1, th => 1,
79  };  };
80  my $formatting_category = {  my $formatting_category = {
# Line 84  my $formatting_category = { Line 83  my $formatting_category = {
83  };  };
84  # $phrasing_category: all other elements  # $phrasing_category: all other elements
85    
86    sub parse_byte_string ($$$$;$) {
87      my $self = ref $_[0] ? shift : shift->new;
88      my $charset = shift;
89      my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);
90      my $s;
91      
92      if (defined $charset) {
93        require Encode; ## TODO: decode(utf8) don't delete BOM
94        $s = \ (Encode::decode ($charset, $$bytes_s));
95        $self->{input_encoding} = lc $charset; ## TODO: normalize name
96        $self->{confident} = 1;
97      } else {
98        ## TODO: Implement HTML5 detection algorithm
99        require Whatpm::Charset::UniversalCharDet;
100        $charset = Whatpm::Charset::UniversalCharDet->detect_byte_string
101            (substr ($$bytes_s, 0, 1024));
102        $charset ||= 'windows-1252';
103        $s = \ (Encode::decode ($charset, $$bytes_s));
104        $self->{input_encoding} = $charset;
105        $self->{confident} = 0;
106      }
107    
108      $self->{change_encoding} = sub {
109        my $self = shift;
110        my $charset = lc shift;
111        ## TODO: if $charset is supported
112        ## TODO: normalize charset name
113    
114        ## "Change the encoding" algorithm:
115    
116        ## Step 1    
117        if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?
118          $charset = 'utf-8';
119        }
120    
121        ## Step 2
122        if (defined $self->{input_encoding} and
123            $self->{input_encoding} eq $charset) {
124          $self->{confident} = 1;
125          return;
126        }
127    
128        !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
129            ':'.$charset, level => 'w');
130    
131        ## Step 3
132        # if (can) {
133          ## change the encoding on the fly.
134          #$self->{confident} = 1;
135          #return;
136        # }
137    
138        ## Step 4
139        throw Whatpm::HTML::RestartParser (charset => $charset);
140      }; # $self->{change_encoding}
141    
142      my @args = @_; shift @args; # $s
143      my $return;
144      try {
145        $return = $self->parse_char_string ($s, @args);  
146      } catch Whatpm::HTML::RestartParser with {
147        my $charset = shift->{charset};
148        $s = \ (Encode::decode ($charset, $$bytes_s));    
149        $self->{input_encoding} = $charset; ## TODO: normalize
150        $self->{confident} = 1;
151        $return = $self->parse_char_string ($s, @args);
152      };
153      return $return;
154    } # parse_byte_string
155    
156    ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
157    ## and the HTML layer MUST ignore it.  However, we does strip BOM in
158    ## the encoding layer and the HTML layer does not ignore any U+FEFF,
159    ## because the core part of our HTML parser expects a string of character,
160    ## not a string of bytes or code units or anything which might contain a BOM.
161    ## Therefore, any parser interface that accepts a string of bytes,
162    ## such as |parse_byte_string| in this module, must ensure that it does
163    ## strip the BOM and never strip any ZWNBSP.
164    
165    *parse_char_string = \&parse_string;
166    
167  sub parse_string ($$$;$) {  sub parse_string ($$$;$) {
168    my $self = shift->new;    my $self = ref $_[0] ? shift : shift->new;
169    my $s = \$_[0];    my $s = ref $_[0] ? $_[0] : \($_[0]);
170    $self->{document} = $_[1];    $self->{document} = $_[1];
171      @{$self->{document}->child_nodes} = ();
172    
173    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
174    
175      $self->{confident} = 1 unless exists $self->{confident};
176      $self->{document}->input_encoding ($self->{input_encoding})
177          if defined $self->{input_encoding};
178    
179    my $i = 0;    my $i = 0;
180    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
181    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
182    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
183      my $self = shift;      my $self = shift;
184    
185      pop @{$self->{prev_input_character}};      pop @{$self->{prev_char}};
186      unshift @{$self->{prev_input_character}}, $self->{next_input_character};      unshift @{$self->{prev_char}}, $self->{next_char};
187    
188        $self->{next_char} = -1 and return if $i >= length $$s;
189        $self->{next_char} = ord substr $$s, $i++, 1;
190    
191      $self->{next_input_character} = -1 and return if $i >= length $$s;      ($self->{line_prev}, $self->{column_prev})
192      $self->{next_input_character} = ord substr $$s, $i++, 1;          = ($self->{line}, $self->{column});
193      $column++;      $self->{column}++;
194            
195      if ($self->{next_input_character} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
196        $line++;        $self->{line}++;
197        $column = 0;        $self->{column} = 0;
198      } elsif ($self->{next_input_character} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
199        $i++ if substr ($$s, $i, 1) eq "\x0A";        $i++ if substr ($$s, $i, 1) eq "\x0A";
200        $self->{next_input_character} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
201        $line++;        $self->{line}++;
202        $column = 0;        $self->{column} = 0;
203      } elsif ($self->{next_input_character} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
204        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
205      } elsif ($self->{next_input_character} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
206        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
207        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
208      }      }
209    };    };
210    $self->{prev_input_character} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
211    $self->{next_input_character} = -1;    $self->{next_char} = -1;
212    
213    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
214      my (%opt) = @_;      my (%opt) = @_;
215      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
216        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
217        warn "Parse error ($opt{type}) at line $line column $column\n";
218    };    };
219    $self->{parse_error} = sub {    $self->{parse_error} = sub {
220      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
221    };    };
222    
223    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 135  sub parse_string ($$$;$) { Line 225  sub parse_string ($$$;$) {
225    $self->_construct_tree;    $self->_construct_tree;
226    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
227    
228      delete $self->{parse_error}; # remove loop
229    
230    return $self->{document};    return $self->{document};
231  } # parse_string  } # parse_string
232    
233  sub new ($) {  sub new ($) {
234    my $class = shift;    my $class = shift;
235    my $self = bless {}, $class;    my $self = bless {}, $class;
236    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
237      $self->{next_input_character} = -1;      $self->{next_char} = -1;
238    };    };
239    $self->{parse_error} = sub {    $self->{parse_error} = sub {
240      #      #
241    };    };
242      $self->{change_encoding} = sub {
243        # if ($_[0] is a supported encoding) {
244        #   run "change the encoding" algorithm;
245        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
246        # }
247      };
248      $self->{application_cache_selection} = sub {
249        #
250      };
251    return $self;    return $self;
252  } # new  } # new
253    
# Line 159  sub CDATA_CONTENT_MODEL () { CM_LIMITED_ Line 260  sub CDATA_CONTENT_MODEL () { CM_LIMITED_
260  sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }  sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
261  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
262    
263    sub DATA_STATE () { 0 }
264    sub ENTITY_DATA_STATE () { 1 }
265    sub TAG_OPEN_STATE () { 2 }
266    sub CLOSE_TAG_OPEN_STATE () { 3 }
267    sub TAG_NAME_STATE () { 4 }
268    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
269    sub ATTRIBUTE_NAME_STATE () { 6 }
270    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
271    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
272    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
273    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
274    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
275    sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
276    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
277    sub COMMENT_START_STATE () { 14 }
278    sub COMMENT_START_DASH_STATE () { 15 }
279    sub COMMENT_STATE () { 16 }
280    sub COMMENT_END_STATE () { 17 }
281    sub COMMENT_END_DASH_STATE () { 18 }
282    sub BOGUS_COMMENT_STATE () { 19 }
283    sub DOCTYPE_STATE () { 20 }
284    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
285    sub DOCTYPE_NAME_STATE () { 22 }
286    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
287    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
288    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
289    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
290    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
291    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
292    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
293    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
294    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
295    sub BOGUS_DOCTYPE_STATE () { 32 }
296    sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
297    
298    sub DOCTYPE_TOKEN () { 1 }
299    sub COMMENT_TOKEN () { 2 }
300    sub START_TAG_TOKEN () { 3 }
301    sub END_TAG_TOKEN () { 4 }
302    sub END_OF_FILE_TOKEN () { 5 }
303    sub CHARACTER_TOKEN () { 6 }
304    
305    sub AFTER_HTML_IMS () { 0b100 }
306    sub HEAD_IMS ()       { 0b1000 }
307    sub BODY_IMS ()       { 0b10000 }
308    sub BODY_TABLE_IMS () { 0b100000 }
309    sub TABLE_IMS ()      { 0b1000000 }
310    sub ROW_IMS ()        { 0b10000000 }
311    sub BODY_AFTER_IMS () { 0b100000000 }
312    sub FRAME_IMS ()      { 0b1000000000 }
313    sub SELECT_IMS ()     { 0b10000000000 }
314    
315    ## NOTE: "initial" and "before html" insertion modes have no constants.
316    
317    ## NOTE: "after after body" insertion mode.
318    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
319    
320    ## NOTE: "after after frameset" insertion mode.
321    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
322    
323    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
324    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
325    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
326    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
327    sub IN_BODY_IM () { BODY_IMS }
328    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
329    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
330    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
331    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
332    sub IN_TABLE_IM () { TABLE_IMS }
333    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
334    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
335    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
336    sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
337    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
338    sub IN_COLUMN_GROUP_IM () { 0b10 }
339    
340  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
341    
342  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
343    my $self = shift;    my $self = shift;
344    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
345    $self->{content_model} = PCDATA_CONTENT_MODEL; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
346    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
347    undef $self->{current_attribute};    undef $self->{current_attribute};
348    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
349    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
350    $self->{char} = [];    $self->{char} = [];
351    # $self->{next_input_character}    # $self->{next_char}
352    !!!next-input-character;    !!!next-input-character;
353    $self->{token} = [];    $self->{token} = [];
354    # $self->{escape}    # $self->{escape}
355  } # _initialize_tokenizer  } # _initialize_tokenizer
356    
357  ## A token has:  ## A token has:
358  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
359  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
360  ##   ->{name} (DOCTYPE, start tag (tag name), end tag (tag name))  ##   ->{name} (DOCTYPE_TOKEN)
361  ##   ->{public_identifier} (DOCTYPE)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
362  ##   ->{system_identifier} (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
363  ##   ->{correct} == 1 or 0 (DOCTYPE)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
364  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
365  ##   ->{data} (comment, character)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
366    ##        ->{name}
367    ##        ->{value}
368    ##        ->{has_reference} == 1 or 0
369    ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
370    
371  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
372    
# Line 194  sub _initialize_tokenizer ($) { Line 376  sub _initialize_tokenizer ($) {
376  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
377  ## and removed from the list.  ## and removed from the list.
378    
379    ## NOTE: HTML5 "Writing HTML documents" section, applied to
380    ## documents and not to user agents and conformance checkers,
381    ## contains some requirements that are not detected by the
382    ## parsing algorithm:
383    ## - Some requirements on character encoding declarations. ## TODO
384    ## - "Elements MUST NOT contain content that their content model disallows."
385    ##   ... Some are parse error, some are not (will be reported by c.c.).
386    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
387    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
388    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
389    
390    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
391    ## be detected by the HTML5 parsing algorithm:
392    ## - Text,
393    
394  sub _get_next_token ($) {  sub _get_next_token ($) {
395    my $self = shift;    my $self = shift;
396    if (@{$self->{token}}) {    if (@{$self->{token}}) {
# Line 201  sub _get_next_token ($) { Line 398  sub _get_next_token ($) {
398    }    }
399    
400    A: {    A: {
401      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
402        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_char} == 0x0026) { # &
403          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
404            $self->{state} = 'entity data';              not $self->{escape}) {
405              !!!cp (1);
406              $self->{state} = ENTITY_DATA_STATE;
407            !!!next-input-character;            !!!next-input-character;
408            redo A;            redo A;
409          } else {          } else {
410              !!!cp (2);
411            #            #
412          }          }
413        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
414          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
415            unless ($self->{escape}) {            unless ($self->{escape}) {
416              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_char}->[0] == 0x002D and # -
417                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_char}->[1] == 0x0021 and # !
418                  $self->{prev_input_character}->[2] == 0x003C) { # <                  $self->{prev_char}->[2] == 0x003C) { # <
419                  !!!cp (3);
420                $self->{escape} = 1;                $self->{escape} = 1;
421                } else {
422                  !!!cp (4);
423              }              }
424              } else {
425                !!!cp (5);
426            }            }
427          }          }
428                    
429          #          #
430        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_char} == 0x003C) { # <
431          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
432              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
433               not $self->{escape})) {               not $self->{escape})) {
434            $self->{state} = 'tag open';            !!!cp (6);
435              $self->{state} = TAG_OPEN_STATE;
436            !!!next-input-character;            !!!next-input-character;
437            redo A;            redo A;
438          } else {          } else {
439              !!!cp (7);
440            #            #
441          }          }
442        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
443          if ($self->{escape} and          if ($self->{escape} and
444              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
445            if ($self->{prev_input_character}->[0] == 0x002D and # -            if ($self->{prev_char}->[0] == 0x002D and # -
446                $self->{prev_input_character}->[1] == 0x002D) { # -                $self->{prev_char}->[1] == 0x002D) { # -
447                !!!cp (8);
448              delete $self->{escape};              delete $self->{escape};
449              } else {
450                !!!cp (9);
451            }            }
452            } else {
453              !!!cp (10);
454          }          }
455                    
456          #          #
457        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
458          !!!emit ({type => 'end-of-file'});          !!!cp (11);
459            !!!emit ({type => END_OF_FILE_TOKEN,
460                      line => $self->{line}, column => $self->{column}});
461          last A; ## TODO: ok?          last A; ## TODO: ok?
462          } else {
463            !!!cp (12);
464        }        }
465        # Anything else        # Anything else
466        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
467                     data => chr $self->{next_input_character}};                     data => chr $self->{next_char},
468                       line => $self->{line}, column => $self->{column}};
469        ## Stay in the data state        ## Stay in the data state
470        !!!next-input-character;        !!!next-input-character;
471    
472        !!!emit ($token);        !!!emit ($token);
473    
474        redo A;        redo A;
475      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
476        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
477    
478          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
479                
480        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
481    
482        $self->{state} = 'data';        $self->{state} = DATA_STATE;
483        # next-input-character is already done        # next-input-character is already done
484    
485        unless (defined $token) {        unless (defined $token) {
486          !!!emit ({type => 'character', data => '&'});          !!!cp (13);
487            !!!emit ({type => CHARACTER_TOKEN, data => '&',
488                      line => $l, column => $c});
489        } else {        } else {
490            !!!cp (14);
491          !!!emit ($token);          !!!emit ($token);
492        }        }
493    
494        redo A;        redo A;
495      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
496        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
497          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_char} == 0x002F) { # /
498              !!!cp (15);
499            !!!next-input-character;            !!!next-input-character;
500            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
501            redo A;            redo A;
502          } else {          } else {
503              !!!cp (16);
504            ## reconsume            ## reconsume
505            $self->{state} = 'data';            $self->{state} = DATA_STATE;
506    
507            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
508                        line => $self->{line_prev},
509                        column => $self->{column_prev}});
510    
511            redo A;            redo A;
512          }          }
513        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
514          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_char} == 0x0021) { # !
515            $self->{state} = 'markup declaration open';            !!!cp (17);
516              $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
517            !!!next-input-character;            !!!next-input-character;
518            redo A;            redo A;
519          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_char} == 0x002F) { # /
520            $self->{state} = 'close tag open';            !!!cp (18);
521              $self->{state} = CLOSE_TAG_OPEN_STATE;
522            !!!next-input-character;            !!!next-input-character;
523            redo A;            redo A;
524          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
525                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_char} <= 0x005A) { # A..Z
526              !!!cp (19);
527            $self->{current_token}            $self->{current_token}
528              = {type => 'start tag',              = {type => START_TAG_TOKEN,
529                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
530            $self->{state} = 'tag name';                 line => $self->{line_prev},
531                   column => $self->{column_prev}};
532              $self->{state} = TAG_NAME_STATE;
533            !!!next-input-character;            !!!next-input-character;
534            redo A;            redo A;
535          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
536                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
537            $self->{current_token} = {type => 'start tag',            !!!cp (20);
538                              tag_name => chr ($self->{next_input_character})};            $self->{current_token} = {type => START_TAG_TOKEN,
539            $self->{state} = 'tag name';                                      tag_name => chr ($self->{next_char}),
540                                        line => $self->{line_prev},
541                                        column => $self->{column_prev}};
542              $self->{state} = TAG_NAME_STATE;
543            !!!next-input-character;            !!!next-input-character;
544            redo A;            redo A;
545          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
546              !!!cp (21);
547            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag');
548            $self->{state} = 'data';            $self->{state} = DATA_STATE;
549            !!!next-input-character;            !!!next-input-character;
550    
551            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
552                        line => $self->{line_prev},
553                        column => $self->{column_prev}});
554    
555            redo A;            redo A;
556          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
557              !!!cp (22);
558            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio');
559            $self->{state} = 'bogus comment';            $self->{state} = BOGUS_COMMENT_STATE;
560            ## $self->{next_input_character} is intentionally left as is            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
561                                        line => $self->{line_prev},
562                                        column => $self->{column_prev}};
563              ## $self->{next_char} is intentionally left as is
564            redo A;            redo A;
565          } else {          } else {
566              !!!cp (23);
567            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago');
568            $self->{state} = 'data';            $self->{state} = DATA_STATE;
569            ## reconsume            ## reconsume
570    
571            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
572                        line => $self->{line_prev},
573                        column => $self->{column_prev}});
574    
575            redo A;            redo A;
576          }          }
577        } else {        } else {
578          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
579        }        }
580      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
581          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
582        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
583          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
584    
585            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
586            my @next_char;            my @next_char;
587            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
588              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
589              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
590              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
591              if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {              if ($self->{next_char} == $c or $self->{next_char} == $C) {
592                  !!!cp (24);
593                !!!next-input-character;                !!!next-input-character;
594                next TAGNAME;                next TAGNAME;
595              } else {              } else {
596                $self->{next_input_character} = shift @next_char; # reconsume                !!!cp (25);
597                  $self->{next_char} = shift @next_char; # reconsume
598                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
599                $self->{state} = 'data';                $self->{state} = DATA_STATE;
600    
601                !!!emit ({type => 'character', data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</',
602                            line => $l, column => $c});
603        
604                redo A;                redo A;
605              }              }
606            }            }
607            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
608                
609            unless ($self->{next_input_character} == 0x0009 or # HT            unless ($self->{next_char} == 0x0009 or # HT
610                    $self->{next_input_character} == 0x000A or # LF                    $self->{next_char} == 0x000A or # LF
611                    $self->{next_input_character} == 0x000B or # VT                    $self->{next_char} == 0x000B or # VT
612                    $self->{next_input_character} == 0x000C or # FF                    $self->{next_char} == 0x000C or # FF
613                    $self->{next_input_character} == 0x0020 or # SP                    $self->{next_char} == 0x0020 or # SP
614                    $self->{next_input_character} == 0x003E or # >                    $self->{next_char} == 0x003E or # >
615                    $self->{next_input_character} == 0x002F or # /                    $self->{next_char} == 0x002F or # /
616                    $self->{next_input_character} == -1) {                    $self->{next_char} == -1) {
617              $self->{next_input_character} = shift @next_char; # reconsume              !!!cp (26);
618                $self->{next_char} = shift @next_char; # reconsume
619              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
620              $self->{state} = 'data';              $self->{state} = DATA_STATE;
621              !!!emit ({type => 'character', data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</',
622                          line => $l, column => $c});
623              redo A;              redo A;
624            } else {            } else {
625              $self->{next_input_character} = shift @next_char;              !!!cp (27);
626                $self->{next_char} = shift @next_char;
627              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
628              # and consume...              # and consume...
629            }            }
630          } else {          } else {
631            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
632              !!!cp (28);
633            # next-input-character is already done            # next-input-character is already done
634            $self->{state} = 'data';            $self->{state} = DATA_STATE;
635            !!!emit ({type => 'character', data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</',
636                        line => $l, column => $c});
637            redo A;            redo A;
638          }          }
639        }        }
640                
641        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_char} and
642            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
643          $self->{current_token} = {type => 'end tag',          !!!cp (29);
644                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{current_token}
645          $self->{state} = 'tag name';              = {type => END_TAG_TOKEN,
646          !!!next-input-character;                 tag_name => chr ($self->{next_char} + 0x0020),
647          redo A;                 line => $l, column => $c};
648        } elsif (0x0061 <= $self->{next_input_character} and          $self->{state} = TAG_NAME_STATE;
649                 $self->{next_input_character} <= 0x007A) { # a..z          !!!next-input-character;
650          $self->{current_token} = {type => 'end tag',          redo A;
651                            tag_name => chr ($self->{next_input_character})};        } elsif (0x0061 <= $self->{next_char} and
652          $self->{state} = 'tag name';                 $self->{next_char} <= 0x007A) { # a..z
653            !!!cp (30);
654            $self->{current_token} = {type => END_TAG_TOKEN,
655                                      tag_name => chr ($self->{next_char}),
656                                      line => $l, column => $c};
657            $self->{state} = TAG_NAME_STATE;
658          !!!next-input-character;          !!!next-input-character;
659          redo A;          redo A;
660        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
661            !!!cp (31);
662          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag');
663          $self->{state} = 'data';          $self->{state} = DATA_STATE;
664          !!!next-input-character;          !!!next-input-character;
665          redo A;          redo A;
666        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
667            !!!cp (32);
668          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
669          $self->{state} = 'data';          $self->{state} = DATA_STATE;
670          # reconsume          # reconsume
671    
672          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
673                      line => $l, column => $c});
674    
675          redo A;          redo A;
676        } else {        } else {
677            !!!cp (33);
678          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
679          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
680          ## $self->{next_input_character} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
681          redo A;                                    line => $self->{line_prev}, # "<" of "</"
682        }                                    column => $self->{column_prev} - 1};
683      } elsif ($self->{state} eq 'tag name') {          ## $self->{next_char} is intentionally left as is
684        if ($self->{next_input_character} == 0x0009 or # HT          redo A;
685            $self->{next_input_character} == 0x000A or # LF        }
686            $self->{next_input_character} == 0x000B or # VT      } elsif ($self->{state} == TAG_NAME_STATE) {
687            $self->{next_input_character} == 0x000C or # FF        if ($self->{next_char} == 0x0009 or # HT
688            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x000A or # LF
689          $self->{state} = 'before attribute name';            $self->{next_char} == 0x000B or # VT
690          !!!next-input-character;            $self->{next_char} == 0x000C or # FF
691          redo A;            $self->{next_char} == 0x0020) { # SP
692        } elsif ($self->{next_input_character} == 0x003E) { # >          !!!cp (34);
693          if ($self->{current_token}->{type} eq 'start tag') {          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
694            !!!next-input-character;
695            redo A;
696          } elsif ($self->{next_char} == 0x003E) { # >
697            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
698              !!!cp (35);
699            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
700                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
701            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
702          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
703            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
704            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
705              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
706            }            #  !!! cp (36);
707              #  !!! parse-error (type => 'end tag attribute');
708              #} else {
709                !!!cp (37);
710              #}
711          } else {          } else {
712            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
713          }          }
714          $self->{state} = 'data';          $self->{state} = DATA_STATE;
715          !!!next-input-character;          !!!next-input-character;
716    
717          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
718    
719          redo A;          redo A;
720        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
721                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
722          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
723            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
724            # start tag or end tag            # start tag or end tag
725          ## Stay in this state          ## Stay in this state
726          !!!next-input-character;          !!!next-input-character;
727          redo A;          redo A;
728        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
729          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
730          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
731              !!!cp (39);
732            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
733                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
734            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
735          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
736            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
737            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
738              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
739            }            #  !!! cp (40);
740              #  !!! parse-error (type => 'end tag attribute');
741              #} else {
742                !!!cp (41);
743              #}
744          } else {          } else {
745            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
746          }          }
747          $self->{state} = 'data';          $self->{state} = DATA_STATE;
748          # reconsume          # reconsume
749    
750          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
751    
752          redo A;          redo A;
753        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
754          !!!next-input-character;          !!!next-input-character;
755          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
756              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
757              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
758            # permitted slash            # permitted slash
759              !!!cp (42);
760            #            #
761          } else {          } else {
762              !!!cp (43);
763            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
764          }          }
765          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
766          # next-input-character is already done          # next-input-character is already done
767          redo A;          redo A;
768        } else {        } else {
769          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
770            $self->{current_token}->{tag_name} .= chr $self->{next_char};
771            # start tag or end tag            # start tag or end tag
772          ## Stay in the state          ## Stay in the state
773          !!!next-input-character;          !!!next-input-character;
774          redo A;          redo A;
775        }        }
776      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
777        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
778            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
779            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
780            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
781            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
782            !!!cp (45);
783          ## Stay in the state          ## Stay in the state
784          !!!next-input-character;          !!!next-input-character;
785          redo A;          redo A;
786        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
787          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
788              !!!cp (46);
789            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
790                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
791            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
792          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
793            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
794            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
795                !!!cp (47);
796              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
797              } else {
798                !!!cp (48);
799            }            }
800          } else {          } else {
801            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
802          }          }
803          $self->{state} = 'data';          $self->{state} = DATA_STATE;
804          !!!next-input-character;          !!!next-input-character;
805    
806          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
807    
808          redo A;          redo A;
809        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
810                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
811          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
812            $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),
813                                value => ''};                                value => ''};
814          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
815          !!!next-input-character;          !!!next-input-character;
816          redo A;          redo A;
817        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
818          !!!next-input-character;          !!!next-input-character;
819          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
820              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
821              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
822            # permitted slash            # permitted slash
823              !!!cp (50);
824            #            #
825          } else {          } else {
826              !!!cp (51);
827            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
828          }          }
829          ## Stay in the state          ## Stay in the state
830          # next-input-character is already done          # next-input-character is already done
831          redo A;          redo A;
832        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
833          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
834          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
835              !!!cp (52);
836            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
837                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
838            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
839          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
840            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
841            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
842                !!!cp (53);
843              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
844              } else {
845                !!!cp (54);
846            }            }
847          } else {          } else {
848            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
849          }          }
850          $self->{state} = 'data';          $self->{state} = DATA_STATE;
851          # reconsume          # reconsume
852    
853          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
854    
855          redo A;          redo A;
856        } else {        } else {
857          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
858                 0x0022 => 1, # "
859                 0x0027 => 1, # '
860                 0x003D => 1, # =
861                }->{$self->{next_char}}) {
862              !!!cp (55);
863              !!!parse-error (type => 'bad attribute name');
864            } else {
865              !!!cp (56);
866            }
867            $self->{current_attribute} = {name => chr ($self->{next_char}),
868                                value => ''};                                value => ''};
869          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
870          !!!next-input-character;          !!!next-input-character;
871          redo A;          redo A;
872        }        }
873      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
874        my $before_leave = sub {        my $before_leave = sub {
875          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
876              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
877              !!!cp (57);
878            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});
879            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
880          } else {          } else {
881              !!!cp (58);
882            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
883              = $self->{current_attribute};              = $self->{current_attribute};
884          }          }
885        }; # $before_leave        }; # $before_leave
886    
887        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
888            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
889            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
890            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
891            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
892            !!!cp (59);
893          $before_leave->();          $before_leave->();
894          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
895          !!!next-input-character;          !!!next-input-character;
896          redo A;          redo A;
897        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
898            !!!cp (60);
899          $before_leave->();          $before_leave->();
900          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
901          !!!next-input-character;          !!!next-input-character;
902          redo A;          redo A;
903        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
904          $before_leave->();          $before_leave->();
905          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
906              !!!cp (61);
907            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
908                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
909            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
910          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
911              !!!cp (62);
912            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
913            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
914              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 607  sub _get_next_token ($) { Line 916  sub _get_next_token ($) {
916          } else {          } else {
917            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
918          }          }
919          $self->{state} = 'data';          $self->{state} = DATA_STATE;
920          !!!next-input-character;          !!!next-input-character;
921    
922          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
923    
924          redo A;          redo A;
925        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
926                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
927          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
928            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
929          ## Stay in the state          ## Stay in the state
930          !!!next-input-character;          !!!next-input-character;
931          redo A;          redo A;
932        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
933          $before_leave->();          $before_leave->();
934          !!!next-input-character;          !!!next-input-character;
935          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
936              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
937              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
938            # permitted slash            # permitted slash
939              !!!cp (64);
940            #            #
941          } else {          } else {
942              !!!cp (65);
943            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
944          }          }
945          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
946          # next-input-character is already done          # next-input-character is already done
947          redo A;          redo A;
948        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
949          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
950          $before_leave->();          $before_leave->();
951          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
952              !!!cp (66);
953            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
954                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
955            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
956          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
957            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
958            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
959                !!!cp (67);
960              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
961              } else {
962                ## NOTE: This state should never be reached.
963                !!!cp (68);
964            }            }
965          } else {          } else {
966            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
967          }          }
968          $self->{state} = 'data';          $self->{state} = DATA_STATE;
969          # reconsume          # reconsume
970    
971          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
972    
973          redo A;          redo A;
974        } else {        } else {
975          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x0022 or # "
976                $self->{next_char} == 0x0027) { # '
977              !!!cp (69);
978              !!!parse-error (type => 'bad attribute name');
979            } else {
980              !!!cp (70);
981            }
982            $self->{current_attribute}->{name} .= chr ($self->{next_char});
983          ## Stay in the state          ## Stay in the state
984          !!!next-input-character;          !!!next-input-character;
985          redo A;          redo A;
986        }        }
987      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
988        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
989            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
990            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
991            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
992            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
993            !!!cp (71);
994          ## Stay in the state          ## Stay in the state
995          !!!next-input-character;          !!!next-input-character;
996          redo A;          redo A;
997        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
998          $self->{state} = 'before attribute value';          !!!cp (72);
999            $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1000          !!!next-input-character;          !!!next-input-character;
1001          redo A;          redo A;
1002        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1003          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1004              !!!cp (73);
1005            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1006                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1007            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1008          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1009            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1010            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1011                !!!cp (74);
1012              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1013              } else {
1014                ## NOTE: This state should never be reached.
1015                !!!cp (75);
1016            }            }
1017          } else {          } else {
1018            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1019          }          }
1020          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1021          !!!next-input-character;          !!!next-input-character;
1022    
1023          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1024    
1025          redo A;          redo A;
1026        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1027                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1028          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
1029            $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),
1030                                value => ''};                                value => ''};
1031          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
1032          !!!next-input-character;          !!!next-input-character;
1033          redo A;          redo A;
1034        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1035          !!!next-input-character;          !!!next-input-character;
1036          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
1037              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
1038              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
1039            # permitted slash            # permitted slash
1040              !!!cp (77);
1041            #            #
1042          } else {          } else {
1043              !!!cp (78);
1044            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
1045            ## TODO: Different error type for <aa / bb> than <aa/>            ## TODO: Different error type for <aa / bb> than <aa/>
1046          }          }
1047          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1048          # next-input-character is already done          # next-input-character is already done
1049          redo A;          redo A;
1050        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1051          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1052          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1053              !!!cp (79);
1054            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1055                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1056            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1057          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1058            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1059            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1060                !!!cp (80);
1061              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1062              } else {
1063                ## NOTE: This state should never be reached.
1064                !!!cp (81);
1065            }            }
1066          } else {          } else {
1067            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1068          }          }
1069          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1070          # reconsume          # reconsume
1071    
1072          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1073    
1074          redo A;          redo A;
1075        } else {        } else {
1076          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          !!!cp (82);
1077            $self->{current_attribute} = {name => chr ($self->{next_char}),
1078                                value => ''};                                value => ''};
1079          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
1080          !!!next-input-character;          !!!next-input-character;
1081          redo A;                  redo A;        
1082        }        }
1083      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1084        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1085            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1086            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1087            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1088            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1089            !!!cp (83);
1090          ## Stay in the state          ## Stay in the state
1091          !!!next-input-character;          !!!next-input-character;
1092          redo A;          redo A;
1093        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1094          $self->{state} = 'attribute value (double-quoted)';          !!!cp (84);
1095            $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1096          !!!next-input-character;          !!!next-input-character;
1097          redo A;          redo A;
1098        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1099          $self->{state} = 'attribute value (unquoted)';          !!!cp (85);
1100            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1101          ## reconsume          ## reconsume
1102          redo A;          redo A;
1103        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1104          $self->{state} = 'attribute value (single-quoted)';          !!!cp (86);
1105            $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1106          !!!next-input-character;          !!!next-input-character;
1107          redo A;          redo A;
1108        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1109          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1110              !!!cp (87);
1111            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1112                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1113            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1114          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1115            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1116            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1117                !!!cp (88);
1118              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1119              } else {
1120                ## NOTE: This state should never be reached.
1121                !!!cp (89);
1122            }            }
1123          } else {          } else {
1124            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1125          }          }
1126          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1127          !!!next-input-character;          !!!next-input-character;
1128    
1129          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1130    
1131          redo A;          redo A;
1132        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1133          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1134          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1135              !!!cp (90);
1136            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1137                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1138            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1139          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1140            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1141            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1142                !!!cp (91);
1143              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1144              } else {
1145                ## NOTE: This state should never be reached.
1146                !!!cp (92);
1147            }            }
1148          } else {          } else {
1149            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1150          }          }
1151          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1152          ## reconsume          ## reconsume
1153    
1154          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1155    
1156          redo A;          redo A;
1157        } else {        } else {
1158          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x003D) { # =
1159          $self->{state} = 'attribute value (unquoted)';            !!!cp (93);
1160              !!!parse-error (type => 'bad attribute value');
1161            } else {
1162              !!!cp (94);
1163            }
1164            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1165            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1166          !!!next-input-character;          !!!next-input-character;
1167          redo A;          redo A;
1168        }        }
1169      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1170        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1171          $self->{state} = 'before attribute name';          !!!cp (95);
1172            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1173          !!!next-input-character;          !!!next-input-character;
1174          redo A;          redo A;
1175        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1176          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          !!!cp (96);
1177          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1178            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1179          !!!next-input-character;          !!!next-input-character;
1180          redo A;          redo A;
1181        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1182          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1183          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1184              !!!cp (97);
1185            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1186                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1187            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1188          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1189            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1190            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1191                !!!cp (98);
1192              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1193              } else {
1194                ## NOTE: This state should never be reached.
1195                !!!cp (99);
1196            }            }
1197          } else {          } else {
1198            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1199          }          }
1200          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1201          ## reconsume          ## reconsume
1202    
1203          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1204    
1205          redo A;          redo A;
1206        } else {        } else {
1207          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1208            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1209          ## Stay in the state          ## Stay in the state
1210          !!!next-input-character;          !!!next-input-character;
1211          redo A;          redo A;
1212        }        }
1213      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1214        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1215          $self->{state} = 'before attribute name';          !!!cp (101);
1216            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1217          !!!next-input-character;          !!!next-input-character;
1218          redo A;          redo A;
1219        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1220          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          !!!cp (102);
1221          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1222            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1223          !!!next-input-character;          !!!next-input-character;
1224          redo A;          redo A;
1225        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1226          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1227          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1228              !!!cp (103);
1229            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1230                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1231            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1232          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1233            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1234            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1235                !!!cp (104);
1236              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1237              } else {
1238                ## NOTE: This state should never be reached.
1239                !!!cp (105);
1240            }            }
1241          } else {          } else {
1242            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1243          }          }
1244          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1245          ## reconsume          ## reconsume
1246    
1247          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1248    
1249          redo A;          redo A;
1250        } else {        } else {
1251          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1252            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1253          ## Stay in the state          ## Stay in the state
1254          !!!next-input-character;          !!!next-input-character;
1255          redo A;          redo A;
1256        }        }
1257      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1258        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1259            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1260            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1261            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1262            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1263          $self->{state} = 'before attribute name';          !!!cp (107);
1264          !!!next-input-character;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1265          redo A;          !!!next-input-character;
1266        } elsif ($self->{next_input_character} == 0x0026) { # &          redo A;
1267          $self->{last_attribute_value_state} = 'attribute value (unquoted)';        } elsif ($self->{next_char} == 0x0026) { # &
1268          $self->{state} = 'entity in attribute value';          !!!cp (108);
1269          !!!next-input-character;          $self->{last_attribute_value_state} = $self->{state};
1270          redo A;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1271        } elsif ($self->{next_input_character} == 0x003E) { # >          !!!next-input-character;
1272          if ($self->{current_token}->{type} eq 'start tag') {          redo A;
1273          } elsif ($self->{next_char} == 0x003E) { # >
1274            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1275              !!!cp (109);
1276            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1277                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1278            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1279          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1280            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1281            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1282                !!!cp (110);
1283              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1284              } else {
1285                ## NOTE: This state should never be reached.
1286                !!!cp (111);
1287            }            }
1288          } else {          } else {
1289            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1290          }          }
1291          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1292          !!!next-input-character;          !!!next-input-character;
1293    
1294          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1295    
1296          redo A;          redo A;
1297        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1298          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1299          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1300              !!!cp (112);
1301            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1302                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1303            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1304          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1305            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1306            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1307                !!!cp (113);
1308              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1309              } else {
1310                ## NOTE: This state should never be reached.
1311                !!!cp (114);
1312            }            }
1313          } else {          } else {
1314            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1315          }          }
1316          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1317          ## reconsume          ## reconsume
1318    
1319          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1320    
1321          redo A;          redo A;
1322        } else {        } else {
1323          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1324                 0x0022 => 1, # "
1325                 0x0027 => 1, # '
1326                 0x003D => 1, # =
1327                }->{$self->{next_char}}) {
1328              !!!cp (115);
1329              !!!parse-error (type => 'bad attribute value');
1330            } else {
1331              !!!cp (116);
1332            }
1333            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1334          ## Stay in the state          ## Stay in the state
1335          !!!next-input-character;          !!!next-input-character;
1336          redo A;          redo A;
1337        }        }
1338      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1339        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);        my $token = $self->_tokenize_attempt_to_consume_an_entity
1340              (1,
1341               $self->{last_attribute_value_state}
1342                 == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "
1343               $self->{last_attribute_value_state}
1344                 == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '
1345               -1);
1346    
1347        unless (defined $token) {        unless (defined $token) {
1348            !!!cp (117);
1349          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1350        } else {        } else {
1351            !!!cp (118);
1352          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1353            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1354          ## ISSUE: spec says "append the returned character token to the current attribute's value"          ## ISSUE: spec says "append the returned character token to the current attribute's value"
1355        }        }
1356    
1357        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1358        # next-input-character is already done        # next-input-character is already done
1359        redo A;        redo A;
1360      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1361          if ($self->{next_char} == 0x0009 or # HT
1362              $self->{next_char} == 0x000A or # LF
1363              $self->{next_char} == 0x000B or # VT
1364              $self->{next_char} == 0x000C or # FF
1365              $self->{next_char} == 0x0020) { # SP
1366            !!!cp (118);
1367            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1368            !!!next-input-character;
1369            redo A;
1370          } elsif ($self->{next_char} == 0x003E) { # >
1371            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1372              !!!cp (119);
1373              $self->{current_token}->{first_start_tag}
1374                  = not defined $self->{last_emitted_start_tag_name};
1375              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1376            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1377              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1378              if ($self->{current_token}->{attributes}) {
1379                !!!cp (120);
1380                !!!parse-error (type => 'end tag attribute');
1381              } else {
1382                ## NOTE: This state should never be reached.
1383                !!!cp (121);
1384              }
1385            } else {
1386              die "$0: $self->{current_token}->{type}: Unknown token type";
1387            }
1388            $self->{state} = DATA_STATE;
1389            !!!next-input-character;
1390    
1391            !!!emit ($self->{current_token}); # start tag or end tag
1392    
1393            redo A;
1394          } elsif ($self->{next_char} == 0x002F) { # /
1395            !!!next-input-character;
1396            if ($self->{next_char} == 0x003E and # >
1397                $self->{current_token}->{type} == START_TAG_TOKEN and
1398                $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
1399              # permitted slash
1400              !!!cp (122);
1401              #
1402            } else {
1403              !!!cp (123);
1404              !!!parse-error (type => 'nestc');
1405            }
1406            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1407            # next-input-character is already done
1408            redo A;
1409          } else {
1410            !!!cp (124);
1411            !!!parse-error (type => 'no space between attributes');
1412            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1413            ## reconsume
1414            redo A;
1415          }
1416        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1417        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1418                
1419        my $token = {type => 'comment', data => ''};        ## NOTE: Set by the previous state
1420          #my $token = {type => COMMENT_TOKEN, data => ''};
1421    
1422        BC: {        BC: {
1423          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
1424            $self->{state} = 'data';            !!!cp (124);
1425              $self->{state} = DATA_STATE;
1426            !!!next-input-character;            !!!next-input-character;
1427    
1428            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1429    
1430            redo A;            redo A;
1431          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_char} == -1) {
1432            $self->{state} = 'data';            !!!cp (125);
1433              $self->{state} = DATA_STATE;
1434            ## reconsume            ## reconsume
1435    
1436            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1437    
1438            redo A;            redo A;
1439          } else {          } else {
1440            $token->{data} .= chr ($self->{next_input_character});            !!!cp (126);
1441              $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1442            !!!next-input-character;            !!!next-input-character;
1443            redo BC;            redo BC;
1444          }          }
1445        } # BC        } # BC
1446      } elsif ($self->{state} eq 'markup declaration open') {  
1447          die "$0: _get_next_token: unexpected case [BC]";
1448        } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1449        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1450    
1451          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1452    
1453        my @next_char;        my @next_char;
1454        push @next_char, $self->{next_input_character};        push @next_char, $self->{next_char};
1455                
1456        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1457          !!!next-input-character;          !!!next-input-character;
1458          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1459          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1460            $self->{current_token} = {type => 'comment', data => ''};            !!!cp (127);
1461            $self->{state} = 'comment start';            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1462                                        line => $l, column => $c};
1463              $self->{state} = COMMENT_START_STATE;
1464            !!!next-input-character;            !!!next-input-character;
1465            redo A;            redo A;
1466            } else {
1467              !!!cp (128);
1468          }          }
1469        } elsif ($self->{next_input_character} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
1470                 $self->{next_input_character} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
1471          !!!next-input-character;          !!!next-input-character;
1472          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1473          if ($self->{next_input_character} == 0x004F or # O          if ($self->{next_char} == 0x004F or # O
1474              $self->{next_input_character} == 0x006F) { # o              $self->{next_char} == 0x006F) { # o
1475            !!!next-input-character;            !!!next-input-character;
1476            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
1477            if ($self->{next_input_character} == 0x0043 or # C            if ($self->{next_char} == 0x0043 or # C
1478                $self->{next_input_character} == 0x0063) { # c                $self->{next_char} == 0x0063) { # c
1479              !!!next-input-character;              !!!next-input-character;
1480              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
1481              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
1482                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
1483                !!!next-input-character;                !!!next-input-character;
1484                push @next_char, $self->{next_input_character};                push @next_char, $self->{next_char};
1485                if ($self->{next_input_character} == 0x0059 or # Y                if ($self->{next_char} == 0x0059 or # Y
1486                    $self->{next_input_character} == 0x0079) { # y                    $self->{next_char} == 0x0079) { # y
1487                  !!!next-input-character;                  !!!next-input-character;
1488                  push @next_char, $self->{next_input_character};                  push @next_char, $self->{next_char};
1489                  if ($self->{next_input_character} == 0x0050 or # P                  if ($self->{next_char} == 0x0050 or # P
1490                      $self->{next_input_character} == 0x0070) { # p                      $self->{next_char} == 0x0070) { # p
1491                    !!!next-input-character;                    !!!next-input-character;
1492                    push @next_char, $self->{next_input_character};                    push @next_char, $self->{next_char};
1493                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_char} == 0x0045 or # E
1494                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_char} == 0x0065) { # e
1495                      ## ISSUE: What a stupid code this is!                      !!!cp (129);
1496                      $self->{state} = 'DOCTYPE';                      ## TODO: What a stupid code this is!
1497                        $self->{state} = DOCTYPE_STATE;
1498                        $self->{current_token} = {type => DOCTYPE_TOKEN,
1499                                                  quirks => 1,
1500                                                  line => $l, column => $c};
1501                      !!!next-input-character;                      !!!next-input-character;
1502                      redo A;                      redo A;
1503                      } else {
1504                        !!!cp (130);
1505                    }                    }
1506                    } else {
1507                      !!!cp (131);
1508                  }                  }
1509                  } else {
1510                    !!!cp (132);
1511                }                }
1512                } else {
1513                  !!!cp (133);
1514              }              }
1515              } else {
1516                !!!cp (134);
1517            }            }
1518            } else {
1519              !!!cp (135);
1520          }          }
1521          } else {
1522            !!!cp (136);
1523        }        }
1524    
1525        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment');
1526        $self->{next_input_character} = shift @next_char;        $self->{next_char} = shift @next_char;
1527        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1528        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
1529          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1530                                    line => $l, column => $c};
1531        redo A;        redo A;
1532                
1533        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1534        ## ISSUE: spec is somewhat unclear on "is the first character that will be in the comment"; what is "that will be in the comment" is what the algorithm defines, isn't it?        ## ISSUE: spec is somewhat unclear on "is the first character that will be in the comment"; what is "that will be in the comment" is what the algorithm defines, isn't it?
1535      } elsif ($self->{state} eq 'comment start') {      } elsif ($self->{state} == COMMENT_START_STATE) {
1536        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1537          $self->{state} = 'comment start dash';          !!!cp (137);
1538            $self->{state} = COMMENT_START_DASH_STATE;
1539          !!!next-input-character;          !!!next-input-character;
1540          redo A;          redo A;
1541        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1542            !!!cp (138);
1543          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1544          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1545          !!!next-input-character;          !!!next-input-character;
1546    
1547          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1548    
1549          redo A;          redo A;
1550        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1551            !!!cp (139);
1552          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1553          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1554          ## reconsume          ## reconsume
1555    
1556          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1557    
1558          redo A;          redo A;
1559        } else {        } else {
1560            !!!cp (140);
1561          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1562              .= chr ($self->{next_input_character});              .= chr ($self->{next_char});
1563          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1564          !!!next-input-character;          !!!next-input-character;
1565          redo A;          redo A;
1566        }        }
1567      } elsif ($self->{state} eq 'comment start dash') {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
1568        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1569          $self->{state} = 'comment end';          !!!cp (141);
1570            $self->{state} = COMMENT_END_STATE;
1571          !!!next-input-character;          !!!next-input-character;
1572          redo A;          redo A;
1573        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1574            !!!cp (142);
1575          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1576          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1577          !!!next-input-character;          !!!next-input-character;
1578    
1579          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1580    
1581          redo A;          redo A;
1582        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1583            !!!cp (143);
1584          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1585          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1586          ## reconsume          ## reconsume
1587    
1588          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1589    
1590          redo A;          redo A;
1591        } else {        } else {
1592            !!!cp (144);
1593          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1594              .= '-' . chr ($self->{next_input_character});              .= '-' . chr ($self->{next_char});
1595          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1596          !!!next-input-character;          !!!next-input-character;
1597          redo A;          redo A;
1598        }        }
1599      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_STATE) {
1600        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1601          $self->{state} = 'comment end dash';          !!!cp (145);
1602            $self->{state} = COMMENT_END_DASH_STATE;
1603          !!!next-input-character;          !!!next-input-character;
1604          redo A;          redo A;
1605        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1606            !!!cp (146);
1607          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1608          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1609          ## reconsume          ## reconsume
1610    
1611          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1612    
1613          redo A;          redo A;
1614        } else {        } else {
1615          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (147);
1616            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1617          ## Stay in the state          ## Stay in the state
1618          !!!next-input-character;          !!!next-input-character;
1619          redo A;          redo A;
1620        }        }
1621      } elsif ($self->{state} eq 'comment end dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
1622        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1623          $self->{state} = 'comment end';          !!!cp (148);
1624            $self->{state} = COMMENT_END_STATE;
1625          !!!next-input-character;          !!!next-input-character;
1626          redo A;          redo A;
1627        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1628            !!!cp (149);
1629          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1630          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1631          ## reconsume          ## reconsume
1632    
1633          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1634    
1635          redo A;          redo A;
1636        } else {        } else {
1637          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
1638          $self->{state} = 'comment';          $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
1639            $self->{state} = COMMENT_STATE;
1640          !!!next-input-character;          !!!next-input-character;
1641          redo A;          redo A;
1642        }        }
1643      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
1644        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
1645          $self->{state} = 'data';          !!!cp (151);
1646            $self->{state} = DATA_STATE;
1647          !!!next-input-character;          !!!next-input-character;
1648    
1649          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1650    
1651          redo A;          redo A;
1652        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
1653            !!!cp (152);
1654          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment');
1655          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
1656          ## Stay in the state          ## Stay in the state
1657          !!!next-input-character;          !!!next-input-character;
1658          redo A;          redo A;
1659        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1660            !!!cp (153);
1661          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1662          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1663          ## reconsume          ## reconsume
1664    
1665          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1666    
1667          redo A;          redo A;
1668        } else {        } else {
1669            !!!cp (154);
1670          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment');
1671          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
1672          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1673          !!!next-input-character;          !!!next-input-character;
1674          redo A;          redo A;
1675        }        }
1676      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
1677        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1678            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1679            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1680            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1681            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1682          $self->{state} = 'before DOCTYPE name';          !!!cp (155);
1683            $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1684          !!!next-input-character;          !!!next-input-character;
1685          redo A;          redo A;
1686        } else {        } else {
1687            !!!cp (156);
1688          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
1689          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1690          ## reconsume          ## reconsume
1691          redo A;          redo A;
1692        }        }
1693      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
1694        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1695            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1696            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1697            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1698            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1699            !!!cp (157);
1700          ## Stay in the state          ## Stay in the state
1701          !!!next-input-character;          !!!next-input-character;
1702          redo A;          redo A;
1703        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1704            !!!cp (158);
1705          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1706          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1707          !!!next-input-character;          !!!next-input-character;
1708    
1709          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
1710    
1711          redo A;          redo A;
1712        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1713            !!!cp (159);
1714          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1715          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1716          ## reconsume          ## reconsume
1717    
1718          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
1719    
1720          redo A;          redo A;
1721        } else {        } else {
1722          $self->{current_token}          !!!cp (160);
1723              = {type => 'DOCTYPE',          $self->{current_token}->{name} = chr $self->{next_char};
1724                 name => chr ($self->{next_input_character}),          delete $self->{current_token}->{quirks};
                correct => 1};  
1725  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
1726          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
1727          !!!next-input-character;          !!!next-input-character;
1728          redo A;          redo A;
1729        }        }
1730      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
1731  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
1732        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1733            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1734            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1735            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1736            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1737          $self->{state} = 'after DOCTYPE name';          !!!cp (161);
1738            $self->{state} = AFTER_DOCTYPE_NAME_STATE;
1739          !!!next-input-character;          !!!next-input-character;
1740          redo A;          redo A;
1741        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1742          $self->{state} = 'data';          !!!cp (162);
1743            $self->{state} = DATA_STATE;
1744          !!!next-input-character;          !!!next-input-character;
1745    
1746          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1747    
1748          redo A;          redo A;
1749        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1750            !!!cp (163);
1751          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1752          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1753          ## reconsume          ## reconsume
1754    
1755          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1756          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1757    
1758          redo A;          redo A;
1759        } else {        } else {
1760            !!!cp (164);
1761          $self->{current_token}->{name}          $self->{current_token}->{name}
1762            .= chr ($self->{next_input_character}); # DOCTYPE            .= chr ($self->{next_char}); # DOCTYPE
1763          ## Stay in the state          ## Stay in the state
1764          !!!next-input-character;          !!!next-input-character;
1765          redo A;          redo A;
1766        }        }
1767      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
1768        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1769            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1770            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1771            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1772            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1773            !!!cp (165);
1774          ## Stay in the state          ## Stay in the state
1775          !!!next-input-character;          !!!next-input-character;
1776          redo A;          redo A;
1777        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1778          $self->{state} = 'data';          !!!cp (166);
1779            $self->{state} = DATA_STATE;
1780          !!!next-input-character;          !!!next-input-character;
1781    
1782          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1783    
1784          redo A;          redo A;
1785        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1786            !!!cp (167);
1787          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1788          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1789          ## reconsume          ## reconsume
1790    
1791          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1792          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1793    
1794          redo A;          redo A;
1795        } elsif ($self->{next_input_character} == 0x0050 or # P        } elsif ($self->{next_char} == 0x0050 or # P
1796                 $self->{next_input_character} == 0x0070) { # p                 $self->{next_char} == 0x0070) { # p
1797          !!!next-input-character;          !!!next-input-character;
1798          if ($self->{next_input_character} == 0x0055 or # U          if ($self->{next_char} == 0x0055 or # U
1799              $self->{next_input_character} == 0x0075) { # u              $self->{next_char} == 0x0075) { # u
1800            !!!next-input-character;            !!!next-input-character;
1801            if ($self->{next_input_character} == 0x0042 or # B            if ($self->{next_char} == 0x0042 or # B
1802                $self->{next_input_character} == 0x0062) { # b                $self->{next_char} == 0x0062) { # b
1803              !!!next-input-character;              !!!next-input-character;
1804              if ($self->{next_input_character} == 0x004C or # L              if ($self->{next_char} == 0x004C or # L
1805                  $self->{next_input_character} == 0x006C) { # l                  $self->{next_char} == 0x006C) { # l
1806                !!!next-input-character;                !!!next-input-character;
1807                if ($self->{next_input_character} == 0x0049 or # I                if ($self->{next_char} == 0x0049 or # I
1808                    $self->{next_input_character} == 0x0069) { # i                    $self->{next_char} == 0x0069) { # i
1809                  !!!next-input-character;                  !!!next-input-character;
1810                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_char} == 0x0043 or # C
1811                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_char} == 0x0063) { # c
1812                    $self->{state} = 'before DOCTYPE public identifier';                    !!!cp (168);
1813                      $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1814                    !!!next-input-character;                    !!!next-input-character;
1815                    redo A;                    redo A;
1816                    } else {
1817                      !!!cp (169);
1818                  }                  }
1819                  } else {
1820                    !!!cp (170);
1821                }                }
1822                } else {
1823                  !!!cp (171);
1824              }              }
1825              } else {
1826                !!!cp (172);
1827            }            }
1828            } else {
1829              !!!cp (173);
1830          }          }
1831    
1832          #          #
1833        } elsif ($self->{next_input_character} == 0x0053 or # S        } elsif ($self->{next_char} == 0x0053 or # S
1834                 $self->{next_input_character} == 0x0073) { # s                 $self->{next_char} == 0x0073) { # s
1835          !!!next-input-character;          !!!next-input-character;
1836          if ($self->{next_input_character} == 0x0059 or # Y          if ($self->{next_char} == 0x0059 or # Y
1837              $self->{next_input_character} == 0x0079) { # y              $self->{next_char} == 0x0079) { # y
1838            !!!next-input-character;            !!!next-input-character;
1839            if ($self->{next_input_character} == 0x0053 or # S            if ($self->{next_char} == 0x0053 or # S
1840                $self->{next_input_character} == 0x0073) { # s                $self->{next_char} == 0x0073) { # s
1841              !!!next-input-character;              !!!next-input-character;
1842              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
1843                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
1844                !!!next-input-character;                !!!next-input-character;
1845                if ($self->{next_input_character} == 0x0045 or # E                if ($self->{next_char} == 0x0045 or # E
1846                    $self->{next_input_character} == 0x0065) { # e                    $self->{next_char} == 0x0065) { # e
1847                  !!!next-input-character;                  !!!next-input-character;
1848                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_char} == 0x004D or # M
1849                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_char} == 0x006D) { # m
1850                    $self->{state} = 'before DOCTYPE system identifier';                    !!!cp (174);
1851                      $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1852                    !!!next-input-character;                    !!!next-input-character;
1853                    redo A;                    redo A;
1854                    } else {
1855                      !!!cp (175);
1856                  }                  }
1857                  } else {
1858                    !!!cp (176);
1859                }                }
1860                } else {
1861                  !!!cp (177);
1862              }              }
1863              } else {
1864                !!!cp (178);
1865            }            }
1866            } else {
1867              !!!cp (179);
1868          }          }
1869    
1870          #          #
1871        } else {        } else {
1872            !!!cp (180);
1873          !!!next-input-character;          !!!next-input-character;
1874          #          #
1875        }        }
1876    
1877        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
1878        $self->{state} = 'bogus DOCTYPE';        $self->{current_token}->{quirks} = 1;
1879    
1880          $self->{state} = BOGUS_DOCTYPE_STATE;
1881        # next-input-character is already done        # next-input-character is already done
1882        redo A;        redo A;
1883      } elsif ($self->{state} eq 'before DOCTYPE public identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1884        if ({        if ({
1885              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1886              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
1887            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
1888            !!!cp (181);
1889          ## Stay in the state          ## Stay in the state
1890          !!!next-input-character;          !!!next-input-character;
1891          redo A;          redo A;
1892        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_char} eq 0x0022) { # "
1893            !!!cp (182);
1894          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1895          $self->{state} = 'DOCTYPE public identifier (double-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
1896          !!!next-input-character;          !!!next-input-character;
1897          redo A;          redo A;
1898        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_char} eq 0x0027) { # '
1899            !!!cp (183);
1900          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1901          $self->{state} = 'DOCTYPE public identifier (single-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
1902          !!!next-input-character;          !!!next-input-character;
1903          redo A;          redo A;
1904        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_char} eq 0x003E) { # >
1905            !!!cp (184);
1906          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
1907    
1908          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1909          !!!next-input-character;          !!!next-input-character;
1910    
1911          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1912          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1913    
1914          redo A;          redo A;
1915        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1916            !!!cp (185);
1917          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1918    
1919          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1920          ## reconsume          ## reconsume
1921    
1922          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1923          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1924    
1925          redo A;          redo A;
1926        } else {        } else {
1927            !!!cp (186);
1928          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
1929          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
1930    
1931            $self->{state} = BOGUS_DOCTYPE_STATE;
1932          !!!next-input-character;          !!!next-input-character;
1933          redo A;          redo A;
1934        }        }
1935      } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1936        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1937          $self->{state} = 'after DOCTYPE public identifier';          !!!cp (187);
1938            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1939          !!!next-input-character;          !!!next-input-character;
1940          redo A;          redo A;
1941        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
1942            !!!cp (188);
1943          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1944    
1945          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1946            !!!next-input-character;
1947    
1948            $self->{current_token}->{quirks} = 1;
1949            !!!emit ($self->{current_token}); # DOCTYPE
1950    
1951            redo A;
1952          } elsif ($self->{next_char} == -1) {
1953            !!!cp (189);
1954            !!!parse-error (type => 'unclosed PUBLIC literal');
1955    
1956            $self->{state} = DATA_STATE;
1957          ## reconsume          ## reconsume
1958    
1959          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1960          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1961    
1962          redo A;          redo A;
1963        } else {        } else {
1964            !!!cp (190);
1965          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
1966              .= chr $self->{next_input_character};              .= chr $self->{next_char};
1967          ## Stay in the state          ## Stay in the state
1968          !!!next-input-character;          !!!next-input-character;
1969          redo A;          redo A;
1970        }        }
1971      } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
1972        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1973          $self->{state} = 'after DOCTYPE public identifier';          !!!cp (191);
1974            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1975            !!!next-input-character;
1976            redo A;
1977          } elsif ($self->{next_char} == 0x003E) { # >
1978            !!!cp (192);
1979            !!!parse-error (type => 'unclosed PUBLIC literal');
1980    
1981            $self->{state} = DATA_STATE;
1982          !!!next-input-character;          !!!next-input-character;
1983    
1984            $self->{current_token}->{quirks} = 1;
1985            !!!emit ($self->{current_token}); # DOCTYPE
1986    
1987          redo A;          redo A;
1988        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1989            !!!cp (193);
1990          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1991    
1992          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1993          ## reconsume          ## reconsume
1994    
1995          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1996          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1997    
1998          redo A;          redo A;
1999        } else {        } else {
2000            !!!cp (194);
2001          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2002              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2003          ## Stay in the state          ## Stay in the state
2004          !!!next-input-character;          !!!next-input-character;
2005          redo A;          redo A;
2006        }        }
2007      } elsif ($self->{state} eq 'after DOCTYPE public identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2008        if ({        if ({
2009              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2010              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2011            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2012            !!!cp (195);
2013          ## Stay in the state          ## Stay in the state
2014          !!!next-input-character;          !!!next-input-character;
2015          redo A;          redo A;
2016        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2017            !!!cp (196);
2018          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2019          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2020          !!!next-input-character;          !!!next-input-character;
2021          redo A;          redo A;
2022        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2023            !!!cp (197);
2024          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2025          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2026          !!!next-input-character;          !!!next-input-character;
2027          redo A;          redo A;
2028        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2029          $self->{state} = 'data';          !!!cp (198);
2030            $self->{state} = DATA_STATE;
2031          !!!next-input-character;          !!!next-input-character;
2032    
2033          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2034    
2035          redo A;          redo A;
2036        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2037            !!!cp (199);
2038          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2039    
2040          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2041          ## reconsume          ## reconsume
2042    
2043          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2044          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2045    
2046          redo A;          redo A;
2047        } else {        } else {
2048            !!!cp (200);
2049          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
2050          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2051    
2052            $self->{state} = BOGUS_DOCTYPE_STATE;
2053          !!!next-input-character;          !!!next-input-character;
2054          redo A;          redo A;
2055        }        }
2056      } elsif ($self->{state} eq 'before DOCTYPE system identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2057        if ({        if ({
2058              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2059              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2060            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2061            !!!cp (201);
2062          ## Stay in the state          ## Stay in the state
2063          !!!next-input-character;          !!!next-input-character;
2064          redo A;          redo A;
2065        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2066            !!!cp (202);
2067          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2068          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2069          !!!next-input-character;          !!!next-input-character;
2070          redo A;          redo A;
2071        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2072            !!!cp (203);
2073          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2074          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2075          !!!next-input-character;          !!!next-input-character;
2076          redo A;          redo A;
2077        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2078            !!!cp (204);
2079          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
2080          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2081          !!!next-input-character;          !!!next-input-character;
2082    
2083          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2084          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2085    
2086          redo A;          redo A;
2087        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2088            !!!cp (205);
2089          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2090    
2091          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2092          ## reconsume          ## reconsume
2093    
2094          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2095          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2096    
2097          redo A;          redo A;
2098        } else {        } else {
2099            !!!cp (206);
2100          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
2101          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2102    
2103            $self->{state} = BOGUS_DOCTYPE_STATE;
2104          !!!next-input-character;          !!!next-input-character;
2105          redo A;          redo A;
2106        }        }
2107      } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2108        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2109          $self->{state} = 'after DOCTYPE system identifier';          !!!cp (207);
2110            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2111          !!!next-input-character;          !!!next-input-character;
2112          redo A;          redo A;
2113        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2114            !!!cp (208);
2115            !!!parse-error (type => 'unclosed PUBLIC literal');
2116    
2117            $self->{state} = DATA_STATE;
2118            !!!next-input-character;
2119    
2120            $self->{current_token}->{quirks} = 1;
2121            !!!emit ($self->{current_token}); # DOCTYPE
2122    
2123            redo A;
2124          } elsif ($self->{next_char} == -1) {
2125            !!!cp (209);
2126          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2127    
2128          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2129          ## reconsume          ## reconsume
2130    
2131          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2132          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2133    
2134          redo A;          redo A;
2135        } else {        } else {
2136            !!!cp (210);
2137          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2138              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2139          ## Stay in the state          ## Stay in the state
2140          !!!next-input-character;          !!!next-input-character;
2141          redo A;          redo A;
2142        }        }
2143      } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2144        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2145          $self->{state} = 'after DOCTYPE system identifier';          !!!cp (211);
2146            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2147            !!!next-input-character;
2148            redo A;
2149          } elsif ($self->{next_char} == 0x003E) { # >
2150            !!!cp (212);
2151            !!!parse-error (type => 'unclosed PUBLIC literal');
2152    
2153            $self->{state} = DATA_STATE;
2154          !!!next-input-character;          !!!next-input-character;
2155    
2156            $self->{current_token}->{quirks} = 1;
2157            !!!emit ($self->{current_token}); # DOCTYPE
2158    
2159          redo A;          redo A;
2160        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2161            !!!cp (213);
2162          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2163    
2164          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2165          ## reconsume          ## reconsume
2166    
2167          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2168          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2169    
2170          redo A;          redo A;
2171        } else {        } else {
2172            !!!cp (214);
2173          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2174              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2175          ## Stay in the state          ## Stay in the state
2176          !!!next-input-character;          !!!next-input-character;
2177          redo A;          redo A;
2178        }        }
2179      } elsif ($self->{state} eq 'after DOCTYPE system identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2180        if ({        if ({
2181              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2182              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2183            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2184            !!!cp (215);
2185          ## Stay in the state          ## Stay in the state
2186          !!!next-input-character;          !!!next-input-character;
2187          redo A;          redo A;
2188        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2189          $self->{state} = 'data';          !!!cp (216);
2190            $self->{state} = DATA_STATE;
2191          !!!next-input-character;          !!!next-input-character;
2192    
2193          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2194    
2195          redo A;          redo A;
2196        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2197            !!!cp (217);
2198          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2199    
2200          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2201          ## reconsume          ## reconsume
2202    
2203          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2204          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2205    
2206          redo A;          redo A;
2207        } else {        } else {
2208            !!!cp (218);
2209          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
2210          $self->{state} = 'bogus DOCTYPE';          #$self->{current_token}->{quirks} = 1;
2211    
2212            $self->{state} = BOGUS_DOCTYPE_STATE;
2213          !!!next-input-character;          !!!next-input-character;
2214          redo A;          redo A;
2215        }        }
2216      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2217        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2218          $self->{state} = 'data';          !!!cp (219);
2219            $self->{state} = DATA_STATE;
2220          !!!next-input-character;          !!!next-input-character;
2221    
         delete $self->{current_token}->{correct};  
2222          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2223    
2224          redo A;          redo A;
2225        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2226            !!!cp (220);
2227          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2228          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2229          ## reconsume          ## reconsume
2230    
         delete $self->{current_token}->{correct};  
2231          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2232    
2233          redo A;          redo A;
2234        } else {        } else {
2235            !!!cp (221);
2236          ## Stay in the state          ## Stay in the state
2237          !!!next-input-character;          !!!next-input-character;
2238          redo A;          redo A;
# Line 1609  sub _get_next_token ($) { Line 2245  sub _get_next_token ($) {
2245    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
2246  } # _get_next_token  } # _get_next_token
2247    
2248  sub _tokenize_attempt_to_consume_an_entity ($$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2249    my ($self, $in_attr) = @_;    my ($self, $in_attr, $additional) = @_;
2250    
2251      my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2252    
2253    if ({    if ({
2254         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2255         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
2256        }->{$self->{next_input_character}}) {         $additional => 1,
2257          }->{$self->{next_char}}) {
2258        !!!cp (1001);
2259      ## Don't consume      ## Don't consume
2260      ## No error      ## No error
2261      return undef;      return undef;
2262    } elsif ($self->{next_input_character} == 0x0023) { # #    } elsif ($self->{next_char} == 0x0023) { # #
2263      !!!next-input-character;      !!!next-input-character;
2264      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_char} == 0x0078 or # x
2265          $self->{next_input_character} == 0x0058) { # X          $self->{next_char} == 0x0058) { # X
2266        my $code;        my $code;
2267        X: {        X: {
2268          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_char};
2269          !!!next-input-character;          !!!next-input-character;
2270          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_char} and
2271              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_char} <= 0x0039) { # 0..9
2272              !!!cp (1002);
2273            $code ||= 0;            $code ||= 0;
2274            $code *= 0x10;            $code *= 0x10;
2275            $code += $self->{next_input_character} - 0x0030;            $code += $self->{next_char} - 0x0030;
2276            redo X;            redo X;
2277          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
2278                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_char} <= 0x0066) { # a..f
2279              !!!cp (1003);
2280            $code ||= 0;            $code ||= 0;
2281            $code *= 0x10;            $code *= 0x10;
2282            $code += $self->{next_input_character} - 0x0060 + 9;            $code += $self->{next_char} - 0x0060 + 9;
2283            redo X;            redo X;
2284          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
2285                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_char} <= 0x0046) { # A..F
2286              !!!cp (1004);
2287            $code ||= 0;            $code ||= 0;
2288            $code *= 0x10;            $code *= 0x10;
2289            $code += $self->{next_input_character} - 0x0040 + 9;            $code += $self->{next_char} - 0x0040 + 9;
2290            redo X;            redo X;
2291          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2292            !!!parse-error (type => 'bare hcro');            !!!cp (1005);
2293            !!!back-next-input-character ($x_char, $self->{next_input_character});            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2294            $self->{next_input_character} = 0x0023; # #            !!!back-next-input-character ($x_char, $self->{next_char});
2295              $self->{next_char} = 0x0023; # #
2296            return undef;            return undef;
2297          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_char} == 0x003B) { # ;
2298              !!!cp (1006);
2299            !!!next-input-character;            !!!next-input-character;
2300          } else {          } else {
2301            !!!parse-error (type => 'no refc');            !!!cp (1007);
2302              !!!parse-error (type => 'no refc', line => $l, column => $c);
2303          }          }
2304    
2305          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2306            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!cp (1008);
2307              !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2308            $code = 0xFFFD;            $code = 0xFFFD;
2309          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2310            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!cp (1009);
2311              !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2312            $code = 0xFFFD;            $code = 0xFFFD;
2313          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2314            !!!parse-error (type => 'CR character reference');            !!!cp (1010);
2315              !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2316            $code = 0x000A;            $code = 0x000A;
2317          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2318            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!cp (1011);
2319              !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2320            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2321          }          }
2322    
2323          return {type => 'character', data => chr $code};          return {type => CHARACTER_TOKEN, data => chr $code,
2324                    has_reference => 1, line => $l, column => $c};
2325        } # X        } # X
2326      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_char} and
2327               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
2328        my $code = $self->{next_input_character} - 0x0030;        my $code = $self->{next_char} - 0x0030;
2329        !!!next-input-character;        !!!next-input-character;
2330                
2331        while (0x0030 <= $self->{next_input_character} and        while (0x0030 <= $self->{next_char} and
2332                  $self->{next_input_character} <= 0x0039) { # 0..9                  $self->{next_char} <= 0x0039) { # 0..9
2333            !!!cp (1012);
2334          $code *= 10;          $code *= 10;
2335          $code += $self->{next_input_character} - 0x0030;          $code += $self->{next_char} - 0x0030;
2336                    
2337          !!!next-input-character;          !!!next-input-character;
2338        }        }
2339    
2340        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{next_char} == 0x003B) { # ;
2341            !!!cp (1013);
2342          !!!next-input-character;          !!!next-input-character;
2343        } else {        } else {
2344          !!!parse-error (type => 'no refc');          !!!cp (1014);
2345            !!!parse-error (type => 'no refc', line => $l, column => $c);
2346        }        }
2347    
2348        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2349          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!cp (1015);
2350            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2351          $code = 0xFFFD;          $code = 0xFFFD;
2352        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
2353          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!cp (1016);
2354            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2355          $code = 0xFFFD;          $code = 0xFFFD;
2356        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
2357          !!!parse-error (type => 'CR character reference');          !!!cp (1017);
2358            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2359          $code = 0x000A;          $code = 0x000A;
2360        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2361          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!cp (1018);
2362            !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2363          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2364        }        }
2365                
2366        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
2367                  line => $l, column => $c};
2368      } else {      } else {
2369        !!!parse-error (type => 'bare nero');        !!!cp (1019);
2370        !!!back-next-input-character ($self->{next_input_character});        !!!parse-error (type => 'bare nero', line => $l, column => $c);
2371        $self->{next_input_character} = 0x0023; # #        !!!back-next-input-character ($self->{next_char});
2372          $self->{next_char} = 0x0023; # #
2373        return undef;        return undef;
2374      }      }
2375    } elsif ((0x0041 <= $self->{next_input_character} and    } elsif ((0x0041 <= $self->{next_char} and
2376              $self->{next_input_character} <= 0x005A) or              $self->{next_char} <= 0x005A) or
2377             (0x0061 <= $self->{next_input_character} and             (0x0061 <= $self->{next_char} and
2378              $self->{next_input_character} <= 0x007A)) {              $self->{next_char} <= 0x007A)) {
2379      my $entity_name = chr $self->{next_input_character};      my $entity_name = chr $self->{next_char};
2380      !!!next-input-character;      !!!next-input-character;
2381    
2382      my $value = $entity_name;      my $value = $entity_name;
# Line 1726  sub _tokenize_attempt_to_consume_an_enti Line 2386  sub _tokenize_attempt_to_consume_an_enti
2386    
2387      while (length $entity_name < 10 and      while (length $entity_name < 10 and
2388             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
2389             ((0x0041 <= $self->{next_input_character} and # a             ((0x0041 <= $self->{next_char} and # a
2390               $self->{next_input_character} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
2391              (0x0061 <= $self->{next_input_character} and # a              (0x0061 <= $self->{next_char} and # a
2392               $self->{next_input_character} <= 0x007A) or # z               $self->{next_char} <= 0x007A) or # z
2393              (0x0030 <= $self->{next_input_character} and # 0              (0x0030 <= $self->{next_char} and # 0
2394               $self->{next_input_character} <= 0x0039) or # 9               $self->{next_char} <= 0x0039) or # 9
2395              $self->{next_input_character} == 0x003B)) { # ;              $self->{next_char} == 0x003B)) { # ;
2396        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_char};
2397        if (defined $EntityChar->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
2398          if ($self->{next_input_character} == 0x003B) { # ;          if ($self->{next_char} == 0x003B) { # ;
2399              !!!cp (1020);
2400            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
2401            $match = 1;            $match = 1;
2402            !!!next-input-character;            !!!next-input-character;
2403            last;            last;
2404          } else {          } else {
2405              !!!cp (1021);
2406            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
2407            $match = -1;            $match = -1;
2408            !!!next-input-character;            !!!next-input-character;
2409          }          }
2410        } else {        } else {
2411          $value .= chr $self->{next_input_character};          !!!cp (1022);
2412            $value .= chr $self->{next_char};
2413          $match *= 2;          $match *= 2;
2414          !!!next-input-character;          !!!next-input-character;
2415        }        }
2416      }      }
2417            
2418      if ($match > 0) {      if ($match > 0) {
2419        return {type => 'character', data => $value};        !!!cp (1023);
2420          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2421                  line => $l, column => $c};
2422      } elsif ($match < 0) {      } elsif ($match < 0) {
2423        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc', line => $l, column => $c);
2424        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
2425          return {type => 'character', data => '&'.$entity_name};          !!!cp (1024);
2426        } else {          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
2427          return {type => 'character', data => $value};                  line => $l, column => $c};
2428          } else {
2429            !!!cp (1025);
2430            return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2431                    line => $l, column => $c};
2432        }        }
2433      } else {      } else {
2434        !!!parse-error (type => 'bare ero');        !!!cp (1026);
2435        ## NOTE: No characters are consumed in the spec.        !!!parse-error (type => 'bare ero', line => $l, column => $c);
2436        return {type => 'character', data => '&'.$value};        ## NOTE: "No characters are consumed" in the spec.
2437          return {type => CHARACTER_TOKEN, data => '&'.$value,
2438                  line => $l, column => $c};
2439      }      }
2440    } else {    } else {
2441        !!!cp (1027);
2442      ## no characters are consumed      ## no characters are consumed
2443      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
2444      return undef;      return undef;
2445    }    }
2446  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 1806  sub _construct_tree ($) { Line 2478  sub _construct_tree ($) {
2478        
2479    !!!next-token;    !!!next-token;
2480    
   $self->{insertion_mode} = 'before head';  
2481    undef $self->{form_element};    undef $self->{form_element};
2482    undef $self->{head_element};    undef $self->{head_element};
2483    $self->{open_elements} = [];    $self->{open_elements} = [];
2484    undef $self->{inner_html_node};    undef $self->{inner_html_node};
2485    
2486      ## NOTE: The "initial" insertion mode.
2487    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
2488    
2489      ## NOTE: The "before html" insertion mode.
2490    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
2491      $self->{insertion_mode} = BEFORE_HEAD_IM;
2492    
2493      ## NOTE: The "before head" insertion mode and so on.
2494    $self->_tree_construction_main;    $self->_tree_construction_main;
2495  } # _construct_tree  } # _construct_tree
2496    
2497  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
2498    my $self = shift;    my $self = shift;
2499    
2500      ## NOTE: "initial" insertion mode
2501    
2502    INITIAL: {    INITIAL: {
2503      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
2504        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
2505        ## error, switch to a conformance checking mode for another        ## error, switch to a conformance checking mode for another
2506        ## language.        ## language.
# Line 1830  sub _tree_construction_initial ($) { Line 2510  sub _tree_construction_initial ($) {
2510        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
2511            defined $token->{public_identifier} or            defined $token->{public_identifier} or
2512            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
2513          !!!parse-error (type => 'not HTML5');          !!!cp ('t1');
2514            !!!parse-error (type => 'not HTML5', token => $token);
2515        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
2516            !!!cp ('t2');
2517          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
2518          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
2519          } else {
2520            !!!cp ('t3');
2521        }        }
2522                
2523        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
# Line 1846  sub _tree_construction_initial ($) { Line 2530  sub _tree_construction_initial ($) {
2530        ## ISSUE: internalSubset = null??        ## ISSUE: internalSubset = null??
2531        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
2532                
2533        if (not $token->{correct} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
2534            !!!cp ('t4');
2535          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
2536        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
2537          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
# Line 1900  sub _tree_construction_initial ($) { Line 2585  sub _tree_construction_initial ($) {
2585            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,
2586            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,
2587            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,
2588              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,
2589              "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,
2590              "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,
2591            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,
2592            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,
2593            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,
# Line 1922  sub _tree_construction_initial ($) { Line 2610  sub _tree_construction_initial ($) {
2610            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,
2611            "HTML" => 1,            "HTML" => 1,
2612          }->{$pubid}) {          }->{$pubid}) {
2613              !!!cp ('t5');
2614            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
2615          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or
2616                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {
2617            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
2618                !!!cp ('t6');
2619              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
2620            } else {            } else {
2621                !!!cp ('t7');
2622              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
2623            }            }
2624          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or
2625                   $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {
2626              !!!cp ('t8');
2627            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
2628            } else {
2629              !!!cp ('t9');
2630          }          }
2631          } else {
2632            !!!cp ('t10');
2633        }        }
2634        if (defined $token->{system_identifier}) {        if (defined $token->{system_identifier}) {
2635          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
2636          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
2637          if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {          if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
2638              ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"
2639            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
2640              !!!cp ('t11');
2641            } else {
2642              !!!cp ('t12');
2643          }          }
2644          } else {
2645            !!!cp ('t13');
2646        }        }
2647                
2648        ## Go to the root element phase.        ## Go to the "before html" insertion mode.
2649        !!!next-token;        !!!next-token;
2650        return;        return;
2651      } elsif ({      } elsif ({
2652                'start tag' => 1,                START_TAG_TOKEN, 1,
2653                'end tag' => 1,                END_TAG_TOKEN, 1,
2654                'end-of-file' => 1,                END_OF_FILE_TOKEN, 1,
2655               }->{$token->{type}}) {               }->{$token->{type}}) {
2656        !!!parse-error (type => 'no DOCTYPE');        !!!cp ('t14');
2657          !!!parse-error (type => 'no DOCTYPE', token => $token);
2658        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2659        ## Go to the root element phase        ## Go to the "before html" insertion mode.
2660        ## reprocess        ## reprocess
2661        return;        return;
2662      } elsif ($token->{type} eq 'character') {      } elsif ($token->{type} == CHARACTER_TOKEN) {
2663        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2664          ## Ignore the token          ## Ignore the token
2665    
2666          unless (length $token->{data}) {          unless (length $token->{data}) {
2667            ## Stay in the phase            !!!cp ('t15');
2668              ## Stay in the insertion mode.
2669            !!!next-token;            !!!next-token;
2670            redo INITIAL;            redo INITIAL;
2671            } else {
2672              !!!cp ('t16');
2673          }          }
2674          } else {
2675            !!!cp ('t17');
2676        }        }
2677    
2678        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
2679        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2680        ## Go to the root element phase        ## Go to the "before html" insertion mode.
2681        ## reprocess        ## reprocess
2682        return;        return;
2683      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
2684          !!!cp ('t18');
2685        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2686        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
2687                
2688        ## Stay in the phase.        ## Stay in the insertion mode.
2689        !!!next-token;        !!!next-token;
2690        redo INITIAL;        redo INITIAL;
2691      } else {      } else {
2692        die "$0: $token->{type}: Unknown token";        die "$0: $token->{type}: Unknown token type";
2693      }      }
2694    } # INITIAL    } # INITIAL
2695    
2696      die "$0: _tree_construction_initial: This should be never reached";
2697  } # _tree_construction_initial  } # _tree_construction_initial
2698    
2699  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
2700    my $self = shift;    my $self = shift;
2701    
2702      ## NOTE: "before html" insertion mode.
2703        
2704    B: {    B: {
2705        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
2706          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
2707            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
2708          ## Ignore the token          ## Ignore the token
2709          ## Stay in the phase          ## Stay in the insertion mode.
2710          !!!next-token;          !!!next-token;
2711          redo B;          redo B;
2712        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
2713            !!!cp ('t20');
2714          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
2715          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2716          ## Stay in the phase          ## Stay in the insertion mode.
2717          !!!next-token;          !!!next-token;
2718          redo B;          redo B;
2719        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
2720          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2721            ## Ignore the token.            ## Ignore the token.
2722    
2723            unless (length $token->{data}) {            unless (length $token->{data}) {
2724              ## Stay in the phase              !!!cp ('t21');
2725                ## Stay in the insertion mode.
2726              !!!next-token;              !!!next-token;
2727              redo B;              redo B;
2728              } else {
2729                !!!cp ('t22');
2730            }            }
2731            } else {
2732              !!!cp ('t23');
2733          }          }
2734    
2735            $self->{application_cache_selection}->(undef);
2736    
2737          #          #
2738          } elsif ($token->{type} == START_TAG_TOKEN) {
2739            if ($token->{tag_name} eq 'html') {
2740              my $root_element;
2741              !!!create-element ($root_element, $token->{tag_name}, $token->{attributes});
2742              $self->{document}->append_child ($root_element);
2743              push @{$self->{open_elements}}, [$root_element, 'html'];
2744    
2745              if ($token->{attributes}->{manifest}) {
2746                !!!cp ('t24');
2747                $self->{application_cache_selection}
2748                    ->($token->{attributes}->{manifest}->{value});
2749                ## ISSUE: No relative reference resolution?
2750              } else {
2751                !!!cp ('t25');
2752                $self->{application_cache_selection}->(undef);
2753              }
2754    
2755              !!!next-token;
2756              return; ## Go to the "before head" insertion mode.
2757            } else {
2758              !!!cp ('t25.1');
2759              #
2760            }
2761        } elsif ({        } elsif ({
2762                  'start tag' => 1,                  END_TAG_TOKEN, 1,
2763                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
2764                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2765          ## ISSUE: There is an issue in the spec          !!!cp ('t26');
2766          #          #
2767        } else {        } else {
2768          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
2769        }        }
2770        my $root_element; !!!create-element ($root_element, 'html');  
2771        $self->{document}->append_child ($root_element);      my $root_element; !!!create-element ($root_element, 'html');
2772        push @{$self->{open_elements}}, [$root_element, 'html'];      $self->{document}->append_child ($root_element);
2773        ## reprocess      push @{$self->{open_elements}}, [$root_element, 'html'];
2774        #redo B;  
2775        return; ## Go to the main phase.      $self->{application_cache_selection}->(undef);
2776    
2777        ## NOTE: Reprocess the token.
2778        return; ## Go to the "before head" insertion mode.
2779    
2780        ## ISSUE: There is an issue in the spec
2781    } # B    } # B
2782    
2783      die "$0: _tree_construction_root_element: This should never be reached";
2784  } # _tree_construction_root_element  } # _tree_construction_root_element
2785    
2786  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 2043  sub _reset_insertion_mode ($) { Line 2795  sub _reset_insertion_mode ($) {
2795            
2796      ## Step 3      ## Step 3
2797      S3: {      S3: {
       ## ISSUE: Oops! "If node is the first node in the stack of open  
       ## elements, then set last to true. If the context element of the  
       ## HTML fragment parsing algorithm is neither a td element nor a  
       ## th element, then set node to the context element. (fragment case)":  
       ## The second "if" is in the scope of the first "if"!?  
2798        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
2799          $last = 1;          $last = 1;
2800          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
2801            if ($self->{inner_html_node}->[1] eq 'td' or            if ($self->{inner_html_node}->[1] eq 'td' or
2802                $self->{inner_html_node}->[1] eq 'th') {                $self->{inner_html_node}->[1] eq 'th') {
2803                !!!cp ('t27');
2804              #              #
2805            } else {            } else {
2806                !!!cp ('t28');
2807              $node = $self->{inner_html_node};              $node = $self->{inner_html_node};
2808            }            }
2809          }          }
# Line 2062  sub _reset_insertion_mode ($) { Line 2811  sub _reset_insertion_mode ($) {
2811            
2812        ## Step 4..13        ## Step 4..13
2813        my $new_mode = {        my $new_mode = {
2814                        select => 'in select',                        select => IN_SELECT_IM,
2815                        td => 'in cell',                        ## NOTE: |option| and |optgroup| do not set
2816                        th => 'in cell',                        ## insertion mode to "in select" by themselves.
2817                        tr => 'in row',                        td => IN_CELL_IM,
2818                        tbody => 'in table body',                        th => IN_CELL_IM,
2819                        thead => 'in table head',                        tr => IN_ROW_IM,
2820                        tfoot => 'in table foot',                        tbody => IN_TABLE_BODY_IM,
2821                        caption => 'in caption',                        thead => IN_TABLE_BODY_IM,
2822                        colgroup => 'in column group',                        tfoot => IN_TABLE_BODY_IM,
2823                        table => 'in table',                        caption => IN_CAPTION_IM,
2824                        head => 'in body', # not in head!                        colgroup => IN_COLUMN_GROUP_IM,
2825                        body => 'in body',                        table => IN_TABLE_IM,
2826                        frameset => 'in frameset',                        head => IN_BODY_IM, # not in head!
2827                          body => IN_BODY_IM,
2828                          frameset => IN_FRAMESET_IM,
2829                       }->{$node->[1]};                       }->{$node->[1]};
2830        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
2831                
2832        ## Step 14        ## Step 14
2833        if ($node->[1] eq 'html') {        if ($node->[1] eq 'html') {
2834          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
2835            $self->{insertion_mode} = 'before head';            !!!cp ('t29');
2836              $self->{insertion_mode} = BEFORE_HEAD_IM;
2837          } else {          } else {
2838            $self->{insertion_mode} = 'after head';            ## ISSUE: Can this state be reached?
2839              !!!cp ('t30');
2840              $self->{insertion_mode} = AFTER_HEAD_IM;
2841          }          }
2842          return;          return;
2843          } else {
2844            !!!cp ('t31');
2845        }        }
2846                
2847        ## Step 15        ## Step 15
2848        $self->{insertion_mode} = 'in body' and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
2849                
2850        ## Step 16        ## Step 16
2851        $i--;        $i--;
# Line 2098  sub _reset_insertion_mode ($) { Line 2854  sub _reset_insertion_mode ($) {
2854        ## Step 17        ## Step 17
2855        redo S3;        redo S3;
2856      } # S3      } # S3
2857    
2858      die "$0: _reset_insertion_mode: This line should never be reached";
2859  } # _reset_insertion_mode  } # _reset_insertion_mode
2860    
2861  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
2862    my $self = shift;    my $self = shift;
2863    
   my $previous_insertion_mode;  
   
2864    my $active_formatting_elements = [];    my $active_formatting_elements = [];
2865    
2866    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 2121  sub _tree_construction_main ($) { Line 2877  sub _tree_construction_main ($) {
2877      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
2878      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
2879        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
2880            !!!cp ('t32');
2881          return;          return;
2882        }        }
2883      }      }
# Line 2135  sub _tree_construction_main ($) { Line 2892  sub _tree_construction_main ($) {
2892    
2893        ## Step 6        ## Step 6
2894        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
2895            !!!cp ('t33_1');
2896          #          #
2897        } else {        } else {
2898          my $in_open_elements;          my $in_open_elements;
2899          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
2900            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
2901                !!!cp ('t33');
2902              $in_open_elements = 1;              $in_open_elements = 1;
2903              last OE;              last OE;
2904            }            }
2905          }          }
2906          if ($in_open_elements) {          if ($in_open_elements) {
2907              !!!cp ('t34');
2908            #            #
2909          } else {          } else {
2910              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
2911              !!!cp ('t35');
2912            redo S4;            redo S4;
2913          }          }
2914        }        }
# Line 2169  sub _tree_construction_main ($) { Line 2931  sub _tree_construction_main ($) {
2931    
2932        ## Step 11        ## Step 11
2933        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
2934            !!!cp ('t36');
2935          ## Step 7'          ## Step 7'
2936          $i++;          $i++;
2937          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
2938                    
2939          redo S7;          redo S7;
2940        }        }
2941    
2942          !!!cp ('t37');
2943      } # S7      } # S7
2944    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
2945    
2946    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
2947      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
2948        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
2949            !!!cp ('t38');
2950          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
2951          return;          return;
2952        }        }
2953      }      }
2954    
2955        !!!cp ('t39');
2956    }; # $clear_up_to_marker    }; # $clear_up_to_marker
2957    
2958    my $parse_rcdata = sub ($$) {    my $insert;
2959      my ($content_model_flag, $insert) = @_;  
2960      my $parse_rcdata = sub ($) {
2961        my ($content_model_flag) = @_;
2962    
2963      ## Step 1      ## Step 1
2964      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
# Line 2196  sub _tree_construction_main ($) { Line 2966  sub _tree_construction_main ($) {
2966      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $start_tag_name, $token->{attributes});
2967    
2968      ## Step 2      ## Step 2
2969      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
2970    
2971      ## Step 3      ## Step 3
2972      $self->{content_model} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
# Line 2205  sub _tree_construction_main ($) { Line 2975  sub _tree_construction_main ($) {
2975      ## Step 4      ## Step 4
2976      my $text = '';      my $text = '';
2977      !!!next-token;      !!!next-token;
2978      while ($token->{type} eq 'character') { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
2979          !!!cp ('t40');
2980        $text .= $token->{data};        $text .= $token->{data};
2981        !!!next-token;        !!!next-token;
2982      }      }
2983    
2984      ## Step 5      ## Step 5
2985      if (length $text) {      if (length $text) {
2986          !!!cp ('t41');
2987        my $text = $self->{document}->create_text_node ($text);        my $text = $self->{document}->create_text_node ($text);
2988        $el->append_child ($text);        $el->append_child ($text);
2989      }      }
# Line 2220  sub _tree_construction_main ($) { Line 2992  sub _tree_construction_main ($) {
2992      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
2993    
2994      ## Step 7      ## Step 7
2995      if ($token->{type} eq 'end tag' and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} == END_TAG_TOKEN and
2996            $token->{tag_name} eq $start_tag_name) {
2997          !!!cp ('t42');
2998        ## Ignore the token        ## Ignore the token
     } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {  
       !!!parse-error (type => 'in CDATA:#'.$token->{type});  
     } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {  
       !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
2999      } else {      } else {
3000        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
3001          if ($content_model_flag == CDATA_CONTENT_MODEL) {
3002            !!!cp ('t43');
3003            !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3004          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3005            !!!cp ('t44');
3006            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
3007          } else {
3008            die "$0: $content_model_flag in parse_rcdata";
3009          }
3010      }      }
3011      !!!next-token;      !!!next-token;
3012    }; # $parse_rcdata    }; # $parse_rcdata
3013    
3014    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
3015      my $script_el;      my $script_el;
3016      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes});
3017      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
# Line 2243  sub _tree_construction_main ($) { Line 3021  sub _tree_construction_main ($) {
3021            
3022      my $text = '';      my $text = '';
3023      !!!next-token;      !!!next-token;
3024      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
3025          !!!cp ('t45');
3026        $text .= $token->{data};        $text .= $token->{data};
3027        !!!next-token;        !!!next-token;
3028      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
3029      if (length $text) {      if (length $text) {
3030          !!!cp ('t46');
3031        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
3032      }      }
3033                                
3034      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
3035    
3036      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
3037          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
3038          !!!cp ('t47');
3039        ## Ignore the token        ## Ignore the token
3040      } else {      } else {
3041        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
3042          !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3043        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3044        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3045      }      }
3046            
3047      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
3048          !!!cp ('t49');
3049        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3050      } else {      } else {
3051          !!!cp ('t50');
3052        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
3053        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
3054    
# Line 2278  sub _tree_construction_main ($) { Line 3062  sub _tree_construction_main ($) {
3062      !!!next-token;      !!!next-token;
3063    }; # $script_start_tag    }; # $script_start_tag
3064    
3065      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3066      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3067      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3068    
3069    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3070      my $tag_name = shift;      my $end_tag_token = shift;
3071        my $tag_name = $end_tag_token->{tag_name};
3072    
3073        ## NOTE: The adoption agency algorithm (AAA).
3074    
3075      FET: {      FET: {
3076        ## Step 1        ## Step 1
# Line 2287  sub _tree_construction_main ($) { Line 3078  sub _tree_construction_main ($) {
3078        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3079        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3080          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {
3081              !!!cp ('t51');
3082            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3083            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3084            last AFE;            last AFE;
3085          } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {          } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {
3086              !!!cp ('t52');
3087            last AFE;            last AFE;
3088          }          }
3089        } # AFE        } # AFE
3090        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3091          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
3092            !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);
3093          ## Ignore the token          ## Ignore the token
3094          !!!next-token;          !!!next-token;
3095          return;          return;
# Line 2307  sub _tree_construction_main ($) { Line 3101  sub _tree_construction_main ($) {
3101          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3102          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
3103            if ($in_scope) {            if ($in_scope) {
3104                !!!cp ('t54');
3105              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
3106              last INSCOPE;              last INSCOPE;
3107            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3108              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
3109                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3110                                token => $end_tag_token);
3111              ## Ignore the token              ## Ignore the token
3112              !!!next-token;              !!!next-token;
3113              return;              return;
3114            }            }
3115          } elsif ({          } elsif ({
3116                    table => 1, caption => 1, td => 1, th => 1,                    applet => 1, table => 1, caption => 1, td => 1, th => 1,
3117                    button => 1, marquee => 1, object => 1, html => 1,                    button => 1, marquee => 1, object => 1, html => 1,
3118                   }->{$node->[1]}) {                   }->{$node->[1]}) {
3119              !!!cp ('t56');
3120            $in_scope = 0;            $in_scope = 0;
3121          }          }
3122        } # INSCOPE        } # INSCOPE
3123        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3124          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
3125            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3126                            token => $end_tag_token);
3127          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3128          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3129          return;          return;
3130        }        }
3131        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3132          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
3133            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1],
3134                            token => $end_tag_token);
3135        }        }
3136                
3137        ## Step 2        ## Step 2
# Line 2340  sub _tree_construction_main ($) { Line 3142  sub _tree_construction_main ($) {
3142          if (not $formatting_category->{$node->[1]} and          if (not $formatting_category->{$node->[1]} and
3143              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3144              ($special_category->{$node->[1]} or              ($special_category->{$node->[1]} or
3145               $scoping_category->{$node->[1]})) {               $scoping_category->{$node->[1]})) { ## Scoping is redundant, maybe
3146              !!!cp ('t59');
3147            $furthest_block = $node;            $furthest_block = $node;
3148            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
3149          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
3150              !!!cp ('t60');
3151            last OE;            last OE;
3152          }          }
3153        } # OE        } # OE
3154                
3155        ## Step 3        ## Step 3
3156        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
3157            !!!cp ('t61');
3158          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
3159          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
3160          !!!next-token;          !!!next-token;
# Line 2362  sub _tree_construction_main ($) { Line 3167  sub _tree_construction_main ($) {
3167        ## Step 5        ## Step 5
3168        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
3169        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
3170            !!!cp ('t62');
3171          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
3172        }        }
3173                
# Line 2384  sub _tree_construction_main ($) { Line 3190  sub _tree_construction_main ($) {
3190          S7S2: {          S7S2: {
3191            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
3192              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
3193                  !!!cp ('t63');
3194                $node_i_in_active = $_;                $node_i_in_active = $_;
3195                last S7S2;                last S7S2;
3196              }              }
# Line 2397  sub _tree_construction_main ($) { Line 3204  sub _tree_construction_main ($) {
3204                    
3205          ## Step 4          ## Step 4
3206          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
3207              !!!cp ('t64');
3208            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
3209          }          }
3210                    
3211          ## Step 5          ## Step 5
3212          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
3213              !!!cp ('t65');
3214            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
3215            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
3216            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2419  sub _tree_construction_main ($) { Line 3228  sub _tree_construction_main ($) {
3228        } # S7          } # S7  
3229                
3230        ## Step 8        ## Step 8
3231        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ({
3232               table => 1, tbody => 1, tfoot => 1, thead => 1, tr => 1,
3233              }->{$common_ancestor_node->[1]}) {
3234            my $foster_parent_element;
3235            my $next_sibling;
3236                             OE: for (reverse 0..$#{$self->{open_elements}}) {
3237                               if ($self->{open_elements}->[$_]->[1] eq 'table') {
3238                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3239                                 if (defined $parent and $parent->node_type == 1) {
3240                                   !!!cp ('t65.1');
3241                                   $foster_parent_element = $parent;
3242                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3243                                 } else {
3244                                   !!!cp ('t65.2');
3245                                   $foster_parent_element
3246                                     = $self->{open_elements}->[$_ - 1]->[0];
3247                                 }
3248                                 last OE;
3249                               }
3250                             } # OE
3251                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3252                               unless defined $foster_parent_element;
3253            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3254            $open_tables->[-1]->[1] = 1; # tainted
3255          } else {
3256            !!!cp ('t65.3');
3257            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3258          }
3259                
3260        ## Step 9        ## Step 9
3261        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2436  sub _tree_construction_main ($) { Line 3272  sub _tree_construction_main ($) {
3272        my $i;        my $i;
3273        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3274          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
3275              !!!cp ('t66');
3276            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
3277            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
3278          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
3279              !!!cp ('t67');
3280            $i = $_;            $i = $_;
3281          }          }
3282        } # AFE        } # AFE
# Line 2448  sub _tree_construction_main ($) { Line 3286  sub _tree_construction_main ($) {
3286        undef $i;        undef $i;
3287        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3288          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
3289              !!!cp ('t68');
3290            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
3291            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
3292          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
3293              !!!cp ('t69');
3294            $i = $_;            $i = $_;
3295          }          }
3296        } # OE        } # OE
# Line 2461  sub _tree_construction_main ($) { Line 3301  sub _tree_construction_main ($) {
3301      } # FET      } # FET
3302    }; # $formatting_end_tag    }; # $formatting_end_tag
3303    
3304    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3305      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3306    }; # $insert_to_current    }; # $insert_to_current
3307    
3308    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3309                         my $child = shift;      my $child = shift;
3310                         if ({      if ({
3311                              table => 1, tbody => 1, tfoot => 1,           table => 1, tbody => 1, tfoot => 1, thead => 1, tr => 1,
3312                              thead => 1, tr => 1,          }->{$self->{open_elements}->[-1]->[1]}) {
3313                             }->{$self->{open_elements}->[-1]->[1]}) {        # MUST
3314                           # MUST        my $foster_parent_element;
3315                           my $foster_parent_element;        my $next_sibling;
                          my $next_sibling;  
3316                           OE: for (reverse 0..$#{$self->{open_elements}}) {                           OE: for (reverse 0..$#{$self->{open_elements}}) {
3317                             if ($self->{open_elements}->[$_]->[1] eq 'table') {                             if ($self->{open_elements}->[$_]->[1] eq 'table') {
3318                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3319                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3320                                   !!!cp ('t70');
3321                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
3322                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
3323                               } else {                               } else {
3324                                   !!!cp ('t71');
3325                                 $foster_parent_element                                 $foster_parent_element
3326                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
3327                               }                               }
# Line 2491  sub _tree_construction_main ($) { Line 3332  sub _tree_construction_main ($) {
3332                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
3333                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3334                             ($child, $next_sibling);                             ($child, $next_sibling);
3335                         } else {        $open_tables->[-1]->[1] = 1; # tainted
3336                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
3337                         }        !!!cp ('t72');
3338    }; # $insert_to_foster        $self->{open_elements}->[-1]->[0]->append_child ($child);
   
   my $in_body = sub {  
     my $insert = shift;  
     if ($token->{type} eq 'start tag') {  
       if ($token->{tag_name} eq 'script') {  
         ## NOTE: This is an "as if in head" code clone  
         $script_start_tag->($insert);  
         return;  
       } elsif ($token->{tag_name} eq 'style') {  
         ## NOTE: This is an "as if in head" code clone  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         return;  
       } elsif ({  
                 base => 1, link => 1,  
                }->{$token->{tag_name}}) {  
         ## NOTE: This is an "as if in head" code clone, only "-t" differs  
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'meta') {  
         ## NOTE: This is an "as if in head" code clone, only "-t" differs  
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.  
   
         unless ($self->{confident}) {  
           my $charset;  
           if ($token->{attributes}->{charset}) { ## TODO: And if supported  
             $charset = $token->{attributes}->{charset}->{value};  
           }  
           if ($token->{attributes}->{'http-equiv'}) {  
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
             if ($token->{attributes}->{'http-equiv'}->{value}  
                 =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=  
                     [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|  
                     ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {  
               $charset = defined $1 ? $1 : defined $2 ? $2 : $3;  
             } ## TODO: And if supported  
           }  
           ## TODO: Change the encoding  
         }  
   
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'title') {  
         !!!parse-error (type => 'in body:title');  
         ## NOTE: This is an "as if in head" code clone  
         $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {  
           if (defined $self->{head_element}) {  
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             $insert->($_[0]);  
           }  
         });  
         return;  
       } elsif ($token->{tag_name} eq 'body') {  
         !!!parse-error (type => 'in body:body');  
                 
         if (@{$self->{open_elements}} == 1 or  
             $self->{open_elements}->[1]->[1] ne 'body') {  
           ## Ignore the token  
         } else {  
           my $body_el = $self->{open_elements}->[1]->[0];  
           for my $attr_name (keys %{$token->{attributes}}) {  
             unless ($body_el->has_attribute_ns (undef, $attr_name)) {  
               $body_el->set_attribute_ns  
                 (undef, [undef, $attr_name],  
                  $token->{attributes}->{$attr_name}->{value});  
             }  
           }  
         }  
         !!!next-token;  
         return;  
       } elsif ({  
                 address => 1, blockquote => 1, center => 1, dir => 1,  
                 div => 1, dl => 1, fieldset => 1, listing => 1,  
                 menu => 1, ol => 1, p => 1, ul => 1,  
                 pre => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         if ($token->{tag_name} eq 'pre') {  
           !!!next-token;  
           if ($token->{type} eq 'character') {  
             $token->{data} =~ s/^\x0A//;  
             unless (length $token->{data}) {  
               !!!next-token;  
             }  
           }  
         } else {  
           !!!next-token;  
         }  
         return;  
       } elsif ($token->{tag_name} eq 'form') {  
         if (defined $self->{form_element}) {  
           !!!parse-error (type => 'in form:form');  
           ## Ignore the token  
           !!!next-token;  
           return;  
         } else {  
           ## has a p element in scope  
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!back-token;  
               $token = {type => 'end tag', tag_name => 'p'};  
               return;  
             } elsif ({  
                       table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
               last INSCOPE;  
             }  
           } # INSCOPE  
               
           !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           $self->{form_element} = $self->{open_elements}->[-1]->[0];  
           !!!next-token;  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'li') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'li') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'plaintext') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{content_model} = PLAINTEXT_CONTENT_MODEL;  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>  
         ## has an element in scope  
         #my $i;  
         #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
         #  my $node = $self->{open_elements}->[$_];  
         #  if ({  
         #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
         #      }->{$node->[1]}) {  
         #    $i = $_;  
         #    last INSCOPE;  
         #  } elsif ({  
         #            table => 1, caption => 1, td => 1, th => 1,  
         #            button => 1, marquee => 1, object => 1, html => 1,  
         #           }->{$node->[1]}) {  
         #    last INSCOPE;  
         #  }  
         #} # INSCOPE  
         #    
         #if (defined $i) {  
         #  !!! parse-error (type => 'in hn:hn');  
         #  splice @{$self->{open_elements}}, $i;  
         #}  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'a') {  
         AFE: for my $i (reverse 0..$#$active_formatting_elements) {  
           my $node = $active_formatting_elements->[$i];  
           if ($node->[1] eq 'a') {  
             !!!parse-error (type => 'in a:a');  
               
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'a'};  
             $formatting_end_tag->($token->{tag_name});  
               
             AFE2: for (reverse 0..$#$active_formatting_elements) {  
               if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {  
                 splice @$active_formatting_elements, $_, 1;  
                 last AFE2;  
               }  
             } # AFE2  
             OE: for (reverse 0..$#{$self->{open_elements}}) {  
               if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {  
                 splice @{$self->{open_elements}}, $_, 1;  
                 last OE;  
               }  
             } # OE  
             last AFE;  
           } elsif ($node->[0] eq '#marker') {  
             last AFE;  
           }  
         } # AFE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
   
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
   
         !!!next-token;  
         return;  
       } elsif ({  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'nobr') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
   
         ## has a |nobr| element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'nobr') {  
             !!!parse-error (type => 'not closed:nobr');  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'nobr'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'button') {  
         ## has a button element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'button') {  
             !!!parse-error (type => 'in button:button');  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'button'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
   
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'marquee' or  
                $token->{tag_name} eq 'object') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         return;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{insertion_mode} = 'in table';  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 area => 1, basefont => 1, bgsound => 1, br => 1,  
                 embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,  
                 image => 1,  
                }->{$token->{tag_name}}) {  
         if ($token->{tag_name} eq 'image') {  
           !!!parse-error (type => 'image');  
           $token->{tag_name} = 'img';  
         }  
   
         ## NOTE: There is an "as if <br>" code clone.  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'hr') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
             
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'input') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         ## TODO: associate with $self->{form_element} if defined  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'isindex') {  
         !!!parse-error (type => 'isindex');  
           
         if (defined $self->{form_element}) {  
           ## Ignore the token  
           !!!next-token;  
           return;  
         } else {  
           my $at = $token->{attributes};  
           my $form_attrs;  
           $form_attrs->{action} = $at->{action} if $at->{action};  
           my $prompt_attr = $at->{prompt};  
           $at->{name} = {name => 'name', value => 'isindex'};  
           delete $at->{action};  
           delete $at->{prompt};  
           my @tokens = (  
                         {type => 'start tag', tag_name => 'form',  
                          attributes => $form_attrs},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'start tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'label'},  
                        );  
           if ($prompt_attr) {  
             push @tokens, {type => 'character', data => $prompt_attr->{value}};  
           } else {  
             push @tokens, {type => 'character',  
                            data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD  
             ## TODO: make this configurable  
           }  
           push @tokens,  
                         {type => 'start tag', tag_name => 'input', attributes => $at},  
                         #{type => 'character', data => ''}, # SHOULD  
                         {type => 'end tag', tag_name => 'label'},  
                         {type => 'end tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'end tag', tag_name => 'form'};  
           $token = shift @tokens;  
           !!!back-token (@tokens);  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'textarea') {  
         my $tag_name = $token->{tag_name};  
         my $el;  
         !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
           
         ## TODO: $self->{form_element} if defined  
         $self->{content_model} = RCDATA_CONTENT_MODEL;  
         delete $self->{escape}; # MUST  
           
         $insert->($el);  
           
         my $text = '';  
         !!!next-token;  
         if ($token->{type} eq 'character') {  
           $token->{data} =~ s/^\x0A//;  
           unless (length $token->{data}) {  
             !!!next-token;  
           }  
         }  
         while ($token->{type} eq 'character') {  
           $text .= $token->{data};  
           !!!next-token;  
         }  
         if (length $text) {  
           $el->manakai_append_text ($text);  
         }  
           
         $self->{content_model} = PCDATA_CONTENT_MODEL;  
           
         if ($token->{type} eq 'end tag' and  
             $token->{tag_name} eq $tag_name) {  
           ## Ignore the token  
         } else {  
           !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
         }  
         !!!next-token;  
         return;  
       } elsif ({  
                 iframe => 1,  
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         return;  
       } elsif ($token->{tag_name} eq 'select') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         $self->{insertion_mode} = 'in select';  
         !!!next-token;  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'in body:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: An issue on HTML5 new elements in the spec.  
       } else {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         !!!next-token;  
         return;  
       }  
     } elsif ($token->{type} eq 'end tag') {  
       if ($token->{tag_name} eq 'body') {  
         if (@{$self->{open_elements}} > 1 and  
             $self->{open_elements}->[1]->[1] eq 'body') {  
           for (@{$self->{open_elements}}) {  
             unless ({  
                        dd => 1, dt => 1, li => 1, p => 1, td => 1,  
                        th => 1, tr => 1, body => 1, html => 1,  
                      tbody => 1, tfoot => 1, thead => 1,  
                     }->{$_->[1]}) {  
               !!!parse-error (type => 'not closed:'.$_->[1]);  
             }  
           }  
   
           $self->{insertion_mode} = 'after body';  
           !!!next-token;  
           return;  
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
           !!!next-token;  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'html') {  
         if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {  
           ## ISSUE: There is an issue in the spec.  
           if ($self->{open_elements}->[-1]->[1] ne 'body') {  
             !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);  
           }  
           $self->{insertion_mode} = 'after body';  
           ## reprocess  
           return;  
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
           !!!next-token;  
           return;  
         }  
       } elsif ({  
                 address => 1, blockquote => 1, center => 1, dir => 1,  
                 div => 1, dl => 1, fieldset => 1, listing => 1,  
                 menu => 1, ol => 1, pre => 1, ul => 1,  
                 p => 1,  
                 dd => 1, dt => 1, li => 1,  
                 button => 1, marquee => 1, object => 1,  
                }->{$token->{tag_name}}) {  
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq $token->{tag_name}) {  
             ## generate implied end tags  
             if ({  
                  dd => ($token->{tag_name} ne 'dd'),  
                  dt => ($token->{tag_name} ne 'dt'),  
                  li => ($token->{tag_name} ne 'li'),  
                  p => ($token->{tag_name} ne 'p'),  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             $i = $_;  
             last INSCOPE unless $token->{tag_name} eq 'p';  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           if (defined $i) {  
             !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
           } else {  
             !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           }  
         }  
           
         if (defined $i) {  
           splice @{$self->{open_elements}}, $i;  
         } elsif ($token->{tag_name} eq 'p') {  
           ## As if <p>, then reprocess the current token  
           my $el;  
           !!!create-element ($el, 'p');  
           $insert->($el);  
         }  
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'form') {  
         ## has an element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq $token->{tag_name}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {  
           pop @{$self->{open_elements}};  
         } else {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
   
         undef $self->{form_element};  
         !!!next-token;  
         return;  
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ({  
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             $i = $_;  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
           
         splice @{$self->{open_elements}}, $i if defined $i;  
         !!!next-token;  
         return;  
       } elsif ({  
                 a => 1,  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 nobr => 1, s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $formatting_end_tag->($token->{tag_name});  
         return;  
       } elsif ($token->{tag_name} eq 'br') {  
         !!!parse-error (type => 'unmatched end tag:br');  
   
         ## As if <br>  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         my $el;  
         !!!create-element ($el, 'br');  
         $insert->($el);  
           
         ## Ignore the token.  
         !!!next-token;  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                 area => 1, basefont => 1, bgsound => 1,  
                 embed => 1, hr => 1, iframe => 1, image => 1,  
                 img => 1, input => 1, isindex => 1, noembed => 1,  
                 noframes => 1, param => 1, select => 1, spacer => 1,  
                 table => 1, textarea => 1, wbr => 1,  
                 noscript => 0, ## TODO: if scripting is enabled  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: Issue on HTML5 new elements in spec  
           
       } else {  
         ## Step 1  
         my $node_i = -1;  
         my $node = $self->{open_elements}->[$node_i];  
   
         ## Step 2  
         S2: {  
           if ($node->[1] eq $token->{tag_name}) {  
             ## Step 1  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
           
             ## Step 2  
             if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {  
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
             }  
               
             ## Step 3  
             splice @{$self->{open_elements}}, $node_i;  
   
             !!!next-token;  
             last S2;  
           } else {  
             ## Step 3  
             if (not $formatting_category->{$node->[1]} and  
                 #not $phrasing_category->{$node->[1]} and  
                 ($special_category->{$node->[1]} or  
                  $scoping_category->{$node->[1]})) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               last S2;  
             }  
           }  
             
           ## Step 4  
           $node_i--;  
           $node = $self->{open_elements}->[$node_i];  
             
           ## Step 5;  
           redo S2;  
         } # S2  
         return;  
       }  
3339      }      }
3340    }; # $in_body    }; # $insert_to_foster
3341    
3342    B: {    B: {
3343      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
3344        !!!parse-error (type => 'DOCTYPE in the middle');        !!!cp ('t73');
3345          !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3346        ## Ignore the token        ## Ignore the token
3347        ## Stay in the phase        ## Stay in the phase
3348        !!!next-token;        !!!next-token;
3349        redo B;        redo B;
3350      } elsif ($token->{type} eq 'end-of-file') {      } elsif ($token->{type} == START_TAG_TOKEN and
       if ($token->{insertion_mode} ne 'trailing end') {  
         ## Generate implied end tags  
         if ({  
              dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,  
              tbody => 1, tfoot=> 1, thead => 1,  
             }->{$self->{open_elements}->[-1]->[1]}) {  
           !!!back-token;  
           $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};  
           redo B;  
         }  
           
         if (@{$self->{open_elements}} > 2 or  
             (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } elsif (defined $self->{inner_html_node} and  
                  @{$self->{open_elements}} > 1 and  
                  $self->{open_elements}->[1]->[1] ne 'body') {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
   
         ## ISSUE: There is an issue in the spec.  
       }  
   
       ## Stop parsing  
       last B;  
     } elsif ($token->{type} eq 'start tag' and  
3351               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
3352        if ($self->{insertion_mode} eq 'trailing end') {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3353          ## Turn into the main phase          !!!cp ('t79');
3354          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3355          $self->{insertion_mode} = $previous_insertion_mode;          $self->{insertion_mode} = AFTER_BODY_IM;
3356          } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3357            !!!cp ('t80');
3358            !!!parse-error (type => 'after html:html', token => $token);
3359            $self->{insertion_mode} = AFTER_FRAMESET_IM;
3360          } else {
3361            !!!cp ('t81');
3362        }        }
3363    
3364  ## ISSUE: "aa<html>" is not a parse error.        !!!cp ('t82');
3365  ## ISSUE: "<html>" in fragment is not a parse error.        !!!parse-error (type => 'not first start tag', token => $token);
       unless ($token->{first_start_tag}) {  
         !!!parse-error (type => 'not first start tag');  
       }  
3366        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
3367        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
3368          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
3369              !!!cp ('t84');
3370            $top_el->set_attribute_ns            $top_el->set_attribute_ns
3371              (undef, [undef, $attr_name],              (undef, [undef, $attr_name],
3372               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
# Line 3397  sub _tree_construction_main ($) { Line 3374  sub _tree_construction_main ($) {
3374        }        }
3375        !!!next-token;        !!!next-token;
3376        redo B;        redo B;
3377      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
3378        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3379        if ($self->{insertion_mode} eq 'trailing end') {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
3380            !!!cp ('t85');
3381          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3382        } elsif ($self->{insertion_mode} eq 'after body') {        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
3383            !!!cp ('t86');
3384          $self->{open_elements}->[0]->[0]->append_child ($comment);          $self->{open_elements}->[0]->[0]->append_child ($comment);
3385        } else {        } else {
3386            !!!cp ('t87');
3387          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
3388        }        }
3389        !!!next-token;        !!!next-token;
3390        redo B;        redo B;
3391      } elsif ($self->{insertion_mode} eq 'before head') {      } elsif ($self->{insertion_mode} & HEAD_IMS) {
3392            if ($token->{type} eq 'character') {        if ($token->{type} == CHARACTER_TOKEN) {
3393              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3394                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3395                unless (length $token->{data}) {              !!!cp ('t88.2');
3396                  !!!next-token;              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3397                  redo B;            } else {
3398                }              !!!cp ('t88.1');
3399              }              ## Ignore the token.
3400              ## As if <head>              !!!next-token;
3401              !!!create-element ($self->{head_element}, 'head');              redo B;
3402              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            }
3403              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            unless (length $token->{data}) {
3404              $self->{insertion_mode} = 'in head';              !!!cp ('t88');
3405                !!!next-token;
3406                redo B;
3407              }
3408            }
3409    
3410            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3411              !!!cp ('t89');
3412              ## As if <head>
3413              !!!create-element ($self->{head_element}, 'head');
3414              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3415              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3416    
3417              ## Reprocess in the "in head" insertion mode...
3418              pop @{$self->{open_elements}};
3419    
3420              ## Reprocess in the "after head" insertion mode...
3421            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3422              !!!cp ('t90');
3423              ## As if </noscript>
3424              pop @{$self->{open_elements}};
3425              !!!parse-error (type => 'in noscript:#character', token => $token);
3426              
3427              ## Reprocess in the "in head" insertion mode...
3428              ## As if </head>
3429              pop @{$self->{open_elements}};
3430    
3431              ## Reprocess in the "after head" insertion mode...
3432            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3433              !!!cp ('t91');
3434              pop @{$self->{open_elements}};
3435    
3436              ## Reprocess in the "after head" insertion mode...
3437            } else {
3438              !!!cp ('t92');
3439            }
3440    
3441                ## "after head" insertion mode
3442                ## As if <body>
3443                !!!insert-element ('body');
3444                $self->{insertion_mode} = IN_BODY_IM;
3445              ## reprocess              ## reprocess
3446              redo B;              redo B;
3447            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
             my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};  
             !!!create-element ($self->{head_element}, 'head', $attr);  
             $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
             push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
             $self->{insertion_mode} = 'in head';  
3448              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
3449                !!!next-token;                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3450              #} elsif ({                  !!!cp ('t93');
3451              #          base => 1, link => 1, meta => 1,                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
3452              #          script => 1, style => 1, title => 1,                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3453              #         }->{$token->{tag_name}}) {                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
3454              #  ## reprocess                  $self->{insertion_mode} = IN_HEAD_IM;
3455              } else {                  !!!next-token;
3456                ## reprocess                  redo B;
3457              }                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3458              redo B;                  !!!cp ('t94');
3459            } elsif ($token->{type} eq 'end tag') {                  #
3460              if ({                } else {
3461                   head => 1, body => 1, html => 1,                  !!!cp ('t95');
3462                   p => 1, br => 1,                  !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
3463                  }->{$token->{tag_name}}) {                  ## Ignore the token
3464                    !!!next-token;
3465                    redo B;
3466                  }
3467                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3468                  !!!cp ('t96');
3469                ## As if <head>                ## As if <head>
3470                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head');
3471                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3472                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3473                $self->{insertion_mode} = 'in head';  
3474                ## reprocess                $self->{insertion_mode} = IN_HEAD_IM;
3475                redo B;                ## Reprocess in the "in head" insertion mode...
3476              } else {              } else {
3477                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t97');
               ## Ignore the token ## ISSUE: An issue in the spec.  
               !!!next-token;  
               redo B;  
3478              }              }
3479            } else {  
3480              die "$0: $token->{type}: Unknown type";              if ($token->{tag_name} eq 'base') {
3481            }                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3482          } elsif ($self->{insertion_mode} eq 'in head' or                  !!!cp ('t98');
3483                   $self->{insertion_mode} eq 'in head noscript' or                  ## As if </noscript>
3484                   $self->{insertion_mode} eq 'after head') {                  pop @{$self->{open_elements}};
3485            if ($token->{type} eq 'character') {                  !!!parse-error (type => 'in noscript:base', token => $token);
3486              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                
3487                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                  $self->{insertion_mode} = IN_HEAD_IM;
3488                unless (length $token->{data}) {                  ## Reprocess in the "in head" insertion mode...
3489                  !!!next-token;                } else {
3490                  redo B;                  !!!cp ('t99');
3491                }                }
3492              }  
               
             #  
           } elsif ($token->{type} eq 'start tag') {  
             if ({base => ($self->{insertion_mode} eq 'in head' or  
                           $self->{insertion_mode} eq 'after head'),  
                  link => 1}->{$token->{tag_name}}) {  
3493                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3494                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3495                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t100');
3496                    !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3497                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3498                  } else {
3499                    !!!cp ('t101');
3500                }                }
3501                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3502                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3503                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3504                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3505                !!!next-token;                !!!next-token;
3506                redo B;                redo B;
3507              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'link') {
3508                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3509                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3510                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t102');
3511                    !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3512                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3513                  } else {
3514                    !!!cp ('t103');
3515                }                }
3516                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3517                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3518                  pop @{$self->{open_elements}} # <head>
3519                      if $self->{insertion_mode} == AFTER_HEAD_IM;
3520                  !!!next-token;
3521                  redo B;
3522                } elsif ($token->{tag_name} eq 'meta') {
3523                  ## NOTE: There is a "as if in head" code clone.
3524                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3525                    !!!cp ('t104');
3526                    !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3527                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3528                  } else {
3529                    !!!cp ('t105');
3530                  }
3531                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3532                  my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3533    
3534                unless ($self->{confident}) {                unless ($self->{confident}) {
                 my $charset;  
3535                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
3536                    $charset = $token->{attributes}->{charset}->{value};                    !!!cp ('t106');
3537                  }                    $self->{change_encoding}
3538                  if ($token->{attributes}->{'http-equiv'}) {                        ->($self, $token->{attributes}->{charset}->{value});
3539                      
3540                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3541                          ->set_user_data (manakai_has_reference =>
3542                                               $token->{attributes}->{charset}
3543                                                   ->{has_reference});
3544                    } elsif ($token->{attributes}->{content}) {
3545                    ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.                    ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
3546                    if ($token->{attributes}->{'http-equiv'}->{value}                    if ($token->{attributes}->{content}->{value}
3547                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
3548                              [\x09-\x0D\x20]*=
3549                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
3550                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
3551                      $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                      !!!cp ('t107');
3552                    } ## TODO: And if supported                      $self->{change_encoding}
3553                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
3554                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3555                            ->set_user_data (manakai_has_reference =>
3556                                                 $token->{attributes}->{content}
3557                                                       ->{has_reference});
3558                      } else {
3559                        !!!cp ('t108');
3560                      }
3561                    }
3562                  } else {
3563                    if ($token->{attributes}->{charset}) {
3564                      !!!cp ('t109');
3565                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3566                          ->set_user_data (manakai_has_reference =>
3567                                               $token->{attributes}->{charset}
3568                                                   ->{has_reference});
3569                    }
3570                    if ($token->{attributes}->{content}) {
3571                      !!!cp ('t110');
3572                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3573                          ->set_user_data (manakai_has_reference =>
3574                                               $token->{attributes}->{content}
3575                                                   ->{has_reference});
3576                  }                  }
                 ## TODO: Change the encoding  
3577                }                }
3578    
3579                ## TODO: Extracting |charset| from |meta|.                pop @{$self->{open_elements}} # <head>
3580                pop @{$self->{open_elements}}                    if $self->{insertion_mode} == AFTER_HEAD_IM;
                   if $self->{insertion_mode} eq 'after head';  
3581                !!!next-token;                !!!next-token;
3582                redo B;                redo B;
3583              } elsif ($token->{tag_name} eq 'title' and              } elsif ($token->{tag_name} eq 'title') {
3584                       $self->{insertion_mode} eq 'in head') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3585                ## NOTE: There is a "as if in head" code clone.                  !!!cp ('t111');
3586                if ($self->{insertion_mode} eq 'after head') {                  ## As if </noscript>
3587                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  pop @{$self->{open_elements}};
3588                    !!!parse-error (type => 'in noscript:title', token => $token);
3589                  
3590                    $self->{insertion_mode} = IN_HEAD_IM;
3591                    ## Reprocess in the "in head" insertion mode...
3592                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3593                    !!!cp ('t112');
3594                    !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3595                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3596                  } else {
3597                    !!!cp ('t113');
3598                }                }
3599    
3600                  ## NOTE: There is a "as if in head" code clone.
3601                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
3602                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
3603                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
3604                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
3605                pop @{$self->{open_elements}}                    if $self->{insertion_mode} == AFTER_HEAD_IM;
                   if $self->{insertion_mode} eq 'after head';  
3606                redo B;                redo B;
3607              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
3608                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
3609                ## insertion mode 'in head')                ## insertion mode IN_HEAD_IM)
3610                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3611                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3612                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t114');
3613                    !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3614                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3615                  } else {
3616                    !!!cp ('t115');
3617                }                }
3618                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
3619                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3620                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3621                redo B;                redo B;
3622              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
3623                if ($self->{insertion_mode} eq 'in head') {                if ($self->{insertion_mode} == IN_HEAD_IM) {
3624                    !!!cp ('t116');
3625                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
3626                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3627                  $self->{insertion_mode} = 'in head noscript';                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
3628                  !!!next-token;                  !!!next-token;
3629                  redo B;                  redo B;
3630                } elsif ($self->{insertion_mode} eq 'in head noscript') {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3631                  !!!parse-error (type => 'in noscript:noscript');                  !!!cp ('t117');
3632                    !!!parse-error (type => 'in noscript:noscript', token => $token);
3633                  ## Ignore the token                  ## Ignore the token
3634                  !!!next-token;                  !!!next-token;
3635                  redo B;                  redo B;
3636                } else {                } else {
3637                    !!!cp ('t118');
3638                  #                  #
3639                }                }
3640              } elsif ($token->{tag_name} eq 'head' and              } elsif ($token->{tag_name} eq 'script') {
3641                       $self->{insertion_mode} ne 'after head') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3642                !!!parse-error (type => 'in head:head'); # or in head noscript                  !!!cp ('t119');
3643                ## Ignore the token                  ## As if </noscript>
3644                !!!next-token;                  pop @{$self->{open_elements}};
3645                redo B;                  !!!parse-error (type => 'in noscript:script', token => $token);
3646              } elsif ($self->{insertion_mode} ne 'in head noscript' and                
3647                       $token->{tag_name} eq 'script') {                  $self->{insertion_mode} = IN_HEAD_IM;
3648                if ($self->{insertion_mode} eq 'after head') {                  ## Reprocess in the "in head" insertion mode...
3649                  !!!parse-error (type => 'after head:'.$token->{tag_name});                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3650                    !!!cp ('t120');
3651                    !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3652                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3653                  } else {
3654                    !!!cp ('t121');
3655                }                }
3656    
3657                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3658                $script_start_tag->($insert_to_current);                $script_start_tag->();
3659                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3660                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
               redo B;  
             } elsif ($self->{insertion_mode} eq 'after head' and  
                      $token->{tag_name} eq 'body') {  
               !!!insert-element ('body', $token->{attributes});  
               $self->{insertion_mode} = 'in body';  
               !!!next-token;  
3661                redo B;                redo B;
3662              } elsif ($self->{insertion_mode} eq 'after head' and              } elsif ($token->{tag_name} eq 'body' or
3663                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
3664                !!!insert-element ('frameset', $token->{attributes});                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3665                $self->{insertion_mode} = 'in frameset';                  !!!cp ('t122');
3666                    ## As if </noscript>
3667                    pop @{$self->{open_elements}};
3668                    !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);
3669                    
3670                    ## Reprocess in the "in head" insertion mode...
3671                    ## As if </head>
3672                    pop @{$self->{open_elements}};
3673                    
3674                    ## Reprocess in the "after head" insertion mode...
3675                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3676                    !!!cp ('t124');
3677                    pop @{$self->{open_elements}};
3678                    
3679                    ## Reprocess in the "after head" insertion mode...
3680                  } else {
3681                    !!!cp ('t125');
3682                  }
3683    
3684                  ## "after head" insertion mode
3685                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3686                  if ($token->{tag_name} eq 'body') {
3687                    !!!cp ('t126');
3688                    $self->{insertion_mode} = IN_BODY_IM;
3689                  } elsif ($token->{tag_name} eq 'frameset') {
3690                    !!!cp ('t127');
3691                    $self->{insertion_mode} = IN_FRAMESET_IM;
3692                  } else {
3693                    die "$0: tag name: $self->{tag_name}";
3694                  }
3695                !!!next-token;                !!!next-token;
3696                redo B;                redo B;
3697              } else {              } else {
3698                  !!!cp ('t128');
3699                #                #
3700              }              }
3701            } elsif ($token->{type} eq 'end tag') {  
3702              if ($self->{insertion_mode} eq 'in head' and              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3703                  $token->{tag_name} eq 'head') {                !!!cp ('t129');
3704                  ## As if </noscript>
3705                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3706                $self->{insertion_mode} = 'after head';                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
3707                !!!next-token;                
3708                redo B;                ## Reprocess in the "in head" insertion mode...
3709              } elsif ($self->{insertion_mode} eq 'in head noscript' and                ## As if </head>
                 $token->{tag_name} eq 'noscript') {  
3710                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3711                $self->{insertion_mode} = 'in head';  
3712                !!!next-token;                ## Reprocess in the "after head" insertion mode...
3713                redo B;              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3714              } elsif ($self->{insertion_mode} eq 'in head' and                !!!cp ('t130');
3715                       {                ## As if </head>
3716                        body => 1, html => 1,                pop @{$self->{open_elements}};
3717                        p => 1, br => 1,  
3718                       }->{$token->{tag_name}}) {                ## Reprocess in the "after head" insertion mode...
               #  
             } elsif ($self->{insertion_mode} eq 'in head noscript' and  
                      {  
                       p => 1, br => 1,  
                      }->{$token->{tag_name}}) {  
               #  
             } elsif ($self->{insertion_mode} ne 'after head') {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
3719              } else {              } else {
3720                #                !!!cp ('t131');
3721              }              }
           } else {  
             #  
           }  
3722    
3723            ## As if </head> or </noscript> or <body>              ## "after head" insertion mode
3724            if ($self->{insertion_mode} eq 'in head') {              ## As if <body>
             pop @{$self->{open_elements}};  
             $self->{insertion_mode} = 'after head';  
           } elsif ($self->{insertion_mode} eq 'in head noscript') {  
             pop @{$self->{open_elements}};  
             !!!parse-error (type => 'in noscript:'.(defined $token->{tag_name} ? ($token->{type} eq 'end tag' ? '/' : '') . $token->{tag_name} : '#' . $token->{type}));  
             $self->{insertion_mode} = 'in head';  
           } else { # 'after head'  
3725              !!!insert-element ('body');              !!!insert-element ('body');
3726              $self->{insertion_mode} = 'in body';              $self->{insertion_mode} = IN_BODY_IM;
3727            }              ## reprocess
           ## reprocess  
           redo B;  
   
           ## ISSUE: An issue in the spec.  
         } elsif ($self->{insertion_mode} eq 'in body') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: There is a code clone of "character in body".  
             $reconstruct_active_formatting_elements->($insert_to_current);  
               
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
             !!!next-token;  
             redo B;  
           } else {  
             $in_body->($insert_to_current);  
3728              redo B;              redo B;
3729            }            } elsif ($token->{type} == END_TAG_TOKEN) {
3730          } elsif ($self->{insertion_mode} eq 'in table') {              if ($token->{tag_name} eq 'head') {
3731            if ($token->{type} eq 'character') {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3732              ## NOTE: There are "character in table" code clones.                  !!!cp ('t132');
3733              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                  ## As if <head>
3734                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                  !!!create-element ($self->{head_element}, 'head');
3735                                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3736                unless (length $token->{data}) {                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3737    
3738                    ## Reprocess in the "in head" insertion mode...
3739                    pop @{$self->{open_elements}};
3740                    $self->{insertion_mode} = AFTER_HEAD_IM;
3741                    !!!next-token;
3742                    redo B;
3743                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3744                    !!!cp ('t133');
3745                    ## As if </noscript>
3746                    pop @{$self->{open_elements}};
3747                    !!!parse-error (type => 'in noscript:/head', token => $token);
3748                    
3749                    ## Reprocess in the "in head" insertion mode...
3750                    pop @{$self->{open_elements}};
3751                    $self->{insertion_mode} = AFTER_HEAD_IM;
3752                    !!!next-token;
3753                    redo B;
3754                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3755                    !!!cp ('t134');
3756                    pop @{$self->{open_elements}};
3757                    $self->{insertion_mode} = AFTER_HEAD_IM;
3758                  !!!next-token;                  !!!next-token;
3759                  redo B;                  redo B;
               }  
             }  
   
             !!!parse-error (type => 'in table:#character');  
   
             ## As if in body, but insert into foster parent element  
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
               
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
3760                } else {                } else {
3761                  $foster_parent_element->insert_before                  !!!cp ('t135');
3762                    ($self->{document}->create_text_node ($token->{data}),                  #
                    $next_sibling);  
3763                }                }
3764              } else {              } elsif ($token->{tag_name} eq 'noscript') {
3765                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3766              }                  !!!cp ('t136');
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  caption => 1,  
                  colgroup => 1,  
                  tbody => 1, tfoot => 1, thead => 1,  
                 }->{$token->{tag_name}}) {  
               ## Clear back to table context  
               while ($self->{open_elements}->[-1]->[1] ne 'table' and  
                      $self->{open_elements}->[-1]->[1] ne 'html') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
3767                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3768                    $self->{insertion_mode} = IN_HEAD_IM;
3769                    !!!next-token;
3770                    redo B;
3771                  } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3772                    !!!cp ('t137');
3773                    !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
3774                    ## Ignore the token ## ISSUE: An issue in the spec.
3775                    !!!next-token;
3776                    redo B;
3777                  } else {
3778                    !!!cp ('t138');
3779                    #
3780                }                }
   
               push @$active_formatting_elements, ['#marker', '']  
                 if $token->{tag_name} eq 'caption';  
   
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = {  
                                  caption => 'in caption',  
                                  colgroup => 'in column group',  
                                  tbody => 'in table body',  
                                  tfoot => 'in table body',  
                                  thead => 'in table body',  
                                 }->{$token->{tag_name}};  
               !!!next-token;  
               redo B;  
3781              } elsif ({              } elsif ({
3782                        col => 1,                        body => 1, html => 1,
                       td => 1, th => 1, tr => 1,  
3783                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3784                ## Clear back to table context                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3785                while ($self->{open_elements}->[-1]->[1] ne 'table' and                  !!!cp ('t139');
3786                       $self->{open_elements}->[-1]->[1] ne 'html') {                  ## As if <head>
3787                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!create-element ($self->{head_element}, 'head');
3788                  pop @{$self->{open_elements}};                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3789                }                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
   
               !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');  
               $self->{insertion_mode} = $token->{tag_name} eq 'col'  
                 ? 'in column group' : 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## NOTE: There are code clones for this "table in table"  
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
3790    
3791                ## As if </table>                  $self->{insertion_mode} = IN_HEAD_IM;
3792                ## have a table element in table scope                  ## Reprocess in the "in head" insertion mode...
3793                my $i;                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3794                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  !!!cp ('t140');
3795                  my $node = $self->{open_elements}->[$_];                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
3796                  if ($node->[1] eq 'table') {                  ## Ignore the token
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:table');  
                 ## Ignore tokens </table><table>  
3797                  !!!next-token;                  !!!next-token;
3798                  redo B;                  redo B;
3799                  } else {
3800                    !!!cp ('t141');
3801                }                }
3802                                
3803                ## generate implied end tags                #
3804                if ({              } elsif ({
3805                     dd => 1, dt => 1, li => 1, p => 1,                        p => 1, br => 1,
3806                     td => 1, th => 1, tr => 1,                       }->{$token->{tag_name}}) {
3807                     tbody => 1, tfoot=> 1, thead => 1,                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3808                    }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t142');
3809                  !!!back-token; # <table>                  ## As if <head>
3810                  $token = {type => 'end tag', tag_name => 'table'};                  !!!create-element ($self->{head_element}, 'head');
3811                  !!!back-token;                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3812                  $token = {type => 'end tag',                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
3813    
3814                if ($self->{open_elements}->[-1]->[1] ne 'table') {                  $self->{insertion_mode} = IN_HEAD_IM;
3815                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  ## Reprocess in the "in head" insertion mode...
3816                  } else {
3817                    !!!cp ('t143');
3818                }                }
3819    
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
3820                #                #
3821              }              } else {
3822            } elsif ($token->{type} eq 'end tag') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3823              if ($token->{tag_name} eq 'table') {                  !!!cp ('t144');
3824                ## have a table element in table scope                  #
3825                my $i;                } else {
3826                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  !!!cp ('t145');
3827                  my $node = $self->{open_elements}->[$_];                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
3828                  ## Ignore the token                  ## Ignore the token
3829                  !!!next-token;                  !!!next-token;
3830                  redo B;                  redo B;
3831                }                }
3832                              }
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
3833    
3834                splice @{$self->{open_elements}}, $i;              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3835                  !!!cp ('t146');
3836                  ## As if </noscript>
3837                  pop @{$self->{open_elements}};
3838                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
3839                  
3840                  ## Reprocess in the "in head" insertion mode...
3841                  ## As if </head>
3842                  pop @{$self->{open_elements}};
3843    
3844                $self->_reset_insertion_mode;                ## Reprocess in the "after head" insertion mode...
3845                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3846                  !!!cp ('t147');
3847                  ## As if </head>
3848                  pop @{$self->{open_elements}};
3849    
3850                !!!next-token;                ## Reprocess in the "after head" insertion mode...
3851                redo B;              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3852              } elsif ({  ## ISSUE: This case cannot be reached?
3853                        body => 1, caption => 1, col => 1, colgroup => 1,                !!!cp ('t148');
3854                        html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
3855                        thead => 1, tr => 1,                ## Ignore the token ## ISSUE: An issue in the spec.
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
3856                !!!next-token;                !!!next-token;
3857                redo B;                redo B;
3858              } else {              } else {
3859                #                !!!cp ('t149');
3860              }              }
           } else {  
             #  
           }  
3861    
3862            !!!parse-error (type => 'in table:'.$token->{tag_name});              ## "after head" insertion mode
3863            $in_body->($insert_to_foster);              ## As if <body>
3864            redo B;              !!!insert-element ('body');
3865          } elsif ($self->{insertion_mode} eq 'in caption') {              $self->{insertion_mode} = IN_BODY_IM;
3866            if ($token->{type} eq 'character') {              ## reprocess
3867              ## NOTE: This is a code clone of "character in body".              redo B;
3868          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
3869            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3870              !!!cp ('t149.1');
3871    
3872              ## NOTE: As if <head>
3873              !!!create-element ($self->{head_element}, 'head');
3874              $self->{open_elements}->[-1]->[0]->append_child
3875                  ($self->{head_element});
3876              #push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3877              #$self->{insertion_mode} = IN_HEAD_IM;
3878              ## NOTE: Reprocess.
3879    
3880              ## NOTE: As if </head>
3881              #pop @{$self->{open_elements}};
3882              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
3883              ## NOTE: Reprocess.
3884              
3885              #
3886            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3887              !!!cp ('t149.2');
3888    
3889              ## NOTE: As if </head>
3890              pop @{$self->{open_elements}};
3891              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
3892              ## NOTE: Reprocess.
3893    
3894              #
3895            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3896              !!!cp ('t149.3');
3897    
3898              !!!parse-error (type => 'in noscript:#eof', token => $token);
3899    
3900              ## As if </noscript>
3901              pop @{$self->{open_elements}};
3902              #$self->{insertion_mode} = IN_HEAD_IM;
3903              ## NOTE: Reprocess.
3904    
3905              ## NOTE: As if </head>
3906              pop @{$self->{open_elements}};
3907              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
3908              ## NOTE: Reprocess.
3909    
3910              #
3911            } else {
3912              !!!cp ('t149.4');
3913              #
3914            }
3915    
3916            ## NOTE: As if <body>
3917            !!!insert-element ('body');
3918            $self->{insertion_mode} = IN_BODY_IM;
3919            ## NOTE: Reprocess.
3920            redo B;
3921          } else {
3922            die "$0: $token->{type}: Unknown token type";
3923          }
3924    
3925              ## ISSUE: An issue in the spec.
3926        } elsif ($self->{insertion_mode} & BODY_IMS) {
3927              if ($token->{type} == CHARACTER_TOKEN) {
3928                !!!cp ('t150');
3929                ## NOTE: There is a code clone of "character in body".
3930              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
3931                            
3932              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3933    
3934              !!!next-token;              !!!next-token;
3935              redo B;              redo B;
3936            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
3937              if ({              if ({
3938                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
3939                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
3940                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3941                !!!parse-error (type => 'not closed:caption');                if ($self->{insertion_mode} == IN_CELL_IM) {
3942                    ## have an element in table scope
3943                ## As if </caption>                  for (reverse 0..$#{$self->{open_elements}}) {
3944                ## have a table element in table scope                    my $node = $self->{open_elements}->[$_];
3945                my $i;                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {
3946                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                      !!!cp ('t151');
3947                  my $node = $self->{open_elements}->[$_];  
3948                  if ($node->[1] eq 'caption') {                      ## Close the cell
3949                    $i = $_;                      !!!back-token; # <?>
3950                    last INSCOPE;                      $token = {type => END_TAG_TOKEN, tag_name => $node->[1]};
3951                  } elsif ({                      redo B;
3952                            table => 1, html => 1,                    } elsif ({
3953                           }->{$node->[1]}) {                              table => 1, html => 1,
3954                    last INSCOPE;                             }->{$node->[1]}) {
3955                        !!!cp ('t152');
3956                        ## ISSUE: This case can never be reached, maybe.
3957                        last;
3958                      }
3959                  }                  }
3960                } # INSCOPE  
3961                unless (defined $i) {                  !!!cp ('t153');
3962                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'start tag not allowed',
3963                        value => $token->{tag_name}, token => $token);
3964                  ## Ignore the token                  ## Ignore the token
3965                  !!!next-token;                  !!!next-token;
3966                  redo B;                  redo B;
3967                }                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3968                                  !!!parse-error (type => 'not closed:caption', token => $token);
3969                ## generate implied end tags                  
3970                if ({                  ## NOTE: As if </caption>.
3971                     dd => 1, dt => 1, li => 1, p => 1,                  ## have a table element in table scope
3972                     td => 1, th => 1, tr => 1,                  my $i;
3973                     tbody => 1, tfoot=> 1, thead => 1,                  INSCOPE: {
3974                    }->{$self->{open_elements}->[-1]->[1]}) {                    for (reverse 0..$#{$self->{open_elements}}) {
3975                  !!!back-token; # <?>                      my $node = $self->{open_elements}->[$_];
3976                  $token = {type => 'end tag', tag_name => 'caption'};                      if ($node->[1] eq 'caption') {
3977                  !!!back-token;                        !!!cp ('t155');
3978                  $token = {type => 'end tag',                        $i = $_;
3979                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                        last INSCOPE;
3980                  redo B;                      } elsif ({
3981                }                                table => 1, html => 1,
3982                                 }->{$node->[1]}) {
3983                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                        !!!cp ('t156');
3984                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                        last;
3985                }                      }
3986                      }
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
3987    
3988                $self->{insertion_mode} = 'in table';                    !!!cp ('t157');
3989                      !!!parse-error (type => 'start tag not allowed',
3990                                      value => $token->{tag_name}, token => $token);
3991                      ## Ignore the token
3992                      !!!next-token;
3993                      redo B;
3994                    } # INSCOPE
3995                    
3996                    ## generate implied end tags
3997                    while ({
3998                            dd => 1, dt => 1, li => 1, p => 1,
3999                           }->{$self->{open_elements}->[-1]->[1]}) {
4000                      !!!cp ('t158');
4001                      pop @{$self->{open_elements}};
4002                    }
4003    
4004                ## reprocess                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
4005                redo B;                    !!!cp ('t159');
4006                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
4007                    } else {
4008                      !!!cp ('t160');
4009                    }
4010                    
4011                    splice @{$self->{open_elements}}, $i;
4012                    
4013                    $clear_up_to_marker->();
4014                    
4015                    $self->{insertion_mode} = IN_TABLE_IM;
4016                    
4017                    ## reprocess
4018                    redo B;
4019                  } else {
4020                    !!!cp ('t161');
4021                    #
4022                  }
4023              } else {              } else {
4024                  !!!cp ('t162');
4025                #                #
4026              }              }
4027            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
4028              if ($token->{tag_name} eq 'caption') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
4029                ## have a table element in table scope                if ($self->{insertion_mode} == IN_CELL_IM) {
4030                my $i;                  ## have an element in table scope
4031                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  my $i;
4032                  my $node = $self->{open_elements}->[$_];                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4033                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
4034                    $i = $_;                    if ($node->[1] eq $token->{tag_name}) {
4035                    last INSCOPE;                      !!!cp ('t163');
4036                  } elsif ({                      $i = $_;
4037                            table => 1, html => 1,                      last INSCOPE;
4038                           }->{$node->[1]}) {                    } elsif ({
4039                    last INSCOPE;                              table => 1, html => 1,
4040                               }->{$node->[1]}) {
4041                        !!!cp ('t164');
4042                        last INSCOPE;
4043                      }
4044                    } # INSCOPE
4045                      unless (defined $i) {
4046                        !!!cp ('t165');
4047                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4048                        ## Ignore the token
4049                        !!!next-token;
4050                        redo B;
4051                      }
4052                    
4053                    ## generate implied end tags
4054                    while ({
4055                            dd => 1, dt => 1, li => 1, p => 1,
4056                           }->{$self->{open_elements}->[-1]->[1]}) {
4057                      !!!cp ('t166');
4058                      pop @{$self->{open_elements}};
4059                  }                  }
4060                } # INSCOPE  
4061                unless (defined $i) {                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
4062                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t167');
4063                  ## Ignore the token                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
4064                    } else {
4065                      !!!cp ('t168');
4066                    }
4067                    
4068                    splice @{$self->{open_elements}}, $i;
4069                    
4070                    $clear_up_to_marker->();
4071                    
4072                    $self->{insertion_mode} = IN_ROW_IM;
4073                    
4074                  !!!next-token;                  !!!next-token;
4075                  redo B;                  redo B;
4076                }                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4077                                  !!!cp ('t169');
4078                ## generate implied end tags                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4079                if ({                  ## Ignore the token
4080                     dd => 1, dt => 1, li => 1, p => 1,                  !!!next-token;
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
4081                  redo B;                  redo B;
4082                  } else {
4083                    !!!cp ('t170');
4084                    #
4085                }                }
4086                } elsif ($token->{tag_name} eq 'caption') {
4087                  if ($self->{insertion_mode} == IN_CAPTION_IM) {
4088                    ## have a table element in table scope
4089                    my $i;
4090                    INSCOPE: {
4091                      for (reverse 0..$#{$self->{open_elements}}) {
4092                        my $node = $self->{open_elements}->[$_];
4093                        if ($node->[1] eq $token->{tag_name}) {
4094                          !!!cp ('t171');
4095                          $i = $_;
4096                          last INSCOPE;
4097                        } elsif ({
4098                                  table => 1, html => 1,
4099                                 }->{$node->[1]}) {
4100                          !!!cp ('t172');
4101                          last;
4102                        }
4103                      }
4104    
4105                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                    !!!cp ('t173');
4106                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'unmatched end tag',
4107                                      value => $token->{tag_name}, token => $token);
4108                      ## Ignore the token
4109                      !!!next-token;
4110                      redo B;
4111                    } # INSCOPE
4112                    
4113                    ## generate implied end tags
4114                    while ({
4115                            dd => 1, dt => 1, li => 1, p => 1,
4116                           }->{$self->{open_elements}->[-1]->[1]}) {
4117                      !!!cp ('t174');
4118                      pop @{$self->{open_elements}};
4119                    }
4120                    
4121                    if ($self->{open_elements}->[-1]->[1] ne 'caption') {
4122                      !!!cp ('t175');
4123                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
4124                    } else {
4125                      !!!cp ('t176');
4126                    }
4127                    
4128                    splice @{$self->{open_elements}}, $i;
4129                    
4130                    $clear_up_to_marker->();
4131                    
4132                    $self->{insertion_mode} = IN_TABLE_IM;
4133                    
4134                    !!!next-token;
4135                    redo B;
4136                  } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4137                    !!!cp ('t177');
4138                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4139                    ## Ignore the token
4140                    !!!next-token;
4141                    redo B;
4142                  } else {
4143                    !!!cp ('t178');
4144                    #
4145                }                }
4146                } elsif ({
4147                          table => 1, tbody => 1, tfoot => 1,
4148                          thead => 1, tr => 1,
4149                         }->{$token->{tag_name}} and
4150                         $self->{insertion_mode} == IN_CELL_IM) {
4151                  ## have an element in table scope
4152                  my $i;
4153                  my $tn;
4154                  INSCOPE: {
4155                    for (reverse 0..$#{$self->{open_elements}}) {
4156                      my $node = $self->{open_elements}->[$_];
4157                      if ($node->[1] eq $token->{tag_name}) {
4158                        !!!cp ('t179');
4159                        $i = $_;
4160    
4161                        ## Close the cell
4162                        !!!back-token; # </?>
4163                        $token = {type => END_TAG_TOKEN, tag_name => $tn};
4164                        redo B;
4165                      } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {
4166                        !!!cp ('t180');
4167                        $tn = $node->[1];
4168                        ## NOTE: There is exactly one |td| or |th| element
4169                        ## in scope in the stack of open elements by definition.
4170                      } elsif ({
4171                                table => 1, html => 1,
4172                               }->{$node->[1]}) {
4173                        ## ISSUE: Can this be reached?
4174                        !!!cp ('t181');
4175                        last;
4176                      }
4177                    }
4178    
4179                splice @{$self->{open_elements}}, $i;                  !!!cp ('t182');
4180                    !!!parse-error (type => 'unmatched end tag',
4181                $clear_up_to_marker->();                      value => $token->{tag_name}, token => $token);
4182                    ## Ignore the token
4183                $self->{insertion_mode} = 'in table';                  !!!next-token;
4184                    redo B;
4185                !!!next-token;                } # INSCOPE
4186                redo B;              } elsif ($token->{tag_name} eq 'table' and
4187              } elsif ($token->{tag_name} eq 'table') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4188                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption', token => $token);
4189    
4190                ## As if </caption>                ## As if </caption>
4191                ## have a table element in table scope                ## have a table element in table scope
# Line 3996  sub _tree_construction_main ($) { Line 4193  sub _tree_construction_main ($) {
4193                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4194                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4195                  if ($node->[1] eq 'caption') {                  if ($node->[1] eq 'caption') {
4196                      !!!cp ('t184');
4197                    $i = $_;                    $i = $_;
4198                    last INSCOPE;                    last INSCOPE;
4199                  } elsif ({                  } elsif ({
4200                            table => 1, html => 1,                            table => 1, html => 1,
4201                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4202                      !!!cp ('t185');
4203                    last INSCOPE;                    last INSCOPE;
4204                  }                  }
4205                } # INSCOPE                } # INSCOPE
4206                unless (defined $i) {                unless (defined $i) {
4207                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
4208                    !!!parse-error (type => 'unmatched end tag:caption', token => $token);
4209                  ## Ignore the token                  ## Ignore the token
4210                  !!!next-token;                  !!!next-token;
4211                  redo B;                  redo B;
4212                }                }
4213                                
4214                ## generate implied end tags                ## generate implied end tags
4215                if ({                while ({
4216                     dd => 1, dt => 1, li => 1, p => 1,                        dd => 1, dt => 1, li => 1, p => 1,
4217                     td => 1, th => 1, tr => 1,                       }->{$self->{open_elements}->[-1]->[1]}) {
4218                     tbody => 1, tfoot=> 1, thead => 1,                  !!!cp ('t187');
4219                    }->{$self->{open_elements}->[-1]->[1]}) {                  pop @{$self->{open_elements}};
                 !!!back-token; # </table>  
                 $token = {type => 'end tag', tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
4220                }                }
4221    
4222                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                if ($self->{open_elements}->[-1]->[1] ne 'caption') {
4223                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
4224                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
4225                  } else {
4226                    !!!cp ('t189');
4227                }                }
4228    
4229                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4230    
4231                $clear_up_to_marker->();                $clear_up_to_marker->();
4232    
4233                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
4234    
4235                ## reprocess                ## reprocess
4236                redo B;                redo B;
4237              } elsif ({              } elsif ({
4238                        body => 1, col => 1, colgroup => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
                       html => 1, tbody => 1, td => 1, tfoot => 1,  
                       th => 1, thead => 1, tr => 1,  
4239                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4240                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
4241                ## Ignore the token                  !!!cp ('t190');
4242                !!!next-token;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
                 
           $in_body->($insert_to_current);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in column group') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'col') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}};  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'colgroup') {  
               if ($self->{open_elements}->[-1]->[1] eq 'html') {  
                 !!!parse-error (type => 'unmatched end tag:colgroup');  
4243                  ## Ignore the token                  ## Ignore the token
4244                  !!!next-token;                  !!!next-token;
4245                  redo B;                  redo B;
4246                } else {                } else {
4247                  pop @{$self->{open_elements}}; # colgroup                  !!!cp ('t191');
4248                  $self->{insertion_mode} = 'in table';                  #
                 !!!next-token;  
                 redo B;              
4249                }                }
4250              } elsif ($token->{tag_name} eq 'col') {              } elsif ({
4251                !!!parse-error (type => 'unmatched end tag:col');                        tbody => 1, tfoot => 1,
4252                          thead => 1, tr => 1,
4253                         }->{$token->{tag_name}} and
4254                         $self->{insertion_mode} == IN_CAPTION_IM) {
4255                  !!!cp ('t192');
4256                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4257                ## Ignore the token                ## Ignore the token
4258                !!!next-token;                !!!next-token;
4259                redo B;                redo B;
4260              } else {              } else {
4261                #                !!!cp ('t193');
4262                  #
4263              }              }
4264            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4265              #          for my $entry (@{$self->{open_elements}}) {
4266              if (not {
4267                dd => 1, dt => 1, li => 1, p => 1, tbody => 1, td => 1, tfoot => 1,
4268                th => 1, thead => 1, tr => 1, body => 1, html => 1,
4269              }->{$entry->[1]}) {
4270                !!!cp ('t75');
4271                !!!parse-error (type => 'in body:#eof', token => $token);
4272                last;
4273            }            }
4274            }
4275    
4276            ## As if </colgroup>          ## Stop parsing.
4277            if ($self->{open_elements}->[-1]->[1] eq 'html') {          last B;
4278              !!!parse-error (type => 'unmatched end tag:colgroup');        } else {
4279              ## Ignore the token          die "$0: $token->{type}: Unknown token type";
4280          }
4281    
4282          $insert = $insert_to_current;
4283          #
4284        } elsif ($self->{insertion_mode} & TABLE_IMS) {
4285          if ($token->{type} == CHARACTER_TOKEN) {
4286            if (not $open_tables->[-1]->[1] and # tainted
4287                $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4288              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4289                  
4290              unless (length $token->{data}) {
4291                !!!cp ('t194');
4292              !!!next-token;              !!!next-token;
4293              redo B;              redo B;
4294            } else {            } else {
4295              pop @{$self->{open_elements}}; # colgroup              !!!cp ('t195');
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
4296            }            }
4297          } elsif ($self->{insertion_mode} eq 'in table body') {          }
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a "character in table" code clone.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
4298    
4299              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character', token => $token);
4300    
4301              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
4302              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
4303              ## into the current node" while characters might not be              ## into the current node" while characters might not be
4304              ## result in a new Text node.              ## result in a new Text node.
4305              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
4306                
4307              if ({              if ({
4308                   table => 1, tbody => 1, tfoot => 1,                   table => 1, tbody => 1, tfoot => 1,
4309                   thead => 1, tr => 1,                   thead => 1, tr => 1,
# Line 4144  sub _tree_construction_main ($) { Line 4316  sub _tree_construction_main ($) {
4316                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] eq 'table') {
4317                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4318                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
4319                        !!!cp ('t196');
4320                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
4321                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
4322                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
4323                    } else {                    } else {
4324                        !!!cp ('t197');
4325                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
4326                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
4327                    }                    }
# Line 4159  sub _tree_construction_main ($) { Line 4333  sub _tree_construction_main ($) {
4333                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
4334                if (defined $prev_sibling and                if (defined $prev_sibling and
4335                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
4336                    !!!cp ('t198');
4337                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
4338                } else {                } else {
4339                    !!!cp ('t199');
4340                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
4341                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
4342                     $next_sibling);                     $next_sibling);
4343                }                }
4344              } else {            $open_tables->[-1]->[1] = 1; # tainted
4345                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
4346              }            !!!cp ('t200');
4347              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4348            }
4349                            
4350              !!!next-token;          !!!next-token;
4351              redo B;          redo B;
4352            } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4353              if ({              if ({
4354                   tr => 1,                   tr => ($self->{insertion_mode} != IN_ROW_IM),
4355                   th => 1, td => 1,                   th => 1, td => 1,
4356                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4357                unless ($token->{tag_name} eq 'tr') {                if ($self->{insertion_mode} == IN_TABLE_IM) {
4358                  !!!parse-error (type => 'missing start tag:tr');                  ## Clear back to table context
4359                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
4360                           $self->{open_elements}->[-1]->[1] ne 'html') {
4361                      !!!cp ('t201');
4362                      pop @{$self->{open_elements}};
4363                    }
4364                    
4365                    !!!insert-element ('tbody');
4366                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
4367                    ## reprocess in the "in table body" insertion mode...
4368                }                }
4369    
4370                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4371                    unless ($token->{tag_name} eq 'tr') {
4372                      !!!cp ('t202');
4373                      !!!parse-error (type => 'missing start tag:tr', token => $token);
4374                    }
4375                    
4376                    ## Clear back to table body context
4377                    while (not {
4378                      tbody => 1, tfoot => 1, thead => 1, html => 1,
4379                    }->{$self->{open_elements}->[-1]->[1]}) {
4380                      !!!cp ('t203');
4381                      ## ISSUE: Can this case be reached?
4382                      pop @{$self->{open_elements}};
4383                    }
4384                    
4385                    $self->{insertion_mode} = IN_ROW_IM;
4386                    if ($token->{tag_name} eq 'tr') {
4387                      !!!cp ('t204');
4388                      !!!insert-element ($token->{tag_name}, $token->{attributes});
4389                      !!!next-token;
4390                      redo B;
4391                    } else {
4392                      !!!cp ('t205');
4393                      !!!insert-element ('tr');
4394                      ## reprocess in the "in row" insertion mode
4395                    }
4396                  } else {
4397                    !!!cp ('t206');
4398                  }
4399    
4400                  ## Clear back to table row context
4401                while (not {                while (not {
4402                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tr => 1, html => 1,
4403                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4404                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t207');
4405                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4406                }                }
4407                                
4408                $self->{insertion_mode} = 'in row';                !!!insert-element ($token->{tag_name}, $token->{attributes});
4409                if ($token->{tag_name} eq 'tr') {                $self->{insertion_mode} = IN_CELL_IM;
4410                  !!!insert-element ($token->{tag_name}, $token->{attributes});  
4411                  !!!next-token;                push @$active_formatting_elements, ['#marker', ''];
4412                } else {                
4413                  !!!insert-element ('tr');                !!!next-token;
                 ## reprocess  
               }  
4414                redo B;                redo B;
4415              } elsif ({              } elsif ({
4416                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
4417                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
4418                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
4419                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4420                ## have an element in table scope                if ($self->{insertion_mode} == IN_ROW_IM) {
4421                my $i;                  ## As if </tr>
4422                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
4423                  my $node = $self->{open_elements}->[$_];                  my $i;
4424                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4425                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
4426                      }->{$node->[1]}) {                    if ($node->[1] eq 'tr') {
4427                    $i = $_;                      !!!cp ('t208');
4428                    last INSCOPE;                      $i = $_;
4429                  } elsif ({                      last INSCOPE;
4430                            table => 1, html => 1,                    } elsif ({
4431                           }->{$node->[1]}) {                              html => 1,
4432                    last INSCOPE;  
4433                                ## NOTE: This element does not appear here, maybe.
4434                                table => 1,
4435                               }->{$node->[1]}) {
4436                        !!!cp ('t209');
4437                        last INSCOPE;
4438                      }
4439                    } # INSCOPE
4440                    unless (defined $i) {
4441                     !!!cp ('t210');
4442    ## TODO: This type is wrong.
4443                     !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
4444                      ## Ignore the token
4445                      !!!next-token;
4446                      redo B;
4447                    }
4448                    
4449                    ## Clear back to table row context
4450                    while (not {
4451                      tr => 1, html => 1,
4452                    }->{$self->{open_elements}->[-1]->[1]}) {
4453                      !!!cp ('t211');
4454                      ## ISSUE: Can this case be reached?
4455                      pop @{$self->{open_elements}};
4456                    }
4457                    
4458                    pop @{$self->{open_elements}}; # tr
4459                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
4460                    if ($token->{tag_name} eq 'tr') {
4461                      !!!cp ('t212');
4462                      ## reprocess
4463                      redo B;
4464                    } else {
4465                      !!!cp ('t213');
4466                      ## reprocess in the "in table body" insertion mode...
4467                  }                  }
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
4468                }                }
4469    
4470                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4471                while (not {                  ## have an element in table scope
4472                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  my $i;
4473                }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4474                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    my $node = $self->{open_elements}->[$_];
4475                      if ({
4476                           tbody => 1, thead => 1, tfoot => 1,
4477                          }->{$node->[1]}) {
4478                        !!!cp ('t214');
4479                        $i = $_;
4480                        last INSCOPE;
4481                      } elsif ({
4482                                table => 1, html => 1,
4483                               }->{$node->[1]}) {
4484                        !!!cp ('t215');
4485                        last INSCOPE;
4486                      }
4487                    } # INSCOPE
4488                    unless (defined $i) {
4489                      !!!cp ('t216');
4490    ## TODO: This erorr type ios wrong.
4491                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4492                      ## Ignore the token
4493                      !!!next-token;
4494                      redo B;
4495                    }
4496    
4497                    ## Clear back to table body context
4498                    while (not {
4499                      tbody => 1, tfoot => 1, thead => 1, html => 1,
4500                    }->{$self->{open_elements}->[-1]->[1]}) {
4501                      !!!cp ('t217');
4502                      ## ISSUE: Can this state be reached?
4503                      pop @{$self->{open_elements}};
4504                    }
4505                    
4506                    ## As if <{current node}>
4507                    ## have an element in table scope
4508                    ## true by definition
4509                    
4510                    ## Clear back to table body context
4511                    ## nop by definition
4512                    
4513                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4514                    $self->{insertion_mode} = IN_TABLE_IM;
4515                    ## reprocess in "in table" insertion mode...
4516                  } else {
4517                    !!!cp ('t218');
4518                }                }
4519    
4520                ## As if <{current node}>                if ($token->{tag_name} eq 'col') {
4521                ## have an element in table scope                  ## Clear back to table context
4522                ## true by definition                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
4523                           $self->{open_elements}->[-1]->[1] ne 'html') {
4524                ## Clear back to table body context                    !!!cp ('t219');
4525                ## nop by definition                    ## ISSUE: Can this state be reached?
4526                      pop @{$self->{open_elements}};
4527                pop @{$self->{open_elements}};                  }
4528                $self->{insertion_mode} = 'in table';                  
4529                ## reprocess                  !!!insert-element ('colgroup');
4530                redo B;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
4531                    ## reprocess
4532                    redo B;
4533                  } elsif ({
4534                            caption => 1,
4535                            colgroup => 1,
4536                            tbody => 1, tfoot => 1, thead => 1,
4537                           }->{$token->{tag_name}}) {
4538                    ## Clear back to table context
4539                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
4540                           $self->{open_elements}->[-1]->[1] ne 'html') {
4541                      !!!cp ('t220');
4542                      ## ISSUE: Can this state be reached?
4543                      pop @{$self->{open_elements}};
4544                    }
4545                    
4546                    push @$active_formatting_elements, ['#marker', '']
4547                        if $token->{tag_name} eq 'caption';
4548                    
4549                    !!!insert-element ($token->{tag_name}, $token->{attributes});
4550                    $self->{insertion_mode} = {
4551                                               caption => IN_CAPTION_IM,
4552                                               colgroup => IN_COLUMN_GROUP_IM,
4553                                               tbody => IN_TABLE_BODY_IM,
4554                                               tfoot => IN_TABLE_BODY_IM,
4555                                               thead => IN_TABLE_BODY_IM,
4556                                              }->{$token->{tag_name}};
4557                    !!!next-token;
4558                    redo B;
4559                  } else {
4560                    die "$0: in table: <>: $token->{tag_name}";
4561                  }
4562              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4563                ## NOTE: This is a code clone of "table in table"                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
               !!!parse-error (type => 'not closed:table');  
4564    
4565                ## As if </table>                ## As if </table>
4566                ## have a table element in table scope                ## have a table element in table scope
# Line 4252  sub _tree_construction_main ($) { Line 4568  sub _tree_construction_main ($) {
4568                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4569                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4570                  if ($node->[1] eq 'table') {                  if ($node->[1] eq 'table') {
4571                      !!!cp ('t221');
4572                    $i = $_;                    $i = $_;
4573                    last INSCOPE;                    last INSCOPE;
4574                  } elsif ({                  } elsif ({
4575                            table => 1, html => 1,                            #table => 1,
4576                              html => 1,
4577                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4578                      !!!cp ('t222');
4579                    last INSCOPE;                    last INSCOPE;
4580                  }                  }
4581                } # INSCOPE                } # INSCOPE
4582                unless (defined $i) {                unless (defined $i) {
4583                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
4584    ## TODO: The following is wrong, maybe.
4585                    !!!parse-error (type => 'unmatched end tag:table', token => $token);
4586                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
4587                  !!!next-token;                  !!!next-token;
4588                  redo B;                  redo B;
4589                }                }
4590                                
4591    ## TODO: Followings are removed from the latest spec.
4592                ## generate implied end tags                ## generate implied end tags
4593                if ({                while ({
4594                     dd => 1, dt => 1, li => 1, p => 1,                        dd => 1, dt => 1, li => 1, p => 1,
4595                     td => 1, th => 1, tr => 1,                       }->{$self->{open_elements}->[-1]->[1]}) {
4596                     tbody => 1, tfoot=> 1, thead => 1,                  !!!cp ('t224');
4597                    }->{$self->{open_elements}->[-1]->[1]}) {                  pop @{$self->{open_elements}};
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
4598                }                }
4599    
4600                if ($self->{open_elements}->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
4601                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
4602    ## ISSUE: Can this case be reached?
4603                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
4604                  } else {
4605                    !!!cp ('t226');
4606                }                }
4607    
4608                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4609                  pop @{$open_tables};
4610    
4611                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
4612    
4613                ## reprocess                ## reprocess
4614                redo B;                redo B;
4615              } else {          } elsif ($token->{tag_name} eq 'style') {
4616                #            if (not $open_tables->[-1]->[1]) { # tainted
4617              }              !!!cp ('t227.8');
4618            } elsif ($token->{type} eq 'end tag') {              ## NOTE: This is a "as if in head" code clone.
4619              if ({              $parse_rcdata->(CDATA_CONTENT_MODEL);
4620                   tbody => 1, tfoot => 1, thead => 1,              redo B;
4621                  }->{$token->{tag_name}}) {            } else {
4622                ## have an element in table scope              !!!cp ('t227.7');
4623                my $i;              #
4624                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            }
4625                  my $node = $self->{open_elements}->[$_];          } elsif ($token->{tag_name} eq 'script') {
4626                  if ($node->[1] eq $token->{tag_name}) {            if (not $open_tables->[-1]->[1]) { # tainted
4627                    $i = $_;              !!!cp ('t227.6');
4628                    last INSCOPE;              ## NOTE: This is a "as if in head" code clone.
4629                  } elsif ({              $script_start_tag->();
4630                            table => 1, html => 1,              redo B;
4631                           }->{$node->[1]}) {            } else {
4632                    last INSCOPE;              !!!cp ('t227.5');
4633                  }              #
4634                } # INSCOPE            }
4635                unless (defined $i) {          } elsif ($token->{tag_name} eq 'input') {
4636                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            if (not $open_tables->[-1]->[1]) { # tainted
4637                  ## Ignore the token              if ($token->{attributes}->{type}) { ## TODO: case
4638                  !!!next-token;                my $type = lc $token->{attributes}->{type}->{value};
4639                  redo B;                if ($type eq 'hidden') {
4640                }                  !!!cp ('t227.3');
4641                    !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
4642    
4643                    !!!insert-element ($token->{tag_name}, $token->{attributes});
4644    
4645                    ## TODO: form element pointer
4646    
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4647                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
               }  
4648    
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ({  
                      tbody => 1, thead => 1, tfoot => 1,  
                     }->{$node->[1]}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
4649                  !!!next-token;                  !!!next-token;
4650                  redo B;                  redo B;
4651                  } else {
4652                    !!!cp ('t227.2');
4653                    #
4654                }                }
   
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
   
               ## Clear back to table body context  
               ## nop by definition  
   
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               ## reprocess  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1, colgroup => 1,  
                       html => 1, td => 1, th => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
4655              } else {              } else {
4656                  !!!cp ('t227.1');
4657                #                #
4658              }              }
4659            } else {            } else {
4660                !!!cp ('t227.4');
4661              #              #
4662            }            }
4663                      } else {
4664            ## As if in table            !!!cp ('t227');
4665            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
4666            $in_body->($insert_to_foster);          }
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in row') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a "character in table" code clone.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
4667    
4668              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
4669    
4670              ## As if in body, but insert into foster parent element          $insert = $insert_to_foster;
4671              ## ISSUE: Spec says that "whenever a node would be inserted          #
4672              ## into the current node" while characters might not be        } elsif ($token->{type} == END_TAG_TOKEN) {
4673              ## result in a new Text node.              if ($token->{tag_name} eq 'tr' and
4674              $reconstruct_active_formatting_elements->($insert_to_foster);                  $self->{insertion_mode} == IN_ROW_IM) {
               
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'th' or  
                 $token->{tag_name} eq 'td') {  
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
                 
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = 'in cell';  
   
               push @$active_formatting_elements, ['#marker', ''];  
                 
               !!!next-token;  
               redo B;  
             } elsif ({  
                       caption => 1, col => 1, colgroup => 1,  
                       tbody => 1, tfoot => 1, thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## As if </tr>  
4675                ## have an element in table scope                ## have an element in table scope
4676                my $i;                my $i;
4677                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4678                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4679                  if ($node->[1] eq 'tr') {                  if ($node->[1] eq $token->{tag_name}) {
4680                      !!!cp ('t228');
4681                    $i = $_;                    $i = $_;
4682                    last INSCOPE;                    last INSCOPE;
4683                  } elsif ({                  } elsif ({
4684                            table => 1, html => 1,                            table => 1, html => 1,
4685                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4686                      !!!cp ('t229');
4687                    last INSCOPE;                    last INSCOPE;
4688                  }                  }
4689                } # INSCOPE                } # INSCOPE
4690                unless (defined $i) {                unless (defined $i) {
4691                  !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                  !!!cp ('t230');
4692                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4693                  ## Ignore the token                  ## Ignore the token
4694                  !!!next-token;                  !!!next-token;
4695                  redo B;                  redo B;
4696                  } else {
4697                    !!!cp ('t232');
4698                }                }
4699    
4700                ## Clear back to table row context                ## Clear back to table row context
4701                while (not {                while (not {
4702                  tr => 1, html => 1,                  tr => 1, html => 1,
4703                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4704                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t231');
4705    ## ISSUE: Can this state be reached?
4706                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4707                }                }
4708    
4709                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
4710                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
4711                ## reprocess                !!!next-token;
4712                redo B;                redo B;
4713              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4714                ## NOTE: This is a code clone of "table in table"                if ($self->{insertion_mode} == IN_ROW_IM) {
4715                !!!parse-error (type => 'not closed:table');                  ## As if </tr>
4716                    ## have an element in table scope
4717                ## As if </table>                  my $i;
4718                ## have a table element in table scope                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4719                my $i;                    my $node = $self->{open_elements}->[$_];
4720                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($node->[1] eq 'tr') {
4721                  my $node = $self->{open_elements}->[$_];                      !!!cp ('t233');
4722                  if ($node->[1] eq 'table') {                      $i = $_;
4723                    $i = $_;                      last INSCOPE;
4724                    last INSCOPE;                    } elsif ({
4725                  } elsif ({                              table => 1, html => 1,
4726                            table => 1, html => 1,                             }->{$node->[1]}) {
4727                           }->{$node->[1]}) {                      !!!cp ('t234');
4728                    last INSCOPE;                      last INSCOPE;
4729                      }
4730                    } # INSCOPE
4731                    unless (defined $i) {
4732                      !!!cp ('t235');
4733    ## TODO: The following is wrong.
4734                      !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
4735                      ## Ignore the token
4736                      !!!next-token;
4737                      redo B;
4738                  }                  }
4739                } # INSCOPE                  
4740                unless (defined $i) {                  ## Clear back to table row context
4741                  !!!parse-error (type => 'unmatched end tag:table');                  while (not {
4742                  ## Ignore tokens </table><table>                    tr => 1, html => 1,
4743                  !!!next-token;                  }->{$self->{open_elements}->[-1]->[1]}) {
4744                  redo B;                    !!!cp ('t236');
4745                }  ## ISSUE: Can this state be reached?
4746                                    pop @{$self->{open_elements}};
4747                ## generate implied end tags                  }
4748                if ({                  
4749                     dd => 1, dt => 1, li => 1, p => 1,                  pop @{$self->{open_elements}}; # tr
4750                     td => 1, th => 1, tr => 1,                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4751                     tbody => 1, tfoot=> 1, thead => 1,                  ## reprocess in the "in table body" insertion mode...
4752                    }->{$self->{open_elements}->[-1]->[1]}) {                }
4753                  !!!back-token; # <table>  
4754                  $token = {type => 'end tag', tag_name => 'table'};                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4755                  !!!back-token;                  ## have an element in table scope
4756                  $token = {type => 'end tag',                  my $i;
4757                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4758                  redo B;                    my $node = $self->{open_elements}->[$_];
4759                }                    if ({
4760                           tbody => 1, thead => 1, tfoot => 1,
4761                if ($self->{open_elements}->[-1]->[1] ne 'table') {                        }->{$node->[1]}) {
4762                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                      !!!cp ('t237');
4763                        $i = $_;
4764                        last INSCOPE;
4765                      } elsif ({
4766                                table => 1, html => 1,
4767                               }->{$node->[1]}) {
4768                        !!!cp ('t238');
4769                        last INSCOPE;
4770                      }
4771                    } # INSCOPE
4772                    unless (defined $i) {
4773                      !!!cp ('t239');
4774                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4775                      ## Ignore the token
4776                      !!!next-token;
4777                      redo B;
4778                    }
4779                    
4780                    ## Clear back to table body context
4781                    while (not {
4782                      tbody => 1, tfoot => 1, thead => 1, html => 1,
4783                    }->{$self->{open_elements}->[-1]->[1]}) {
4784                      !!!cp ('t240');
4785                      pop @{$self->{open_elements}};
4786                    }
4787                    
4788                    ## As if <{current node}>
4789                    ## have an element in table scope
4790                    ## true by definition
4791                    
4792                    ## Clear back to table body context
4793                    ## nop by definition
4794                    
4795                    pop @{$self->{open_elements}};
4796                    $self->{insertion_mode} = IN_TABLE_IM;
4797                    ## reprocess in the "in table" insertion mode...
4798                }                }
4799    
4800                splice @{$self->{open_elements}}, $i;                ## NOTE: </table> in the "in table" insertion mode.
4801                  ## When you edit the code fragment below, please ensure that
4802                  ## the code for <table> in the "in table" insertion mode
4803                  ## is synced with it.
4804    
4805                $self->_reset_insertion_mode;                ## have a table element in table scope
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'tr') {  
               ## have an element in table scope  
4806                my $i;                my $i;
4807                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4808                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4809                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4810                      !!!cp ('t241');
4811                    $i = $_;                    $i = $_;
4812                    last INSCOPE;                    last INSCOPE;
4813                  } elsif ({                  } elsif ({
4814                            table => 1, html => 1,                            table => 1, html => 1,
4815                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4816                      !!!cp ('t242');
4817                    last INSCOPE;                    last INSCOPE;
4818                  }                  }
4819                } # INSCOPE                } # INSCOPE
4820                unless (defined $i) {                unless (defined $i) {
4821                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t243');
4822                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4823                  ## Ignore the token                  ## Ignore the token
4824                  !!!next-token;                  !!!next-token;
4825                  redo B;                  redo B;
4826                }                }
4827                    
4828                ## Clear back to table row context                splice @{$self->{open_elements}}, $i;
4829                while (not {                pop @{$open_tables};
4830                  tr => 1, html => 1,                
4831                }->{$self->{open_elements}->[-1]->[1]}) {                $self->_reset_insertion_mode;
4832                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
4833                !!!next-token;                !!!next-token;
4834                redo B;                redo B;
             } elsif ($token->{tag_name} eq 'table') {  
               ## As if </tr>  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'tr') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{type});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
4835              } elsif ({              } elsif ({
4836                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
4837                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}} and
4838                ## have an element in table scope                       $self->{insertion_mode} & ROW_IMS) {
4839                my $i;                if ($self->{insertion_mode} == IN_ROW_IM) {
4840                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
4841                  my $node = $self->{open_elements}->[$_];                  my $i;
4842                  if ($node->[1] eq $token->{tag_name}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4843                    $i = $_;                    my $node = $self->{open_elements}->[$_];
4844                    last INSCOPE;                    if ($node->[1] eq $token->{tag_name}) {
4845                  } elsif ({                      !!!cp ('t247');
4846                            table => 1, html => 1,                      $i = $_;
4847                           }->{$node->[1]}) {                      last INSCOPE;
4848                    last INSCOPE;                    } elsif ({
4849                                table => 1, html => 1,
4850                               }->{$node->[1]}) {
4851                        !!!cp ('t248');
4852                        last INSCOPE;
4853                      }
4854                    } # INSCOPE
4855                      unless (defined $i) {
4856                        !!!cp ('t249');
4857                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4858                        ## Ignore the token
4859                        !!!next-token;
4860                        redo B;
4861                      }
4862                    
4863                    ## As if </tr>
4864                    ## have an element in table scope
4865                    my $i;
4866                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4867                      my $node = $self->{open_elements}->[$_];
4868                      if ($node->[1] eq 'tr') {
4869                        !!!cp ('t250');
4870                        $i = $_;
4871                        last INSCOPE;
4872                      } elsif ({
4873                                table => 1, html => 1,
4874                               }->{$node->[1]}) {
4875                        !!!cp ('t251');
4876                        last INSCOPE;
4877                      }
4878                    } # INSCOPE
4879                      unless (defined $i) {
4880                        !!!cp ('t252');
4881                        !!!parse-error (type => 'unmatched end tag:tr', token => $token);
4882                        ## Ignore the token
4883                        !!!next-token;
4884                        redo B;
4885                      }
4886                    
4887                    ## Clear back to table row context
4888                    while (not {
4889                      tr => 1, html => 1,
4890                    }->{$self->{open_elements}->[-1]->[1]}) {
4891                      !!!cp ('t253');
4892    ## ISSUE: Can this case be reached?
4893                      pop @{$self->{open_elements}};
4894                  }                  }
4895                } # INSCOPE                  
4896                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
4897                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4898                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
4899                }                }
4900    
               ## As if </tr>  
4901                ## have an element in table scope                ## have an element in table scope
4902                my $i;                my $i;
4903                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4904                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4905                  if ($node->[1] eq 'tr') {                  if ($node->[1] eq $token->{tag_name}) {
4906                      !!!cp ('t254');
4907                    $i = $_;                    $i = $_;
4908                    last INSCOPE;                    last INSCOPE;
4909                  } elsif ({                  } elsif ({
4910                            table => 1, html => 1,                            table => 1, html => 1,
4911                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4912                      !!!cp ('t255');
4913                    last INSCOPE;                    last INSCOPE;
4914                  }                  }
4915                } # INSCOPE                } # INSCOPE
4916                unless (defined $i) {                unless (defined $i) {
4917                  !!!parse-error (type => 'unmatched end tag:tr');                  !!!cp ('t256');
4918                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4919                  ## Ignore the token                  ## Ignore the token
4920                  !!!next-token;                  !!!next-token;
4921                  redo B;                  redo B;
4922                }                }
4923    
4924                ## Clear back to table row context                ## Clear back to table body context
4925                while (not {                while (not {
4926                  tr => 1, html => 1,                  tbody => 1, tfoot => 1, thead => 1, html => 1,
4927                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4928                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t257');
4929    ## ISSUE: Can this case be reached?
4930                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4931                }                }
4932    
4933                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}};
4934                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_IM;
4935                ## reprocess                !!!next-token;
4936                redo B;                redo B;
4937              } elsif ({              } elsif ({
4938                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
4939                        colgroup => 1, html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
4940                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
4941                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
4942                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4943                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t258');
4944                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4945                ## Ignore the token                ## Ignore the token
4946                !!!next-token;                !!!next-token;
4947                redo B;                redo B;
4948              } else {          } else {
4949                #            !!!cp ('t259');
4950              }            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
           } else {  
             #  
           }  
4951    
4952            ## As if in table            $insert = $insert_to_foster;
4953            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
4954            $in_body->($insert_to_foster);          }
4955            redo B;        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4956          } elsif ($self->{insertion_mode} eq 'in cell') {          unless ($self->{open_elements}->[-1]->[1] eq 'html' and
4957            if ($token->{type} eq 'character') {                  @{$self->{open_elements}} == 1) { # redundant, maybe
4958              ## NOTE: This is a code clone of "character in body".            !!!parse-error (type => 'in body:#eof', token => $token);
4959              $reconstruct_active_formatting_elements->($insert_to_current);            !!!cp ('t259.1');
4960                          #
4961              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
4962              !!!cp ('t259.2');
4963              #
4964            }
4965    
4966              !!!next-token;          ## Stop parsing
4967              redo B;          last B;
4968            } elsif ($token->{type} eq 'start tag') {        } else {
4969              if ({          die "$0: $token->{type}: Unknown token type";
4970                   caption => 1, col => 1, colgroup => 1,        }
4971                   tbody => 1, td => 1, tfoot => 1, th => 1,      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
4972                   thead => 1, tr => 1,            if ($token->{type} == CHARACTER_TOKEN) {
4973                  }->{$token->{tag_name}}) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4974                ## have an element in table scope                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4975                my $tn;                unless (length $token->{data}) {
4976                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  !!!cp ('t260');
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'td' or $node->[1] eq 'th') {  
                   $tn = $node->[1];  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $tn) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
4977                  !!!next-token;                  !!!next-token;
4978                  redo B;                  redo B;
4979                }                }
4980                }
4981                ## Close the cell              
4982                !!!back-token; # <?>              !!!cp ('t261');
4983                $token = {type => 'end tag', tag_name => $tn};              #
4984              } elsif ($token->{type} == START_TAG_TOKEN) {
4985                if ($token->{tag_name} eq 'col') {
4986                  !!!cp ('t262');
4987                  !!!insert-element ($token->{tag_name}, $token->{attributes});
4988                  pop @{$self->{open_elements}};
4989                  !!!next-token;
4990                redo B;                redo B;
4991              } else {              } else {
4992                  !!!cp ('t263');
4993                #                #
4994              }              }
4995            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
4996              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'colgroup') {
4997                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] eq 'html') {
4998                my $i;                  !!!cp ('t264');
4999                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
5000                  ## Ignore the token                  ## Ignore the token
5001                  !!!next-token;                  !!!next-token;
5002                  redo B;                  redo B;
5003                  } else {
5004                    !!!cp ('t265');
5005                    pop @{$self->{open_elements}}; # colgroup
5006                    $self->{insertion_mode} = IN_TABLE_IM;
5007                    !!!next-token;
5008                    redo B;            
5009                }                }
5010                              } elsif ($token->{tag_name} eq 'col') {
5011                ## generate implied end tags                !!!cp ('t266');
5012                if ({                !!!parse-error (type => 'unmatched end tag:col', token => $token);
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => ($token->{tag_name} eq 'th'),  
                    th => ($token->{tag_name} eq 'td'),  
                    tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in row';  
   
               !!!next-token;  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1,  
                       colgroup => 1, html => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
5013                ## Ignore the token                ## Ignore the token
5014                !!!next-token;                !!!next-token;
5015                redo B;                redo B;
             } elsif ({  
                       table => 1, tbody => 1, tfoot => 1,  
                       thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               my $tn;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
                   $tn = $node->[1];  
                   ## NOTE: There is exactly one |td| or |th| element  
                   ## in scope in the stack of open elements by definition.  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => 'end tag', tag_name => $tn};  
               redo B;  
5016              } else {              } else {
5017                #                !!!cp ('t267');
5018                  #
5019              }              }
5020            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5021              #          if ($self->{open_elements}->[-1]->[1] eq 'html' or
5022            }              @{$self->{open_elements}} == 1) { # redundant, maybe
5023                        !!!cp ('t270.2');
5024            $in_body->($insert_to_current);            ## Stop parsing.
5025              last B;
5026            } else {
5027              ## NOTE: As if </colgroup>.
5028              !!!cp ('t270.1');
5029              pop @{$self->{open_elements}}; # colgroup
5030              $self->{insertion_mode} = IN_TABLE_IM;
5031              ## Reprocess.
5032            redo B;            redo B;
5033          } elsif ($self->{insertion_mode} eq 'in select') {          }
5034            if ($token->{type} eq 'character') {        } else {
5035              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          die "$0: $token->{type}: Unknown token type";
5036          }
5037    
5038              ## As if </colgroup>
5039              if ($self->{open_elements}->[-1]->[1] eq 'html') {
5040                !!!cp ('t269');
5041    ## TODO: Wrong error type?
5042                !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5043                ## Ignore the token
5044              !!!next-token;              !!!next-token;
5045              redo B;              redo B;
5046            } elsif ($token->{type} eq 'start tag') {            } else {
5047                !!!cp ('t270');
5048                pop @{$self->{open_elements}}; # colgroup
5049                $self->{insertion_mode} = IN_TABLE_IM;
5050                ## reprocess
5051                redo B;
5052              }
5053        } elsif ($self->{insertion_mode} & SELECT_IMS) {
5054          if ($token->{type} == CHARACTER_TOKEN) {
5055            !!!cp ('t271');
5056            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5057            !!!next-token;
5058            redo B;
5059          } elsif ($token->{type} == START_TAG_TOKEN) {
5060              if ($token->{tag_name} eq 'option') {              if ($token->{tag_name} eq 'option') {
5061                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
5062                    !!!cp ('t272');
5063                  ## As if </option>                  ## As if </option>
5064                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5065                  } else {
5066                    !!!cp ('t273');
5067                }                }
5068    
5069                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
# Line 4857  sub _tree_construction_main ($) { Line 5071  sub _tree_construction_main ($) {
5071                redo B;                redo B;
5072              } elsif ($token->{tag_name} eq 'optgroup') {              } elsif ($token->{tag_name} eq 'optgroup') {
5073                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
5074                    !!!cp ('t274');
5075                  ## As if </option>                  ## As if </option>
5076                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5077                  } else {
5078                    !!!cp ('t275');
5079                }                }
5080    
5081                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
5082                    !!!cp ('t276');
5083                  ## As if </optgroup>                  ## As if </optgroup>
5084                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5085                  } else {
5086                    !!!cp ('t277');
5087                }                }
5088    
5089                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
5090                !!!next-token;                !!!next-token;
5091                redo B;                redo B;
5092              } elsif ($token->{tag_name} eq 'select') {          } elsif ($token->{tag_name} eq 'select' or
5093                !!!parse-error (type => 'not closed:select');                   $token->{tag_name} eq 'input' or
5094                ## As if </select> instead                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5095                      {
5096                       caption => 1, table => 1,
5097                       tbody => 1, tfoot => 1, thead => 1,
5098                       tr => 1, td => 1, th => 1,
5099                      }->{$token->{tag_name}})) {
5100              ## TODO: The type below is not good - <select> is replaced by </select>
5101              !!!parse-error (type => 'not closed:select', token => $token);
5102              ## NOTE: As if the token were </select> (<select> case) or
5103              ## as if there were </select> (otherwise).
5104                ## have an element in table scope                ## have an element in table scope
5105                my $i;                my $i;
5106                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5107                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5108                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq 'select') {
5109                      !!!cp ('t278');
5110                    $i = $_;                    $i = $_;
5111                    last INSCOPE;                    last INSCOPE;
5112                  } elsif ({                  } elsif ({
5113                            table => 1, html => 1,                            table => 1, html => 1,
5114                           }->{$node->[1]}) {                           }->{$node->[1]}) {
5115                      !!!cp ('t279');
5116                    last INSCOPE;                    last INSCOPE;
5117                  }                  }
5118                } # INSCOPE                } # INSCOPE
5119                unless (defined $i) {                unless (defined $i) {
5120                  !!!parse-error (type => 'unmatched end tag:select');                  !!!cp ('t280');
5121                    !!!parse-error (type => 'unmatched end tag:select', token => $token);
5122                  ## Ignore the token                  ## Ignore the token
5123                  !!!next-token;                  !!!next-token;
5124                  redo B;                  redo B;
5125                }                }
5126                                
5127                  !!!cp ('t281');
5128                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5129    
5130                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5131    
5132                !!!next-token;            if ($token->{tag_name} eq 'select') {
5133                redo B;              !!!cp ('t281.2');
5134              } else {              !!!next-token;
5135                #              redo B;
5136              }            } else {
5137            } elsif ($token->{type} eq 'end tag') {              !!!cp ('t281.1');
5138                ## Reprocess the token.
5139                redo B;
5140              }
5141            } else {
5142              !!!cp ('t282');
5143              !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5144              ## Ignore the token
5145              !!!next-token;
5146              redo B;
5147            }
5148          } elsif ($token->{type} == END_TAG_TOKEN) {
5149              if ($token->{tag_name} eq 'optgroup') {              if ($token->{tag_name} eq 'optgroup') {
5150                if ($self->{open_elements}->[-1]->[1] eq 'option' and                if ($self->{open_elements}->[-1]->[1] eq 'option' and
5151                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {
5152                    !!!cp ('t283');
5153                  ## As if </option>                  ## As if </option>
5154                  splice @{$self->{open_elements}}, -2;                  splice @{$self->{open_elements}}, -2;
5155                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
5156                    !!!cp ('t284');
5157                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5158                } else {                } else {
5159                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t285');
5160                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5161                  ## Ignore the token                  ## Ignore the token
5162                }                }
5163                !!!next-token;                !!!next-token;
5164                redo B;                redo B;
5165              } elsif ($token->{tag_name} eq 'option') {              } elsif ($token->{tag_name} eq 'option') {
5166                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
5167                    !!!cp ('t286');
5168                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5169                } else {                } else {
5170                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t287');
5171                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5172                  ## Ignore the token                  ## Ignore the token
5173                }                }
5174                !!!next-token;                !!!next-token;
# Line 4930  sub _tree_construction_main ($) { Line 5179  sub _tree_construction_main ($) {
5179                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5180                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5181                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
5182                      !!!cp ('t288');
5183                    $i = $_;                    $i = $_;
5184                    last INSCOPE;                    last INSCOPE;
5185                  } elsif ({                  } elsif ({
5186                            table => 1, html => 1,                            table => 1, html => 1,
5187                           }->{$node->[1]}) {                           }->{$node->[1]}) {
5188                      !!!cp ('t289');
5189                    last INSCOPE;                    last INSCOPE;
5190                  }                  }
5191                } # INSCOPE                } # INSCOPE
5192                unless (defined $i) {                unless (defined $i) {
5193                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t290');
5194                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5195                  ## Ignore the token                  ## Ignore the token
5196                  !!!next-token;                  !!!next-token;
5197                  redo B;                  redo B;
5198                }                }
5199                                
5200                  !!!cp ('t291');
5201                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5202    
5203                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5204    
5205                !!!next-token;                !!!next-token;
5206                redo B;                redo B;
5207              } elsif ({          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5208                        caption => 1, table => 1, tbody => 1,                   {
5209                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                    caption => 1, table => 1, tbody => 1,
5210                       }->{$token->{tag_name}}) {                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5211                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                   }->{$token->{tag_name}}) {
5212    ## TODO: The following is wrong?
5213                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5214                                
5215                ## have an element in table scope                ## have an element in table scope
5216                my $i;                my $i;
5217                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5218                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5219                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
5220                      !!!cp ('t292');
5221                    $i = $_;                    $i = $_;
5222                    last INSCOPE;                    last INSCOPE;
5223                  } elsif ({                  } elsif ({
5224                            table => 1, html => 1,                            table => 1, html => 1,
5225                           }->{$node->[1]}) {                           }->{$node->[1]}) {
5226                      !!!cp ('t293');
5227                    last INSCOPE;                    last INSCOPE;
5228                  }                  }
5229                } # INSCOPE                } # INSCOPE
5230                unless (defined $i) {                unless (defined $i) {
5231                    !!!cp ('t294');
5232                  ## Ignore the token                  ## Ignore the token
5233                  !!!next-token;                  !!!next-token;
5234                  redo B;                  redo B;
# Line 4982  sub _tree_construction_main ($) { Line 5240  sub _tree_construction_main ($) {
5240                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5241                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5242                  if ($node->[1] eq 'select') {                  if ($node->[1] eq 'select') {
5243                      !!!cp ('t295');
5244                    $i = $_;                    $i = $_;
5245                    last INSCOPE;                    last INSCOPE;
5246                  } elsif ({                  } elsif ({
5247                            table => 1, html => 1,                            table => 1, html => 1,
5248                           }->{$node->[1]}) {                           }->{$node->[1]}) {
5249    ## ISSUE: Can this state be reached?
5250                      !!!cp ('t296');
5251                    last INSCOPE;                    last INSCOPE;
5252                  }                  }
5253                } # INSCOPE                } # INSCOPE
5254                unless (defined $i) {                unless (defined $i) {
5255                  !!!parse-error (type => 'unmatched end tag:select');                  !!!cp ('t297');
5256    ## TODO: The following error type is correct?
5257                    !!!parse-error (type => 'unmatched end tag:select', token => $token);
5258                  ## Ignore the </select> token                  ## Ignore the </select> token
5259                  !!!next-token; ## TODO: ok?                  !!!next-token; ## TODO: ok?
5260                  redo B;                  redo B;
5261                }                }
5262                                
5263                  !!!cp ('t298');
5264                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5265    
5266                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5267    
5268                ## reprocess                ## reprocess
5269                redo B;                redo B;
5270              } else {          } else {
5271                #            !!!cp ('t299');
5272              }            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in select:'.$token->{tag_name});  
5273            ## Ignore the token            ## Ignore the token
5274            !!!next-token;            !!!next-token;
5275            redo B;            redo B;
5276          } elsif ($self->{insertion_mode} eq 'after body') {          }
5277            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5278              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          unless ($self->{open_elements}->[-1]->[1] eq 'html' and
5279                my $data = $1;                  @{$self->{open_elements}} == 1) { # redundant, maybe
5280                ## As if in body            !!!cp ('t299.1');
5281                $reconstruct_active_formatting_elements->($insert_to_current);            !!!parse-error (type => 'in body:#eof', token => $token);
5282            } else {
5283              !!!cp ('t299.2');
5284            }
5285    
5286            ## Stop parsing.
5287            last B;
5288          } else {
5289            die "$0: $token->{type}: Unknown token type";
5290          }
5291        } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
5292          if ($token->{type} == CHARACTER_TOKEN) {
5293            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5294              my $data = $1;
5295              ## As if in body
5296              $reconstruct_active_formatting_elements->($insert_to_current);
5297                                
5298                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5299              
5300              unless (length $token->{data}) {
5301                !!!cp ('t300');
5302                !!!next-token;
5303                redo B;
5304              }
5305            }
5306            
5307            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5308              !!!cp ('t301');
5309              !!!parse-error (type => 'after html:#character', token => $token);
5310    
5311                unless (length $token->{data}) {            ## Reprocess in the "after body" insertion mode.
5312                  !!!next-token;          } else {
5313                  redo B;            !!!cp ('t302');
5314                }          }
5315              }          
5316                        ## "after body" insertion mode
5317              #          !!!parse-error (type => 'after body:#character', token => $token);
5318              !!!parse-error (type => 'after body:#character');  
5319            } elsif ($token->{type} eq 'start tag') {          $self->{insertion_mode} = IN_BODY_IM;
5320              !!!parse-error (type => 'after body:'.$token->{tag_name});          ## reprocess
5321              #          redo B;
5322            } elsif ($token->{type} eq 'end tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
5323              if ($token->{tag_name} eq 'html') {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5324                if (defined $self->{inner_html_node}) {            !!!cp ('t303');
5325                  !!!parse-error (type => 'unmatched end tag:html');            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
5326                  ## Ignore the token            
5327                  !!!next-token;            ## Reprocess in the "after body" insertion mode.
5328                  redo B;          } else {
5329                } else {            !!!cp ('t304');
5330                  $previous_insertion_mode = $self->{insertion_mode};          }
5331                  $self->{insertion_mode} = 'trailing end';  
5332                  !!!next-token;          ## "after body" insertion mode
5333                  redo B;          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
5334                }  
5335              } else {          $self->{insertion_mode} = IN_BODY_IM;
5336                !!!parse-error (type => 'after body:/'.$token->{tag_name});          ## reprocess
5337              }          redo B;
5338          } elsif ($token->{type} == END_TAG_TOKEN) {
5339            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5340              !!!cp ('t305');
5341              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
5342              
5343              $self->{insertion_mode} = AFTER_BODY_IM;
5344              ## Reprocess in the "after body" insertion mode.
5345            } else {
5346              !!!cp ('t306');
5347            }
5348    
5349            ## "after body" insertion mode
5350            if ($token->{tag_name} eq 'html') {
5351              if (defined $self->{inner_html_node}) {
5352                !!!cp ('t307');
5353                !!!parse-error (type => 'unmatched end tag:html', token => $token);
5354                ## Ignore the token
5355                !!!next-token;
5356                redo B;
5357            } else {            } else {
5358              die "$0: $token->{type}: Unknown token type";              !!!cp ('t308');
5359                $self->{insertion_mode} = AFTER_HTML_BODY_IM;
5360                !!!next-token;
5361                redo B;
5362            }            }
5363            } else {
5364              !!!cp ('t309');
5365              !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);
5366    
5367            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = IN_BODY_IM;
5368            ## reprocess            ## reprocess
5369            redo B;            redo B;
5370      } elsif ($self->{insertion_mode} eq 'in frameset') {          }
5371        if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5372            !!!cp ('t309.2');
5373            ## Stop parsing
5374            last B;
5375          } else {
5376            die "$0: $token->{type}: Unknown token type";
5377          }
5378        } elsif ($self->{insertion_mode} & FRAME_IMS) {
5379          if ($token->{type} == CHARACTER_TOKEN) {
5380          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5381            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5382              
5383            unless (length $token->{data}) {            unless (length $token->{data}) {
5384                !!!cp ('t310');
5385              !!!next-token;              !!!next-token;
5386              redo B;              redo B;
5387            }            }
5388          }          }
5389            
5390          !!!parse-error (type => 'in frameset:#character');          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
5391          ## Ignore the token            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5392          !!!next-token;              !!!cp ('t311');
5393          redo B;              !!!parse-error (type => 'in frameset:#character', token => $token);
5394        } elsif ($token->{type} eq 'start tag') {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
5395          if ($token->{tag_name} eq 'frameset') {              !!!cp ('t312');
5396                !!!parse-error (type => 'after frameset:#character', token => $token);
5397              } else { # "after html frameset"
5398                !!!cp ('t313');
5399                !!!parse-error (type => 'after html:#character', token => $token);
5400    
5401                $self->{insertion_mode} = AFTER_FRAMESET_IM;
5402                ## Reprocess in the "after frameset" insertion mode.
5403                !!!parse-error (type => 'after frameset:#character', token => $token);
5404              }
5405              
5406              ## Ignore the token.
5407              if (length $token->{data}) {
5408                !!!cp ('t314');
5409                ## reprocess the rest of characters
5410              } else {
5411                !!!cp ('t315');
5412                !!!next-token;
5413              }
5414              redo B;
5415            }
5416            
5417            die qq[$0: Character "$token->{data}"];
5418          } elsif ($token->{type} == START_TAG_TOKEN) {
5419            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5420              !!!cp ('t316');
5421              !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
5422    
5423              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5424              ## Process in the "after frameset" insertion mode.
5425            } else {
5426              !!!cp ('t317');
5427            }
5428    
5429            if ($token->{tag_name} eq 'frameset' and
5430                $self->{insertion_mode} == IN_FRAMESET_IM) {
5431              !!!cp ('t318');
5432            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes});
5433            !!!next-token;            !!!next-token;
5434            redo B;            redo B;
5435          } elsif ($token->{tag_name} eq 'frame') {          } elsif ($token->{tag_name} eq 'frame' and
5436                     $self->{insertion_mode} == IN_FRAMESET_IM) {
5437              !!!cp ('t319');
5438            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes});
5439            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
5440            !!!next-token;            !!!next-token;
5441            redo B;            redo B;
5442          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
5443            $in_body->($insert_to_current);            !!!cp ('t320');
5444              ## NOTE: As if in body.
5445              $parse_rcdata->(CDATA_CONTENT_MODEL);
5446            redo B;            redo B;
5447          } else {          } else {
5448            !!!parse-error (type => 'in frameset:'.$token->{tag_name});            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5449                !!!cp ('t321');
5450                !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);
5451              } else {
5452                !!!cp ('t322');
5453                !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
5454              }
5455            ## Ignore the token            ## Ignore the token
5456            !!!next-token;            !!!next-token;
5457            redo B;            redo B;
5458          }          }
5459        } elsif ($token->{type} eq 'end tag') {        } elsif ($token->{type} == END_TAG_TOKEN) {
5460          if ($token->{tag_name} eq 'frameset') {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5461              !!!cp ('t323');
5462              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
5463    
5464              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5465              ## Process in the "after frameset" insertion mode.
5466            } else {
5467              !!!cp ('t324');
5468            }
5469    
5470            if ($token->{tag_name} eq 'frameset' and
5471                $self->{insertion_mode} == IN_FRAMESET_IM) {
5472            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] eq 'html' and
5473                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
5474              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t325');
5475                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5476              ## Ignore the token              ## Ignore the token
5477              !!!next-token;              !!!next-token;
5478            } else {            } else {
5479                !!!cp ('t326');
5480              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5481              !!!next-token;              !!!next-token;
5482            }            }
5483    
5484            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
5485                $self->{open_elements}->[-1]->[1] ne 'frameset') {                $self->{open_elements}->[-1]->[1] ne 'frameset') {
5486              $self->{insertion_mode} = 'after frameset';              !!!cp ('t327');
5487                $self->{insertion_mode} = AFTER_FRAMESET_IM;
5488              } else {
5489                !!!cp ('t328');
5490            }            }
5491            redo B;            redo B;
5492            } elsif ($token->{tag_name} eq 'html' and
5493                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
5494              !!!cp ('t329');
5495              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
5496              !!!next-token;
5497              redo B;
5498          } else {          } else {
5499            !!!parse-error (type => 'in frameset:/'.$token->{tag_name});            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5500                !!!cp ('t330');
5501                !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);
5502              } else {
5503                !!!cp ('t331');
5504                !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);
5505              }
5506            ## Ignore the token            ## Ignore the token
5507            !!!next-token;            !!!next-token;
5508            redo B;            redo B;
5509          }          }
5510          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5511            unless ($self->{open_elements}->[-1]->[1] eq 'html' and
5512                    @{$self->{open_elements}} == 1) { # redundant, maybe
5513              !!!cp ('t331.1');
5514              !!!parse-error (type => 'in body:#eof', token => $token);
5515            } else {
5516              !!!cp ('t331.2');
5517            }
5518            
5519            ## Stop parsing
5520            last B;
5521        } else {        } else {
5522          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5523        }        }
     } elsif ($self->{insertion_mode} eq 'after frameset') {  
       if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
5524    
5525                unless (length $token->{data}) {        ## ISSUE: An issue in spec here
5526                  !!!next-token;      } else {
5527                  redo B;        die "$0: $self->{insertion_mode}: Unknown insertion mode";
5528                }      }
             }  
5529    
5530              if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {      ## "in body" insertion mode
5531                !!!parse-error (type => 'after frameset:#character');      if ($token->{type} == START_TAG_TOKEN) {
5532          if ($token->{tag_name} eq 'script') {
5533            !!!cp ('t332');
5534            ## NOTE: This is an "as if in head" code clone
5535            $script_start_tag->();
5536            redo B;
5537          } elsif ($token->{tag_name} eq 'style') {
5538            !!!cp ('t333');
5539            ## NOTE: This is an "as if in head" code clone
5540            $parse_rcdata->(CDATA_CONTENT_MODEL);
5541            redo B;
5542          } elsif ({
5543                    base => 1, link => 1,
5544                   }->{$token->{tag_name}}) {
5545            !!!cp ('t334');
5546            ## NOTE: This is an "as if in head" code clone, only "-t" differs
5547            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5548            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
5549            !!!next-token;
5550            redo B;
5551          } elsif ($token->{tag_name} eq 'meta') {
5552            ## NOTE: This is an "as if in head" code clone, only "-t" differs
5553            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5554            my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
5555    
5556                ## Ignore the token.          unless ($self->{confident}) {
5557                if (length $token->{data}) {            if ($token->{attributes}->{charset}) { ## TODO: And if supported
5558                  ## reprocess the rest of characters              !!!cp ('t335');
5559                } else {              $self->{change_encoding}
5560                  !!!next-token;                  ->($self, $token->{attributes}->{charset}->{value});
5561                }              
5562                redo B;              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
5563                    ->set_user_data (manakai_has_reference =>
5564                                         $token->{attributes}->{charset}
5565                                             ->{has_reference});
5566              } elsif ($token->{attributes}->{content}) {
5567                ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
5568                if ($token->{attributes}->{content}->{value}
5569                    =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
5570                        [\x09-\x0D\x20]*=
5571                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
5572                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
5573                  !!!cp ('t336');
5574                  $self->{change_encoding}
5575                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
5576                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
5577                      ->set_user_data (manakai_has_reference =>
5578                                           $token->{attributes}->{content}
5579                                                 ->{has_reference});
5580              }              }
5581              }
5582            } else {
5583              if ($token->{attributes}->{charset}) {
5584                !!!cp ('t337');
5585                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
5586                    ->set_user_data (manakai_has_reference =>
5587                                         $token->{attributes}->{charset}
5588                                             ->{has_reference});
5589              }
5590              if ($token->{attributes}->{content}) {
5591                !!!cp ('t338');
5592                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
5593                    ->set_user_data (manakai_has_reference =>
5594                                         $token->{attributes}->{content}
5595                                             ->{has_reference});
5596              }
5597            }
5598    
5599          die qq[$0: Character "$token->{data}"];          !!!next-token;
5600        } elsif ($token->{type} eq 'start tag') {          redo B;
5601          if ($token->{tag_name} eq 'noframes') {        } elsif ($token->{tag_name} eq 'title') {
5602            $in_body->($insert_to_current);          !!!cp ('t341');
5603            redo B;          ## NOTE: This is an "as if in head" code clone
5604            $parse_rcdata->(RCDATA_CONTENT_MODEL);
5605            redo B;
5606          } elsif ($token->{tag_name} eq 'body') {
5607            !!!parse-error (type => 'in body:body', token => $token);
5608                  
5609            if (@{$self->{open_elements}} == 1 or
5610                $self->{open_elements}->[1]->[1] ne 'body') {
5611              !!!cp ('t342');
5612              ## Ignore the token
5613          } else {          } else {
5614            !!!parse-error (type => 'after frameset:'.$token->{tag_name});            my $body_el = $self->{open_elements}->[1]->[0];
5615              for my $attr_name (keys %{$token->{attributes}}) {
5616                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
5617                  !!!cp ('t343');
5618                  $body_el->set_attribute_ns
5619                    (undef, [undef, $attr_name],
5620                     $token->{attributes}->{$attr_name}->{value});
5621                }
5622              }
5623            }
5624            !!!next-token;
5625            redo B;
5626          } elsif ({
5627                    address => 1, blockquote => 1, center => 1, dir => 1,
5628                    div => 1, dl => 1, fieldset => 1,
5629                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5630                    menu => 1, ol => 1, p => 1, ul => 1,
5631                    pre => 1, listing => 1,
5632                    form => 1,
5633                    table => 1,
5634                    hr => 1,
5635                   }->{$token->{tag_name}}) {
5636            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
5637              !!!cp ('t350');
5638              !!!parse-error (type => 'in form:form', token => $token);
5639            ## Ignore the token            ## Ignore the token
5640            !!!next-token;            !!!next-token;
5641            redo B;            redo B;
5642          }          }
5643        } elsif ($token->{type} eq 'end tag') {  
5644          if ($token->{tag_name} eq 'html') {          ## has a p element in scope
5645            $previous_insertion_mode = $self->{insertion_mode};          INSCOPE: for (reverse @{$self->{open_elements}}) {
5646            $self->{insertion_mode} = 'trailing end';            if ($_->[1] eq 'p') {
5647                !!!cp ('t344');
5648                !!!back-token;
5649                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5650                redo B;
5651              } elsif ({
5652                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
5653                        button => 1, marquee => 1, object => 1, html => 1,
5654                       }->{$_->[1]}) {
5655                !!!cp ('t345');
5656                last INSCOPE;
5657              }
5658            } # INSCOPE
5659              
5660            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5661            if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
5662            !!!next-token;            !!!next-token;
5663            redo B;            if ($token->{type} == CHARACTER_TOKEN) {
5664                $token->{data} =~ s/^\x0A//;
5665                unless (length $token->{data}) {
5666                  !!!cp ('t346');
5667                  !!!next-token;
5668                } else {
5669                  !!!cp ('t349');
5670                }
5671              } else {
5672                !!!cp ('t348');
5673              }
5674            } elsif ($token->{tag_name} eq 'form') {
5675              !!!cp ('t347.1');
5676              $self->{form_element} = $self->{open_elements}->[-1]->[0];
5677    
5678              !!!next-token;
5679            } elsif ($token->{tag_name} eq 'table') {
5680              !!!cp ('t382');
5681              push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
5682              
5683              $self->{insertion_mode} = IN_TABLE_IM;
5684    
5685              !!!next-token;
5686            } elsif ($token->{tag_name} eq 'hr') {
5687              !!!cp ('t386');
5688              pop @{$self->{open_elements}};
5689            
5690              !!!next-token;
5691            } else {
5692              !!!cp ('t347');
5693              !!!next-token;
5694            }
5695            redo B;
5696          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
5697            ## has a p element in scope
5698            INSCOPE: for (reverse @{$self->{open_elements}}) {
5699              if ($_->[1] eq 'p') {
5700                !!!cp ('t353');
5701                !!!back-token;
5702                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5703                redo B;
5704              } elsif ({
5705                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
5706                        button => 1, marquee => 1, object => 1, html => 1,
5707                       }->{$_->[1]}) {
5708                !!!cp ('t354');
5709                last INSCOPE;
5710              }
5711            } # INSCOPE
5712              
5713            ## Step 1
5714            my $i = -1;
5715            my $node = $self->{open_elements}->[$i];
5716            my $li_or_dtdd = {li => {li => 1},
5717                              dt => {dt => 1, dd => 1},
5718                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
5719            LI: {
5720              ## Step 2
5721              if ($li_or_dtdd->{$node->[1]}) {
5722                if ($i != -1) {
5723                  !!!cp ('t355');
5724                  !!!parse-error (type => 'end tag missing:'.
5725                                  $self->{open_elements}->[-1]->[1], token => $token);
5726                } else {
5727                  !!!cp ('t356');
5728                }
5729                splice @{$self->{open_elements}}, $i;
5730                last LI;
5731              } else {
5732                !!!cp ('t357');
5733              }
5734              
5735              ## Step 3
5736              if (not $formatting_category->{$node->[1]} and
5737                  #not $phrasing_category->{$node->[1]} and
5738                  ($special_category->{$node->[1]} or
5739                   $scoping_category->{$node->[1]}) and
5740                  $node->[1] ne 'address' and $node->[1] ne 'div') {
5741                !!!cp ('t358');
5742                last LI;
5743              }
5744              
5745              !!!cp ('t359');
5746              ## Step 4
5747              $i--;
5748              $node = $self->{open_elements}->[$i];
5749              redo LI;
5750            } # LI
5751              
5752            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5753            !!!next-token;
5754            redo B;
5755          } elsif ($token->{tag_name} eq 'plaintext') {
5756            ## has a p element in scope
5757            INSCOPE: for (reverse @{$self->{open_elements}}) {
5758              if ($_->[1] eq 'p') {
5759                !!!cp ('t367');
5760                !!!back-token;
5761                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5762                redo B;
5763              } elsif ({
5764                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
5765                        button => 1, marquee => 1, object => 1, html => 1,
5766                       }->{$_->[1]}) {
5767                !!!cp ('t368');
5768                last INSCOPE;
5769              }
5770            } # INSCOPE
5771              
5772            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5773              
5774            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
5775              
5776            !!!next-token;
5777            redo B;
5778          } elsif ($token->{tag_name} eq 'a') {
5779            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
5780              my $node = $active_formatting_elements->[$i];
5781              if ($node->[1] eq 'a') {
5782                !!!cp ('t371');
5783                !!!parse-error (type => 'in a:a', token => $token);
5784                
5785                !!!back-token;
5786                $token = {type => END_TAG_TOKEN, tag_name => 'a'};
5787                $formatting_end_tag->($token);
5788                
5789                AFE2: for (reverse 0..$#$active_formatting_elements) {
5790                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
5791                    !!!cp ('t372');
5792                    splice @$active_formatting_elements, $_, 1;
5793                    last AFE2;
5794                  }
5795                } # AFE2
5796                OE: for (reverse 0..$#{$self->{open_elements}}) {
5797                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
5798                    !!!cp ('t373');
5799                    splice @{$self->{open_elements}}, $_, 1;
5800                    last OE;
5801                  }
5802                } # OE
5803                last AFE;
5804              } elsif ($node->[0] eq '#marker') {
5805                !!!cp ('t374');
5806                last AFE;
5807              }
5808            } # AFE
5809              
5810            $reconstruct_active_formatting_elements->($insert_to_current);
5811    
5812            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5813            push @$active_formatting_elements, $self->{open_elements}->[-1];
5814    
5815            !!!next-token;
5816            redo B;
5817          } elsif ($token->{tag_name} eq 'nobr') {
5818            $reconstruct_active_formatting_elements->($insert_to_current);
5819    
5820            ## has a |nobr| element in scope
5821            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5822              my $node = $self->{open_elements}->[$_];
5823              if ($node->[1] eq 'nobr') {
5824                !!!cp ('t376');
5825                !!!parse-error (type => 'in nobr:nobr', token => $token);
5826                !!!back-token;
5827                $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};
5828                redo B;
5829              } elsif ({
5830                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
5831                        button => 1, marquee => 1, object => 1, html => 1,
5832                       }->{$node->[1]}) {
5833                !!!cp ('t377');
5834                last INSCOPE;
5835              }
5836            } # INSCOPE
5837            
5838            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5839            push @$active_formatting_elements, $self->{open_elements}->[-1];
5840            
5841            !!!next-token;
5842            redo B;
5843          } elsif ($token->{tag_name} eq 'button') {
5844            ## has a button element in scope
5845            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5846              my $node = $self->{open_elements}->[$_];
5847              if ($node->[1] eq 'button') {
5848                !!!cp ('t378');
5849                !!!parse-error (type => 'in button:button', token => $token);
5850                !!!back-token;
5851                $token = {type => END_TAG_TOKEN, tag_name => 'button'};
5852                redo B;
5853              } elsif ({
5854                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
5855                        button => 1, marquee => 1, object => 1, html => 1,
5856                       }->{$node->[1]}) {
5857                !!!cp ('t379');
5858                last INSCOPE;
5859              }
5860            } # INSCOPE
5861              
5862            $reconstruct_active_formatting_elements->($insert_to_current);
5863              
5864            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5865    
5866            ## TODO: associate with $self->{form_element} if defined
5867    
5868            push @$active_formatting_elements, ['#marker', ''];
5869    
5870            !!!next-token;
5871            redo B;
5872          } elsif ({
5873                    xmp => 1,
5874                    iframe => 1,
5875                    noembed => 1,
5876                    noframes => 1,
5877                    noscript => 0, ## TODO: 1 if scripting is enabled
5878                   }->{$token->{tag_name}}) {
5879            if ($token->{tag_name} eq 'xmp') {
5880              !!!cp ('t381');
5881              $reconstruct_active_formatting_elements->($insert_to_current);
5882          } else {          } else {
5883            !!!parse-error (type => 'after frameset:/'.$token->{tag_name});            !!!cp ('t399');
5884            }
5885            ## NOTE: There is an "as if in body" code clone.
5886            $parse_rcdata->(CDATA_CONTENT_MODEL);
5887            redo B;
5888          } elsif ($token->{tag_name} eq 'isindex') {
5889            !!!parse-error (type => 'isindex', token => $token);
5890            
5891            if (defined $self->{form_element}) {
5892              !!!cp ('t389');
5893            ## Ignore the token            ## Ignore the token
5894            !!!next-token;            !!!next-token;
5895            redo B;            redo B;
5896            } else {
5897              my $at = $token->{attributes};
5898              my $form_attrs;
5899              $form_attrs->{action} = $at->{action} if $at->{action};
5900              my $prompt_attr = $at->{prompt};
5901              $at->{name} = {name => 'name', value => 'isindex'};
5902              delete $at->{action};
5903              delete $at->{prompt};
5904              my @tokens = (
5905                            {type => START_TAG_TOKEN, tag_name => 'form',
5906                             attributes => $form_attrs},
5907                            {type => START_TAG_TOKEN, tag_name => 'hr'},
5908                            {type => START_TAG_TOKEN, tag_name => 'p'},
5909                            {type => START_TAG_TOKEN, tag_name => 'label'},
5910                           );
5911              if ($prompt_attr) {
5912                !!!cp ('t390');
5913                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};
5914              } else {
5915                !!!cp ('t391');
5916                push @tokens, {type => CHARACTER_TOKEN,
5917                               data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
5918                ## TODO: make this configurable
5919              }
5920              push @tokens,
5921                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},
5922                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
5923                            {type => END_TAG_TOKEN, tag_name => 'label'},
5924                            {type => END_TAG_TOKEN, tag_name => 'p'},
5925                            {type => START_TAG_TOKEN, tag_name => 'hr'},
5926                            {type => END_TAG_TOKEN, tag_name => 'form'};
5927              $token = shift @tokens;
5928              !!!back-token (@tokens);
5929              redo B;
5930          }          }
5931          } elsif ($token->{tag_name} eq 'textarea') {
5932            my $tag_name = $token->{tag_name};
5933            my $el;
5934            !!!create-element ($el, $token->{tag_name}, $token->{attributes});
5935            
5936            ## TODO: $self->{form_element} if defined
5937            $self->{content_model} = RCDATA_CONTENT_MODEL;
5938            delete $self->{escape}; # MUST
5939            
5940            $insert->($el);
5941            
5942            my $text = '';
5943            !!!next-token;
5944            if ($token->{type} == CHARACTER_TOKEN) {
5945              $token->{data} =~ s/^\x0A//;
5946              unless (length $token->{data}) {
5947                !!!cp ('t392');
5948                !!!next-token;
5949              } else {
5950                !!!cp ('t393');
5951              }
5952            } else {
5953              !!!cp ('t394');
5954            }
5955            while ($token->{type} == CHARACTER_TOKEN) {
5956              !!!cp ('t395');
5957              $text .= $token->{data};
5958              !!!next-token;
5959            }
5960            if (length $text) {
5961              !!!cp ('t396');
5962              $el->manakai_append_text ($text);
5963            }
5964            
5965            $self->{content_model} = PCDATA_CONTENT_MODEL;
5966            
5967            if ($token->{type} == END_TAG_TOKEN and
5968                $token->{tag_name} eq $tag_name) {
5969              !!!cp ('t397');
5970              ## Ignore the token
5971            } else {
5972              !!!cp ('t398');
5973              !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
5974            }
5975            !!!next-token;
5976            redo B;
5977          } elsif ({
5978                    caption => 1, col => 1, colgroup => 1, frame => 1,
5979                    frameset => 1, head => 1, option => 1, optgroup => 1,
5980                    tbody => 1, td => 1, tfoot => 1, th => 1,
5981                    thead => 1, tr => 1,
5982                   }->{$token->{tag_name}}) {
5983            !!!cp ('t401');
5984            !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
5985            ## Ignore the token
5986            !!!next-token;
5987            redo B;
5988            
5989            ## ISSUE: An issue on HTML5 new elements in the spec.
5990        } else {        } else {
5991          die "$0: $token->{type}: Unknown token type";          if ($token->{tag_name} eq 'image') {
5992              !!!cp ('t384');
5993              !!!parse-error (type => 'image', token => $token);
5994              $token->{tag_name} = 'img';
5995            } else {
5996              !!!cp ('t385');
5997            }
5998    
5999            ## NOTE: There is an "as if <br>" code clone.
6000            $reconstruct_active_formatting_elements->($insert_to_current);
6001            
6002            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
6003    
6004            if ({
6005                 applet => 1, marquee => 1, object => 1,
6006                }->{$token->{tag_name}}) {
6007              !!!cp ('t380');
6008              push @$active_formatting_elements, ['#marker', ''];
6009            } elsif ({
6010                      b => 1, big => 1, em => 1, font => 1, i => 1,
6011                      s => 1, small => 1, strile => 1,
6012                      strong => 1, tt => 1, u => 1,
6013                     }->{$token->{tag_name}}) {
6014              !!!cp ('t375');
6015              push @$active_formatting_elements, $self->{open_elements}->[-1];
6016            } elsif ($token->{tag_name} eq 'input') {
6017              !!!cp ('t388');
6018              ## TODO: associate with $self->{form_element} if defined
6019              pop @{$self->{open_elements}};
6020            } elsif ({
6021                      area => 1, basefont => 1, bgsound => 1, br => 1,
6022                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
6023                      #image => 1,
6024                     }->{$token->{tag_name}}) {
6025              !!!cp ('t388.1');
6026              pop @{$self->{open_elements}};
6027            } elsif ($token->{tag_name} eq 'select') {
6028              ## TODO: associate with $self->{form_element} if defined
6029            
6030              if ($self->{insertion_mode} & TABLE_IMS or
6031                  $self->{insertion_mode} & BODY_TABLE_IMS or
6032                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6033                !!!cp ('t400.1');
6034                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
6035              } else {
6036                !!!cp ('t400.2');
6037                $self->{insertion_mode} = IN_SELECT_IM;
6038              }
6039            } else {
6040              !!!cp ('t402');
6041            }
6042            
6043            !!!next-token;
6044            redo B;
6045        }        }
6046        } elsif ($token->{type} == END_TAG_TOKEN) {
6047          if ($token->{tag_name} eq 'body') {
6048            ## has a |body| element in scope
6049            my $i;
6050            INSCOPE: {
6051              for (reverse @{$self->{open_elements}}) {
6052                if ($_->[1] eq 'body') {
6053                  !!!cp ('t405');
6054                  $i = $_;
6055                  last INSCOPE;
6056                } elsif ({
6057                          applet => 1, table => 1, caption => 1, td => 1, th => 1,
6058                          button => 1, marquee => 1, object => 1, html => 1,
6059                         }->{$_->[1]}) {
6060                  !!!cp ('t405.1');
6061                  last;
6062                }
6063              }
6064    
6065        ## ISSUE: An issue in spec here            !!!parse-error (type => 'start tag not allowed',
6066      } elsif ($self->{insertion_mode} eq 'trailing end') {                            value => $token->{tag_name}, token => $token);
6067        ## states in the main stage is preserved yet # MUST            ## NOTE: Ignore the token.
6068                    !!!next-token;
6069        if ($token->{type} eq 'character') {            redo B;
6070          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          } # INSCOPE
6071            my $data = $1;  
6072            ## As if in the main phase.          for (@{$self->{open_elements}}) {
6073            ## NOTE: The insertion mode in the main phase            unless ({
6074            ## just before the phase has been changed to the trailing                     dd => 1, dt => 1, li => 1, p => 1, td => 1,
6075            ## end phase is either "after body" or "after frameset".                     th => 1, tr => 1, body => 1, html => 1,
6076            $reconstruct_active_formatting_elements->($insert_to_current);                     tbody => 1, tfoot => 1, thead => 1,
6077                      }->{$_->[1]}) {
6078                !!!cp ('t403');
6079                !!!parse-error (type => 'not closed:'.$_->[1], token => $token);
6080                last;
6081              } else {
6082                !!!cp ('t404');
6083              }
6084            }
6085    
6086            $self->{insertion_mode} = AFTER_BODY_IM;
6087            !!!next-token;
6088            redo B;
6089          } elsif ($token->{tag_name} eq 'html') {
6090            if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {
6091              ## ISSUE: There is an issue in the spec.
6092              if ($self->{open_elements}->[-1]->[1] ne 'body') {
6093                !!!cp ('t406');
6094                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1], token => $token);
6095              } else {
6096                !!!cp ('t407');
6097              }
6098              $self->{insertion_mode} = AFTER_BODY_IM;
6099              ## reprocess
6100              redo B;
6101            } else {
6102              !!!cp ('t408');
6103              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6104              ## Ignore the token
6105              !!!next-token;
6106              redo B;
6107            }
6108          } elsif ({
6109                    address => 1, blockquote => 1, center => 1, dir => 1,
6110                    div => 1, dl => 1, fieldset => 1, listing => 1,
6111                    menu => 1, ol => 1, pre => 1, ul => 1,
6112                    dd => 1, dt => 1, li => 1,
6113                    applet => 1, button => 1, marquee => 1, object => 1,
6114                   }->{$token->{tag_name}}) {
6115            ## has an element in scope
6116            my $i;
6117            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6118              my $node = $self->{open_elements}->[$_];
6119              if ($node->[1] eq $token->{tag_name}) {
6120                !!!cp ('t410');
6121                $i = $_;
6122                last INSCOPE;
6123              } elsif ({
6124                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
6125                        button => 1, marquee => 1, object => 1, html => 1,
6126                       }->{$node->[1]}) {
6127                !!!cp ('t411');
6128                last INSCOPE;
6129              }
6130            } # INSCOPE
6131    
6132            unless (defined $i) { # has an element in scope
6133              !!!cp ('t413');
6134              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6135            } else {
6136              ## Step 1. generate implied end tags
6137              while ({
6138                      dd => ($token->{tag_name} ne 'dd'),
6139                      dt => ($token->{tag_name} ne 'dt'),
6140                      li => ($token->{tag_name} ne 'li'),
6141                      p => 1,
6142                     }->{$self->{open_elements}->[-1]->[1]}) {
6143                !!!cp ('t409');
6144                pop @{$self->{open_elements}};
6145              }
6146    
6147              ## Step 2.
6148              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6149                !!!cp ('t412');
6150                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
6151              } else {
6152                !!!cp ('t414');
6153              }
6154    
6155              ## Step 3.
6156              splice @{$self->{open_elements}}, $i;
6157    
6158              ## Step 4.
6159              $clear_up_to_marker->()
6160                  if {
6161                    applet => 1, button => 1, marquee => 1, object => 1,
6162                  }->{$token->{tag_name}};
6163            }
6164            !!!next-token;
6165            redo B;
6166          } elsif ($token->{tag_name} eq 'form') {
6167            undef $self->{form_element};
6168    
6169            ## has an element in scope
6170            my $i;
6171            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6172              my $node = $self->{open_elements}->[$_];
6173              if ($node->[1] eq $token->{tag_name}) {
6174                !!!cp ('t418');
6175                $i = $_;
6176                last INSCOPE;
6177              } elsif ({
6178                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
6179                        button => 1, marquee => 1, object => 1, html => 1,
6180                       }->{$node->[1]}) {
6181                !!!cp ('t419');
6182                last INSCOPE;
6183              }
6184            } # INSCOPE
6185    
6186            unless (defined $i) { # has an element in scope
6187              !!!cp ('t421');
6188              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6189            } else {
6190              ## Step 1. generate implied end tags
6191              while ({
6192                      dd => 1, dt => 1, li => 1, p => 1,
6193                     }->{$self->{open_elements}->[-1]->[1]}) {
6194                !!!cp ('t417');
6195                pop @{$self->{open_elements}};
6196              }
6197                        
6198            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);            ## Step 2.
6199              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6200                !!!cp ('t417.1');
6201                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
6202              } else {
6203                !!!cp ('t420');
6204              }  
6205                        
6206            unless (length $token->{data}) {            ## Step 3.
6207              !!!next-token;            splice @{$self->{open_elements}}, $i;
6208              redo B;          }
6209    
6210            !!!next-token;
6211            redo B;
6212          } elsif ({
6213                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6214                   }->{$token->{tag_name}}) {
6215            ## has an element in scope
6216            my $i;
6217            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6218              my $node = $self->{open_elements}->[$_];
6219              if ({
6220                   h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6221                  }->{$node->[1]}) {
6222                !!!cp ('t423');
6223                $i = $_;
6224                last INSCOPE;
6225              } elsif ({
6226                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
6227                        button => 1, marquee => 1, object => 1, html => 1,
6228                       }->{$node->[1]}) {
6229                !!!cp ('t424');
6230                last INSCOPE;
6231            }            }
6232            } # INSCOPE
6233    
6234            unless (defined $i) { # has an element in scope
6235              !!!cp ('t425.1');
6236              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6237            } else {
6238              ## Step 1. generate implied end tags
6239              while ({
6240                      dd => 1, dt => 1, li => 1, p => 1,
6241                     }->{$self->{open_elements}->[-1]->[1]}) {
6242                !!!cp ('t422');
6243                pop @{$self->{open_elements}};
6244              }
6245              
6246              ## Step 2.
6247              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6248                !!!cp ('t425');
6249                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6250              } else {
6251                !!!cp ('t426');
6252              }
6253    
6254              ## Step 3.
6255              splice @{$self->{open_elements}}, $i;
6256          }          }
6257            
6258            !!!next-token;
6259            redo B;
6260          } elsif ($token->{tag_name} eq 'p') {
6261            ## has an element in scope
6262            my $i;
6263            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6264              my $node = $self->{open_elements}->[$_];
6265              if ($node->[1] eq $token->{tag_name}) {
6266                !!!cp ('t410.1');
6267                $i = $_;
6268                last INSCOPE;
6269              } elsif ({
6270                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
6271                        button => 1, marquee => 1, object => 1, html => 1,
6272                       }->{$node->[1]}) {
6273                !!!cp ('t411.1');
6274                last INSCOPE;
6275              }
6276            } # INSCOPE
6277    
6278          !!!parse-error (type => 'after html:#character');          if (defined $i) {
6279          $self->{insertion_mode} = $previous_insertion_mode;            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6280          ## reprocess              !!!cp ('t412.1');
6281                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
6282              } else {
6283                !!!cp ('t414.1');
6284              }
6285    
6286              splice @{$self->{open_elements}}, $i;
6287            } else {
6288              !!!cp ('t413.1');
6289              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6290    
6291              !!!cp ('t415.1');
6292              ## As if <p>, then reprocess the current token
6293              my $el;
6294              !!!create-element ($el, 'p');
6295              $insert->($el);
6296              ## NOTE: Not inserted into |$self->{open_elements}|.
6297            }
6298    
6299            !!!next-token;
6300          redo B;          redo B;
6301        } elsif ($token->{type} eq 'start tag') {        } elsif ({
6302          !!!parse-error (type => 'after html:'.$token->{tag_name});                  a => 1,
6303          $self->{insertion_mode} = $previous_insertion_mode;                  b => 1, big => 1, em => 1, font => 1, i => 1,
6304          ## reprocess                  nobr => 1, s => 1, small => 1, strile => 1,
6305                    strong => 1, tt => 1, u => 1,
6306                   }->{$token->{tag_name}}) {
6307            !!!cp ('t427');
6308            $formatting_end_tag->($token);
6309          redo B;          redo B;
6310        } elsif ($token->{type} eq 'end tag') {        } elsif ($token->{tag_name} eq 'br') {
6311          !!!parse-error (type => 'after html:/'.$token->{tag_name});          !!!cp ('t428');
6312          $self->{insertion_mode} = $previous_insertion_mode;          !!!parse-error (type => 'unmatched end tag:br', token => $token);
6313          ## reprocess  
6314            ## As if <br>
6315            $reconstruct_active_formatting_elements->($insert_to_current);
6316            
6317            my $el;
6318            !!!create-element ($el, 'br');
6319            $insert->($el);
6320            
6321            ## Ignore the token.
6322            !!!next-token;
6323            redo B;
6324          } elsif ({
6325                    caption => 1, col => 1, colgroup => 1, frame => 1,
6326                    frameset => 1, head => 1, option => 1, optgroup => 1,
6327                    tbody => 1, td => 1, tfoot => 1, th => 1,
6328                    thead => 1, tr => 1,
6329                    area => 1, basefont => 1, bgsound => 1,
6330                    embed => 1, hr => 1, iframe => 1, image => 1,
6331                    img => 1, input => 1, isindex => 1, noembed => 1,
6332                    noframes => 1, param => 1, select => 1, spacer => 1,
6333                    table => 1, textarea => 1, wbr => 1,
6334                    noscript => 0, ## TODO: if scripting is enabled
6335                   }->{$token->{tag_name}}) {
6336            !!!cp ('t429');
6337            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6338            ## Ignore the token
6339            !!!next-token;
6340          redo B;          redo B;
6341            
6342            ## ISSUE: Issue on HTML5 new elements in spec
6343            
6344        } else {        } else {
6345          die "$0: $token->{type}: Unknown token";          ## Step 1
6346            my $node_i = -1;
6347            my $node = $self->{open_elements}->[$node_i];
6348    
6349            ## Step 2
6350            S2: {
6351              if ($node->[1] eq $token->{tag_name}) {
6352                ## Step 1
6353                ## generate implied end tags
6354                while ({
6355                        dd => 1, dt => 1, li => 1, p => 1,
6356                       }->{$self->{open_elements}->[-1]->[1]}) {
6357                  !!!cp ('t430');
6358                  ## ISSUE: Can this case be reached?
6359                  pop @{$self->{open_elements}};
6360                }
6361            
6362                ## Step 2
6363                if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
6364                  !!!cp ('t431');
6365                  ## NOTE: <x><y></x>
6366                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
6367                } else {
6368                  !!!cp ('t432');
6369                }
6370                
6371                ## Step 3
6372                splice @{$self->{open_elements}}, $node_i;
6373    
6374                !!!next-token;
6375                last S2;
6376              } else {
6377                ## Step 3
6378                if (not $formatting_category->{$node->[1]} and
6379                    #not $phrasing_category->{$node->[1]} and
6380                    ($special_category->{$node->[1]} or
6381                     $scoping_category->{$node->[1]})) {
6382                  !!!cp ('t433');
6383                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6384                  ## Ignore the token
6385                  !!!next-token;
6386                  last S2;
6387                }
6388    
6389                !!!cp ('t434');
6390              }
6391              
6392              ## Step 4
6393              $node_i--;
6394              $node = $self->{open_elements}->[$node_i];
6395              
6396              ## Step 5;
6397              redo S2;
6398            } # S2
6399            redo B;
6400        }        }
     } else {  
       die "$0: $self->{insertion_mode}: Unknown insertion mode";  
6401      }      }
6402        redo B;
6403    } # B    } # B
6404    
6405    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 5221  sub set_inner_html ($$$) { Line 6413  sub set_inner_html ($$$) {
6413    my $s = \$_[0];    my $s = \$_[0];
6414    my $onerror = $_[1];    my $onerror = $_[1];
6415    
6416      ## ISSUE: Should {confident} be true?
6417    
6418    my $nt = $node->node_type;    my $nt = $node->node_type;
6419    if ($nt == 9) {    if ($nt == 9) {
6420      # MUST      # MUST
# Line 5249  sub set_inner_html ($$$) { Line 6443  sub set_inner_html ($$$) {
6443      my $p = $class->new;      my $p = $class->new;
6444      $p->{document} = $doc;      $p->{document} = $doc;
6445    
6446      ## Step 9 # MUST      ## Step 8 # MUST
6447      my $i = 0;      my $i = 0;
6448      my $line = 1;      my $line = 1;
6449      my $column = 0;      my $column = 0;
6450      $p->{set_next_input_character} = sub {      $p->{set_next_char} = sub {
6451        my $self = shift;        my $self = shift;
6452    
6453        pop @{$self->{prev_input_character}};        pop @{$self->{prev_char}};
6454        unshift @{$self->{prev_input_character}}, $self->{next_input_character};        unshift @{$self->{prev_char}}, $self->{next_char};
6455    
6456        $self->{next_input_character} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
6457        $self->{next_input_character} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
6458        $column++;        $column++;
6459    
6460        if ($self->{next_input_character} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
6461          $line++;          $line++;
6462          $column = 0;          $column = 0;
6463        } elsif ($self->{next_input_character} == 0x000D) { # CR          !!!cp ('i1');
6464          } elsif ($self->{next_char} == 0x000D) { # CR
6465          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
6466          $self->{next_input_character} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
6467          $line++;          $line++;
6468          $column = 0;          $column = 0;
6469        } elsif ($self->{next_input_character} > 0x10FFFF) {          !!!cp ('i2');
6470          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        } elsif ($self->{next_char} > 0x10FFFF) {
6471        } elsif ($self->{next_input_character} == 0x0000) { # NULL          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
6472            !!!cp ('i3');
6473          } elsif ($self->{next_char} == 0x0000) { # NULL
6474            !!!cp ('i4');
6475          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
6476          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
6477        }        }
6478      };      };
6479      $p->{prev_input_character} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
6480      $p->{next_input_character} = -1;      $p->{next_char} = -1;
6481            
6482      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
6483        my (%opt) = @_;        my (%opt) = @_;
# Line 5293  sub set_inner_html ($$$) { Line 6491  sub set_inner_html ($$$) {
6491      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
6492    
6493      ## Step 2      ## Step 2
6494      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
6495      $p->{content_model} = {      $p->{content_model} = {
6496        title => RCDATA_CONTENT_MODEL,        title => RCDATA_CONTENT_MODEL,
6497        textarea => RCDATA_CONTENT_MODEL,        textarea => RCDATA_CONTENT_MODEL,
# Line 5312  sub set_inner_html ($$$) { Line 6510  sub set_inner_html ($$$) {
6510    
6511      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $node_ln];
6512    
6513      ## Step 4      ## Step 3
6514      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
6515        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
6516    
6517      ## Step 5 # MUST      ## Step 4 # MUST
6518      $doc->append_child ($root);      $doc->append_child ($root);
6519    
6520      ## Step 6 # MUST      ## Step 5 # MUST
6521      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, 'html'];
6522    
6523      undef $p->{head_element};      undef $p->{head_element};
6524    
6525      ## Step 7 # MUST      ## Step 6 # MUST
6526      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
6527    
6528      ## Step 8 # MUST      ## Step 7 # MUST
6529      my $anode = $node;      my $anode = $node;
6530      AN: while (defined $anode) {      AN: while (defined $anode) {
6531        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
6532          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
6533          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
6534            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
6535                !!!cp ('i5');
6536              $p->{form_element} = $anode;              $p->{form_element} = $anode;
6537              last AN;              last AN;
6538            }            }
# Line 5342  sub set_inner_html ($$$) { Line 6541  sub set_inner_html ($$$) {
6541        $anode = $anode->parent_node;        $anode = $anode->parent_node;
6542      } # AN      } # AN
6543            
6544      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
6545      {      {
6546        my $self = $p;        my $self = $p;
6547        !!!next-token;        !!!next-token;
6548      }      }
6549      $p->_tree_construction_main;      $p->_tree_construction_main;
6550    
6551      ## Step 11 # MUST      ## Step 10 # MUST
6552      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
6553      for (@cn) {      for (@cn) {
6554        $node->remove_child ($_);        $node->remove_child ($_);
6555      }      }
6556      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
6557    
6558      ## Step 12 # MUST      ## Step 11 # MUST
6559      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
6560      for (@cn) {      for (@cn) {
6561        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);
# Line 5373  sub set_inner_html ($$$) { Line 6571  sub set_inner_html ($$$) {
6571    
6572  } # tree construction stage  } # tree construction stage
6573    
6574  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
6575    my (undef, $node, $on_error) = @_;  push our @ISA, 'Error';
   
   ## Step 1  
   my $s = '';  
   
   my $in_cdata;  
   my $parent = $node;  
   while (defined $parent) {  
     if ($parent->node_type == 1 and  
         $parent->namespace_uri eq 'http://www.w3.org/1999/xhtml' and  
         {  
           style => 1, script => 1, xmp => 1, iframe => 1,  
           noembed => 1, noframes => 1, noscript => 1,  
         }->{$parent->local_name}) { ## TODO: case thingy  
       $in_cdata = 1;  
     }  
     $parent = $parent->parent_node;  
   }  
   
   ## Step 2  
   my @node = @{$node->child_nodes};  
   C: while (@node) {  
     my $child = shift @node;  
     unless (ref $child) {  
       if ($child eq 'cdata-out') {  
         $in_cdata = 0;  
       } else {  
         $s .= $child; # end tag  
       }  
       next C;  
     }  
       
     my $nt = $child->node_type;  
     if ($nt == 1) { # Element  
       my $tag_name = $child->tag_name; ## TODO: manakai_tag_name  
       $s .= '<' . $tag_name;  
       ## NOTE: Non-HTML case:  
       ## <http://permalink.gmane.org/gmane.org.w3c.whatwg.discuss/11191>  
   
       my @attrs = @{$child->attributes}; # sort order MUST be stable  
       for my $attr (@attrs) { # order is implementation dependent  
         my $attr_name = $attr->name; ## TODO: manakai_name  
         $s .= ' ' . $attr_name . '="';  
         my $attr_value = $attr->value;  
         ## escape  
         $attr_value =~ s/&/&amp;/g;  
         $attr_value =~ s/</&lt;/g;  
         $attr_value =~ s/>/&gt;/g;  
         $attr_value =~ s/"/&quot;/g;  
         $s .= $attr_value . '"';  
       }  
       $s .= '>';  
         
       next C if {  
         area => 1, base => 1, basefont => 1, bgsound => 1,  
         br => 1, col => 1, embed => 1, frame => 1, hr => 1,  
         img => 1, input => 1, link => 1, meta => 1, param => 1,  
         spacer => 1, wbr => 1,  
       }->{$tag_name};  
   
       $s .= "\x0A" if $tag_name eq 'pre' or $tag_name eq 'textarea';  
   
       if (not $in_cdata and {  
         style => 1, script => 1, xmp => 1, iframe => 1,  
         noembed => 1, noframes => 1, noscript => 1,  
         plaintext => 1,  
       }->{$tag_name}) {  
         unshift @node, 'cdata-out';  
         $in_cdata = 1;  
       }  
   
       unshift @node, @{$child->child_nodes}, '</' . $tag_name . '>';  
     } elsif ($nt == 3 or $nt == 4) {  
       if ($in_cdata) {  
         $s .= $child->data;  
       } else {  
         my $value = $child->data;  
         $value =~ s/&/&amp;/g;  
         $value =~ s/</&lt;/g;  
         $value =~ s/>/&gt;/g;  
         $value =~ s/"/&quot;/g;  
         $s .= $value;  
       }  
     } elsif ($nt == 8) {  
       $s .= '<!--' . $child->data . '-->';  
     } elsif ($nt == 10) {  
       $s .= '<!DOCTYPE ' . $child->name . '>';  
     } elsif ($nt == 5) { # entrefs  
       push @node, @{$child->child_nodes};  
     } else {  
       $on_error->($child) if defined $on_error;  
     }  
     ## ISSUE: This code does not support PIs.  
   } # C  
     
   ## Step 3  
   return \$s;  
 } # get_inner_html  
6576    
6577  1;  1;
6578  # $Date$  # $Date$

Legend:
Removed from v.1.41  
changed lines
  Added in v.1.113

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24