/[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.37 by wakaba, Mon Jul 16 07:03:09 2007 UTC revision 1.112 by wakaba, Sun Mar 16 06:39:57 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_input_character} = -1 and return if $i >= length $$s;      $self->{next_char} = -1 and return if $i >= length $$s;
189      $self->{next_input_character} = ord substr $$s, $i++, 1;      $self->{next_char} = ord substr $$s, $i++, 1;
190      $column++;  
191        ($self->{line_prev}, $self->{column_prev})
192            = ($self->{line}, $self->{column});
193        $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    
254    sub CM_ENTITY () { 0b001 } # & markup in data
255    sub CM_LIMITED_MARKUP () { 0b010 } # < markup in data (limited)
256    sub CM_FULL_MARKUP () { 0b100 } # < markup in data (any)
257    
258    sub PLAINTEXT_CONTENT_MODEL () { 0 }
259    sub CDATA_CONTENT_MODEL () { CM_LIMITED_MARKUP }
260    sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
261    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_flag} = 'PCDATA'; # 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 185  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 192  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_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
404              $self->{content_model_flag} eq 'RCDATA') {              not $self->{escape}) {
405            $self->{state} = 'entity data';            !!!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_flag} eq 'RCDATA' or          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
             $self->{content_model_flag} eq '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_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
432              (($self->{content_model_flag} eq 'CDATA' or              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
               $self->{content_model_flag} eq 'RCDATA') and  
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_flag} eq 'RCDATA' or              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
445               $self->{content_model_flag} eq 'CDATA')) {            if ($self->{prev_char}->[0] == 0x002D and # -
446            if ($self->{prev_input_character}->[0] == 0x002D and # -                $self->{prev_char}->[1] == 0x002D) { # -
447                $self->{prev_input_character}->[1] == 0x002D) { # -              !!!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_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
497            $self->{content_model_flag} eq 'CDATA') {          if ($self->{next_char} == 0x002F) { # /
498          if ($self->{next_input_character} == 0x002F) { # /            !!!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_flag} eq '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_flag}: Unknown content model flag";          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        if ($self->{content_model_flag} eq 'RCDATA' or        my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
582            $self->{content_model_flag} eq '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_flag} = 'PCDATA'; # 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_flag} = 'PCDATA'; # 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_flag} = 'PCDATA'; # 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_flag} = 'PCDATA'; # 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            !!!parse-error (type => 'dupulicate attribute');            !!!cp (57);
878              !!!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            $self->{content_model_flag} = 'PCDATA'; # MUST            !!!cp (62);
912              $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');
915            }            }
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_flag} = 'PCDATA'; # 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_flag} = 'PCDATA'; # 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_flag} = 'PCDATA'; # 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_flag} = 'PCDATA'; # 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_flag} = 'PCDATA'; # 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_flag} = 'PCDATA'; # 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_flag} = 'PCDATA'; # 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_flag} = 'PCDATA'; # 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_flag} = 'PCDATA'; # 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;
1940            redo A;
1941          } elsif ($self->{next_char} == 0x003E) { # >
1942            !!!cp (188);
1943            !!!parse-error (type => 'unclosed PUBLIC literal');
1944    
1945            $self->{state} = DATA_STATE;
1946          !!!next-input-character;          !!!next-input-character;
1947    
1948            $self->{current_token}->{quirks} = 1;
1949            !!!emit ($self->{current_token}); # DOCTYPE
1950    
1951          redo A;          redo A;
1952        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1953            !!!cp (189);
1954          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1955    
1956          $self->{state} = 'data';          $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;          !!!next-input-character;
1976          redo A;          redo A;
1977        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
1978            !!!cp (192);
1979          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1980    
1981          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1982            !!!next-input-character;
1983    
1984            $self->{current_token}->{quirks} = 1;
1985            !!!emit ($self->{current_token}); # DOCTYPE
1986    
1987            redo A;
1988          } elsif ($self->{next_char} == -1) {
1989            !!!cp (193);
1990            !!!parse-error (type => 'unclosed PUBLIC literal');
1991    
1992            $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 1606  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 1723  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 1803  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 1827  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            !!!cp ('t1');
2514          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5');
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');
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 1843  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 1897  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 1919  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          !!!cp ('t14');
2657        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE');
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');
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            !!!cp ('t19');
2707          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE');
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 2040  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 2059  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 2095  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 2118  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 2132  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 2166  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 2193  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_flag} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
2973      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
2974    
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      }      }
2990    
2991      ## Step 6      ## Step 6
2992      $self->{content_model_flag} = 'PCDATA';      $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
2999      } else {      } else {
3000        !!!parse-error (type => 'in '.$content_model_flag.':#'.$token->{type});        ## 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});
3004          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3005            !!!cp ('t44');
3006            !!!parse-error (type => 'in RCDATA:#'.$token->{type});
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"
3018    
3019      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
3020      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
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_flag} = 'PCDATA';      $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          !!!cp ('t48');
3042        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type});
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 2271  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 $tag_name = shift;
3071    
3072        ## NOTE: The adoption agency algorithm (AAA).
3073    
3074      FET: {      FET: {
3075        ## Step 1        ## Step 1
3076        my $formatting_element;        my $formatting_element;
3077        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3078        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3079          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {
3080              !!!cp ('t51');
3081            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3082            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3083            last AFE;            last AFE;
3084          } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {          } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {
3085              !!!cp ('t52');
3086            last AFE;            last AFE;
3087          }          }
3088        } # AFE        } # AFE
3089        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3090            !!!cp ('t53');
3091          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!parse-error (type => 'unmatched end tag:'.$tag_name);
3092          ## Ignore the token          ## Ignore the token
3093          !!!next-token;          !!!next-token;
# Line 2300  sub _tree_construction_main ($) { Line 3100  sub _tree_construction_main ($) {
3100          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3101          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
3102            if ($in_scope) {            if ($in_scope) {
3103                !!!cp ('t54');
3104              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
3105              last INSCOPE;              last INSCOPE;
3106            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3107                !!!cp ('t55');
3108              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3109              ## Ignore the token              ## Ignore the token
3110              !!!next-token;              !!!next-token;
3111              return;              return;
3112            }            }
3113          } elsif ({          } elsif ({
3114                    table => 1, caption => 1, td => 1, th => 1,                    applet => 1, table => 1, caption => 1, td => 1, th => 1,
3115                    button => 1, marquee => 1, object => 1, html => 1,                    button => 1, marquee => 1, object => 1, html => 1,
3116                   }->{$node->[1]}) {                   }->{$node->[1]}) {
3117              !!!cp ('t56');
3118            $in_scope = 0;            $in_scope = 0;
3119          }          }
3120        } # INSCOPE        } # INSCOPE
3121        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3122            !!!cp ('t57');
3123          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3124          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3125          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3126          return;          return;
3127        }        }
3128        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3129            !!!cp ('t58');
3130          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3131        }        }
3132                
# Line 2333  sub _tree_construction_main ($) { Line 3138  sub _tree_construction_main ($) {
3138          if (not $formatting_category->{$node->[1]} and          if (not $formatting_category->{$node->[1]} and
3139              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3140              ($special_category->{$node->[1]} or              ($special_category->{$node->[1]} or
3141               $scoping_category->{$node->[1]})) {               $scoping_category->{$node->[1]})) { ## Scoping is redundant, maybe
3142              !!!cp ('t59');
3143            $furthest_block = $node;            $furthest_block = $node;
3144            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
3145          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
3146              !!!cp ('t60');
3147            last OE;            last OE;
3148          }          }
3149        } # OE        } # OE
3150                
3151        ## Step 3        ## Step 3
3152        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
3153            !!!cp ('t61');
3154          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
3155          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
3156          !!!next-token;          !!!next-token;
# Line 2355  sub _tree_construction_main ($) { Line 3163  sub _tree_construction_main ($) {
3163        ## Step 5        ## Step 5
3164        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
3165        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
3166            !!!cp ('t62');
3167          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
3168        }        }
3169                
# Line 2377  sub _tree_construction_main ($) { Line 3186  sub _tree_construction_main ($) {
3186          S7S2: {          S7S2: {
3187            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
3188              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
3189                  !!!cp ('t63');
3190                $node_i_in_active = $_;                $node_i_in_active = $_;
3191                last S7S2;                last S7S2;
3192              }              }
# Line 2390  sub _tree_construction_main ($) { Line 3200  sub _tree_construction_main ($) {
3200                    
3201          ## Step 4          ## Step 4
3202          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
3203              !!!cp ('t64');
3204            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
3205          }          }
3206                    
3207          ## Step 5          ## Step 5
3208          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
3209              !!!cp ('t65');
3210            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
3211            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
3212            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2412  sub _tree_construction_main ($) { Line 3224  sub _tree_construction_main ($) {
3224        } # S7          } # S7  
3225                
3226        ## Step 8        ## Step 8
3227        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ({
3228               table => 1, tbody => 1, tfoot => 1, thead => 1, tr => 1,
3229              }->{$common_ancestor_node->[1]}) {
3230            my $foster_parent_element;
3231            my $next_sibling;
3232                             OE: for (reverse 0..$#{$self->{open_elements}}) {
3233                               if ($self->{open_elements}->[$_]->[1] eq 'table') {
3234                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3235                                 if (defined $parent and $parent->node_type == 1) {
3236                                   !!!cp ('t65.1');
3237                                   $foster_parent_element = $parent;
3238                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3239                                 } else {
3240                                   !!!cp ('t65.2');
3241                                   $foster_parent_element
3242                                     = $self->{open_elements}->[$_ - 1]->[0];
3243                                 }
3244                                 last OE;
3245                               }
3246                             } # OE
3247                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3248                               unless defined $foster_parent_element;
3249            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3250            $open_tables->[-1]->[1] = 1; # tainted
3251          } else {
3252            !!!cp ('t65.3');
3253            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3254          }
3255                
3256        ## Step 9        ## Step 9
3257        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2429  sub _tree_construction_main ($) { Line 3268  sub _tree_construction_main ($) {
3268        my $i;        my $i;
3269        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3270          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
3271              !!!cp ('t66');
3272            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
3273            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
3274          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
3275              !!!cp ('t67');
3276            $i = $_;            $i = $_;
3277          }          }
3278        } # AFE        } # AFE
# Line 2441  sub _tree_construction_main ($) { Line 3282  sub _tree_construction_main ($) {
3282        undef $i;        undef $i;
3283        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3284          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
3285              !!!cp ('t68');
3286            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
3287            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
3288          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
3289              !!!cp ('t69');
3290            $i = $_;            $i = $_;
3291          }          }
3292        } # OE        } # OE
# Line 2454  sub _tree_construction_main ($) { Line 3297  sub _tree_construction_main ($) {
3297      } # FET      } # FET
3298    }; # $formatting_end_tag    }; # $formatting_end_tag
3299    
3300    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3301      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3302    }; # $insert_to_current    }; # $insert_to_current
3303    
3304    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3305                         my $child = shift;      my $child = shift;
3306                         if ({      if ({
3307                              table => 1, tbody => 1, tfoot => 1,           table => 1, tbody => 1, tfoot => 1, thead => 1, tr => 1,
3308                              thead => 1, tr => 1,          }->{$self->{open_elements}->[-1]->[1]}) {
3309                             }->{$self->{open_elements}->[-1]->[1]}) {        # MUST
3310                           # MUST        my $foster_parent_element;
3311                           my $foster_parent_element;        my $next_sibling;
                          my $next_sibling;  
3312                           OE: for (reverse 0..$#{$self->{open_elements}}) {                           OE: for (reverse 0..$#{$self->{open_elements}}) {
3313                             if ($self->{open_elements}->[$_]->[1] eq 'table') {                             if ($self->{open_elements}->[$_]->[1] eq 'table') {
3314                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3315                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3316                                   !!!cp ('t70');
3317                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
3318                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
3319                               } else {                               } else {
3320                                   !!!cp ('t71');
3321                                 $foster_parent_element                                 $foster_parent_element
3322                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
3323                               }                               }
# Line 2484  sub _tree_construction_main ($) { Line 3328  sub _tree_construction_main ($) {
3328                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
3329                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3330                             ($child, $next_sibling);                             ($child, $next_sibling);
3331                         } else {        $open_tables->[-1]->[1] = 1; # tainted
3332                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
3333                         }        !!!cp ('t72');
3334    }; # $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', $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', 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_flag} = 'PLAINTEXT';  
             
         !!!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', $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_flag} = 'RCDATA';  
         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_flag} = 'PCDATA';  
           
         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', $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;  
       }  
3335      }      }
3336    }; # $in_body    }; # $insert_to_foster
3337    
3338    B: {    B: {
3339      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
3340          !!!cp ('t73');
3341        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'DOCTYPE in the middle');
3342        ## Ignore the token        ## Ignore the token
3343        ## Stay in the phase        ## Stay in the phase
3344        !!!next-token;        !!!next-token;
3345        redo B;        redo B;
3346      } 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  
3347               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
3348        if ($self->{insertion_mode} eq 'trailing end') {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3349          ## Turn into the main phase          !!!cp ('t79');
3350          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html');
3351          $self->{insertion_mode} = $previous_insertion_mode;          $self->{insertion_mode} = AFTER_BODY_IM;
3352          } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3353            !!!cp ('t80');
3354            !!!parse-error (type => 'after html:html');
3355            $self->{insertion_mode} = AFTER_FRAMESET_IM;
3356          } else {
3357            !!!cp ('t81');
3358        }        }
3359    
3360  ## ISSUE: "aa<html>" is not a parse error.        !!!cp ('t82');
3361  ## ISSUE: "<html>" in fragment is not a parse error.        !!!parse-error (type => 'not first start tag');
       unless ($token->{first_start_tag}) {  
         !!!parse-error (type => 'not first start tag');  
       }  
3362        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
3363        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
3364          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
3365              !!!cp ('t84');
3366            $top_el->set_attribute_ns            $top_el->set_attribute_ns
3367              (undef, [undef, $attr_name],              (undef, [undef, $attr_name],
3368               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
# Line 3390  sub _tree_construction_main ($) { Line 3370  sub _tree_construction_main ($) {
3370        }        }
3371        !!!next-token;        !!!next-token;
3372        redo B;        redo B;
3373      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
3374        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3375        if ($self->{insertion_mode} eq 'trailing end') {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
3376            !!!cp ('t85');
3377          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3378        } elsif ($self->{insertion_mode} eq 'after body') {        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
3379            !!!cp ('t86');
3380          $self->{open_elements}->[0]->[0]->append_child ($comment);          $self->{open_elements}->[0]->[0]->append_child ($comment);
3381        } else {        } else {
3382            !!!cp ('t87');
3383          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
3384        }        }
3385        !!!next-token;        !!!next-token;
3386        redo B;        redo B;
3387      } elsif ($self->{insertion_mode} eq 'before head') {      } elsif ($self->{insertion_mode} & HEAD_IMS) {
3388            if ($token->{type} eq 'character') {        if ($token->{type} == CHARACTER_TOKEN) {
3389              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3390                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3391                unless (length $token->{data}) {              !!!cp ('t88.2');
3392                  !!!next-token;              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3393                  redo B;            } else {
3394                }              !!!cp ('t88.1');
3395              }              ## Ignore the token.
3396              ## As if <head>              !!!next-token;
3397              !!!create-element ($self->{head_element}, 'head');              redo B;
3398              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            }
3399              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            unless (length $token->{data}) {
3400              $self->{insertion_mode} = 'in head';              !!!cp ('t88');
3401                !!!next-token;
3402                redo B;
3403              }
3404            }
3405    
3406            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3407              !!!cp ('t89');
3408              ## As if <head>
3409              !!!create-element ($self->{head_element}, 'head');
3410              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3411              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3412    
3413              ## Reprocess in the "in head" insertion mode...
3414              pop @{$self->{open_elements}};
3415    
3416              ## Reprocess in the "after head" insertion mode...
3417            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3418              !!!cp ('t90');
3419              ## As if </noscript>
3420              pop @{$self->{open_elements}};
3421              !!!parse-error (type => 'in noscript:#character');
3422              
3423              ## Reprocess in the "in head" insertion mode...
3424              ## As if </head>
3425              pop @{$self->{open_elements}};
3426    
3427              ## Reprocess in the "after head" insertion mode...
3428            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3429              !!!cp ('t91');
3430              pop @{$self->{open_elements}};
3431    
3432              ## Reprocess in the "after head" insertion mode...
3433            } else {
3434              !!!cp ('t92');
3435            }
3436    
3437                ## "after head" insertion mode
3438                ## As if <body>
3439                !!!insert-element ('body');
3440                $self->{insertion_mode} = IN_BODY_IM;
3441              ## reprocess              ## reprocess
3442              redo B;              redo B;
3443            } 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';  
3444              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
3445                !!!next-token;                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3446              #} elsif ({                  !!!cp ('t93');
3447              #          base => 1, link => 1, meta => 1,                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
3448              #          script => 1, style => 1, title => 1,                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3449              #         }->{$token->{tag_name}}) {                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
3450              #  ## reprocess                  $self->{insertion_mode} = IN_HEAD_IM;
3451              } else {                  !!!next-token;
3452                ## reprocess                  redo B;
3453              }                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3454              redo B;                  !!!cp ('t94');
3455            } elsif ($token->{type} eq 'end tag') {                  #
3456              if ({                } else {
3457                   head => 1, body => 1, html => 1,                  !!!cp ('t95');
3458                   p => 1, br => 1,                  !!!parse-error (type => 'in head:head'); # or in head noscript
3459                  }->{$token->{tag_name}}) {                  ## Ignore the token
3460                    !!!next-token;
3461                    redo B;
3462                  }
3463                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3464                  !!!cp ('t96');
3465                ## As if <head>                ## As if <head>
3466                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head');
3467                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3468                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3469                $self->{insertion_mode} = 'in head';  
3470                ## reprocess                $self->{insertion_mode} = IN_HEAD_IM;
3471                redo B;                ## Reprocess in the "in head" insertion mode...
3472              } else {              } else {
3473                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t97');
               ## Ignore the token ## ISSUE: An issue in the spec.  
               !!!next-token;  
               redo B;  
3474              }              }
3475            } else {  
3476              die "$0: $token->{type}: Unknown type";              if ($token->{tag_name} eq 'base') {
3477            }                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3478          } elsif ($self->{insertion_mode} eq 'in head' or                  !!!cp ('t98');
3479                   $self->{insertion_mode} eq 'in head noscript' or                  ## As if </noscript>
3480                   $self->{insertion_mode} eq 'after head') {                  pop @{$self->{open_elements}};
3481            if ($token->{type} eq 'character') {                  !!!parse-error (type => 'in noscript:base');
3482              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                
3483                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                  $self->{insertion_mode} = IN_HEAD_IM;
3484                unless (length $token->{data}) {                  ## Reprocess in the "in head" insertion mode...
3485                  !!!next-token;                } else {
3486                  redo B;                  !!!cp ('t99');
3487                }                }
3488              }  
               
             #  
           } 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}}) {  
3489                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3490                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3491                    !!!cp ('t100');
3492                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3493                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3494                  } else {
3495                    !!!cp ('t101');
3496                }                }
3497                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3498                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3499                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3500                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3501                !!!next-token;                !!!next-token;
3502                redo B;                redo B;
3503              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'link') {
3504                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3505                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3506                    !!!cp ('t102');
3507                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3508                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3509                  } else {
3510                    !!!cp ('t103');
3511                }                }
3512                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3513                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3514                  pop @{$self->{open_elements}} # <head>
3515                      if $self->{insertion_mode} == AFTER_HEAD_IM;
3516                  !!!next-token;
3517                  redo B;
3518                } elsif ($token->{tag_name} eq 'meta') {
3519                  ## NOTE: There is a "as if in head" code clone.
3520                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3521                    !!!cp ('t104');
3522                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3523                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3524                  } else {
3525                    !!!cp ('t105');
3526                  }
3527                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3528                  my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3529    
3530                unless ($self->{confident}) {                unless ($self->{confident}) {
                 my $charset;  
3531                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
3532                    $charset = $token->{attributes}->{charset}->{value};                    !!!cp ('t106');
3533                  }                    $self->{change_encoding}
3534                  if ($token->{attributes}->{'http-equiv'}) {                        ->($self, $token->{attributes}->{charset}->{value});
3535                      
3536                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3537                          ->set_user_data (manakai_has_reference =>
3538                                               $token->{attributes}->{charset}
3539                                                   ->{has_reference});
3540                    } elsif ($token->{attributes}->{content}) {
3541                    ## 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.
3542                    if ($token->{attributes}->{'http-equiv'}->{value}                    if ($token->{attributes}->{content}->{value}
3543                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
3544                              [\x09-\x0D\x20]*=
3545                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
3546                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
3547                      $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                      !!!cp ('t107');
3548                    } ## TODO: And if supported                      $self->{change_encoding}
3549                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
3550                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3551                            ->set_user_data (manakai_has_reference =>
3552                                                 $token->{attributes}->{content}
3553                                                       ->{has_reference});
3554                      } else {
3555                        !!!cp ('t108');
3556                      }
3557                    }
3558                  } else {
3559                    if ($token->{attributes}->{charset}) {
3560                      !!!cp ('t109');
3561                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3562                          ->set_user_data (manakai_has_reference =>
3563                                               $token->{attributes}->{charset}
3564                                                   ->{has_reference});
3565                    }
3566                    if ($token->{attributes}->{content}) {
3567                      !!!cp ('t110');
3568                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3569                          ->set_user_data (manakai_has_reference =>
3570                                               $token->{attributes}->{content}
3571                                                   ->{has_reference});
3572                  }                  }
                 ## TODO: Change the encoding  
3573                }                }
3574    
3575                ## TODO: Extracting |charset| from |meta|.                pop @{$self->{open_elements}} # <head>
3576                pop @{$self->{open_elements}}                    if $self->{insertion_mode} == AFTER_HEAD_IM;
                   if $self->{insertion_mode} eq 'after head';  
3577                !!!next-token;                !!!next-token;
3578                redo B;                redo B;
3579              } elsif ($token->{tag_name} eq 'title' and              } elsif ($token->{tag_name} eq 'title') {
3580                       $self->{insertion_mode} eq 'in head') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3581                ## NOTE: There is a "as if in head" code clone.                  !!!cp ('t111');
3582                if ($self->{insertion_mode} eq 'after head') {                  ## As if </noscript>
3583                    pop @{$self->{open_elements}};
3584                    !!!parse-error (type => 'in noscript:title');
3585                  
3586                    $self->{insertion_mode} = IN_HEAD_IM;
3587                    ## Reprocess in the "in head" insertion mode...
3588                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3589                    !!!cp ('t112');
3590                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3591                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3592                  } else {
3593                    !!!cp ('t113');
3594                }                }
3595    
3596                  ## NOTE: There is a "as if in head" code clone.
3597                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
3598                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
3599                $parse_rcdata->('RCDATA', sub { $parent->append_child ($_[0]) });                $parse_rcdata->(RCDATA_CONTENT_MODEL);
3600                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3601                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3602                redo B;                redo B;
3603              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
3604                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
3605                ## insertion mode 'in head')                ## insertion mode IN_HEAD_IM)
3606                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3607                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3608                    !!!cp ('t114');
3609                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3610                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3611                  } else {
3612                    !!!cp ('t115');
3613                }                }
3614                $parse_rcdata->('CDATA', $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
3615                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3616                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3617                redo B;                redo B;
3618              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
3619                if ($self->{insertion_mode} eq 'in head') {                if ($self->{insertion_mode} == IN_HEAD_IM) {
3620                    !!!cp ('t116');
3621                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
3622                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3623                  $self->{insertion_mode} = 'in head noscript';                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
3624                  !!!next-token;                  !!!next-token;
3625                  redo B;                  redo B;
3626                } elsif ($self->{insertion_mode} eq 'in head noscript') {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3627                    !!!cp ('t117');
3628                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript:noscript');
3629                  ## Ignore the token                  ## Ignore the token
3630                    !!!next-token;
3631                  redo B;                  redo B;
3632                } else {                } else {
3633                    !!!cp ('t118');
3634                  #                  #
3635                }                }
3636              } elsif ($token->{tag_name} eq 'head' and              } elsif ($token->{tag_name} eq 'script') {
3637                       $self->{insertion_mode} ne 'after head') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3638                !!!parse-error (type => 'in head:head'); # or in head noscript                  !!!cp ('t119');
3639                ## Ignore the token                  ## As if </noscript>
3640                !!!next-token;                  pop @{$self->{open_elements}};
3641                redo B;                  !!!parse-error (type => 'in noscript:script');
3642              } elsif ($self->{insertion_mode} ne 'in head noscript' and                
3643                       $token->{tag_name} eq 'script') {                  $self->{insertion_mode} = IN_HEAD_IM;
3644                if ($self->{insertion_mode} eq 'after head') {                  ## Reprocess in the "in head" insertion mode...
3645                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3646                    !!!cp ('t120');
3647                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3648                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3649                  } else {
3650                    !!!cp ('t121');
3651                }                }
3652    
3653                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3654                $script_start_tag->($insert_to_current);                $script_start_tag->();
3655                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3656                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3657                redo B;                redo B;
3658              } elsif ($self->{insertion_mode} eq 'after head' and              } elsif ($token->{tag_name} eq 'body' or
                      $token->{tag_name} eq 'body') {  
               !!!insert-element ('body', $token->{attributes});  
               $self->{insertion_mode} = 'in body';  
               !!!next-token;  
               redo B;  
             } elsif ($self->{insertion_mode} eq 'after head' and  
3659                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
3660                !!!insert-element ('frameset', $token->{attributes});                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3661                $self->{insertion_mode} = 'in frameset';                  !!!cp ('t122');
3662                    ## As if </noscript>
3663                    pop @{$self->{open_elements}};
3664                    !!!parse-error (type => 'in noscript:'.$token->{tag_name});
3665                    
3666                    ## Reprocess in the "in head" insertion mode...
3667                    ## As if </head>
3668                    pop @{$self->{open_elements}};
3669                    
3670                    ## Reprocess in the "after head" insertion mode...
3671                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3672                    !!!cp ('t124');
3673                    pop @{$self->{open_elements}};
3674                    
3675                    ## Reprocess in the "after head" insertion mode...
3676                  } else {
3677                    !!!cp ('t125');
3678                  }
3679    
3680                  ## "after head" insertion mode
3681                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3682                  if ($token->{tag_name} eq 'body') {
3683                    !!!cp ('t126');
3684                    $self->{insertion_mode} = IN_BODY_IM;
3685                  } elsif ($token->{tag_name} eq 'frameset') {
3686                    !!!cp ('t127');
3687                    $self->{insertion_mode} = IN_FRAMESET_IM;
3688                  } else {
3689                    die "$0: tag name: $self->{tag_name}";
3690                  }
3691                !!!next-token;                !!!next-token;
3692                redo B;                redo B;
3693              } else {              } else {
3694                  !!!cp ('t128');
3695                #                #
3696              }              }
3697            } elsif ($token->{type} eq 'end tag') {  
3698              if ($self->{insertion_mode} eq 'in head' and              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3699                  $token->{tag_name} eq 'head') {                !!!cp ('t129');
3700                  ## As if </noscript>
3701                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3702                $self->{insertion_mode} = 'after head';                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
3703                !!!next-token;                
3704                redo B;                ## Reprocess in the "in head" insertion mode...
3705              } elsif ($self->{insertion_mode} eq 'in head noscript' and                ## As if </head>
                 $token->{tag_name} eq 'noscript') {  
3706                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3707                $self->{insertion_mode} = 'in head';  
3708                !!!next-token;                ## Reprocess in the "after head" insertion mode...
3709                redo B;              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3710              } elsif ($self->{insertion_mode} eq 'in head' and                !!!cp ('t130');
3711                       {                ## As if </head>
3712                        body => 1, html => 1,                pop @{$self->{open_elements}};
3713                        p => 1, br => 1,  
3714                       }->{$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;  
3715              } else {              } else {
3716                #                !!!cp ('t131');
3717              }              }
           } else {  
             #  
           }  
3718    
3719            ## As if </head> or </noscript> or <body>              ## "after head" insertion mode
3720            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'  
3721              !!!insert-element ('body');              !!!insert-element ('body');
3722              $self->{insertion_mode} = 'in body';              $self->{insertion_mode} = IN_BODY_IM;
3723            }              ## 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);  
3724              redo B;              redo B;
3725            }            } elsif ($token->{type} == END_TAG_TOKEN) {
3726          } elsif ($self->{insertion_mode} eq 'in table') {              if ($token->{tag_name} eq 'head') {
3727            if ($token->{type} eq 'character') {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3728              ## NOTE: There are "character in table" code clones.                  !!!cp ('t132');
3729              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                  ## As if <head>
3730                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                  !!!create-element ($self->{head_element}, 'head');
3731                                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3732                unless (length $token->{data}) {                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3733    
3734                    ## Reprocess in the "in head" insertion mode...
3735                    pop @{$self->{open_elements}};
3736                    $self->{insertion_mode} = AFTER_HEAD_IM;
3737                    !!!next-token;
3738                    redo B;
3739                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3740                    !!!cp ('t133');
3741                    ## As if </noscript>
3742                    pop @{$self->{open_elements}};
3743                    !!!parse-error (type => 'in noscript:/head');
3744                    
3745                    ## Reprocess in the "in head" insertion mode...
3746                    pop @{$self->{open_elements}};
3747                    $self->{insertion_mode} = AFTER_HEAD_IM;
3748                    !!!next-token;
3749                    redo B;
3750                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3751                    !!!cp ('t134');
3752                    pop @{$self->{open_elements}};
3753                    $self->{insertion_mode} = AFTER_HEAD_IM;
3754                  !!!next-token;                  !!!next-token;
3755                  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});  
3756                } else {                } else {
3757                  $foster_parent_element->insert_before                  !!!cp ('t135');
3758                    ($self->{document}->create_text_node ($token->{data}),                  #
                    $next_sibling);  
3759                }                }
3760              } else {              } elsif ($token->{tag_name} eq 'noscript') {
3761                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3762              }                  !!!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]);  
3763                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3764                    $self->{insertion_mode} = IN_HEAD_IM;
3765                    !!!next-token;
3766                    redo B;
3767                  } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3768                    !!!cp ('t137');
3769                    !!!parse-error (type => 'unmatched end tag:noscript');
3770                    ## Ignore the token ## ISSUE: An issue in the spec.
3771                    !!!next-token;
3772                    redo B;
3773                  } else {
3774                    !!!cp ('t138');
3775                    #
3776                }                }
   
               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;  
3777              } elsif ({              } elsif ({
3778                        col => 1,                        body => 1, html => 1,
                       td => 1, th => 1, tr => 1,  
3779                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3780                ## Clear back to table context                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3781                while ($self->{open_elements}->[-1]->[1] ne 'table' and                  !!!cp ('t139');
3782                       $self->{open_elements}->[-1]->[1] ne 'html') {                  ## As if <head>
3783                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!create-element ($self->{head_element}, 'head');
3784                  pop @{$self->{open_elements}};                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3785                }                  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]);  
3786    
3787                ## As if </table>                  $self->{insertion_mode} = IN_HEAD_IM;
3788                ## have a table element in table scope                  ## Reprocess in the "in head" insertion mode...
3789                my $i;                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3790                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  !!!cp ('t140');
3791                  my $node = $self->{open_elements}->[$_];                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3792                  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>  
3793                  !!!next-token;                  !!!next-token;
3794                  redo B;                  redo B;
3795                  } else {
3796                    !!!cp ('t141');
3797                }                }
3798                                
3799                ## generate implied end tags                #
3800                if ({              } elsif ({
3801                     dd => 1, dt => 1, li => 1, p => 1,                        p => 1, br => 1,
3802                     td => 1, th => 1, tr => 1,                       }->{$token->{tag_name}}) {
3803                     tbody => 1, tfoot=> 1, thead => 1,                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3804                    }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t142');
3805                  !!!back-token; # <table>                  ## As if <head>
3806                  $token = {type => 'end tag', tag_name => 'table'};                  !!!create-element ($self->{head_element}, 'head');
3807                  !!!back-token;                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3808                  $token = {type => 'end tag',                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
3809    
3810                if ($self->{open_elements}->[-1]->[1] ne 'table') {                  $self->{insertion_mode} = IN_HEAD_IM;
3811                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  ## Reprocess in the "in head" insertion mode...
3812                  } else {
3813                    !!!cp ('t143');
3814                }                }
3815    
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
3816                #                #
3817              }              } else {
3818            } elsif ($token->{type} eq 'end tag') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3819              if ($token->{tag_name} eq 'table') {                  !!!cp ('t144');
3820                ## have a table element in table scope                  #
3821                my $i;                } else {
3822                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  !!!cp ('t145');
                 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) {  
3823                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3824                  ## Ignore the token                  ## Ignore the token
3825                  !!!next-token;                  !!!next-token;
3826                  redo B;                  redo B;
3827                }                }
3828                              }
               ## 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]);  
               }  
3829    
3830                splice @{$self->{open_elements}}, $i;              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3831                  !!!cp ('t146');
3832                  ## As if </noscript>
3833                  pop @{$self->{open_elements}};
3834                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
3835                  
3836                  ## Reprocess in the "in head" insertion mode...
3837                  ## As if </head>
3838                  pop @{$self->{open_elements}};
3839    
3840                $self->_reset_insertion_mode;                ## Reprocess in the "after head" insertion mode...
3841                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3842                  !!!cp ('t147');
3843                  ## As if </head>
3844                  pop @{$self->{open_elements}};
3845    
3846                !!!next-token;                ## Reprocess in the "after head" insertion mode...
3847                redo B;              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3848              } elsif ({  ## ISSUE: This case cannot be reached?
3849                        body => 1, caption => 1, col => 1, colgroup => 1,                !!!cp ('t148');
                       html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,  
                       thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
3850                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3851                ## Ignore the token                ## Ignore the token ## ISSUE: An issue in the spec.
3852                !!!next-token;                !!!next-token;
3853                redo B;                redo B;
3854              } else {              } else {
3855                #                !!!cp ('t149');
3856              }              }
           } else {  
             #  
           }  
3857    
3858            !!!parse-error (type => 'in table:'.$token->{tag_name});              ## "after head" insertion mode
3859            $in_body->($insert_to_foster);              ## As if <body>
3860            redo B;              !!!insert-element ('body');
3861          } elsif ($self->{insertion_mode} eq 'in caption') {              $self->{insertion_mode} = IN_BODY_IM;
3862            if ($token->{type} eq 'character') {              ## reprocess
3863              ## NOTE: This is a code clone of "character in body".              redo B;
3864          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
3865            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3866              !!!cp ('t149.1');
3867    
3868              ## NOTE: As if <head>
3869              !!!create-element ($self->{head_element}, 'head');
3870              $self->{open_elements}->[-1]->[0]->append_child
3871                  ($self->{head_element});
3872              #push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3873              #$self->{insertion_mode} = IN_HEAD_IM;
3874              ## NOTE: Reprocess.
3875    
3876              ## NOTE: As if </head>
3877              #pop @{$self->{open_elements}};
3878              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
3879              ## NOTE: Reprocess.
3880              
3881              #
3882            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3883              !!!cp ('t149.2');
3884    
3885              ## NOTE: As if </head>
3886              pop @{$self->{open_elements}};
3887              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
3888              ## NOTE: Reprocess.
3889    
3890              #
3891            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3892              !!!cp ('t149.3');
3893    
3894              !!!parse-error (type => 'in noscript:#eof');
3895    
3896              ## As if </noscript>
3897              pop @{$self->{open_elements}};
3898              #$self->{insertion_mode} = IN_HEAD_IM;
3899              ## NOTE: Reprocess.
3900    
3901              ## NOTE: As if </head>
3902              pop @{$self->{open_elements}};
3903              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
3904              ## NOTE: Reprocess.
3905    
3906              #
3907            } else {
3908              !!!cp ('t149.4');
3909              #
3910            }
3911    
3912            ## NOTE: As if <body>
3913            !!!insert-element ('body');
3914            $self->{insertion_mode} = IN_BODY_IM;
3915            ## NOTE: Reprocess.
3916            redo B;
3917          } else {
3918            die "$0: $token->{type}: Unknown token type";
3919          }
3920    
3921              ## ISSUE: An issue in the spec.
3922        } elsif ($self->{insertion_mode} & BODY_IMS) {
3923              if ($token->{type} == CHARACTER_TOKEN) {
3924                !!!cp ('t150');
3925                ## NOTE: There is a code clone of "character in body".
3926              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
3927                            
3928              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3929    
3930              !!!next-token;              !!!next-token;
3931              redo B;              redo B;
3932            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
3933              if ({              if ({
3934                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
3935                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
3936                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3937                !!!parse-error (type => 'not closed:caption');                if ($self->{insertion_mode} == IN_CELL_IM) {
3938                    ## have an element in table scope
3939                ## As if </caption>                  for (reverse 0..$#{$self->{open_elements}}) {
3940                ## have a table element in table scope                    my $node = $self->{open_elements}->[$_];
3941                my $i;                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {
3942                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                      !!!cp ('t151');
3943                  my $node = $self->{open_elements}->[$_];  
3944                  if ($node->[1] eq 'caption') {                      ## Close the cell
3945                    $i = $_;                      !!!back-token; # <?>
3946                    last INSCOPE;                      $token = {type => END_TAG_TOKEN, tag_name => $node->[1]};
3947                  } elsif ({                      redo B;
3948                            table => 1, html => 1,                    } elsif ({
3949                           }->{$node->[1]}) {                              table => 1, html => 1,
3950                    last INSCOPE;                             }->{$node->[1]}) {
3951                        !!!cp ('t152');
3952                        ## ISSUE: This case can never be reached, maybe.
3953                        last;
3954                      }
3955                  }                  }
3956                } # INSCOPE  
3957                unless (defined $i) {                  !!!cp ('t153');
3958                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'start tag not allowed',
3959                        value => $token->{tag_name});
3960                  ## Ignore the token                  ## Ignore the token
3961                  !!!next-token;                  !!!next-token;
3962                  redo B;                  redo B;
3963                }                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3964                                  !!!parse-error (type => 'not closed:caption');
3965                ## generate implied end tags                  
3966                if ({                  ## NOTE: As if </caption>.
3967                     dd => 1, dt => 1, li => 1, p => 1,                  ## have a table element in table scope
3968                     td => 1, th => 1, tr => 1,                  my $i;
3969                     tbody => 1, tfoot=> 1, thead => 1,                  INSCOPE: {
3970                    }->{$self->{open_elements}->[-1]->[1]}) {                    for (reverse 0..$#{$self->{open_elements}}) {
3971                  !!!back-token; # <?>                      my $node = $self->{open_elements}->[$_];
3972                  $token = {type => 'end tag', tag_name => 'caption'};                      if ($node->[1] eq 'caption') {
3973                  !!!back-token;                        !!!cp ('t155');
3974                  $token = {type => 'end tag',                        $i = $_;
3975                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                        last INSCOPE;
3976                  redo B;                      } elsif ({
3977                }                                table => 1, html => 1,
3978                                 }->{$node->[1]}) {
3979                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                        !!!cp ('t156');
3980                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                        last;
3981                }                      }
3982                      }
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
3983    
3984                $self->{insertion_mode} = 'in table';                    !!!cp ('t157');
3985                      !!!parse-error (type => 'start tag not allowed',
3986                                      value => $token->{tag_name});
3987                      ## Ignore the token
3988                      !!!next-token;
3989                      redo B;
3990                    } # INSCOPE
3991                    
3992                    ## generate implied end tags
3993                    while ({
3994                            dd => 1, dt => 1, li => 1, p => 1,
3995                           }->{$self->{open_elements}->[-1]->[1]}) {
3996                      !!!cp ('t158');
3997                      pop @{$self->{open_elements}};
3998                    }
3999    
4000                ## reprocess                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
4001                redo B;                    !!!cp ('t159');
4002                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4003                    } else {
4004                      !!!cp ('t160');
4005                    }
4006                    
4007                    splice @{$self->{open_elements}}, $i;
4008                    
4009                    $clear_up_to_marker->();
4010                    
4011                    $self->{insertion_mode} = IN_TABLE_IM;
4012                    
4013                    ## reprocess
4014                    redo B;
4015                  } else {
4016                    !!!cp ('t161');
4017                    #
4018                  }
4019              } else {              } else {
4020                  !!!cp ('t162');
4021                #                #
4022              }              }
4023            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
4024              if ($token->{tag_name} eq 'caption') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
4025                ## have a table element in table scope                if ($self->{insertion_mode} == IN_CELL_IM) {
4026                my $i;                  ## have an element in table scope
4027                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  my $i;
4028                  my $node = $self->{open_elements}->[$_];                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4029                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
4030                    $i = $_;                    if ($node->[1] eq $token->{tag_name}) {
4031                    last INSCOPE;                      !!!cp ('t163');
4032                  } elsif ({                      $i = $_;
4033                            table => 1, html => 1,                      last INSCOPE;
4034                           }->{$node->[1]}) {                    } elsif ({
4035                    last INSCOPE;                              table => 1, html => 1,
4036                               }->{$node->[1]}) {
4037                        !!!cp ('t164');
4038                        last INSCOPE;
4039                      }
4040                    } # INSCOPE
4041                      unless (defined $i) {
4042                        !!!cp ('t165');
4043                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4044                        ## Ignore the token
4045                        !!!next-token;
4046                        redo B;
4047                      }
4048                    
4049                    ## generate implied end tags
4050                    while ({
4051                            dd => 1, dt => 1, li => 1, p => 1,
4052                           }->{$self->{open_elements}->[-1]->[1]}) {
4053                      !!!cp ('t166');
4054                      pop @{$self->{open_elements}};
4055                  }                  }
4056                } # INSCOPE  
4057                unless (defined $i) {                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
4058                      !!!cp ('t167');
4059                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4060                    } else {
4061                      !!!cp ('t168');
4062                    }
4063                    
4064                    splice @{$self->{open_elements}}, $i;
4065                    
4066                    $clear_up_to_marker->();
4067                    
4068                    $self->{insertion_mode} = IN_ROW_IM;
4069                    
4070                    !!!next-token;
4071                    redo B;
4072                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4073                    !!!cp ('t169');
4074                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4075                  ## Ignore the token                  ## Ignore the token
4076                  !!!next-token;                  !!!next-token;
4077                  redo B;                  redo B;
4078                  } else {
4079                    !!!cp ('t170');
4080                    #
4081                }                }
4082                              } elsif ($token->{tag_name} eq 'caption') {
4083                ## generate implied end tags                if ($self->{insertion_mode} == IN_CAPTION_IM) {
4084                if ({                  ## have a table element in table scope
4085                     dd => 1, dt => 1, li => 1, p => 1,                  my $i;
4086                     td => 1, th => 1, tr => 1,                  INSCOPE: {
4087                     tbody => 1, tfoot=> 1, thead => 1,                    for (reverse 0..$#{$self->{open_elements}}) {
4088                    }->{$self->{open_elements}->[-1]->[1]}) {                      my $node = $self->{open_elements}->[$_];
4089                  !!!back-token;                      if ($node->[1] eq $token->{tag_name}) {
4090                  $token = {type => 'end tag',                        !!!cp ('t171');
4091                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                        $i = $_;
4092                  redo B;                        last INSCOPE;
4093                }                      } elsif ({
4094                                  table => 1, html => 1,
4095                                 }->{$node->[1]}) {
4096                          !!!cp ('t172');
4097                          last;
4098                        }
4099                      }
4100    
4101                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                    !!!cp ('t173');
4102                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'unmatched end tag',
4103                                      value => $token->{tag_name});
4104                      ## Ignore the token
4105                      !!!next-token;
4106                      redo B;
4107                    } # INSCOPE
4108                    
4109                    ## generate implied end tags
4110                    while ({
4111                            dd => 1, dt => 1, li => 1, p => 1,
4112                           }->{$self->{open_elements}->[-1]->[1]}) {
4113                      !!!cp ('t174');
4114                      pop @{$self->{open_elements}};
4115                    }
4116                    
4117                    if ($self->{open_elements}->[-1]->[1] ne 'caption') {
4118                      !!!cp ('t175');
4119                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4120                    } else {
4121                      !!!cp ('t176');
4122                    }
4123                    
4124                    splice @{$self->{open_elements}}, $i;
4125                    
4126                    $clear_up_to_marker->();
4127                    
4128                    $self->{insertion_mode} = IN_TABLE_IM;
4129                    
4130                    !!!next-token;
4131                    redo B;
4132                  } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4133                    !!!cp ('t177');
4134                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4135                    ## Ignore the token
4136                    !!!next-token;
4137                    redo B;
4138                  } else {
4139                    !!!cp ('t178');
4140                    #
4141                }                }
4142                } elsif ({
4143                          table => 1, tbody => 1, tfoot => 1,
4144                          thead => 1, tr => 1,
4145                         }->{$token->{tag_name}} and
4146                         $self->{insertion_mode} == IN_CELL_IM) {
4147                  ## have an element in table scope
4148                  my $i;
4149                  my $tn;
4150                  INSCOPE: {
4151                    for (reverse 0..$#{$self->{open_elements}}) {
4152                      my $node = $self->{open_elements}->[$_];
4153                      if ($node->[1] eq $token->{tag_name}) {
4154                        !!!cp ('t179');
4155                        $i = $_;
4156    
4157                        ## Close the cell
4158                        !!!back-token; # </?>
4159                        $token = {type => END_TAG_TOKEN, tag_name => $tn};
4160                        redo B;
4161                      } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {
4162                        !!!cp ('t180');
4163                        $tn = $node->[1];
4164                        ## NOTE: There is exactly one |td| or |th| element
4165                        ## in scope in the stack of open elements by definition.
4166                      } elsif ({
4167                                table => 1, html => 1,
4168                               }->{$node->[1]}) {
4169                        ## ISSUE: Can this be reached?
4170                        !!!cp ('t181');
4171                        last;
4172                      }
4173                    }
4174    
4175                splice @{$self->{open_elements}}, $i;                  !!!cp ('t182');
4176                    !!!parse-error (type => 'unmatched end tag',
4177                $clear_up_to_marker->();                      value => $token->{tag_name});
4178                    ## Ignore the token
4179                $self->{insertion_mode} = 'in table';                  !!!next-token;
4180                    redo B;
4181                !!!next-token;                } # INSCOPE
4182                redo B;              } elsif ($token->{tag_name} eq 'table' and
4183              } elsif ($token->{tag_name} eq 'table') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4184                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption');
4185    
4186                ## As if </caption>                ## As if </caption>
# Line 3987  sub _tree_construction_main ($) { Line 4189  sub _tree_construction_main ($) {
4189                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4190                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4191                  if ($node->[1] eq 'caption') {                  if ($node->[1] eq 'caption') {
4192                      !!!cp ('t184');
4193                    $i = $_;                    $i = $_;
4194                    last INSCOPE;                    last INSCOPE;
4195                  } elsif ({                  } elsif ({
4196                            table => 1, html => 1,                            table => 1, html => 1,
4197                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4198                      !!!cp ('t185');
4199                    last INSCOPE;                    last INSCOPE;
4200                  }                  }
4201                } # INSCOPE                } # INSCOPE
4202                unless (defined $i) {                unless (defined $i) {
4203                    !!!cp ('t186');
4204                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'unmatched end tag:caption');
4205                  ## Ignore the token                  ## Ignore the token
4206                  !!!next-token;                  !!!next-token;
# Line 4003  sub _tree_construction_main ($) { Line 4208  sub _tree_construction_main ($) {
4208                }                }
4209                                
4210                ## generate implied end tags                ## generate implied end tags
4211                if ({                while ({
4212                     dd => 1, dt => 1, li => 1, p => 1,                        dd => 1, dt => 1, li => 1, p => 1,
4213                     td => 1, th => 1, tr => 1,                       }->{$self->{open_elements}->[-1]->[1]}) {
4214                     tbody => 1, tfoot=> 1, thead => 1,                  !!!cp ('t187');
4215                    }->{$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;  
4216                }                }
4217    
4218                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                if ($self->{open_elements}->[-1]->[1] ne 'caption') {
4219                    !!!cp ('t188');
4220                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4221                  } else {
4222                    !!!cp ('t189');
4223                }                }
4224    
4225                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4226    
4227                $clear_up_to_marker->();                $clear_up_to_marker->();
4228    
4229                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
4230    
4231                ## reprocess                ## reprocess
4232                redo B;                redo B;
4233              } elsif ({              } elsif ({
4234                        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,  
4235                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4236                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
4237                ## Ignore the token                  !!!cp ('t190');
4238                redo B;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
             } 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');  
4239                  ## Ignore the token                  ## Ignore the token
4240                  !!!next-token;                  !!!next-token;
4241                  redo B;                  redo B;
4242                } else {                } else {
4243                  pop @{$self->{open_elements}}; # colgroup                  !!!cp ('t191');
4244                  $self->{insertion_mode} = 'in table';                  #
                 !!!next-token;  
                 redo B;              
4245                }                }
4246              } elsif ($token->{tag_name} eq 'col') {              } elsif ({
4247                !!!parse-error (type => 'unmatched end tag:col');                        tbody => 1, tfoot => 1,
4248                          thead => 1, tr => 1,
4249                         }->{$token->{tag_name}} and
4250                         $self->{insertion_mode} == IN_CAPTION_IM) {
4251                  !!!cp ('t192');
4252                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4253                ## Ignore the token                ## Ignore the token
4254                !!!next-token;                !!!next-token;
4255                redo B;                redo B;
4256              } else {              } else {
4257                #                !!!cp ('t193');
4258                  #
4259              }              }
4260            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4261              #          for my $entry (@{$self->{open_elements}}) {
4262              if (not {
4263                dd => 1, dt => 1, li => 1, p => 1, tbody => 1, td => 1, tfoot => 1,
4264                th => 1, thead => 1, tr => 1, body => 1, html => 1,
4265              }->{$entry->[1]}) {
4266                !!!cp ('t75');
4267                !!!parse-error (type => 'in body:#eof');
4268                last;
4269            }            }
4270            }
4271    
4272            ## As if </colgroup>          ## Stop parsing.
4273            if ($self->{open_elements}->[-1]->[1] eq 'html') {          last B;
4274              !!!parse-error (type => 'unmatched end tag:colgroup');        } else {
4275              ## Ignore the token          die "$0: $token->{type}: Unknown token type";
4276          }
4277    
4278          $insert = $insert_to_current;
4279          #
4280        } elsif ($self->{insertion_mode} & TABLE_IMS) {
4281          if ($token->{type} == CHARACTER_TOKEN) {
4282            if (not $open_tables->[-1]->[1] and # tainted
4283                $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4284              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4285                  
4286              unless (length $token->{data}) {
4287                !!!cp ('t194');
4288              !!!next-token;              !!!next-token;
4289              redo B;              redo B;
4290            } else {            } else {
4291              pop @{$self->{open_elements}}; # colgroup              !!!cp ('t195');
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
4292            }            }
4293          } 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;  
               }  
             }  
4294    
4295              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character');
4296    
# Line 4121  sub _tree_construction_main ($) { Line 4299  sub _tree_construction_main ($) {
4299              ## into the current node" while characters might not be              ## into the current node" while characters might not be
4300              ## result in a new Text node.              ## result in a new Text node.
4301              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
4302                
4303              if ({              if ({
4304                   table => 1, tbody => 1, tfoot => 1,                   table => 1, tbody => 1, tfoot => 1,
4305                   thead => 1, tr => 1,                   thead => 1, tr => 1,
# Line 4134  sub _tree_construction_main ($) { Line 4312  sub _tree_construction_main ($) {
4312                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] eq 'table') {
4313                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4314                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
4315                        !!!cp ('t196');
4316                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
4317                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
4318                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
4319                    } else {                    } else {
4320                        !!!cp ('t197');
4321                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
4322                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
4323                    }                    }
# Line 4149  sub _tree_construction_main ($) { Line 4329  sub _tree_construction_main ($) {
4329                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
4330                if (defined $prev_sibling and                if (defined $prev_sibling and
4331                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
4332                    !!!cp ('t198');
4333                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
4334                } else {                } else {
4335                    !!!cp ('t199');
4336                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
4337                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
4338                     $next_sibling);                     $next_sibling);
4339                }                }
4340              } else {            $open_tables->[-1]->[1] = 1; # tainted
4341                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
4342              }            !!!cp ('t200');
4343              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4344            }
4345                            
4346              !!!next-token;          !!!next-token;
4347              redo B;          redo B;
4348            } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4349              if ({              if ({
4350                   tr => 1,                   tr => ($self->{insertion_mode} != IN_ROW_IM),
4351                   th => 1, td => 1,                   th => 1, td => 1,
4352                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4353                unless ($token->{tag_name} eq 'tr') {                if ($self->{insertion_mode} == IN_TABLE_IM) {
4354                  !!!parse-error (type => 'missing start tag:tr');                  ## Clear back to table context
4355                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
4356                           $self->{open_elements}->[-1]->[1] ne 'html') {
4357                      !!!cp ('t201');
4358                      pop @{$self->{open_elements}};
4359                    }
4360                    
4361                    !!!insert-element ('tbody');
4362                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
4363                    ## reprocess in the "in table body" insertion mode...
4364                }                }
4365    
4366                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4367                    unless ($token->{tag_name} eq 'tr') {
4368                      !!!cp ('t202');
4369                      !!!parse-error (type => 'missing start tag:tr');
4370                    }
4371                    
4372                    ## Clear back to table body context
4373                    while (not {
4374                      tbody => 1, tfoot => 1, thead => 1, html => 1,
4375                    }->{$self->{open_elements}->[-1]->[1]}) {
4376                      !!!cp ('t203');
4377                      ## ISSUE: Can this case be reached?
4378                      pop @{$self->{open_elements}};
4379                    }
4380                    
4381                    $self->{insertion_mode} = IN_ROW_IM;
4382                    if ($token->{tag_name} eq 'tr') {
4383                      !!!cp ('t204');
4384                      !!!insert-element ($token->{tag_name}, $token->{attributes});
4385                      !!!next-token;
4386                      redo B;
4387                    } else {
4388                      !!!cp ('t205');
4389                      !!!insert-element ('tr');
4390                      ## reprocess in the "in row" insertion mode
4391                    }
4392                  } else {
4393                    !!!cp ('t206');
4394                  }
4395    
4396                  ## Clear back to table row context
4397                while (not {                while (not {
4398                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tr => 1, html => 1,
4399                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4400                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t207');
4401                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4402                }                }
4403                                
4404                $self->{insertion_mode} = 'in row';                !!!insert-element ($token->{tag_name}, $token->{attributes});
4405                if ($token->{tag_name} eq 'tr') {                $self->{insertion_mode} = IN_CELL_IM;
4406                  !!!insert-element ($token->{tag_name}, $token->{attributes});  
4407                  !!!next-token;                push @$active_formatting_elements, ['#marker', ''];
4408                } else {                
4409                  !!!insert-element ('tr');                !!!next-token;
                 ## reprocess  
               }  
4410                redo B;                redo B;
4411              } elsif ({              } elsif ({
4412                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
4413                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
4414                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
4415                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4416                ## have an element in table scope                if ($self->{insertion_mode} == IN_ROW_IM) {
4417                my $i;                  ## As if </tr>
4418                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
4419                  my $node = $self->{open_elements}->[$_];                  my $i;
4420                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4421                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
4422                      }->{$node->[1]}) {                    if ($node->[1] eq 'tr') {
4423                    $i = $_;                      !!!cp ('t208');
4424                    last INSCOPE;                      $i = $_;
4425                  } elsif ({                      last INSCOPE;
4426                            table => 1, html => 1,                    } elsif ({
4427                           }->{$node->[1]}) {                              html => 1,
4428                    last INSCOPE;  
4429                                ## NOTE: This element does not appear here, maybe.
4430                                table => 1,
4431                               }->{$node->[1]}) {
4432                        !!!cp ('t209');
4433                        last INSCOPE;
4434                      }
4435                    } # INSCOPE
4436                    unless (defined $i) {
4437                     !!!cp ('t210');
4438    ## TODO: This type is wrong.
4439                     !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});
4440                      ## Ignore the token
4441                      !!!next-token;
4442                      redo B;
4443                    }
4444                    
4445                    ## Clear back to table row context
4446                    while (not {
4447                      tr => 1, html => 1,
4448                    }->{$self->{open_elements}->[-1]->[1]}) {
4449                      !!!cp ('t211');
4450                      ## ISSUE: Can this case be reached?
4451                      pop @{$self->{open_elements}};
4452                    }
4453                    
4454                    pop @{$self->{open_elements}}; # tr
4455                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
4456                    if ($token->{tag_name} eq 'tr') {
4457                      !!!cp ('t212');
4458                      ## reprocess
4459                      redo B;
4460                    } else {
4461                      !!!cp ('t213');
4462                      ## reprocess in the "in table body" insertion mode...
4463                  }                  }
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
4464                }                }
4465    
4466                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4467                while (not {                  ## have an element in table scope
4468                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  my $i;
4469                }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4470                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    my $node = $self->{open_elements}->[$_];
4471                      if ({
4472                           tbody => 1, thead => 1, tfoot => 1,
4473                          }->{$node->[1]}) {
4474                        !!!cp ('t214');
4475                        $i = $_;
4476                        last INSCOPE;
4477                      } elsif ({
4478                                table => 1, html => 1,
4479                               }->{$node->[1]}) {
4480                        !!!cp ('t215');
4481                        last INSCOPE;
4482                      }
4483                    } # INSCOPE
4484                    unless (defined $i) {
4485                      !!!cp ('t216');
4486    ## TODO: This erorr type ios wrong.
4487                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4488                      ## Ignore the token
4489                      !!!next-token;
4490                      redo B;
4491                    }
4492    
4493                    ## Clear back to table body context
4494                    while (not {
4495                      tbody => 1, tfoot => 1, thead => 1, html => 1,
4496                    }->{$self->{open_elements}->[-1]->[1]}) {
4497                      !!!cp ('t217');
4498                      ## ISSUE: Can this state be reached?
4499                      pop @{$self->{open_elements}};
4500                    }
4501                    
4502                    ## As if <{current node}>
4503                    ## have an element in table scope
4504                    ## true by definition
4505                    
4506                    ## Clear back to table body context
4507                    ## nop by definition
4508                    
4509                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4510                    $self->{insertion_mode} = IN_TABLE_IM;
4511                    ## reprocess in "in table" insertion mode...
4512                  } else {
4513                    !!!cp ('t218');
4514                }                }
4515    
4516                ## As if <{current node}>                if ($token->{tag_name} eq 'col') {
4517                ## have an element in table scope                  ## Clear back to table context
4518                ## true by definition                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
4519                           $self->{open_elements}->[-1]->[1] ne 'html') {
4520                ## Clear back to table body context                    !!!cp ('t219');
4521                ## nop by definition                    ## ISSUE: Can this state be reached?
4522                      pop @{$self->{open_elements}};
4523                pop @{$self->{open_elements}};                  }
4524                $self->{insertion_mode} = 'in table';                  
4525                ## reprocess                  !!!insert-element ('colgroup');
4526                redo B;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
4527                    ## reprocess
4528                    redo B;
4529                  } elsif ({
4530                            caption => 1,
4531                            colgroup => 1,
4532                            tbody => 1, tfoot => 1, thead => 1,
4533                           }->{$token->{tag_name}}) {
4534                    ## Clear back to table context
4535                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
4536                           $self->{open_elements}->[-1]->[1] ne 'html') {
4537                      !!!cp ('t220');
4538                      ## ISSUE: Can this state be reached?
4539                      pop @{$self->{open_elements}};
4540                    }
4541                    
4542                    push @$active_formatting_elements, ['#marker', '']
4543                        if $token->{tag_name} eq 'caption';
4544                    
4545                    !!!insert-element ($token->{tag_name}, $token->{attributes});
4546                    $self->{insertion_mode} = {
4547                                               caption => IN_CAPTION_IM,
4548                                               colgroup => IN_COLUMN_GROUP_IM,
4549                                               tbody => IN_TABLE_BODY_IM,
4550                                               tfoot => IN_TABLE_BODY_IM,
4551                                               thead => IN_TABLE_BODY_IM,
4552                                              }->{$token->{tag_name}};
4553                    !!!next-token;
4554                    redo B;
4555                  } else {
4556                    die "$0: in table: <>: $token->{tag_name}";
4557                  }
4558              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4559                ## NOTE: This is a code clone of "table in table"                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
               !!!parse-error (type => 'not closed:table');  
4560    
4561                ## As if </table>                ## As if </table>
4562                ## have a table element in table scope                ## have a table element in table scope
# Line 4242  sub _tree_construction_main ($) { Line 4564  sub _tree_construction_main ($) {
4564                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4565                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4566                  if ($node->[1] eq 'table') {                  if ($node->[1] eq 'table') {
4567                      !!!cp ('t221');
4568                    $i = $_;                    $i = $_;
4569                    last INSCOPE;                    last INSCOPE;
4570                  } elsif ({                  } elsif ({
4571                            table => 1, html => 1,                            #table => 1,
4572                              html => 1,
4573                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4574                      !!!cp ('t222');
4575                    last INSCOPE;                    last INSCOPE;
4576                  }                  }
4577                } # INSCOPE                } # INSCOPE
4578                unless (defined $i) {                unless (defined $i) {
4579                    !!!cp ('t223');
4580    ## TODO: The following is wrong, maybe.
4581                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag:table');
4582                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
4583                  !!!next-token;                  !!!next-token;
4584                  redo B;                  redo B;
4585                }                }
4586                                
4587    ## TODO: Followings are removed from the latest spec.
4588                ## generate implied end tags                ## generate implied end tags
4589                if ({                while ({
4590                     dd => 1, dt => 1, li => 1, p => 1,                        dd => 1, dt => 1, li => 1, p => 1,
4591                     td => 1, th => 1, tr => 1,                       }->{$self->{open_elements}->[-1]->[1]}) {
4592                     tbody => 1, tfoot=> 1, thead => 1,                  !!!cp ('t224');
4593                    }->{$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;  
4594                }                }
4595    
4596                if ($self->{open_elements}->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
4597                    !!!cp ('t225');
4598    ## ISSUE: Can this case be reached?
4599                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4600                  } else {
4601                    !!!cp ('t226');
4602                }                }
4603    
4604                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4605                  pop @{$open_tables};
4606    
4607                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
4608    
4609                ## reprocess                ## reprocess
4610                redo B;                redo B;
4611              } else {          } elsif ($token->{tag_name} eq 'style') {
4612                #            if (not $open_tables->[-1]->[1]) { # tainted
4613              }              !!!cp ('t227.8');
4614            } elsif ($token->{type} eq 'end tag') {              ## NOTE: This is a "as if in head" code clone.
4615              if ({              $parse_rcdata->(CDATA_CONTENT_MODEL);
4616                   tbody => 1, tfoot => 1, thead => 1,              redo B;
4617                  }->{$token->{tag_name}}) {            } else {
4618                ## have an element in table scope              !!!cp ('t227.7');
4619                my $i;              #
4620                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            }
4621                  my $node = $self->{open_elements}->[$_];          } elsif ($token->{tag_name} eq 'script') {
4622                  if ($node->[1] eq $token->{tag_name}) {            if (not $open_tables->[-1]->[1]) { # tainted
4623                    $i = $_;              !!!cp ('t227.6');
4624                    last INSCOPE;              ## NOTE: This is a "as if in head" code clone.
4625                  } elsif ({              $script_start_tag->();
4626                            table => 1, html => 1,              redo B;
4627                           }->{$node->[1]}) {            } else {
4628                    last INSCOPE;              !!!cp ('t227.5');
4629                  }              #
4630                } # INSCOPE            }
4631                unless (defined $i) {          } elsif ($token->{tag_name} eq 'input') {
4632                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            if (not $open_tables->[-1]->[1]) { # tainted
4633                  ## Ignore the token              if ($token->{attributes}->{type}) { ## TODO: case
4634                  !!!next-token;                my $type = lc $token->{attributes}->{type}->{value};
4635                  redo B;                if ($type eq 'hidden') {
4636                }                  !!!cp ('t227.3');
4637                    !!!parse-error (type => 'in table:'.$token->{tag_name});
4638    
4639                    !!!insert-element ($token->{tag_name}, $token->{attributes});
4640    
4641                    ## TODO: form element pointer
4642    
               ## 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]);  
4643                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
               }  
4644    
               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  
4645                  !!!next-token;                  !!!next-token;
4646                  redo B;                  redo B;
4647                  } else {
4648                    !!!cp ('t227.2');
4649                    #
4650                }                }
   
               ## 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;  
4651              } else {              } else {
4652                  !!!cp ('t227.1');
4653                #                #
4654              }              }
4655            } else {            } else {
4656                !!!cp ('t227.4');
4657              #              #
4658            }            }
4659                      } else {
4660            ## As if in table            !!!cp ('t227');
4661            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
4662            $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;  
               }  
             }  
4663    
4664              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table:'.$token->{tag_name});
4665    
4666              ## As if in body, but insert into foster parent element          $insert = $insert_to_foster;
4667              ## ISSUE: Spec says that "whenever a node would be inserted          #
4668              ## into the current node" while characters might not be        } elsif ($token->{type} == END_TAG_TOKEN) {
4669              ## result in a new Text node.              if ($token->{tag_name} eq 'tr' and
4670              $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>  
4671                ## have an element in table scope                ## have an element in table scope
4672                my $i;                my $i;
4673                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4674                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4675                  if ($node->[1] eq 'tr') {                  if ($node->[1] eq $token->{tag_name}) {
4676                      !!!cp ('t228');
4677                    $i = $_;                    $i = $_;
4678                    last INSCOPE;                    last INSCOPE;
4679                  } elsif ({                  } elsif ({
4680                            table => 1, html => 1,                            table => 1, html => 1,
4681                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4682                      !!!cp ('t229');
4683                    last INSCOPE;                    last INSCOPE;
4684                  }                  }
4685                } # INSCOPE                } # INSCOPE
4686                unless (defined $i) {                unless (defined $i) {
4687                  !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                  !!!cp ('t230');
4688                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4689                  ## Ignore the token                  ## Ignore the token
4690                  !!!next-token;                  !!!next-token;
4691                  redo B;                  redo B;
4692                  } else {
4693                    !!!cp ('t232');
4694                }                }
4695    
4696                ## Clear back to table row context                ## Clear back to table row context
4697                while (not {                while (not {
4698                  tr => 1, html => 1,                  tr => 1, html => 1,
4699                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4700                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t231');
4701    ## ISSUE: Can this state be reached?
4702                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4703                }                }
4704    
4705                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
4706                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
4707                ## reprocess                !!!next-token;
4708                redo B;                redo B;
4709              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4710                ## NOTE: This is a code clone of "table in table"                if ($self->{insertion_mode} == IN_ROW_IM) {
4711                !!!parse-error (type => 'not closed:table');                  ## As if </tr>
4712                    ## have an element in table scope
4713                ## As if </table>                  my $i;
4714                ## have a table element in table scope                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4715                my $i;                    my $node = $self->{open_elements}->[$_];
4716                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($node->[1] eq 'tr') {
4717                  my $node = $self->{open_elements}->[$_];                      !!!cp ('t233');
4718                  if ($node->[1] eq 'table') {                      $i = $_;
4719                    $i = $_;                      last INSCOPE;
4720                    last INSCOPE;                    } elsif ({
4721                  } elsif ({                              table => 1, html => 1,
4722                            table => 1, html => 1,                             }->{$node->[1]}) {
4723                           }->{$node->[1]}) {                      !!!cp ('t234');
4724                    last INSCOPE;                      last INSCOPE;
4725                      }
4726                    } # INSCOPE
4727                    unless (defined $i) {
4728                      !!!cp ('t235');
4729    ## TODO: The following is wrong.
4730                      !!!parse-error (type => 'unmatched end tag:'.$token->{type});
4731                      ## Ignore the token
4732                      !!!next-token;
4733                      redo B;
4734                  }                  }
4735                } # INSCOPE                  
4736                unless (defined $i) {                  ## Clear back to table row context
4737                  !!!parse-error (type => 'unmatched end tag:table');                  while (not {
4738                  ## Ignore tokens </table><table>                    tr => 1, html => 1,
4739                  !!!next-token;                  }->{$self->{open_elements}->[-1]->[1]}) {
4740                  redo B;                    !!!cp ('t236');
4741                }  ## ISSUE: Can this state be reached?
4742                                    pop @{$self->{open_elements}};
4743                ## generate implied end tags                  }
4744                if ({                  
4745                     dd => 1, dt => 1, li => 1, p => 1,                  pop @{$self->{open_elements}}; # tr
4746                     td => 1, th => 1, tr => 1,                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4747                     tbody => 1, tfoot=> 1, thead => 1,                  ## reprocess in the "in table body" insertion mode...
4748                    }->{$self->{open_elements}->[-1]->[1]}) {                }
4749                  !!!back-token; # <table>  
4750                  $token = {type => 'end tag', tag_name => 'table'};                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4751                  !!!back-token;                  ## have an element in table scope
4752                  $token = {type => 'end tag',                  my $i;
4753                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4754                  redo B;                    my $node = $self->{open_elements}->[$_];
4755                }                    if ({
4756                           tbody => 1, thead => 1, tfoot => 1,
4757                if ($self->{open_elements}->[-1]->[1] ne 'table') {                        }->{$node->[1]}) {
4758                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                      !!!cp ('t237');
4759                        $i = $_;
4760                        last INSCOPE;
4761                      } elsif ({
4762                                table => 1, html => 1,
4763                               }->{$node->[1]}) {
4764                        !!!cp ('t238');
4765                        last INSCOPE;
4766                      }
4767                    } # INSCOPE
4768                    unless (defined $i) {
4769                      !!!cp ('t239');
4770                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4771                      ## Ignore the token
4772                      !!!next-token;
4773                      redo B;
4774                    }
4775                    
4776                    ## Clear back to table body context
4777                    while (not {
4778                      tbody => 1, tfoot => 1, thead => 1, html => 1,
4779                    }->{$self->{open_elements}->[-1]->[1]}) {
4780                      !!!cp ('t240');
4781                      pop @{$self->{open_elements}};
4782                    }
4783                    
4784                    ## As if <{current node}>
4785                    ## have an element in table scope
4786                    ## true by definition
4787                    
4788                    ## Clear back to table body context
4789                    ## nop by definition
4790                    
4791                    pop @{$self->{open_elements}};
4792                    $self->{insertion_mode} = IN_TABLE_IM;
4793                    ## reprocess in the "in table" insertion mode...
4794                }                }
4795    
4796                splice @{$self->{open_elements}}, $i;                ## NOTE: </table> in the "in table" insertion mode.
4797                  ## When you edit the code fragment below, please ensure that
4798                  ## the code for <table> in the "in table" insertion mode
4799                  ## is synced with it.
4800    
4801                $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  
4802                my $i;                my $i;
4803                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4804                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4805                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4806                      !!!cp ('t241');
4807                    $i = $_;                    $i = $_;
4808                    last INSCOPE;                    last INSCOPE;
4809                  } elsif ({                  } elsif ({
4810                            table => 1, html => 1,                            table => 1, html => 1,
4811                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4812                      !!!cp ('t242');
4813                    last INSCOPE;                    last INSCOPE;
4814                  }                  }
4815                } # INSCOPE                } # INSCOPE
4816                unless (defined $i) {                unless (defined $i) {
4817                    !!!cp ('t243');
4818                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4819                  ## Ignore the token                  ## Ignore the token
4820                  !!!next-token;                  !!!next-token;
4821                  redo B;                  redo B;
4822                }                }
4823                    
4824                ## Clear back to table row context                splice @{$self->{open_elements}}, $i;
4825                while (not {                pop @{$open_tables};
4826                  tr => 1, html => 1,                
4827                }->{$self->{open_elements}->[-1]->[1]}) {                $self->_reset_insertion_mode;
4828                  !!!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';  
4829                !!!next-token;                !!!next-token;
4830                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;  
4831              } elsif ({              } elsif ({
4832                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
4833                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}} and
4834                ## have an element in table scope                       $self->{insertion_mode} & ROW_IMS) {
4835                my $i;                if ($self->{insertion_mode} == IN_ROW_IM) {
4836                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
4837                  my $node = $self->{open_elements}->[$_];                  my $i;
4838                  if ($node->[1] eq $token->{tag_name}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4839                    $i = $_;                    my $node = $self->{open_elements}->[$_];
4840                    last INSCOPE;                    if ($node->[1] eq $token->{tag_name}) {
4841                  } elsif ({                      !!!cp ('t247');
4842                            table => 1, html => 1,                      $i = $_;
4843                           }->{$node->[1]}) {                      last INSCOPE;
4844                    last INSCOPE;                    } elsif ({
4845                                table => 1, html => 1,
4846                               }->{$node->[1]}) {
4847                        !!!cp ('t248');
4848                        last INSCOPE;
4849                      }
4850                    } # INSCOPE
4851                      unless (defined $i) {
4852                        !!!cp ('t249');
4853                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4854                        ## Ignore the token
4855                        !!!next-token;
4856                        redo B;
4857                      }
4858                    
4859                    ## As if </tr>
4860                    ## have an element in table scope
4861                    my $i;
4862                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4863                      my $node = $self->{open_elements}->[$_];
4864                      if ($node->[1] eq 'tr') {
4865                        !!!cp ('t250');
4866                        $i = $_;
4867                        last INSCOPE;
4868                      } elsif ({
4869                                table => 1, html => 1,
4870                               }->{$node->[1]}) {
4871                        !!!cp ('t251');
4872                        last INSCOPE;
4873                      }
4874                    } # INSCOPE
4875                      unless (defined $i) {
4876                        !!!cp ('t252');
4877                        !!!parse-error (type => 'unmatched end tag:tr');
4878                        ## Ignore the token
4879                        !!!next-token;
4880                        redo B;
4881                      }
4882                    
4883                    ## Clear back to table row context
4884                    while (not {
4885                      tr => 1, html => 1,
4886                    }->{$self->{open_elements}->[-1]->[1]}) {
4887                      !!!cp ('t253');
4888    ## ISSUE: Can this case be reached?
4889                      pop @{$self->{open_elements}};
4890                  }                  }
4891                } # INSCOPE                  
4892                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
4893                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4894                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
4895                }                }
4896    
               ## As if </tr>  
4897                ## have an element in table scope                ## have an element in table scope
4898                my $i;                my $i;
4899                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4900                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4901                  if ($node->[1] eq 'tr') {                  if ($node->[1] eq $token->{tag_name}) {
4902                      !!!cp ('t254');
4903                    $i = $_;                    $i = $_;
4904                    last INSCOPE;                    last INSCOPE;
4905                  } elsif ({                  } elsif ({
4906                            table => 1, html => 1,                            table => 1, html => 1,
4907                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4908                      !!!cp ('t255');
4909                    last INSCOPE;                    last INSCOPE;
4910                  }                  }
4911                } # INSCOPE                } # INSCOPE
4912                unless (defined $i) {                unless (defined $i) {
4913                  !!!parse-error (type => 'unmatched end tag:tr');                  !!!cp ('t256');
4914                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4915                  ## Ignore the token                  ## Ignore the token
4916                  !!!next-token;                  !!!next-token;
4917                  redo B;                  redo B;
4918                }                }
4919    
4920                ## Clear back to table row context                ## Clear back to table body context
4921                while (not {                while (not {
4922                  tr => 1, html => 1,                  tbody => 1, tfoot => 1, thead => 1, html => 1,
4923                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4924                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t257');
4925    ## ISSUE: Can this case be reached?
4926                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4927                }                }
4928    
4929                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}};
4930                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_IM;
4931                ## reprocess                !!!next-token;
4932                redo B;                redo B;
4933              } elsif ({              } elsif ({
4934                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
4935                        colgroup => 1, html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
4936                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
4937                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
4938                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4939                  !!!cp ('t258');
4940                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4941                ## Ignore the token                ## Ignore the token
4942                !!!next-token;                !!!next-token;
4943                redo B;                redo B;
4944              } else {          } else {
4945                #            !!!cp ('t259');
4946              }            !!!parse-error (type => 'in table:/'.$token->{tag_name});
           } else {  
             #  
           }  
4947    
4948            ## As if in table            $insert = $insert_to_foster;
4949            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
4950            $in_body->($insert_to_foster);          }
4951            redo B;        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4952          } elsif ($self->{insertion_mode} eq 'in cell') {          unless ($self->{open_elements}->[-1]->[1] eq 'html' and
4953            if ($token->{type} eq 'character') {                  @{$self->{open_elements}} == 1) { # redundant, maybe
4954              ## NOTE: This is a code clone of "character in body".            !!!parse-error (type => 'in body:#eof');
4955              $reconstruct_active_formatting_elements->($insert_to_current);            !!!cp ('t259.1');
4956                          #
4957              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
4958              !!!cp ('t259.2');
4959              #
4960            }
4961    
4962              !!!next-token;          ## Stop parsing
4963              redo B;          last B;
4964            } elsif ($token->{type} eq 'start tag') {        } else {
4965              if ({          die "$0: $token->{type}: Unknown token type";
4966                   caption => 1, col => 1, colgroup => 1,        }
4967                   tbody => 1, td => 1, tfoot => 1, th => 1,      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
4968                   thead => 1, tr => 1,            if ($token->{type} == CHARACTER_TOKEN) {
4969                  }->{$token->{tag_name}}) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4970                ## have an element in table scope                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4971                my $tn;                unless (length $token->{data}) {
4972                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  
4973                  !!!next-token;                  !!!next-token;
4974                  redo B;                  redo B;
4975                }                }
4976                }
4977                ## Close the cell              
4978                !!!back-token; # <?>              !!!cp ('t261');
4979                $token = {type => 'end tag', tag_name => $tn};              #
4980              } elsif ($token->{type} == START_TAG_TOKEN) {
4981                if ($token->{tag_name} eq 'col') {
4982                  !!!cp ('t262');
4983                  !!!insert-element ($token->{tag_name}, $token->{attributes});
4984                  pop @{$self->{open_elements}};
4985                  !!!next-token;
4986                redo B;                redo B;
4987              } else {              } else {
4988                  !!!cp ('t263');
4989                #                #
4990              }              }
4991            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
4992              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'colgroup') {
4993                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] eq 'html') {
4994                my $i;                  !!!cp ('t264');
4995                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  !!!parse-error (type => 'unmatched end tag:colgroup');
                 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});  
4996                  ## Ignore the token                  ## Ignore the token
4997                  !!!next-token;                  !!!next-token;
4998                  redo B;                  redo B;
4999                  } else {
5000                    !!!cp ('t265');
5001                    pop @{$self->{open_elements}}; # colgroup
5002                    $self->{insertion_mode} = IN_TABLE_IM;
5003                    !!!next-token;
5004                    redo B;            
5005                }                }
5006                              } elsif ($token->{tag_name} eq 'col') {
5007                ## generate implied end tags                !!!cp ('t266');
5008                if ({                !!!parse-error (type => 'unmatched end tag:col');
                    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});  
5009                ## Ignore the token                ## Ignore the token
5010                !!!next-token;                !!!next-token;
5011                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;  
5012              } else {              } else {
5013                #                !!!cp ('t267');
5014                  #
5015              }              }
5016            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5017              #          if ($self->{open_elements}->[-1]->[1] eq 'html' or
5018            }              @{$self->{open_elements}} == 1) { # redundant, maybe
5019                        !!!cp ('t270.2');
5020            $in_body->($insert_to_current);            ## Stop parsing.
5021              last B;
5022            } else {
5023              ## NOTE: As if </colgroup>.
5024              !!!cp ('t270.1');
5025              pop @{$self->{open_elements}}; # colgroup
5026              $self->{insertion_mode} = IN_TABLE_IM;
5027              ## Reprocess.
5028            redo B;            redo B;
5029          } elsif ($self->{insertion_mode} eq 'in select') {          }
5030            if ($token->{type} eq 'character') {        } else {
5031              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          die "$0: $token->{type}: Unknown token type";
5032          }
5033    
5034              ## As if </colgroup>
5035              if ($self->{open_elements}->[-1]->[1] eq 'html') {
5036                !!!cp ('t269');
5037    ## TODO: Wrong error type?
5038                !!!parse-error (type => 'unmatched end tag:colgroup');
5039                ## Ignore the token
5040              !!!next-token;              !!!next-token;
5041              redo B;              redo B;
5042            } elsif ($token->{type} eq 'start tag') {            } else {
5043                !!!cp ('t270');
5044                pop @{$self->{open_elements}}; # colgroup
5045                $self->{insertion_mode} = IN_TABLE_IM;
5046                ## reprocess
5047                redo B;
5048              }
5049        } elsif ($self->{insertion_mode} & SELECT_IMS) {
5050          if ($token->{type} == CHARACTER_TOKEN) {
5051            !!!cp ('t271');
5052            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5053            !!!next-token;
5054            redo B;
5055          } elsif ($token->{type} == START_TAG_TOKEN) {
5056              if ($token->{tag_name} eq 'option') {              if ($token->{tag_name} eq 'option') {
5057                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
5058                    !!!cp ('t272');
5059                  ## As if </option>                  ## As if </option>
5060                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5061                  } else {
5062                    !!!cp ('t273');
5063                }                }
5064    
5065                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
# Line 4847  sub _tree_construction_main ($) { Line 5067  sub _tree_construction_main ($) {
5067                redo B;                redo B;
5068              } elsif ($token->{tag_name} eq 'optgroup') {              } elsif ($token->{tag_name} eq 'optgroup') {
5069                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
5070                    !!!cp ('t274');
5071                  ## As if </option>                  ## As if </option>
5072                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5073                  } else {
5074                    !!!cp ('t275');
5075                }                }
5076    
5077                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
5078                    !!!cp ('t276');
5079                  ## As if </optgroup>                  ## As if </optgroup>
5080                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5081                  } else {
5082                    !!!cp ('t277');
5083                }                }
5084    
5085                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
5086                !!!next-token;                !!!next-token;
5087                redo B;                redo B;
5088              } elsif ($token->{tag_name} eq 'select') {          } elsif ($token->{tag_name} eq 'select' or
5089                !!!parse-error (type => 'not closed:select');                   $token->{tag_name} eq 'input' or
5090                ## As if </select> instead                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5091                      {
5092                       caption => 1, table => 1,
5093                       tbody => 1, tfoot => 1, thead => 1,
5094                       tr => 1, td => 1, th => 1,
5095                      }->{$token->{tag_name}})) {
5096              ## TODO: The type below is not good - <select> is replaced by </select>
5097              !!!parse-error (type => 'not closed:select');
5098              ## NOTE: As if the token were </select> (<select> case) or
5099              ## as if there were </select> (otherwise).
5100                ## have an element in table scope                ## have an element in table scope
5101                my $i;                my $i;
5102                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5103                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5104                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq 'select') {
5105                      !!!cp ('t278');
5106                    $i = $_;                    $i = $_;
5107                    last INSCOPE;                    last INSCOPE;
5108                  } elsif ({                  } elsif ({
5109                            table => 1, html => 1,                            table => 1, html => 1,
5110                           }->{$node->[1]}) {                           }->{$node->[1]}) {
5111                      !!!cp ('t279');
5112                    last INSCOPE;                    last INSCOPE;
5113                  }                  }
5114                } # INSCOPE                } # INSCOPE
5115                unless (defined $i) {                unless (defined $i) {
5116                    !!!cp ('t280');
5117                  !!!parse-error (type => 'unmatched end tag:select');                  !!!parse-error (type => 'unmatched end tag:select');
5118                  ## Ignore the token                  ## Ignore the token
5119                  !!!next-token;                  !!!next-token;
5120                  redo B;                  redo B;
5121                }                }
5122                                
5123                  !!!cp ('t281');
5124                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5125    
5126                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5127    
5128                !!!next-token;            if ($token->{tag_name} eq 'select') {
5129                redo B;              !!!cp ('t281.2');
5130              } else {              !!!next-token;
5131                #              redo B;
5132              }            } else {
5133            } elsif ($token->{type} eq 'end tag') {              !!!cp ('t281.1');
5134                ## Reprocess the token.
5135                redo B;
5136              }
5137            } else {
5138              !!!cp ('t282');
5139              !!!parse-error (type => 'in select:'.$token->{tag_name});
5140              ## Ignore the token
5141              !!!next-token;
5142              redo B;
5143            }
5144          } elsif ($token->{type} == END_TAG_TOKEN) {
5145              if ($token->{tag_name} eq 'optgroup') {              if ($token->{tag_name} eq 'optgroup') {
5146                if ($self->{open_elements}->[-1]->[1] eq 'option' and                if ($self->{open_elements}->[-1]->[1] eq 'option' and
5147                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {
5148                    !!!cp ('t283');
5149                  ## As if </option>                  ## As if </option>
5150                  splice @{$self->{open_elements}}, -2;                  splice @{$self->{open_elements}}, -2;
5151                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
5152                    !!!cp ('t284');
5153                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5154                } else {                } else {
5155                    !!!cp ('t285');
5156                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5157                  ## Ignore the token                  ## Ignore the token
5158                }                }
# Line 4907  sub _tree_construction_main ($) { Line 5160  sub _tree_construction_main ($) {
5160                redo B;                redo B;
5161              } elsif ($token->{tag_name} eq 'option') {              } elsif ($token->{tag_name} eq 'option') {
5162                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
5163                    !!!cp ('t286');
5164                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5165                } else {                } else {
5166                    !!!cp ('t287');
5167                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5168                  ## Ignore the token                  ## Ignore the token
5169                }                }
# Line 4920  sub _tree_construction_main ($) { Line 5175  sub _tree_construction_main ($) {
5175                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5176                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5177                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
5178                      !!!cp ('t288');
5179                    $i = $_;                    $i = $_;
5180                    last INSCOPE;                    last INSCOPE;
5181                  } elsif ({                  } elsif ({
5182                            table => 1, html => 1,                            table => 1, html => 1,
5183                           }->{$node->[1]}) {                           }->{$node->[1]}) {
5184                      !!!cp ('t289');
5185                    last INSCOPE;                    last INSCOPE;
5186                  }                  }
5187                } # INSCOPE                } # INSCOPE
5188                unless (defined $i) {                unless (defined $i) {
5189                    !!!cp ('t290');
5190                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5191                  ## Ignore the token                  ## Ignore the token
5192                  !!!next-token;                  !!!next-token;
5193                  redo B;                  redo B;
5194                }                }
5195                                
5196                  !!!cp ('t291');
5197                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5198    
5199                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5200    
5201                !!!next-token;                !!!next-token;
5202                redo B;                redo B;
5203              } elsif ({          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5204                        caption => 1, table => 1, tbody => 1,                   {
5205                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                    caption => 1, table => 1, tbody => 1,
5206                       }->{$token->{tag_name}}) {                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5207                     }->{$token->{tag_name}}) {
5208    ## TODO: The following is wrong?
5209                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5210                                
5211                ## have an element in table scope                ## have an element in table scope
# Line 4952  sub _tree_construction_main ($) { Line 5213  sub _tree_construction_main ($) {
5213                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5214                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5215                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
5216                      !!!cp ('t292');
5217                    $i = $_;                    $i = $_;
5218                    last INSCOPE;                    last INSCOPE;
5219                  } elsif ({                  } elsif ({
5220                            table => 1, html => 1,                            table => 1, html => 1,
5221                           }->{$node->[1]}) {                           }->{$node->[1]}) {
5222                      !!!cp ('t293');
5223                    last INSCOPE;                    last INSCOPE;
5224                  }                  }
5225                } # INSCOPE                } # INSCOPE
5226                unless (defined $i) {                unless (defined $i) {
5227                    !!!cp ('t294');
5228                  ## Ignore the token                  ## Ignore the token
5229                  !!!next-token;                  !!!next-token;
5230                  redo B;                  redo B;
# Line 4972  sub _tree_construction_main ($) { Line 5236  sub _tree_construction_main ($) {
5236                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5237                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5238                  if ($node->[1] eq 'select') {                  if ($node->[1] eq 'select') {
5239                      !!!cp ('t295');
5240                    $i = $_;                    $i = $_;
5241                    last INSCOPE;                    last INSCOPE;
5242                  } elsif ({                  } elsif ({
5243                            table => 1, html => 1,                            table => 1, html => 1,
5244                           }->{$node->[1]}) {                           }->{$node->[1]}) {
5245    ## ISSUE: Can this state be reached?
5246                      !!!cp ('t296');
5247                    last INSCOPE;                    last INSCOPE;
5248                  }                  }
5249                } # INSCOPE                } # INSCOPE
5250                unless (defined $i) {                unless (defined $i) {
5251                    !!!cp ('t297');
5252    ## TODO: The following error type is correct?
5253                  !!!parse-error (type => 'unmatched end tag:select');                  !!!parse-error (type => 'unmatched end tag:select');
5254                  ## Ignore the </select> token                  ## Ignore the </select> token
5255                  !!!next-token; ## TODO: ok?                  !!!next-token; ## TODO: ok?
5256                  redo B;                  redo B;
5257                }                }
5258                                
5259                  !!!cp ('t298');
5260                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5261    
5262                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5263    
5264                ## reprocess                ## reprocess
5265                redo B;                redo B;
5266              } else {          } else {
5267                #            !!!cp ('t299');
5268              }            !!!parse-error (type => 'in select:/'.$token->{tag_name});
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in select:'.$token->{tag_name});  
5269            ## Ignore the token            ## Ignore the token
5270            !!!next-token;            !!!next-token;
5271            redo B;            redo B;
5272          } elsif ($self->{insertion_mode} eq 'after body') {          }
5273            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5274              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          unless ($self->{open_elements}->[-1]->[1] eq 'html' and
5275                my $data = $1;                  @{$self->{open_elements}} == 1) { # redundant, maybe
5276                ## As if in body            !!!cp ('t299.1');
5277                $reconstruct_active_formatting_elements->($insert_to_current);            !!!parse-error (type => 'in body:#eof');
5278            } else {
5279              !!!cp ('t299.2');
5280            }
5281    
5282            ## Stop parsing.
5283            last B;
5284          } else {
5285            die "$0: $token->{type}: Unknown token type";
5286          }
5287        } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
5288          if ($token->{type} == CHARACTER_TOKEN) {
5289            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5290              my $data = $1;
5291              ## As if in body
5292              $reconstruct_active_formatting_elements->($insert_to_current);
5293                                
5294                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5295              
5296              unless (length $token->{data}) {
5297                !!!cp ('t300');
5298                !!!next-token;
5299                redo B;
5300              }
5301            }
5302            
5303            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5304              !!!cp ('t301');
5305              !!!parse-error (type => 'after html:#character');
5306    
5307                unless (length $token->{data}) {            ## Reprocess in the "after body" insertion mode.
5308                  !!!next-token;          } else {
5309                  redo B;            !!!cp ('t302');
5310                }          }
5311              }          
5312                        ## "after body" insertion mode
5313              #          !!!parse-error (type => 'after body:#character');
5314              !!!parse-error (type => 'after body:#character');  
5315            } elsif ($token->{type} eq 'start tag') {          $self->{insertion_mode} = IN_BODY_IM;
5316              !!!parse-error (type => 'after body:'.$token->{tag_name});          ## reprocess
5317              #          redo B;
5318            } elsif ($token->{type} eq 'end tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
5319              if ($token->{tag_name} eq 'html') {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5320                if (defined $self->{inner_html_node}) {            !!!cp ('t303');
5321                  !!!parse-error (type => 'unmatched end tag:html');            !!!parse-error (type => 'after html:'.$token->{tag_name});
5322                  ## Ignore the token            
5323                  !!!next-token;            ## Reprocess in the "after body" insertion mode.
5324                  redo B;          } else {
5325                } else {            !!!cp ('t304');
5326                  $previous_insertion_mode = $self->{insertion_mode};          }
5327                  $self->{insertion_mode} = 'trailing end';  
5328                  !!!next-token;          ## "after body" insertion mode
5329                  redo B;          !!!parse-error (type => 'after body:'.$token->{tag_name});
5330                }  
5331              } else {          $self->{insertion_mode} = IN_BODY_IM;
5332                !!!parse-error (type => 'after body:/'.$token->{tag_name});          ## reprocess
5333              }          redo B;
5334          } elsif ($token->{type} == END_TAG_TOKEN) {
5335            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5336              !!!cp ('t305');
5337              !!!parse-error (type => 'after html:/'.$token->{tag_name});
5338              
5339              $self->{insertion_mode} = AFTER_BODY_IM;
5340              ## Reprocess in the "after body" insertion mode.
5341            } else {
5342              !!!cp ('t306');
5343            }
5344    
5345            ## "after body" insertion mode
5346            if ($token->{tag_name} eq 'html') {
5347              if (defined $self->{inner_html_node}) {
5348                !!!cp ('t307');
5349                !!!parse-error (type => 'unmatched end tag:html');
5350                ## Ignore the token
5351                !!!next-token;
5352                redo B;
5353            } else {            } else {
5354              die "$0: $token->{type}: Unknown token type";              !!!cp ('t308');
5355                $self->{insertion_mode} = AFTER_HTML_BODY_IM;
5356                !!!next-token;
5357                redo B;
5358            }            }
5359            } else {
5360              !!!cp ('t309');
5361              !!!parse-error (type => 'after body:/'.$token->{tag_name});
5362    
5363            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = IN_BODY_IM;
5364            ## reprocess            ## reprocess
5365            redo B;            redo B;
5366      } elsif ($self->{insertion_mode} eq 'in frameset') {          }
5367        if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5368            !!!cp ('t309.2');
5369            ## Stop parsing
5370            last B;
5371          } else {
5372            die "$0: $token->{type}: Unknown token type";
5373          }
5374        } elsif ($self->{insertion_mode} & FRAME_IMS) {
5375          if ($token->{type} == CHARACTER_TOKEN) {
5376          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5377            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5378              
5379            unless (length $token->{data}) {            unless (length $token->{data}) {
5380                !!!cp ('t310');
5381              !!!next-token;              !!!next-token;
5382              redo B;              redo B;
5383            }            }
5384          }          }
5385            
5386          !!!parse-error (type => 'in frameset:#character');          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
5387          ## Ignore the token            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5388          !!!next-token;              !!!cp ('t311');
5389          redo B;              !!!parse-error (type => 'in frameset:#character');
5390        } elsif ($token->{type} eq 'start tag') {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
5391          if ($token->{tag_name} eq 'frameset') {              !!!cp ('t312');
5392                !!!parse-error (type => 'after frameset:#character');
5393              } else { # "after html frameset"
5394                !!!cp ('t313');
5395                !!!parse-error (type => 'after html:#character');
5396    
5397                $self->{insertion_mode} = AFTER_FRAMESET_IM;
5398                ## Reprocess in the "after frameset" insertion mode.
5399                !!!parse-error (type => 'after frameset:#character');
5400              }
5401              
5402              ## Ignore the token.
5403              if (length $token->{data}) {
5404                !!!cp ('t314');
5405                ## reprocess the rest of characters
5406              } else {
5407                !!!cp ('t315');
5408                !!!next-token;
5409              }
5410              redo B;
5411            }
5412            
5413            die qq[$0: Character "$token->{data}"];
5414          } elsif ($token->{type} == START_TAG_TOKEN) {
5415            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5416              !!!cp ('t316');
5417              !!!parse-error (type => 'after html:'.$token->{tag_name});
5418    
5419              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5420              ## Process in the "after frameset" insertion mode.
5421            } else {
5422              !!!cp ('t317');
5423            }
5424    
5425            if ($token->{tag_name} eq 'frameset' and
5426                $self->{insertion_mode} == IN_FRAMESET_IM) {
5427              !!!cp ('t318');
5428            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes});
5429            !!!next-token;            !!!next-token;
5430            redo B;            redo B;
5431          } elsif ($token->{tag_name} eq 'frame') {          } elsif ($token->{tag_name} eq 'frame' and
5432                     $self->{insertion_mode} == IN_FRAMESET_IM) {
5433              !!!cp ('t319');
5434            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes});
5435            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
5436            !!!next-token;            !!!next-token;
5437            redo B;            redo B;
5438          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
5439            $in_body->($insert_to_current);            !!!cp ('t320');
5440              ## NOTE: As if in body.
5441              $parse_rcdata->(CDATA_CONTENT_MODEL);
5442            redo B;            redo B;
5443          } else {          } else {
5444            !!!parse-error (type => 'in frameset:'.$token->{tag_name});            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5445                !!!cp ('t321');
5446                !!!parse-error (type => 'in frameset:'.$token->{tag_name});
5447              } else {
5448                !!!cp ('t322');
5449                !!!parse-error (type => 'after frameset:'.$token->{tag_name});
5450              }
5451            ## Ignore the token            ## Ignore the token
5452            !!!next-token;            !!!next-token;
5453            redo B;            redo B;
5454          }          }
5455        } elsif ($token->{type} eq 'end tag') {        } elsif ($token->{type} == END_TAG_TOKEN) {
5456          if ($token->{tag_name} eq 'frameset') {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5457              !!!cp ('t323');
5458              !!!parse-error (type => 'after html:/'.$token->{tag_name});
5459    
5460              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5461              ## Process in the "after frameset" insertion mode.
5462            } else {
5463              !!!cp ('t324');
5464            }
5465    
5466            if ($token->{tag_name} eq 'frameset' and
5467                $self->{insertion_mode} == IN_FRAMESET_IM) {
5468            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] eq 'html' and
5469                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
5470                !!!cp ('t325');
5471              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5472              ## Ignore the token              ## Ignore the token
5473              !!!next-token;              !!!next-token;
5474            } else {            } else {
5475                !!!cp ('t326');
5476              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5477              !!!next-token;              !!!next-token;
5478            }            }
5479    
5480            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
5481                $self->{open_elements}->[-1]->[1] ne 'frameset') {                $self->{open_elements}->[-1]->[1] ne 'frameset') {
5482              $self->{insertion_mode} = 'after frameset';              !!!cp ('t327');
5483                $self->{insertion_mode} = AFTER_FRAMESET_IM;
5484              } else {
5485                !!!cp ('t328');
5486            }            }
5487            redo B;            redo B;
5488            } elsif ($token->{tag_name} eq 'html' and
5489                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
5490              !!!cp ('t329');
5491              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
5492              !!!next-token;
5493              redo B;
5494          } else {          } else {
5495            !!!parse-error (type => 'in frameset:/'.$token->{tag_name});            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5496                !!!cp ('t330');
5497                !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
5498              } else {
5499                !!!cp ('t331');
5500                !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
5501              }
5502            ## Ignore the token            ## Ignore the token
5503            !!!next-token;            !!!next-token;
5504            redo B;            redo B;
5505          }          }
5506          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5507            unless ($self->{open_elements}->[-1]->[1] eq 'html' and
5508                    @{$self->{open_elements}} == 1) { # redundant, maybe
5509              !!!cp ('t331.1');
5510              !!!parse-error (type => 'in body:#eof');
5511            } else {
5512              !!!cp ('t331.2');
5513            }
5514            
5515            ## Stop parsing
5516            last B;
5517        } else {        } else {
5518          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5519        }        }
     } 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);  
5520    
5521                unless (length $token->{data}) {        ## ISSUE: An issue in spec here
5522                  !!!next-token;      } else {
5523                  redo B;        die "$0: $self->{insertion_mode}: Unknown insertion mode";
5524                }      }
             }  
5525    
5526              if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {      ## "in body" insertion mode
5527                !!!parse-error (type => 'after frameset:#character');      if ($token->{type} == START_TAG_TOKEN) {
5528          if ($token->{tag_name} eq 'script') {
5529            !!!cp ('t332');
5530            ## NOTE: This is an "as if in head" code clone
5531            $script_start_tag->();
5532            redo B;
5533          } elsif ($token->{tag_name} eq 'style') {
5534            !!!cp ('t333');
5535            ## NOTE: This is an "as if in head" code clone
5536            $parse_rcdata->(CDATA_CONTENT_MODEL);
5537            redo B;
5538          } elsif ({
5539                    base => 1, link => 1,
5540                   }->{$token->{tag_name}}) {
5541            !!!cp ('t334');
5542            ## NOTE: This is an "as if in head" code clone, only "-t" differs
5543            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5544            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
5545            !!!next-token;
5546            redo B;
5547          } elsif ($token->{tag_name} eq 'meta') {
5548            ## NOTE: This is an "as if in head" code clone, only "-t" differs
5549            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5550            my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
5551    
5552                ## Ignore the token.          unless ($self->{confident}) {
5553                if (length $token->{data}) {            if ($token->{attributes}->{charset}) { ## TODO: And if supported
5554                  ## reprocess the rest of characters              !!!cp ('t335');
5555                } else {              $self->{change_encoding}
5556                  !!!next-token;                  ->($self, $token->{attributes}->{charset}->{value});
5557                }              
5558                redo B;              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
5559                    ->set_user_data (manakai_has_reference =>
5560                                         $token->{attributes}->{charset}
5561                                             ->{has_reference});
5562              } elsif ($token->{attributes}->{content}) {
5563                ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
5564                if ($token->{attributes}->{content}->{value}
5565                    =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
5566                        [\x09-\x0D\x20]*=
5567                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
5568                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
5569                  !!!cp ('t336');
5570                  $self->{change_encoding}
5571                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
5572                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
5573                      ->set_user_data (manakai_has_reference =>
5574                                           $token->{attributes}->{content}
5575                                                 ->{has_reference});
5576              }              }
5577              }
5578            } else {
5579              if ($token->{attributes}->{charset}) {
5580                !!!cp ('t337');
5581                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
5582                    ->set_user_data (manakai_has_reference =>
5583                                         $token->{attributes}->{charset}
5584                                             ->{has_reference});
5585              }
5586              if ($token->{attributes}->{content}) {
5587                !!!cp ('t338');
5588                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
5589                    ->set_user_data (manakai_has_reference =>
5590                                         $token->{attributes}->{content}
5591                                             ->{has_reference});
5592              }
5593            }
5594    
5595          die qq[$0: Character "$token->{data}"];          !!!next-token;
5596        } elsif ($token->{type} eq 'start tag') {          redo B;
5597          if ($token->{tag_name} eq 'noframes') {        } elsif ($token->{tag_name} eq 'title') {
5598            $in_body->($insert_to_current);          !!!cp ('t341');
5599            redo B;          ## NOTE: This is an "as if in head" code clone
5600            $parse_rcdata->(RCDATA_CONTENT_MODEL);
5601            redo B;
5602          } elsif ($token->{tag_name} eq 'body') {
5603            !!!parse-error (type => 'in body:body');
5604                  
5605            if (@{$self->{open_elements}} == 1 or
5606                $self->{open_elements}->[1]->[1] ne 'body') {
5607              !!!cp ('t342');
5608              ## Ignore the token
5609          } else {          } else {
5610            !!!parse-error (type => 'after frameset:'.$token->{tag_name});            my $body_el = $self->{open_elements}->[1]->[0];
5611              for my $attr_name (keys %{$token->{attributes}}) {
5612                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
5613                  !!!cp ('t343');
5614                  $body_el->set_attribute_ns
5615                    (undef, [undef, $attr_name],
5616                     $token->{attributes}->{$attr_name}->{value});
5617                }
5618              }
5619            }
5620            !!!next-token;
5621            redo B;
5622          } elsif ({
5623                    address => 1, blockquote => 1, center => 1, dir => 1,
5624                    div => 1, dl => 1, fieldset => 1,
5625                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5626                    menu => 1, ol => 1, p => 1, ul => 1,
5627                    pre => 1, listing => 1,
5628                    form => 1,
5629                    table => 1,
5630                    hr => 1,
5631                   }->{$token->{tag_name}}) {
5632            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
5633              !!!cp ('t350');
5634              !!!parse-error (type => 'in form:form');
5635            ## Ignore the token            ## Ignore the token
5636            !!!next-token;            !!!next-token;
5637            redo B;            redo B;
5638          }          }
5639        } elsif ($token->{type} eq 'end tag') {  
5640          if ($token->{tag_name} eq 'html') {          ## has a p element in scope
5641            $previous_insertion_mode = $self->{insertion_mode};          INSCOPE: for (reverse @{$self->{open_elements}}) {
5642            $self->{insertion_mode} = 'trailing end';            if ($_->[1] eq 'p') {
5643                !!!cp ('t344');
5644                !!!back-token;
5645                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5646                redo B;
5647              } elsif ({
5648                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
5649                        button => 1, marquee => 1, object => 1, html => 1,
5650                       }->{$_->[1]}) {
5651                !!!cp ('t345');
5652                last INSCOPE;
5653              }
5654            } # INSCOPE
5655              
5656            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5657            if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
5658            !!!next-token;            !!!next-token;
5659            redo B;            if ($token->{type} == CHARACTER_TOKEN) {
5660                $token->{data} =~ s/^\x0A//;
5661                unless (length $token->{data}) {
5662                  !!!cp ('t346');
5663                  !!!next-token;
5664                } else {
5665                  !!!cp ('t349');
5666                }
5667              } else {
5668                !!!cp ('t348');
5669              }
5670            } elsif ($token->{tag_name} eq 'form') {
5671              !!!cp ('t347.1');
5672              $self->{form_element} = $self->{open_elements}->[-1]->[0];
5673    
5674              !!!next-token;
5675            } elsif ($token->{tag_name} eq 'table') {
5676              !!!cp ('t382');
5677              push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
5678              
5679              $self->{insertion_mode} = IN_TABLE_IM;
5680    
5681              !!!next-token;
5682            } elsif ($token->{tag_name} eq 'hr') {
5683              !!!cp ('t386');
5684              pop @{$self->{open_elements}};
5685            
5686              !!!next-token;
5687            } else {
5688              !!!cp ('t347');
5689              !!!next-token;
5690            }
5691            redo B;
5692          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
5693            ## has a p element in scope
5694            INSCOPE: for (reverse @{$self->{open_elements}}) {
5695              if ($_->[1] eq 'p') {
5696                !!!cp ('t353');
5697                !!!back-token;
5698                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5699                redo B;
5700              } elsif ({
5701                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
5702                        button => 1, marquee => 1, object => 1, html => 1,
5703                       }->{$_->[1]}) {
5704                !!!cp ('t354');
5705                last INSCOPE;
5706              }
5707            } # INSCOPE
5708              
5709            ## Step 1
5710            my $i = -1;
5711            my $node = $self->{open_elements}->[$i];
5712            my $li_or_dtdd = {li => {li => 1},
5713                              dt => {dt => 1, dd => 1},
5714                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
5715            LI: {
5716              ## Step 2
5717              if ($li_or_dtdd->{$node->[1]}) {
5718                if ($i != -1) {
5719                  !!!cp ('t355');
5720                  !!!parse-error (type => 'end tag missing:'.
5721                                  $self->{open_elements}->[-1]->[1]);
5722                } else {
5723                  !!!cp ('t356');
5724                }
5725                splice @{$self->{open_elements}}, $i;
5726                last LI;
5727              } else {
5728                !!!cp ('t357');
5729              }
5730              
5731              ## Step 3
5732              if (not $formatting_category->{$node->[1]} and
5733                  #not $phrasing_category->{$node->[1]} and
5734                  ($special_category->{$node->[1]} or
5735                   $scoping_category->{$node->[1]}) and
5736                  $node->[1] ne 'address' and $node->[1] ne 'div') {
5737                !!!cp ('t358');
5738                last LI;
5739              }
5740              
5741              !!!cp ('t359');
5742              ## Step 4
5743              $i--;
5744              $node = $self->{open_elements}->[$i];
5745              redo LI;
5746            } # LI
5747              
5748            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5749            !!!next-token;
5750            redo B;
5751          } elsif ($token->{tag_name} eq 'plaintext') {
5752            ## has a p element in scope
5753            INSCOPE: for (reverse @{$self->{open_elements}}) {
5754              if ($_->[1] eq 'p') {
5755                !!!cp ('t367');
5756                !!!back-token;
5757                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5758                redo B;
5759              } elsif ({
5760                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
5761                        button => 1, marquee => 1, object => 1, html => 1,
5762                       }->{$_->[1]}) {
5763                !!!cp ('t368');
5764                last INSCOPE;
5765              }
5766            } # INSCOPE
5767              
5768            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5769              
5770            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
5771              
5772            !!!next-token;
5773            redo B;
5774          } elsif ($token->{tag_name} eq 'a') {
5775            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
5776              my $node = $active_formatting_elements->[$i];
5777              if ($node->[1] eq 'a') {
5778                !!!cp ('t371');
5779                !!!parse-error (type => 'in a:a');
5780                
5781                !!!back-token;
5782                $token = {type => END_TAG_TOKEN, tag_name => 'a'};
5783                $formatting_end_tag->($token->{tag_name});
5784                
5785                AFE2: for (reverse 0..$#$active_formatting_elements) {
5786                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
5787                    !!!cp ('t372');
5788                    splice @$active_formatting_elements, $_, 1;
5789                    last AFE2;
5790                  }
5791                } # AFE2
5792                OE: for (reverse 0..$#{$self->{open_elements}}) {
5793                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
5794                    !!!cp ('t373');
5795                    splice @{$self->{open_elements}}, $_, 1;
5796                    last OE;
5797                  }
5798                } # OE
5799                last AFE;
5800              } elsif ($node->[0] eq '#marker') {
5801                !!!cp ('t374');
5802                last AFE;
5803              }
5804            } # AFE
5805              
5806            $reconstruct_active_formatting_elements->($insert_to_current);
5807    
5808            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5809            push @$active_formatting_elements, $self->{open_elements}->[-1];
5810    
5811            !!!next-token;
5812            redo B;
5813          } elsif ($token->{tag_name} eq 'nobr') {
5814            $reconstruct_active_formatting_elements->($insert_to_current);
5815    
5816            ## has a |nobr| element in scope
5817            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5818              my $node = $self->{open_elements}->[$_];
5819              if ($node->[1] eq 'nobr') {
5820                !!!cp ('t376');
5821                !!!parse-error (type => 'in nobr:nobr');
5822                !!!back-token;
5823                $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};
5824                redo B;
5825              } elsif ({
5826                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
5827                        button => 1, marquee => 1, object => 1, html => 1,
5828                       }->{$node->[1]}) {
5829                !!!cp ('t377');
5830                last INSCOPE;
5831              }
5832            } # INSCOPE
5833            
5834            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5835            push @$active_formatting_elements, $self->{open_elements}->[-1];
5836            
5837            !!!next-token;
5838            redo B;
5839          } elsif ($token->{tag_name} eq 'button') {
5840            ## has a button element in scope
5841            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5842              my $node = $self->{open_elements}->[$_];
5843              if ($node->[1] eq 'button') {
5844                !!!cp ('t378');
5845                !!!parse-error (type => 'in button:button');
5846                !!!back-token;
5847                $token = {type => END_TAG_TOKEN, tag_name => 'button'};
5848                redo B;
5849              } elsif ({
5850                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
5851                        button => 1, marquee => 1, object => 1, html => 1,
5852                       }->{$node->[1]}) {
5853                !!!cp ('t379');
5854                last INSCOPE;
5855              }
5856            } # INSCOPE
5857              
5858            $reconstruct_active_formatting_elements->($insert_to_current);
5859              
5860            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5861    
5862            ## TODO: associate with $self->{form_element} if defined
5863    
5864            push @$active_formatting_elements, ['#marker', ''];
5865    
5866            !!!next-token;
5867            redo B;
5868          } elsif ({
5869                    xmp => 1,
5870                    iframe => 1,
5871                    noembed => 1,
5872                    noframes => 1,
5873                    noscript => 0, ## TODO: 1 if scripting is enabled
5874                   }->{$token->{tag_name}}) {
5875            if ($token->{tag_name} eq 'xmp') {
5876              !!!cp ('t381');
5877              $reconstruct_active_formatting_elements->($insert_to_current);
5878          } else {          } else {
5879            !!!parse-error (type => 'after frameset:/'.$token->{tag_name});            !!!cp ('t399');
5880            }
5881            ## NOTE: There is an "as if in body" code clone.
5882            $parse_rcdata->(CDATA_CONTENT_MODEL);
5883            redo B;
5884          } elsif ($token->{tag_name} eq 'isindex') {
5885            !!!parse-error (type => 'isindex');
5886            
5887            if (defined $self->{form_element}) {
5888              !!!cp ('t389');
5889            ## Ignore the token            ## Ignore the token
5890            !!!next-token;            !!!next-token;
5891            redo B;            redo B;
5892            } else {
5893              my $at = $token->{attributes};
5894              my $form_attrs;
5895              $form_attrs->{action} = $at->{action} if $at->{action};
5896              my $prompt_attr = $at->{prompt};
5897              $at->{name} = {name => 'name', value => 'isindex'};
5898              delete $at->{action};
5899              delete $at->{prompt};
5900              my @tokens = (
5901                            {type => START_TAG_TOKEN, tag_name => 'form',
5902                             attributes => $form_attrs},
5903                            {type => START_TAG_TOKEN, tag_name => 'hr'},
5904                            {type => START_TAG_TOKEN, tag_name => 'p'},
5905                            {type => START_TAG_TOKEN, tag_name => 'label'},
5906                           );
5907              if ($prompt_attr) {
5908                !!!cp ('t390');
5909                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};
5910              } else {
5911                !!!cp ('t391');
5912                push @tokens, {type => CHARACTER_TOKEN,
5913                               data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
5914                ## TODO: make this configurable
5915              }
5916              push @tokens,
5917                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},
5918                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
5919                            {type => END_TAG_TOKEN, tag_name => 'label'},
5920                            {type => END_TAG_TOKEN, tag_name => 'p'},
5921                            {type => START_TAG_TOKEN, tag_name => 'hr'},
5922                            {type => END_TAG_TOKEN, tag_name => 'form'};
5923              $token = shift @tokens;
5924              !!!back-token (@tokens);
5925              redo B;
5926          }          }
5927          } elsif ($token->{tag_name} eq 'textarea') {
5928            my $tag_name = $token->{tag_name};
5929            my $el;
5930            !!!create-element ($el, $token->{tag_name}, $token->{attributes});
5931            
5932            ## TODO: $self->{form_element} if defined
5933            $self->{content_model} = RCDATA_CONTENT_MODEL;
5934            delete $self->{escape}; # MUST
5935            
5936            $insert->($el);
5937            
5938            my $text = '';
5939            !!!next-token;
5940            if ($token->{type} == CHARACTER_TOKEN) {
5941              $token->{data} =~ s/^\x0A//;
5942              unless (length $token->{data}) {
5943                !!!cp ('t392');
5944                !!!next-token;
5945              } else {
5946                !!!cp ('t393');
5947              }
5948            } else {
5949              !!!cp ('t394');
5950            }
5951            while ($token->{type} == CHARACTER_TOKEN) {
5952              !!!cp ('t395');
5953              $text .= $token->{data};
5954              !!!next-token;
5955            }
5956            if (length $text) {
5957              !!!cp ('t396');
5958              $el->manakai_append_text ($text);
5959            }
5960            
5961            $self->{content_model} = PCDATA_CONTENT_MODEL;
5962            
5963            if ($token->{type} == END_TAG_TOKEN and
5964                $token->{tag_name} eq $tag_name) {
5965              !!!cp ('t397');
5966              ## Ignore the token
5967            } else {
5968              !!!cp ('t398');
5969              !!!parse-error (type => 'in RCDATA:#'.$token->{type});
5970            }
5971            !!!next-token;
5972            redo B;
5973          } elsif ({
5974                    caption => 1, col => 1, colgroup => 1, frame => 1,
5975                    frameset => 1, head => 1, option => 1, optgroup => 1,
5976                    tbody => 1, td => 1, tfoot => 1, th => 1,
5977                    thead => 1, tr => 1,
5978                   }->{$token->{tag_name}}) {
5979            !!!cp ('t401');
5980            !!!parse-error (type => 'in body:'.$token->{tag_name});
5981            ## Ignore the token
5982            !!!next-token;
5983            redo B;
5984            
5985            ## ISSUE: An issue on HTML5 new elements in the spec.
5986        } else {        } else {
5987          die "$0: $token->{type}: Unknown token type";          if ($token->{tag_name} eq 'image') {
5988              !!!cp ('t384');
5989              !!!parse-error (type => 'image');
5990              $token->{tag_name} = 'img';
5991            } else {
5992              !!!cp ('t385');
5993            }
5994    
5995            ## NOTE: There is an "as if <br>" code clone.
5996            $reconstruct_active_formatting_elements->($insert_to_current);
5997            
5998            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5999    
6000            if ({
6001                 applet => 1, marquee => 1, object => 1,
6002                }->{$token->{tag_name}}) {
6003              !!!cp ('t380');
6004              push @$active_formatting_elements, ['#marker', ''];
6005            } elsif ({
6006                      b => 1, big => 1, em => 1, font => 1, i => 1,
6007                      s => 1, small => 1, strile => 1,
6008                      strong => 1, tt => 1, u => 1,
6009                     }->{$token->{tag_name}}) {
6010              !!!cp ('t375');
6011              push @$active_formatting_elements, $self->{open_elements}->[-1];
6012            } elsif ($token->{tag_name} eq 'input') {
6013              !!!cp ('t388');
6014              ## TODO: associate with $self->{form_element} if defined
6015              pop @{$self->{open_elements}};
6016            } elsif ({
6017                      area => 1, basefont => 1, bgsound => 1, br => 1,
6018                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
6019                      #image => 1,
6020                     }->{$token->{tag_name}}) {
6021              !!!cp ('t388.1');
6022              pop @{$self->{open_elements}};
6023            } elsif ($token->{tag_name} eq 'select') {
6024              ## TODO: associate with $self->{form_element} if defined
6025            
6026              if ($self->{insertion_mode} & TABLE_IMS or
6027                  $self->{insertion_mode} & BODY_TABLE_IMS or
6028                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6029                !!!cp ('t400.1');
6030                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
6031              } else {
6032                !!!cp ('t400.2');
6033                $self->{insertion_mode} = IN_SELECT_IM;
6034              }
6035            } else {
6036              !!!cp ('t402');
6037            }
6038            
6039            !!!next-token;
6040            redo B;
6041        }        }
6042        } elsif ($token->{type} == END_TAG_TOKEN) {
6043          if ($token->{tag_name} eq 'body') {
6044            ## has a |body| element in scope
6045            my $i;
6046            INSCOPE: {
6047              for (reverse @{$self->{open_elements}}) {
6048                if ($_->[1] eq 'body') {
6049                  !!!cp ('t405');
6050                  $i = $_;
6051                  last INSCOPE;
6052                } elsif ({
6053                          applet => 1, table => 1, caption => 1, td => 1, th => 1,
6054                          button => 1, marquee => 1, object => 1, html => 1,
6055                         }->{$_->[1]}) {
6056                  !!!cp ('t405.1');
6057                  last;
6058                }
6059              }
6060    
6061        ## ISSUE: An issue in spec here            !!!parse-error (type => 'start tag not allowed',
6062      } elsif ($self->{insertion_mode} eq 'trailing end') {                            value => $token->{tag_name});
6063        ## states in the main stage is preserved yet # MUST            ## NOTE: Ignore the token.
6064                    !!!next-token;
6065        if ($token->{type} eq 'character') {            redo B;
6066          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          } # INSCOPE
6067            my $data = $1;  
6068            ## As if in the main phase.          for (@{$self->{open_elements}}) {
6069            ## NOTE: The insertion mode in the main phase            unless ({
6070            ## just before the phase has been changed to the trailing                     dd => 1, dt => 1, li => 1, p => 1, td => 1,
6071            ## end phase is either "after body" or "after frameset".                     th => 1, tr => 1, body => 1, html => 1,
6072            $reconstruct_active_formatting_elements->($insert_to_current);                     tbody => 1, tfoot => 1, thead => 1,
6073                      }->{$_->[1]}) {
6074                !!!cp ('t403');
6075                !!!parse-error (type => 'not closed:'.$_->[1]);
6076                last;
6077              } else {
6078                !!!cp ('t404');
6079              }
6080            }
6081    
6082            $self->{insertion_mode} = AFTER_BODY_IM;
6083            !!!next-token;
6084            redo B;
6085          } elsif ($token->{tag_name} eq 'html') {
6086            if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {
6087              ## ISSUE: There is an issue in the spec.
6088              if ($self->{open_elements}->[-1]->[1] ne 'body') {
6089                !!!cp ('t406');
6090                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
6091              } else {
6092                !!!cp ('t407');
6093              }
6094              $self->{insertion_mode} = AFTER_BODY_IM;
6095              ## reprocess
6096              redo B;
6097            } else {
6098              !!!cp ('t408');
6099              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6100              ## Ignore the token
6101              !!!next-token;
6102              redo B;
6103            }
6104          } elsif ({
6105                    address => 1, blockquote => 1, center => 1, dir => 1,
6106                    div => 1, dl => 1, fieldset => 1, listing => 1,
6107                    menu => 1, ol => 1, pre => 1, ul => 1,
6108                    dd => 1, dt => 1, li => 1,
6109                    applet => 1, button => 1, marquee => 1, object => 1,
6110                   }->{$token->{tag_name}}) {
6111            ## has an element in scope
6112            my $i;
6113            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6114              my $node = $self->{open_elements}->[$_];
6115              if ($node->[1] eq $token->{tag_name}) {
6116                !!!cp ('t410');
6117                $i = $_;
6118                last INSCOPE;
6119              } elsif ({
6120                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
6121                        button => 1, marquee => 1, object => 1, html => 1,
6122                       }->{$node->[1]}) {
6123                !!!cp ('t411');
6124                last INSCOPE;
6125              }
6126            } # INSCOPE
6127    
6128            unless (defined $i) { # has an element in scope
6129              !!!cp ('t413');
6130              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6131            } else {
6132              ## Step 1. generate implied end tags
6133              while ({
6134                      dd => ($token->{tag_name} ne 'dd'),
6135                      dt => ($token->{tag_name} ne 'dt'),
6136                      li => ($token->{tag_name} ne 'li'),
6137                      p => 1,
6138                     }->{$self->{open_elements}->[-1]->[1]}) {
6139                !!!cp ('t409');
6140                pop @{$self->{open_elements}};
6141              }
6142    
6143              ## Step 2.
6144              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6145                !!!cp ('t412');
6146                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
6147              } else {
6148                !!!cp ('t414');
6149              }
6150    
6151              ## Step 3.
6152              splice @{$self->{open_elements}}, $i;
6153    
6154              ## Step 4.
6155              $clear_up_to_marker->()
6156                  if {
6157                    applet => 1, button => 1, marquee => 1, object => 1,
6158                  }->{$token->{tag_name}};
6159            }
6160            !!!next-token;
6161            redo B;
6162          } elsif ($token->{tag_name} eq 'form') {
6163            undef $self->{form_element};
6164    
6165            ## has an element in scope
6166            my $i;
6167            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6168              my $node = $self->{open_elements}->[$_];
6169              if ($node->[1] eq $token->{tag_name}) {
6170                !!!cp ('t418');
6171                $i = $_;
6172                last INSCOPE;
6173              } elsif ({
6174                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
6175                        button => 1, marquee => 1, object => 1, html => 1,
6176                       }->{$node->[1]}) {
6177                !!!cp ('t419');
6178                last INSCOPE;
6179              }
6180            } # INSCOPE
6181    
6182            unless (defined $i) { # has an element in scope
6183              !!!cp ('t421');
6184              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6185            } else {
6186              ## Step 1. generate implied end tags
6187              while ({
6188                      dd => 1, dt => 1, li => 1, p => 1,
6189                     }->{$self->{open_elements}->[-1]->[1]}) {
6190                !!!cp ('t417');
6191                pop @{$self->{open_elements}};
6192              }
6193                        
6194            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);            ## Step 2.
6195              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6196                !!!cp ('t417.1');
6197                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
6198              } else {
6199                !!!cp ('t420');
6200              }  
6201                        
6202            unless (length $token->{data}) {            ## Step 3.
6203              !!!next-token;            splice @{$self->{open_elements}}, $i;
6204              redo B;          }
6205    
6206            !!!next-token;
6207            redo B;
6208          } elsif ({
6209                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6210                   }->{$token->{tag_name}}) {
6211            ## has an element in scope
6212            my $i;
6213            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6214              my $node = $self->{open_elements}->[$_];
6215              if ({
6216                   h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6217                  }->{$node->[1]}) {
6218                !!!cp ('t423');
6219                $i = $_;
6220                last INSCOPE;
6221              } elsif ({
6222                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
6223                        button => 1, marquee => 1, object => 1, html => 1,
6224                       }->{$node->[1]}) {
6225                !!!cp ('t424');
6226                last INSCOPE;
6227            }            }
6228            } # INSCOPE
6229    
6230            unless (defined $i) { # has an element in scope
6231              !!!cp ('t425.1');
6232              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6233            } else {
6234              ## Step 1. generate implied end tags
6235              while ({
6236                      dd => 1, dt => 1, li => 1, p => 1,
6237                     }->{$self->{open_elements}->[-1]->[1]}) {
6238                !!!cp ('t422');
6239                pop @{$self->{open_elements}};
6240              }
6241              
6242              ## Step 2.
6243              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6244                !!!cp ('t425');
6245                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6246              } else {
6247                !!!cp ('t426');
6248              }
6249    
6250              ## Step 3.
6251              splice @{$self->{open_elements}}, $i;
6252          }          }
6253            
6254            !!!next-token;
6255            redo B;
6256          } elsif ($token->{tag_name} eq 'p') {
6257            ## has an element in scope
6258            my $i;
6259            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6260              my $node = $self->{open_elements}->[$_];
6261              if ($node->[1] eq $token->{tag_name}) {
6262                !!!cp ('t410.1');
6263                $i = $_;
6264                last INSCOPE;
6265              } elsif ({
6266                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
6267                        button => 1, marquee => 1, object => 1, html => 1,
6268                       }->{$node->[1]}) {
6269                !!!cp ('t411.1');
6270                last INSCOPE;
6271              }
6272            } # INSCOPE
6273    
6274          !!!parse-error (type => 'after html:#character');          if (defined $i) {
6275          $self->{insertion_mode} = $previous_insertion_mode;            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6276          ## reprocess              !!!cp ('t412.1');
6277                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
6278              } else {
6279                !!!cp ('t414.1');
6280              }
6281    
6282              splice @{$self->{open_elements}}, $i;
6283            } else {
6284              !!!cp ('t413.1');
6285              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6286    
6287              !!!cp ('t415.1');
6288              ## As if <p>, then reprocess the current token
6289              my $el;
6290              !!!create-element ($el, 'p');
6291              $insert->($el);
6292              ## NOTE: Not inserted into |$self->{open_elements}|.
6293            }
6294    
6295            !!!next-token;
6296          redo B;          redo B;
6297        } elsif ($token->{type} eq 'start tag') {        } elsif ({
6298          !!!parse-error (type => 'after html:'.$token->{tag_name});                  a => 1,
6299          $self->{insertion_mode} = $previous_insertion_mode;                  b => 1, big => 1, em => 1, font => 1, i => 1,
6300          ## reprocess                  nobr => 1, s => 1, small => 1, strile => 1,
6301                    strong => 1, tt => 1, u => 1,
6302                   }->{$token->{tag_name}}) {
6303            !!!cp ('t427');
6304            $formatting_end_tag->($token->{tag_name});
6305          redo B;          redo B;
6306        } elsif ($token->{type} eq 'end tag') {        } elsif ($token->{tag_name} eq 'br') {
6307          !!!parse-error (type => 'after html:/'.$token->{tag_name});          !!!cp ('t428');
6308          $self->{insertion_mode} = $previous_insertion_mode;          !!!parse-error (type => 'unmatched end tag:br');
6309          ## reprocess  
6310            ## As if <br>
6311            $reconstruct_active_formatting_elements->($insert_to_current);
6312            
6313            my $el;
6314            !!!create-element ($el, 'br');
6315            $insert->($el);
6316            
6317            ## Ignore the token.
6318            !!!next-token;
6319            redo B;
6320          } elsif ({
6321                    caption => 1, col => 1, colgroup => 1, frame => 1,
6322                    frameset => 1, head => 1, option => 1, optgroup => 1,
6323                    tbody => 1, td => 1, tfoot => 1, th => 1,
6324                    thead => 1, tr => 1,
6325                    area => 1, basefont => 1, bgsound => 1,
6326                    embed => 1, hr => 1, iframe => 1, image => 1,
6327                    img => 1, input => 1, isindex => 1, noembed => 1,
6328                    noframes => 1, param => 1, select => 1, spacer => 1,
6329                    table => 1, textarea => 1, wbr => 1,
6330                    noscript => 0, ## TODO: if scripting is enabled
6331                   }->{$token->{tag_name}}) {
6332            !!!cp ('t429');
6333            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6334            ## Ignore the token
6335            !!!next-token;
6336          redo B;          redo B;
6337            
6338            ## ISSUE: Issue on HTML5 new elements in spec
6339            
6340        } else {        } else {
6341          die "$0: $token->{type}: Unknown token";          ## Step 1
6342            my $node_i = -1;
6343            my $node = $self->{open_elements}->[$node_i];
6344    
6345            ## Step 2
6346            S2: {
6347              if ($node->[1] eq $token->{tag_name}) {
6348                ## Step 1
6349                ## generate implied end tags
6350                while ({
6351                        dd => 1, dt => 1, li => 1, p => 1,
6352                       }->{$self->{open_elements}->[-1]->[1]}) {
6353                  !!!cp ('t430');
6354                  ## ISSUE: Can this case be reached?
6355                  pop @{$self->{open_elements}};
6356                }
6357            
6358                ## Step 2
6359                if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
6360                  !!!cp ('t431');
6361                  ## NOTE: <x><y></x>
6362                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
6363                } else {
6364                  !!!cp ('t432');
6365                }
6366                
6367                ## Step 3
6368                splice @{$self->{open_elements}}, $node_i;
6369    
6370                !!!next-token;
6371                last S2;
6372              } else {
6373                ## Step 3
6374                if (not $formatting_category->{$node->[1]} and
6375                    #not $phrasing_category->{$node->[1]} and
6376                    ($special_category->{$node->[1]} or
6377                     $scoping_category->{$node->[1]})) {
6378                  !!!cp ('t433');
6379                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6380                  ## Ignore the token
6381                  !!!next-token;
6382                  last S2;
6383                }
6384    
6385                !!!cp ('t434');
6386              }
6387              
6388              ## Step 4
6389              $node_i--;
6390              $node = $self->{open_elements}->[$node_i];
6391              
6392              ## Step 5;
6393              redo S2;
6394            } # S2
6395            redo B;
6396        }        }
     } else {  
       die "$0: $self->{insertion_mode}: Unknown insertion mode";  
6397      }      }
6398        redo B;
6399    } # B    } # B
6400    
6401    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 5211  sub set_inner_html ($$$) { Line 6409  sub set_inner_html ($$$) {
6409    my $s = \$_[0];    my $s = \$_[0];
6410    my $onerror = $_[1];    my $onerror = $_[1];
6411    
6412      ## ISSUE: Should {confident} be true?
6413    
6414    my $nt = $node->node_type;    my $nt = $node->node_type;
6415    if ($nt == 9) {    if ($nt == 9) {
6416      # MUST      # MUST
# Line 5239  sub set_inner_html ($$$) { Line 6439  sub set_inner_html ($$$) {
6439      my $p = $class->new;      my $p = $class->new;
6440      $p->{document} = $doc;      $p->{document} = $doc;
6441    
6442      ## Step 9 # MUST      ## Step 8 # MUST
6443      my $i = 0;      my $i = 0;
6444      my $line = 1;      my $line = 1;
6445      my $column = 0;      my $column = 0;
6446      $p->{set_next_input_character} = sub {      $p->{set_next_char} = sub {
6447        my $self = shift;        my $self = shift;
6448    
6449        pop @{$self->{prev_input_character}};        pop @{$self->{prev_char}};
6450        unshift @{$self->{prev_input_character}}, $self->{next_input_character};        unshift @{$self->{prev_char}}, $self->{next_char};
6451    
6452        $self->{next_input_character} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
6453        $self->{next_input_character} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
6454        $column++;        $column++;
6455    
6456        if ($self->{next_input_character} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
6457          $line++;          $line++;
6458          $column = 0;          $column = 0;
6459        } elsif ($self->{next_input_character} == 0x000D) { # CR          !!!cp ('i1');
6460          } elsif ($self->{next_char} == 0x000D) { # CR
6461          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
6462          $self->{next_input_character} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
6463          $line++;          $line++;
6464          $column = 0;          $column = 0;
6465        } elsif ($self->{next_input_character} > 0x10FFFF) {          !!!cp ('i2');
6466          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        } elsif ($self->{next_char} > 0x10FFFF) {
6467        } elsif ($self->{next_input_character} == 0x0000) { # NULL          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
6468            !!!cp ('i3');
6469          } elsif ($self->{next_char} == 0x0000) { # NULL
6470            !!!cp ('i4');
6471          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
6472          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
6473        }        }
6474      };      };
6475      $p->{prev_input_character} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
6476      $p->{next_input_character} = -1;      $p->{next_char} = -1;
6477            
6478      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
6479        my (%opt) = @_;        my (%opt) = @_;
# Line 5283  sub set_inner_html ($$$) { Line 6487  sub set_inner_html ($$$) {
6487      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
6488    
6489      ## Step 2      ## Step 2
6490      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
6491      $p->{content_model_flag} = {      $p->{content_model} = {
6492        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
6493        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
6494        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
6495        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
6496        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
6497        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
6498        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
6499        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
6500        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
6501        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
6502      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
6503         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
6504            unless defined $p->{content_model};
6505            ## ISSUE: What is "the name of the element"? local name?
6506    
6507      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $node_ln];
6508    
6509      ## Step 4      ## Step 3
6510      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
6511        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
6512    
6513      ## Step 5 # MUST      ## Step 4 # MUST
6514      $doc->append_child ($root);      $doc->append_child ($root);
6515    
6516      ## Step 6 # MUST      ## Step 5 # MUST
6517      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, 'html'];
6518    
6519      undef $p->{head_element};      undef $p->{head_element};
6520    
6521      ## Step 7 # MUST      ## Step 6 # MUST
6522      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
6523    
6524      ## Step 8 # MUST      ## Step 7 # MUST
6525      my $anode = $node;      my $anode = $node;
6526      AN: while (defined $anode) {      AN: while (defined $anode) {
6527        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
6528          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
6529          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
6530            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
6531                !!!cp ('i5');
6532              $p->{form_element} = $anode;              $p->{form_element} = $anode;
6533              last AN;              last AN;
6534            }            }
# Line 5330  sub set_inner_html ($$$) { Line 6537  sub set_inner_html ($$$) {
6537        $anode = $anode->parent_node;        $anode = $anode->parent_node;
6538      } # AN      } # AN
6539            
6540      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
6541      {      {
6542        my $self = $p;        my $self = $p;
6543        !!!next-token;        !!!next-token;
6544      }      }
6545      $p->_tree_construction_main;      $p->_tree_construction_main;
6546    
6547      ## Step 11 # MUST      ## Step 10 # MUST
6548      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
6549      for (@cn) {      for (@cn) {
6550        $node->remove_child ($_);        $node->remove_child ($_);
6551      }      }
6552      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
6553    
6554      ## Step 12 # MUST      ## Step 11 # MUST
6555      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
6556      for (@cn) {      for (@cn) {
6557        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);
# Line 5361  sub set_inner_html ($$$) { Line 6567  sub set_inner_html ($$$) {
6567    
6568  } # tree construction stage  } # tree construction stage
6569    
6570  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
6571    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  
6572    
6573  1;  1;
6574  # $Date$  # $Date$

Legend:
Removed from v.1.37  
changed lines
  Added in v.1.112

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24