/[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.38 by wakaba, Tue Jul 17 13:54:57 2007 UTC revision 1.84 by wakaba, Thu Mar 6 15:23:17 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 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;    my $line = 1;
181    my $column = 0;    my $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++;      $column++;
191            
192      if ($self->{next_input_character} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
193        $line++;        $line++;
194        $column = 0;        $column = 0;
195      } elsif ($self->{next_input_character} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
196        $i++ if substr ($$s, $i, 1) eq "\x0A";        $i++ if substr ($$s, $i, 1) eq "\x0A";
197        $self->{next_input_character} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
198        $line++;        $line++;
199        $column = 0;        $column = 0;
200      } elsif ($self->{next_input_character} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
201        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
202      } elsif ($self->{next_input_character} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
203        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
204        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
205      }      }
206    };    };
207    $self->{prev_input_character} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
208    $self->{next_input_character} = -1;    $self->{next_char} = -1;
209    
210    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
211      my (%opt) = @_;      my (%opt) = @_;
# Line 141  sub parse_string ($$$;$) { Line 226  sub parse_string ($$$;$) {
226  sub new ($) {  sub new ($) {
227    my $class = shift;    my $class = shift;
228    my $self = bless {}, $class;    my $self = bless {}, $class;
229    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
230      $self->{next_input_character} = -1;      $self->{next_char} = -1;
231    };    };
232    $self->{parse_error} = sub {    $self->{parse_error} = sub {
233      #      #
234    };    };
235      $self->{change_encoding} = sub {
236        # if ($_[0] is a supported encoding) {
237        #   run "change the encoding" algorithm;
238        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
239        # }
240      };
241      $self->{application_cache_selection} = sub {
242        #
243      };
244    return $self;    return $self;
245  } # new  } # new
246    
247    sub CM_ENTITY () { 0b001 } # & markup in data
248    sub CM_LIMITED_MARKUP () { 0b010 } # < markup in data (limited)
249    sub CM_FULL_MARKUP () { 0b100 } # < markup in data (any)
250    
251    sub PLAINTEXT_CONTENT_MODEL () { 0 }
252    sub CDATA_CONTENT_MODEL () { CM_LIMITED_MARKUP }
253    sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
254    sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
255    
256    sub DATA_STATE () { 0 }
257    sub ENTITY_DATA_STATE () { 1 }
258    sub TAG_OPEN_STATE () { 2 }
259    sub CLOSE_TAG_OPEN_STATE () { 3 }
260    sub TAG_NAME_STATE () { 4 }
261    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
262    sub ATTRIBUTE_NAME_STATE () { 6 }
263    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
264    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
265    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
266    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
267    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
268    sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
269    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
270    sub COMMENT_START_STATE () { 14 }
271    sub COMMENT_START_DASH_STATE () { 15 }
272    sub COMMENT_STATE () { 16 }
273    sub COMMENT_END_STATE () { 17 }
274    sub COMMENT_END_DASH_STATE () { 18 }
275    sub BOGUS_COMMENT_STATE () { 19 }
276    sub DOCTYPE_STATE () { 20 }
277    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
278    sub DOCTYPE_NAME_STATE () { 22 }
279    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
280    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
281    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
282    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
283    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
284    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
285    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
286    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
287    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
288    sub BOGUS_DOCTYPE_STATE () { 32 }
289    sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
290    
291    sub DOCTYPE_TOKEN () { 1 }
292    sub COMMENT_TOKEN () { 2 }
293    sub START_TAG_TOKEN () { 3 }
294    sub END_TAG_TOKEN () { 4 }
295    sub END_OF_FILE_TOKEN () { 5 }
296    sub CHARACTER_TOKEN () { 6 }
297    
298    sub AFTER_HTML_IMS () { 0b100 }
299    sub HEAD_IMS ()       { 0b1000 }
300    sub BODY_IMS ()       { 0b10000 }
301    sub BODY_TABLE_IMS () { 0b100000 }
302    sub TABLE_IMS ()      { 0b1000000 }
303    sub ROW_IMS ()        { 0b10000000 }
304    sub BODY_AFTER_IMS () { 0b100000000 }
305    sub FRAME_IMS ()      { 0b1000000000 }
306    
307    ## NOTE: "initial" and "before html" insertion modes have no constants.
308    
309    ## NOTE: "after after body" insertion mode.
310    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
311    
312    ## NOTE: "after after frameset" insertion mode.
313    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
314    
315    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
316    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
317    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
318    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
319    sub IN_BODY_IM () { BODY_IMS }
320    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
321    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
322    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
323    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
324    sub IN_TABLE_IM () { TABLE_IMS }
325    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
326    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
327    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
328    sub IN_SELECT_IM () { 0b01 }
329    sub IN_COLUMN_GROUP_IM () { 0b10 }
330    
331  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
332    
333  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
334    my $self = shift;    my $self = shift;
335    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
336    $self->{content_model_flag} = 'PCDATA'; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
337    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
338    undef $self->{current_attribute};    undef $self->{current_attribute};
339    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
340    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
341    $self->{char} = [];    $self->{char} = [];
342    # $self->{next_input_character}    # $self->{next_char}
343    !!!next-input-character;    !!!next-input-character;
344    $self->{token} = [];    $self->{token} = [];
345    # $self->{escape}    # $self->{escape}
346  } # _initialize_tokenizer  } # _initialize_tokenizer
347    
348  ## A token has:  ## A token has:
349  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
350  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
351  ##   ->{name} (DOCTYPE, start tag (tag name), end tag (tag name))  ##   ->{name} (DOCTYPE_TOKEN)
352  ##   ->{public_identifier} (DOCTYPE)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
353  ##   ->{system_identifier} (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
354  ##   ->{correct} == 1 or 0 (DOCTYPE)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
355  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
356  ##   ->{data} (comment, character)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
357    ##        ->{name}
358    ##        ->{value}
359    ##        ->{has_reference} == 1 or 0
360    ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
361    
362  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
363    
# Line 185  sub _initialize_tokenizer ($) { Line 367  sub _initialize_tokenizer ($) {
367  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
368  ## and removed from the list.  ## and removed from the list.
369    
370    ## NOTE: HTML5 "Writing HTML documents" section, applied to
371    ## documents and not to user agents and conformance checkers,
372    ## contains some requirements that are not detected by the
373    ## parsing algorithm:
374    ## - Some requirements on character encoding declarations. ## TODO
375    ## - "Elements MUST NOT contain content that their content model disallows."
376    ##   ... Some are parse error, some are not (will be reported by c.c.).
377    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
378    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
379    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
380    
381    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
382    ## be detected by the HTML5 parsing algorithm:
383    ## - Text,
384    
385  sub _get_next_token ($) {  sub _get_next_token ($) {
386    my $self = shift;    my $self = shift;
387    if (@{$self->{token}}) {    if (@{$self->{token}}) {
# Line 192  sub _get_next_token ($) { Line 389  sub _get_next_token ($) {
389    }    }
390    
391    A: {    A: {
392      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
393        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_char} == 0x0026) { # &
394          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
395              $self->{content_model_flag} eq 'RCDATA') {              not $self->{escape}) {
396            $self->{state} = 'entity data';            !!!cp (1);
397              $self->{state} = ENTITY_DATA_STATE;
398            !!!next-input-character;            !!!next-input-character;
399            redo A;            redo A;
400          } else {          } else {
401              !!!cp (2);
402            #            #
403          }          }
404        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
405          if ($self->{content_model_flag} eq 'RCDATA' or          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
             $self->{content_model_flag} eq 'CDATA') {  
406            unless ($self->{escape}) {            unless ($self->{escape}) {
407              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_char}->[0] == 0x002D and # -
408                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_char}->[1] == 0x0021 and # !
409                  $self->{prev_input_character}->[2] == 0x003C) { # <                  $self->{prev_char}->[2] == 0x003C) { # <
410                  !!!cp (3);
411                $self->{escape} = 1;                $self->{escape} = 1;
412                } else {
413                  !!!cp (4);
414              }              }
415              } else {
416                !!!cp (5);
417            }            }
418          }          }
419                    
420          #          #
421        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_char} == 0x003C) { # <
422          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
423              (($self->{content_model_flag} eq 'CDATA' or              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
               $self->{content_model_flag} eq 'RCDATA') and  
424               not $self->{escape})) {               not $self->{escape})) {
425            $self->{state} = 'tag open';            !!!cp (6);
426              $self->{state} = TAG_OPEN_STATE;
427            !!!next-input-character;            !!!next-input-character;
428            redo A;            redo A;
429          } else {          } else {
430              !!!cp (7);
431            #            #
432          }          }
433        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
434          if ($self->{escape} and          if ($self->{escape} and
435              ($self->{content_model_flag} eq 'RCDATA' or              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
436               $self->{content_model_flag} eq 'CDATA')) {            if ($self->{prev_char}->[0] == 0x002D and # -
437            if ($self->{prev_input_character}->[0] == 0x002D and # -                $self->{prev_char}->[1] == 0x002D) { # -
438                $self->{prev_input_character}->[1] == 0x002D) { # -              !!!cp (8);
439              delete $self->{escape};              delete $self->{escape};
440              } else {
441                !!!cp (9);
442            }            }
443            } else {
444              !!!cp (10);
445          }          }
446                    
447          #          #
448        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
449          !!!emit ({type => 'end-of-file'});          !!!cp (11);
450            !!!emit ({type => END_OF_FILE_TOKEN});
451          last A; ## TODO: ok?          last A; ## TODO: ok?
452          } else {
453            !!!cp (12);
454        }        }
455        # Anything else        # Anything else
456        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
457                     data => chr $self->{next_input_character}};                     data => chr $self->{next_char}};
458        ## Stay in the data state        ## Stay in the data state
459        !!!next-input-character;        !!!next-input-character;
460    
461        !!!emit ($token);        !!!emit ($token);
462    
463        redo A;        redo A;
464      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
465        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
466                
467        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
468    
469        $self->{state} = 'data';        $self->{state} = DATA_STATE;
470        # next-input-character is already done        # next-input-character is already done
471    
472        unless (defined $token) {        unless (defined $token) {
473          !!!emit ({type => 'character', data => '&'});          !!!cp (13);
474            !!!emit ({type => CHARACTER_TOKEN, data => '&'});
475        } else {        } else {
476            !!!cp (14);
477          !!!emit ($token);          !!!emit ($token);
478        }        }
479    
480        redo A;        redo A;
481      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
482        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
483            $self->{content_model_flag} eq 'CDATA') {          if ($self->{next_char} == 0x002F) { # /
484          if ($self->{next_input_character} == 0x002F) { # /            !!!cp (15);
485            !!!next-input-character;            !!!next-input-character;
486            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
487            redo A;            redo A;
488          } else {          } else {
489              !!!cp (16);
490            ## reconsume            ## reconsume
491            $self->{state} = 'data';            $self->{state} = DATA_STATE;
492    
493            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
494    
495            redo A;            redo A;
496          }          }
497        } elsif ($self->{content_model_flag} eq 'PCDATA') {        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
498          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_char} == 0x0021) { # !
499            $self->{state} = 'markup declaration open';            !!!cp (17);
500              $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
501            !!!next-input-character;            !!!next-input-character;
502            redo A;            redo A;
503          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_char} == 0x002F) { # /
504            $self->{state} = 'close tag open';            !!!cp (18);
505              $self->{state} = CLOSE_TAG_OPEN_STATE;
506            !!!next-input-character;            !!!next-input-character;
507            redo A;            redo A;
508          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
509                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_char} <= 0x005A) { # A..Z
510              !!!cp (19);
511            $self->{current_token}            $self->{current_token}
512              = {type => 'start tag',              = {type => START_TAG_TOKEN,
513                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020)};
514            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
515            !!!next-input-character;            !!!next-input-character;
516            redo A;            redo A;
517          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
518                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
519            $self->{current_token} = {type => 'start tag',            !!!cp (20);
520                              tag_name => chr ($self->{next_input_character})};            $self->{current_token} = {type => START_TAG_TOKEN,
521            $self->{state} = 'tag name';                              tag_name => chr ($self->{next_char})};
522              $self->{state} = TAG_NAME_STATE;
523            !!!next-input-character;            !!!next-input-character;
524            redo A;            redo A;
525          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
526              !!!cp (21);
527            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag');
528            $self->{state} = 'data';            $self->{state} = DATA_STATE;
529            !!!next-input-character;            !!!next-input-character;
530    
531            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});
532    
533            redo A;            redo A;
534          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
535              !!!cp (22);
536            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio');
537            $self->{state} = 'bogus comment';            $self->{state} = BOGUS_COMMENT_STATE;
538            ## $self->{next_input_character} is intentionally left as is            ## $self->{next_char} is intentionally left as is
539            redo A;            redo A;
540          } else {          } else {
541              !!!cp (23);
542            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago');
543            $self->{state} = 'data';            $self->{state} = DATA_STATE;
544            ## reconsume            ## reconsume
545    
546            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
547    
548            redo A;            redo A;
549          }          }
550        } else {        } else {
551          die "$0: $self->{content_model_flag}: Unknown content model flag";          die "$0: $self->{content_model} in tag open";
552        }        }
553      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
554        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
           $self->{content_model_flag} eq 'CDATA') {  
555          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
556            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
557            my @next_char;            my @next_char;
558            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++) {
559              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
560              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
561              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
562              if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {              if ($self->{next_char} == $c or $self->{next_char} == $C) {
563                  !!!cp (24);
564                !!!next-input-character;                !!!next-input-character;
565                next TAGNAME;                next TAGNAME;
566              } else {              } else {
567                $self->{next_input_character} = shift @next_char; # reconsume                !!!cp (25);
568                  $self->{next_char} = shift @next_char; # reconsume
569                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
570                $self->{state} = 'data';                $self->{state} = DATA_STATE;
571    
572                !!!emit ({type => 'character', data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</'});
573        
574                redo A;                redo A;
575              }              }
576            }            }
577            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
578                
579            unless ($self->{next_input_character} == 0x0009 or # HT            unless ($self->{next_char} == 0x0009 or # HT
580                    $self->{next_input_character} == 0x000A or # LF                    $self->{next_char} == 0x000A or # LF
581                    $self->{next_input_character} == 0x000B or # VT                    $self->{next_char} == 0x000B or # VT
582                    $self->{next_input_character} == 0x000C or # FF                    $self->{next_char} == 0x000C or # FF
583                    $self->{next_input_character} == 0x0020 or # SP                    $self->{next_char} == 0x0020 or # SP
584                    $self->{next_input_character} == 0x003E or # >                    $self->{next_char} == 0x003E or # >
585                    $self->{next_input_character} == 0x002F or # /                    $self->{next_char} == 0x002F or # /
586                    $self->{next_input_character} == -1) {                    $self->{next_char} == -1) {
587              $self->{next_input_character} = shift @next_char; # reconsume              !!!cp (26);
588                $self->{next_char} = shift @next_char; # reconsume
589              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
590              $self->{state} = 'data';              $self->{state} = DATA_STATE;
591              !!!emit ({type => 'character', data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</'});
592              redo A;              redo A;
593            } else {            } else {
594              $self->{next_input_character} = shift @next_char;              !!!cp (27);
595                $self->{next_char} = shift @next_char;
596              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
597              # and consume...              # and consume...
598            }            }
599          } else {          } else {
600            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
601              !!!cp (28);
602            # next-input-character is already done            # next-input-character is already done
603            $self->{state} = 'data';            $self->{state} = DATA_STATE;
604            !!!emit ({type => 'character', data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</'});
605            redo A;            redo A;
606          }          }
607        }        }
608                
609        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_char} and
610            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
611          $self->{current_token} = {type => 'end tag',          !!!cp (29);
612                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{current_token} = {type => END_TAG_TOKEN,
613          $self->{state} = 'tag name';                            tag_name => chr ($self->{next_char} + 0x0020)};
614          !!!next-input-character;          $self->{state} = TAG_NAME_STATE;
615          redo A;          !!!next-input-character;
616        } elsif (0x0061 <= $self->{next_input_character} and          redo A;
617                 $self->{next_input_character} <= 0x007A) { # a..z        } elsif (0x0061 <= $self->{next_char} and
618          $self->{current_token} = {type => 'end tag',                 $self->{next_char} <= 0x007A) { # a..z
619                            tag_name => chr ($self->{next_input_character})};          !!!cp (30);
620          $self->{state} = 'tag name';          $self->{current_token} = {type => END_TAG_TOKEN,
621                              tag_name => chr ($self->{next_char})};
622            $self->{state} = TAG_NAME_STATE;
623          !!!next-input-character;          !!!next-input-character;
624          redo A;          redo A;
625        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
626            !!!cp (31);
627          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag');
628          $self->{state} = 'data';          $self->{state} = DATA_STATE;
629          !!!next-input-character;          !!!next-input-character;
630          redo A;          redo A;
631        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
632            !!!cp (32);
633          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
634          $self->{state} = 'data';          $self->{state} = DATA_STATE;
635          # reconsume          # reconsume
636    
637          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</'});
638    
639          redo A;          redo A;
640        } else {        } else {
641            !!!cp (33);
642          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
643          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
644          ## $self->{next_input_character} is intentionally left as is          ## $self->{next_char} is intentionally left as is
645          redo A;          redo A;
646        }        }
647      } elsif ($self->{state} eq 'tag name') {      } elsif ($self->{state} == TAG_NAME_STATE) {
648        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
649            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
650            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
651            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
652            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
653          $self->{state} = 'before attribute name';          !!!cp (34);
654          !!!next-input-character;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
655          redo A;          !!!next-input-character;
656        } elsif ($self->{next_input_character} == 0x003E) { # >          redo A;
657          if ($self->{current_token}->{type} eq 'start tag') {        } elsif ($self->{next_char} == 0x003E) { # >
658            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
659              !!!cp (35);
660            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
661                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
662            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
663          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
664            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
665            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
666              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
667            }            #  !!! cp (36);
668              #  !!! parse-error (type => 'end tag attribute');
669              #} else {
670                !!!cp (37);
671              #}
672          } else {          } else {
673            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
674          }          }
675          $self->{state} = 'data';          $self->{state} = DATA_STATE;
676          !!!next-input-character;          !!!next-input-character;
677    
678          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
679    
680          redo A;          redo A;
681        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
682                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
683          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
684            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
685            # start tag or end tag            # start tag or end tag
686          ## Stay in this state          ## Stay in this state
687          !!!next-input-character;          !!!next-input-character;
688          redo A;          redo A;
689        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
690          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
691          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
692              !!!cp (39);
693            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
694                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
695            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
696          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
697            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
698            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
699              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
700            }            #  !!! cp (40);
701              #  !!! parse-error (type => 'end tag attribute');
702              #} else {
703                !!!cp (41);
704              #}
705          } else {          } else {
706            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
707          }          }
708          $self->{state} = 'data';          $self->{state} = DATA_STATE;
709          # reconsume          # reconsume
710    
711          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
712    
713          redo A;          redo A;
714        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
715          !!!next-input-character;          !!!next-input-character;
716          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
717              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
718              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
719            # permitted slash            # permitted slash
720              !!!cp (42);
721            #            #
722          } else {          } else {
723              !!!cp (43);
724            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
725          }          }
726          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
727          # next-input-character is already done          # next-input-character is already done
728          redo A;          redo A;
729        } else {        } else {
730          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
731            $self->{current_token}->{tag_name} .= chr $self->{next_char};
732            # start tag or end tag            # start tag or end tag
733          ## Stay in the state          ## Stay in the state
734          !!!next-input-character;          !!!next-input-character;
735          redo A;          redo A;
736        }        }
737      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
738        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
739            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
740            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
741            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
742            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
743            !!!cp (45);
744          ## Stay in the state          ## Stay in the state
745          !!!next-input-character;          !!!next-input-character;
746          redo A;          redo A;
747        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
748          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
749              !!!cp (46);
750            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
751                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
752            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
753          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
754            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
755            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
756                !!!cp (47);
757              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
758              } else {
759                !!!cp (48);
760            }            }
761          } else {          } else {
762            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
763          }          }
764          $self->{state} = 'data';          $self->{state} = DATA_STATE;
765          !!!next-input-character;          !!!next-input-character;
766    
767          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
768    
769          redo A;          redo A;
770        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
771                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
772          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
773            $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),
774                                value => ''};                                value => ''};
775          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
776          !!!next-input-character;          !!!next-input-character;
777          redo A;          redo A;
778        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
779          !!!next-input-character;          !!!next-input-character;
780          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
781              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
782              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
783            # permitted slash            # permitted slash
784              !!!cp (50);
785            #            #
786          } else {          } else {
787              !!!cp (51);
788            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
789          }          }
790          ## Stay in the state          ## Stay in the state
791          # next-input-character is already done          # next-input-character is already done
792          redo A;          redo A;
793        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
794          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
795          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
796              !!!cp (52);
797            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
798                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
799            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
800          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
801            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
802            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
803                !!!cp (53);
804              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
805              } else {
806                !!!cp (54);
807            }            }
808          } else {          } else {
809            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
810          }          }
811          $self->{state} = 'data';          $self->{state} = DATA_STATE;
812          # reconsume          # reconsume
813    
814          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
815    
816          redo A;          redo A;
817        } else {        } else {
818          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
819                 0x0022 => 1, # "
820                 0x0027 => 1, # '
821                 0x003D => 1, # =
822                }->{$self->{next_char}}) {
823              !!!cp (55);
824              !!!parse-error (type => 'bad attribute name');
825            } else {
826              !!!cp (56);
827            }
828            $self->{current_attribute} = {name => chr ($self->{next_char}),
829                                value => ''};                                value => ''};
830          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
831          !!!next-input-character;          !!!next-input-character;
832          redo A;          redo A;
833        }        }
834      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
835        my $before_leave = sub {        my $before_leave = sub {
836          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
837              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
838            !!!parse-error (type => 'duplicate attribute');            !!!cp (57);
839              !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});
840            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
841          } else {          } else {
842              !!!cp (58);
843            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
844              = $self->{current_attribute};              = $self->{current_attribute};
845          }          }
846        }; # $before_leave        }; # $before_leave
847    
848        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
849            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
850            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
851            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
852            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
853            !!!cp (59);
854          $before_leave->();          $before_leave->();
855          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
856          !!!next-input-character;          !!!next-input-character;
857          redo A;          redo A;
858        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
859            !!!cp (60);
860          $before_leave->();          $before_leave->();
861          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
862          !!!next-input-character;          !!!next-input-character;
863          redo A;          redo A;
864        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
865          $before_leave->();          $before_leave->();
866          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
867              !!!cp (61);
868            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
869                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
870            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
871          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
872            $self->{content_model_flag} = 'PCDATA'; # MUST            !!!cp (62);
873              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
874            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
875              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
876            }            }
877          } else {          } else {
878            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
879          }          }
880          $self->{state} = 'data';          $self->{state} = DATA_STATE;
881          !!!next-input-character;          !!!next-input-character;
882    
883          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
884    
885          redo A;          redo A;
886        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
887                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
888          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
889            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
890          ## Stay in the state          ## Stay in the state
891          !!!next-input-character;          !!!next-input-character;
892          redo A;          redo A;
893        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
894          $before_leave->();          $before_leave->();
895          !!!next-input-character;          !!!next-input-character;
896          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
897              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
898              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
899            # permitted slash            # permitted slash
900              !!!cp (64);
901            #            #
902          } else {          } else {
903              !!!cp (65);
904            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
905          }          }
906          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
907          # next-input-character is already done          # next-input-character is already done
908          redo A;          redo A;
909        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
910          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
911          $before_leave->();          $before_leave->();
912          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
913              !!!cp (66);
914            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
915                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
916            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
917          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
918            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
919            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
920                !!!cp (67);
921              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
922              } else {
923                ## NOTE: This state should never be reached.
924                !!!cp (68);
925            }            }
926          } else {          } else {
927            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
928          }          }
929          $self->{state} = 'data';          $self->{state} = DATA_STATE;
930          # reconsume          # reconsume
931    
932          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
933    
934          redo A;          redo A;
935        } else {        } else {
936          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x0022 or # "
937                $self->{next_char} == 0x0027) { # '
938              !!!cp (69);
939              !!!parse-error (type => 'bad attribute name');
940            } else {
941              !!!cp (70);
942            }
943            $self->{current_attribute}->{name} .= chr ($self->{next_char});
944          ## Stay in the state          ## Stay in the state
945          !!!next-input-character;          !!!next-input-character;
946          redo A;          redo A;
947        }        }
948      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
949        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
950            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
951            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
952            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
953            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
954            !!!cp (71);
955          ## Stay in the state          ## Stay in the state
956          !!!next-input-character;          !!!next-input-character;
957          redo A;          redo A;
958        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
959          $self->{state} = 'before attribute value';          !!!cp (72);
960            $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
961          !!!next-input-character;          !!!next-input-character;
962          redo A;          redo A;
963        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
964          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
965              !!!cp (73);
966            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
967                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
968            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
969          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
970            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
971            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
972                !!!cp (74);
973              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
974              } else {
975                ## NOTE: This state should never be reached.
976                !!!cp (75);
977            }            }
978          } else {          } else {
979            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
980          }          }
981          $self->{state} = 'data';          $self->{state} = DATA_STATE;
982          !!!next-input-character;          !!!next-input-character;
983    
984          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
985    
986          redo A;          redo A;
987        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
988                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
989          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
990            $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),
991                                value => ''};                                value => ''};
992          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
993          !!!next-input-character;          !!!next-input-character;
994          redo A;          redo A;
995        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
996          !!!next-input-character;          !!!next-input-character;
997          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
998              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
999              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
1000            # permitted slash            # permitted slash
1001              !!!cp (77);
1002            #            #
1003          } else {          } else {
1004              !!!cp (78);
1005            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
1006            ## TODO: Different error type for <aa / bb> than <aa/>            ## TODO: Different error type for <aa / bb> than <aa/>
1007          }          }
1008          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1009          # next-input-character is already done          # next-input-character is already done
1010          redo A;          redo A;
1011        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1012          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1013          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1014              !!!cp (79);
1015            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1016                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1017            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1018          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1019            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1020            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1021                !!!cp (80);
1022              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1023              } else {
1024                ## NOTE: This state should never be reached.
1025                !!!cp (81);
1026            }            }
1027          } else {          } else {
1028            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1029          }          }
1030          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1031          # reconsume          # reconsume
1032    
1033          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1034    
1035          redo A;          redo A;
1036        } else {        } else {
1037          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          !!!cp (82);
1038            $self->{current_attribute} = {name => chr ($self->{next_char}),
1039                                value => ''};                                value => ''};
1040          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
1041          !!!next-input-character;          !!!next-input-character;
1042          redo A;                  redo A;        
1043        }        }
1044      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1045        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1046            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1047            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1048            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1049            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1050            !!!cp (83);
1051          ## Stay in the state          ## Stay in the state
1052          !!!next-input-character;          !!!next-input-character;
1053          redo A;          redo A;
1054        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1055          $self->{state} = 'attribute value (double-quoted)';          !!!cp (84);
1056            $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1057          !!!next-input-character;          !!!next-input-character;
1058          redo A;          redo A;
1059        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1060          $self->{state} = 'attribute value (unquoted)';          !!!cp (85);
1061            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1062          ## reconsume          ## reconsume
1063          redo A;          redo A;
1064        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1065          $self->{state} = 'attribute value (single-quoted)';          !!!cp (86);
1066            $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1067          !!!next-input-character;          !!!next-input-character;
1068          redo A;          redo A;
1069        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1070          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1071              !!!cp (87);
1072            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1073                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1074            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1075          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1076            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1077            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1078                !!!cp (88);
1079              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1080              } else {
1081                ## NOTE: This state should never be reached.
1082                !!!cp (89);
1083            }            }
1084          } else {          } else {
1085            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1086          }          }
1087          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1088          !!!next-input-character;          !!!next-input-character;
1089    
1090          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1091    
1092          redo A;          redo A;
1093        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1094          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1095          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1096              !!!cp (90);
1097            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1098                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1099            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1100          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1101            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1102            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1103                !!!cp (91);
1104              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1105              } else {
1106                ## NOTE: This state should never be reached.
1107                !!!cp (92);
1108            }            }
1109          } else {          } else {
1110            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1111          }          }
1112          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1113          ## reconsume          ## reconsume
1114    
1115          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1116    
1117          redo A;          redo A;
1118        } else {        } else {
1119          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x003D) { # =
1120          $self->{state} = 'attribute value (unquoted)';            !!!cp (93);
1121              !!!parse-error (type => 'bad attribute value');
1122            } else {
1123              !!!cp (94);
1124            }
1125            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1126            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1127          !!!next-input-character;          !!!next-input-character;
1128          redo A;          redo A;
1129        }        }
1130      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1131        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1132          $self->{state} = 'before attribute name';          !!!cp (95);
1133            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1134          !!!next-input-character;          !!!next-input-character;
1135          redo A;          redo A;
1136        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1137          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          !!!cp (96);
1138          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1139            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1140          !!!next-input-character;          !!!next-input-character;
1141          redo A;          redo A;
1142        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1143          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1144          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1145              !!!cp (97);
1146            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1147                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1148            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1149          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1150            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1151            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1152                !!!cp (98);
1153              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1154              } else {
1155                ## NOTE: This state should never be reached.
1156                !!!cp (99);
1157            }            }
1158          } else {          } else {
1159            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1160          }          }
1161          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1162          ## reconsume          ## reconsume
1163    
1164          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1165    
1166          redo A;          redo A;
1167        } else {        } else {
1168          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1169            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1170          ## Stay in the state          ## Stay in the state
1171          !!!next-input-character;          !!!next-input-character;
1172          redo A;          redo A;
1173        }        }
1174      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1175        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1176          $self->{state} = 'before attribute name';          !!!cp (101);
1177            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1178          !!!next-input-character;          !!!next-input-character;
1179          redo A;          redo A;
1180        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1181          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          !!!cp (102);
1182          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1183            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1184          !!!next-input-character;          !!!next-input-character;
1185          redo A;          redo A;
1186        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1187          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1188          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1189              !!!cp (103);
1190            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1191                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1192            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1193          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1194            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1195            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1196                !!!cp (104);
1197              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1198              } else {
1199                ## NOTE: This state should never be reached.
1200                !!!cp (105);
1201            }            }
1202          } else {          } else {
1203            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1204          }          }
1205          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1206          ## reconsume          ## reconsume
1207    
1208          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1209    
1210          redo A;          redo A;
1211        } else {        } else {
1212          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1213            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1214          ## Stay in the state          ## Stay in the state
1215          !!!next-input-character;          !!!next-input-character;
1216          redo A;          redo A;
1217        }        }
1218      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1219        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1220            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1221            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1222            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1223            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1224          $self->{state} = 'before attribute name';          !!!cp (107);
1225          !!!next-input-character;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1226          redo A;          !!!next-input-character;
1227        } elsif ($self->{next_input_character} == 0x0026) { # &          redo A;
1228          $self->{last_attribute_value_state} = 'attribute value (unquoted)';        } elsif ($self->{next_char} == 0x0026) { # &
1229          $self->{state} = 'entity in attribute value';          !!!cp (108);
1230          !!!next-input-character;          $self->{last_attribute_value_state} = $self->{state};
1231          redo A;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1232        } elsif ($self->{next_input_character} == 0x003E) { # >          !!!next-input-character;
1233          if ($self->{current_token}->{type} eq 'start tag') {          redo A;
1234          } elsif ($self->{next_char} == 0x003E) { # >
1235            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1236              !!!cp (109);
1237            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1238                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1239            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1240          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1241            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1242            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1243                !!!cp (110);
1244              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1245              } else {
1246                ## NOTE: This state should never be reached.
1247                !!!cp (111);
1248            }            }
1249          } else {          } else {
1250            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1251          }          }
1252          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1253          !!!next-input-character;          !!!next-input-character;
1254    
1255          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1256    
1257          redo A;          redo A;
1258        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1259          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1260          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1261              !!!cp (112);
1262            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1263                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1264            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1265          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1266            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1267            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1268                !!!cp (113);
1269              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1270              } else {
1271                ## NOTE: This state should never be reached.
1272                !!!cp (114);
1273            }            }
1274          } else {          } else {
1275            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1276          }          }
1277          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1278          ## reconsume          ## reconsume
1279    
1280          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1281    
1282          redo A;          redo A;
1283        } else {        } else {
1284          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1285                 0x0022 => 1, # "
1286                 0x0027 => 1, # '
1287                 0x003D => 1, # =
1288                }->{$self->{next_char}}) {
1289              !!!cp (115);
1290              !!!parse-error (type => 'bad attribute value');
1291            } else {
1292              !!!cp (116);
1293            }
1294            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1295          ## Stay in the state          ## Stay in the state
1296          !!!next-input-character;          !!!next-input-character;
1297          redo A;          redo A;
1298        }        }
1299      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1300        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);        my $token = $self->_tokenize_attempt_to_consume_an_entity
1301              (1,
1302               $self->{last_attribute_value_state}
1303                 == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "
1304               $self->{last_attribute_value_state}
1305                 == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '
1306               -1);
1307    
1308        unless (defined $token) {        unless (defined $token) {
1309            !!!cp (117);
1310          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1311        } else {        } else {
1312            !!!cp (118);
1313          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1314            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1315          ## 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"
1316        }        }
1317    
1318        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1319        # next-input-character is already done        # next-input-character is already done
1320        redo A;        redo A;
1321      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1322          if ($self->{next_char} == 0x0009 or # HT
1323              $self->{next_char} == 0x000A or # LF
1324              $self->{next_char} == 0x000B or # VT
1325              $self->{next_char} == 0x000C or # FF
1326              $self->{next_char} == 0x0020) { # SP
1327            !!!cp (118);
1328            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1329            !!!next-input-character;
1330            redo A;
1331          } elsif ($self->{next_char} == 0x003E) { # >
1332            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1333              !!!cp (119);
1334              $self->{current_token}->{first_start_tag}
1335                  = not defined $self->{last_emitted_start_tag_name};
1336              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1337            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1338              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1339              if ($self->{current_token}->{attributes}) {
1340                !!!cp (120);
1341                !!!parse-error (type => 'end tag attribute');
1342              } else {
1343                ## NOTE: This state should never be reached.
1344                !!!cp (121);
1345              }
1346            } else {
1347              die "$0: $self->{current_token}->{type}: Unknown token type";
1348            }
1349            $self->{state} = DATA_STATE;
1350            !!!next-input-character;
1351    
1352            !!!emit ($self->{current_token}); # start tag or end tag
1353    
1354            redo A;
1355          } elsif ($self->{next_char} == 0x002F) { # /
1356            !!!next-input-character;
1357            if ($self->{next_char} == 0x003E and # >
1358                $self->{current_token}->{type} == START_TAG_TOKEN and
1359                $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
1360              # permitted slash
1361              !!!cp (122);
1362              #
1363            } else {
1364              !!!cp (123);
1365              !!!parse-error (type => 'nestc');
1366            }
1367            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1368            # next-input-character is already done
1369            redo A;
1370          } else {
1371            !!!cp (124);
1372            !!!parse-error (type => 'no space between attributes');
1373            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1374            ## reconsume
1375            redo A;
1376          }
1377        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1378        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1379                
1380        my $token = {type => 'comment', data => ''};        my $token = {type => COMMENT_TOKEN, data => ''};
1381    
1382        BC: {        BC: {
1383          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
1384            $self->{state} = 'data';            !!!cp (124);
1385              $self->{state} = DATA_STATE;
1386            !!!next-input-character;            !!!next-input-character;
1387    
1388            !!!emit ($token);            !!!emit ($token);
1389    
1390            redo A;            redo A;
1391          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_char} == -1) {
1392            $self->{state} = 'data';            !!!cp (125);
1393              $self->{state} = DATA_STATE;
1394            ## reconsume            ## reconsume
1395    
1396            !!!emit ($token);            !!!emit ($token);
1397    
1398            redo A;            redo A;
1399          } else {          } else {
1400            $token->{data} .= chr ($self->{next_input_character});            !!!cp (126);
1401              $token->{data} .= chr ($self->{next_char});
1402            !!!next-input-character;            !!!next-input-character;
1403            redo BC;            redo BC;
1404          }          }
1405        } # BC        } # BC
1406      } elsif ($self->{state} eq 'markup declaration open') {  
1407          die "$0: _get_next_token: unexpected case [BC]";
1408        } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1409        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1410    
1411        my @next_char;        my @next_char;
1412        push @next_char, $self->{next_input_character};        push @next_char, $self->{next_char};
1413                
1414        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1415          !!!next-input-character;          !!!next-input-character;
1416          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1417          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1418            $self->{current_token} = {type => 'comment', data => ''};            !!!cp (127);
1419            $self->{state} = 'comment start';            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};
1420              $self->{state} = COMMENT_START_STATE;
1421            !!!next-input-character;            !!!next-input-character;
1422            redo A;            redo A;
1423            } else {
1424              !!!cp (128);
1425          }          }
1426        } elsif ($self->{next_input_character} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
1427                 $self->{next_input_character} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
1428          !!!next-input-character;          !!!next-input-character;
1429          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1430          if ($self->{next_input_character} == 0x004F or # O          if ($self->{next_char} == 0x004F or # O
1431              $self->{next_input_character} == 0x006F) { # o              $self->{next_char} == 0x006F) { # o
1432            !!!next-input-character;            !!!next-input-character;
1433            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
1434            if ($self->{next_input_character} == 0x0043 or # C            if ($self->{next_char} == 0x0043 or # C
1435                $self->{next_input_character} == 0x0063) { # c                $self->{next_char} == 0x0063) { # c
1436              !!!next-input-character;              !!!next-input-character;
1437              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
1438              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
1439                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
1440                !!!next-input-character;                !!!next-input-character;
1441                push @next_char, $self->{next_input_character};                push @next_char, $self->{next_char};
1442                if ($self->{next_input_character} == 0x0059 or # Y                if ($self->{next_char} == 0x0059 or # Y
1443                    $self->{next_input_character} == 0x0079) { # y                    $self->{next_char} == 0x0079) { # y
1444                  !!!next-input-character;                  !!!next-input-character;
1445                  push @next_char, $self->{next_input_character};                  push @next_char, $self->{next_char};
1446                  if ($self->{next_input_character} == 0x0050 or # P                  if ($self->{next_char} == 0x0050 or # P
1447                      $self->{next_input_character} == 0x0070) { # p                      $self->{next_char} == 0x0070) { # p
1448                    !!!next-input-character;                    !!!next-input-character;
1449                    push @next_char, $self->{next_input_character};                    push @next_char, $self->{next_char};
1450                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_char} == 0x0045 or # E
1451                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_char} == 0x0065) { # e
1452                      ## ISSUE: What a stupid code this is!                      !!!cp (129);
1453                      $self->{state} = 'DOCTYPE';                      ## TODO: What a stupid code this is!
1454                        $self->{state} = DOCTYPE_STATE;
1455                      !!!next-input-character;                      !!!next-input-character;
1456                      redo A;                      redo A;
1457                      } else {
1458                        !!!cp (130);
1459                    }                    }
1460                    } else {
1461                      !!!cp (131);
1462                  }                  }
1463                  } else {
1464                    !!!cp (132);
1465                }                }
1466                } else {
1467                  !!!cp (133);
1468              }              }
1469              } else {
1470                !!!cp (134);
1471            }            }
1472            } else {
1473              !!!cp (135);
1474          }          }
1475          } else {
1476            !!!cp (136);
1477        }        }
1478    
1479        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment');
1480        $self->{next_input_character} = shift @next_char;        $self->{next_char} = shift @next_char;
1481        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1482        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
1483        redo A;        redo A;
1484                
1485        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1486        ## 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?
1487      } elsif ($self->{state} eq 'comment start') {      } elsif ($self->{state} == COMMENT_START_STATE) {
1488        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1489          $self->{state} = 'comment start dash';          !!!cp (137);
1490            $self->{state} = COMMENT_START_DASH_STATE;
1491          !!!next-input-character;          !!!next-input-character;
1492          redo A;          redo A;
1493        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1494            !!!cp (138);
1495          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1496          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1497          !!!next-input-character;          !!!next-input-character;
1498    
1499          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1500    
1501          redo A;          redo A;
1502        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1503            !!!cp (139);
1504          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1505          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1506          ## reconsume          ## reconsume
1507    
1508          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1509    
1510          redo A;          redo A;
1511        } else {        } else {
1512            !!!cp (140);
1513          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1514              .= chr ($self->{next_input_character});              .= chr ($self->{next_char});
1515          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1516          !!!next-input-character;          !!!next-input-character;
1517          redo A;          redo A;
1518        }        }
1519      } elsif ($self->{state} eq 'comment start dash') {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
1520        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1521          $self->{state} = 'comment end';          !!!cp (141);
1522            $self->{state} = COMMENT_END_STATE;
1523          !!!next-input-character;          !!!next-input-character;
1524          redo A;          redo A;
1525        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1526            !!!cp (142);
1527          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1528          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1529          !!!next-input-character;          !!!next-input-character;
1530    
1531          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1532    
1533          redo A;          redo A;
1534        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1535            !!!cp (143);
1536          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1537          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1538          ## reconsume          ## reconsume
1539    
1540          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1541    
1542          redo A;          redo A;
1543        } else {        } else {
1544            !!!cp (144);
1545          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1546              .= '-' . chr ($self->{next_input_character});              .= '-' . chr ($self->{next_char});
1547          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1548          !!!next-input-character;          !!!next-input-character;
1549          redo A;          redo A;
1550        }        }
1551      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_STATE) {
1552        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1553          $self->{state} = 'comment end dash';          !!!cp (145);
1554            $self->{state} = COMMENT_END_DASH_STATE;
1555          !!!next-input-character;          !!!next-input-character;
1556          redo A;          redo A;
1557        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1558            !!!cp (146);
1559          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1560          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1561          ## reconsume          ## reconsume
1562    
1563          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1564    
1565          redo A;          redo A;
1566        } else {        } else {
1567          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (147);
1568            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1569          ## Stay in the state          ## Stay in the state
1570          !!!next-input-character;          !!!next-input-character;
1571          redo A;          redo A;
1572        }        }
1573      } elsif ($self->{state} eq 'comment end dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
1574        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1575          $self->{state} = 'comment end';          !!!cp (148);
1576            $self->{state} = COMMENT_END_STATE;
1577          !!!next-input-character;          !!!next-input-character;
1578          redo A;          redo A;
1579        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1580            !!!cp (149);
1581          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1582          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1583          ## reconsume          ## reconsume
1584    
1585          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1586    
1587          redo A;          redo A;
1588        } else {        } else {
1589          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
1590          $self->{state} = 'comment';          $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
1591            $self->{state} = COMMENT_STATE;
1592          !!!next-input-character;          !!!next-input-character;
1593          redo A;          redo A;
1594        }        }
1595      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
1596        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
1597          $self->{state} = 'data';          !!!cp (151);
1598            $self->{state} = DATA_STATE;
1599          !!!next-input-character;          !!!next-input-character;
1600    
1601          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1602    
1603          redo A;          redo A;
1604        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
1605            !!!cp (152);
1606          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment');
1607          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
1608          ## Stay in the state          ## Stay in the state
1609          !!!next-input-character;          !!!next-input-character;
1610          redo A;          redo A;
1611        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1612            !!!cp (153);
1613          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1614          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1615          ## reconsume          ## reconsume
1616    
1617          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1618    
1619          redo A;          redo A;
1620        } else {        } else {
1621            !!!cp (154);
1622          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment');
1623          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
1624          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1625          !!!next-input-character;          !!!next-input-character;
1626          redo A;          redo A;
1627        }        }
1628      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
1629        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1630            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1631            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1632            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1633            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1634          $self->{state} = 'before DOCTYPE name';          !!!cp (155);
1635            $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1636          !!!next-input-character;          !!!next-input-character;
1637          redo A;          redo A;
1638        } else {        } else {
1639            !!!cp (156);
1640          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
1641          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1642          ## reconsume          ## reconsume
1643          redo A;          redo A;
1644        }        }
1645      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
1646        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1647            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1648            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1649            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1650            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1651            !!!cp (157);
1652          ## Stay in the state          ## Stay in the state
1653          !!!next-input-character;          !!!next-input-character;
1654          redo A;          redo A;
1655        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1656            !!!cp (158);
1657          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1658          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1659          !!!next-input-character;          !!!next-input-character;
1660    
1661          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});
1662    
1663          redo A;          redo A;
1664        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1665            !!!cp (159);
1666          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1667          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1668          ## reconsume          ## reconsume
1669    
1670          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});
1671    
1672          redo A;          redo A;
1673        } else {        } else {
1674            !!!cp (160);
1675          $self->{current_token}          $self->{current_token}
1676              = {type => 'DOCTYPE',              = {type => DOCTYPE_TOKEN,
1677                 name => chr ($self->{next_input_character}),                 name => chr ($self->{next_char}),
1678                 correct => 1};                 #quirks => 0,
1679                  };
1680  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
1681          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
1682          !!!next-input-character;          !!!next-input-character;
1683          redo A;          redo A;
1684        }        }
1685      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
1686  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
1687        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1688            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1689            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1690            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1691            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1692          $self->{state} = 'after DOCTYPE name';          !!!cp (161);
1693            $self->{state} = AFTER_DOCTYPE_NAME_STATE;
1694          !!!next-input-character;          !!!next-input-character;
1695          redo A;          redo A;
1696        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1697          $self->{state} = 'data';          !!!cp (162);
1698            $self->{state} = DATA_STATE;
1699          !!!next-input-character;          !!!next-input-character;
1700    
1701          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1702    
1703          redo A;          redo A;
1704        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1705            !!!cp (163);
1706          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1707          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1708          ## reconsume          ## reconsume
1709    
1710          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1711          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1712    
1713          redo A;          redo A;
1714        } else {        } else {
1715            !!!cp (164);
1716          $self->{current_token}->{name}          $self->{current_token}->{name}
1717            .= chr ($self->{next_input_character}); # DOCTYPE            .= chr ($self->{next_char}); # DOCTYPE
1718          ## Stay in the state          ## Stay in the state
1719          !!!next-input-character;          !!!next-input-character;
1720          redo A;          redo A;
1721        }        }
1722      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
1723        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1724            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1725            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1726            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1727            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1728            !!!cp (165);
1729          ## Stay in the state          ## Stay in the state
1730          !!!next-input-character;          !!!next-input-character;
1731          redo A;          redo A;
1732        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1733          $self->{state} = 'data';          !!!cp (166);
1734            $self->{state} = DATA_STATE;
1735          !!!next-input-character;          !!!next-input-character;
1736    
1737          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1738    
1739          redo A;          redo A;
1740        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1741            !!!cp (167);
1742          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1743          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1744          ## reconsume          ## reconsume
1745    
1746          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1747          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1748    
1749          redo A;          redo A;
1750        } elsif ($self->{next_input_character} == 0x0050 or # P        } elsif ($self->{next_char} == 0x0050 or # P
1751                 $self->{next_input_character} == 0x0070) { # p                 $self->{next_char} == 0x0070) { # p
1752          !!!next-input-character;          !!!next-input-character;
1753          if ($self->{next_input_character} == 0x0055 or # U          if ($self->{next_char} == 0x0055 or # U
1754              $self->{next_input_character} == 0x0075) { # u              $self->{next_char} == 0x0075) { # u
1755            !!!next-input-character;            !!!next-input-character;
1756            if ($self->{next_input_character} == 0x0042 or # B            if ($self->{next_char} == 0x0042 or # B
1757                $self->{next_input_character} == 0x0062) { # b                $self->{next_char} == 0x0062) { # b
1758              !!!next-input-character;              !!!next-input-character;
1759              if ($self->{next_input_character} == 0x004C or # L              if ($self->{next_char} == 0x004C or # L
1760                  $self->{next_input_character} == 0x006C) { # l                  $self->{next_char} == 0x006C) { # l
1761                !!!next-input-character;                !!!next-input-character;
1762                if ($self->{next_input_character} == 0x0049 or # I                if ($self->{next_char} == 0x0049 or # I
1763                    $self->{next_input_character} == 0x0069) { # i                    $self->{next_char} == 0x0069) { # i
1764                  !!!next-input-character;                  !!!next-input-character;
1765                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_char} == 0x0043 or # C
1766                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_char} == 0x0063) { # c
1767                    $self->{state} = 'before DOCTYPE public identifier';                    !!!cp (168);
1768                      $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1769                    !!!next-input-character;                    !!!next-input-character;
1770                    redo A;                    redo A;
1771                    } else {
1772                      !!!cp (169);
1773                  }                  }
1774                  } else {
1775                    !!!cp (170);
1776                }                }
1777                } else {
1778                  !!!cp (171);
1779              }              }
1780              } else {
1781                !!!cp (172);
1782            }            }
1783            } else {
1784              !!!cp (173);
1785          }          }
1786    
1787          #          #
1788        } elsif ($self->{next_input_character} == 0x0053 or # S        } elsif ($self->{next_char} == 0x0053 or # S
1789                 $self->{next_input_character} == 0x0073) { # s                 $self->{next_char} == 0x0073) { # s
1790          !!!next-input-character;          !!!next-input-character;
1791          if ($self->{next_input_character} == 0x0059 or # Y          if ($self->{next_char} == 0x0059 or # Y
1792              $self->{next_input_character} == 0x0079) { # y              $self->{next_char} == 0x0079) { # y
1793            !!!next-input-character;            !!!next-input-character;
1794            if ($self->{next_input_character} == 0x0053 or # S            if ($self->{next_char} == 0x0053 or # S
1795                $self->{next_input_character} == 0x0073) { # s                $self->{next_char} == 0x0073) { # s
1796              !!!next-input-character;              !!!next-input-character;
1797              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
1798                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
1799                !!!next-input-character;                !!!next-input-character;
1800                if ($self->{next_input_character} == 0x0045 or # E                if ($self->{next_char} == 0x0045 or # E
1801                    $self->{next_input_character} == 0x0065) { # e                    $self->{next_char} == 0x0065) { # e
1802                  !!!next-input-character;                  !!!next-input-character;
1803                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_char} == 0x004D or # M
1804                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_char} == 0x006D) { # m
1805                    $self->{state} = 'before DOCTYPE system identifier';                    !!!cp (174);
1806                      $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1807                    !!!next-input-character;                    !!!next-input-character;
1808                    redo A;                    redo A;
1809                    } else {
1810                      !!!cp (175);
1811                  }                  }
1812                  } else {
1813                    !!!cp (176);
1814                }                }
1815                } else {
1816                  !!!cp (177);
1817              }              }
1818              } else {
1819                !!!cp (178);
1820            }            }
1821            } else {
1822              !!!cp (179);
1823          }          }
1824    
1825          #          #
1826        } else {        } else {
1827            !!!cp (180);
1828          !!!next-input-character;          !!!next-input-character;
1829          #          #
1830        }        }
1831    
1832        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
1833        $self->{state} = 'bogus DOCTYPE';        $self->{current_token}->{quirks} = 1;
1834    
1835          $self->{state} = BOGUS_DOCTYPE_STATE;
1836        # next-input-character is already done        # next-input-character is already done
1837        redo A;        redo A;
1838      } elsif ($self->{state} eq 'before DOCTYPE public identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1839        if ({        if ({
1840              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1841              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
1842            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
1843            !!!cp (181);
1844          ## Stay in the state          ## Stay in the state
1845          !!!next-input-character;          !!!next-input-character;
1846          redo A;          redo A;
1847        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_char} eq 0x0022) { # "
1848            !!!cp (182);
1849          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1850          $self->{state} = 'DOCTYPE public identifier (double-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
1851          !!!next-input-character;          !!!next-input-character;
1852          redo A;          redo A;
1853        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_char} eq 0x0027) { # '
1854            !!!cp (183);
1855          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1856          $self->{state} = 'DOCTYPE public identifier (single-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
1857          !!!next-input-character;          !!!next-input-character;
1858          redo A;          redo A;
1859        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_char} eq 0x003E) { # >
1860            !!!cp (184);
1861          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
1862    
1863          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1864          !!!next-input-character;          !!!next-input-character;
1865    
1866          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1867          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1868    
1869          redo A;          redo A;
1870        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1871            !!!cp (185);
1872          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1873    
1874          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1875          ## reconsume          ## reconsume
1876    
1877          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1878          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1879    
1880          redo A;          redo A;
1881        } else {        } else {
1882            !!!cp (186);
1883          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
1884          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
1885    
1886            $self->{state} = BOGUS_DOCTYPE_STATE;
1887          !!!next-input-character;          !!!next-input-character;
1888          redo A;          redo A;
1889        }        }
1890      } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1891        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1892          $self->{state} = 'after DOCTYPE public identifier';          !!!cp (187);
1893            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1894          !!!next-input-character;          !!!next-input-character;
1895          redo A;          redo A;
1896        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
1897            !!!cp (188);
1898          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1899    
1900          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1901            !!!next-input-character;
1902    
1903            $self->{current_token}->{quirks} = 1;
1904            !!!emit ($self->{current_token}); # DOCTYPE
1905    
1906            redo A;
1907          } elsif ($self->{next_char} == -1) {
1908            !!!cp (189);
1909            !!!parse-error (type => 'unclosed PUBLIC literal');
1910    
1911            $self->{state} = DATA_STATE;
1912          ## reconsume          ## reconsume
1913    
1914          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1915          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1916    
1917          redo A;          redo A;
1918        } else {        } else {
1919            !!!cp (190);
1920          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
1921              .= chr $self->{next_input_character};              .= chr $self->{next_char};
1922          ## Stay in the state          ## Stay in the state
1923          !!!next-input-character;          !!!next-input-character;
1924          redo A;          redo A;
1925        }        }
1926      } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
1927        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1928          $self->{state} = 'after DOCTYPE public identifier';          !!!cp (191);
1929            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1930            !!!next-input-character;
1931            redo A;
1932          } elsif ($self->{next_char} == 0x003E) { # >
1933            !!!cp (192);
1934            !!!parse-error (type => 'unclosed PUBLIC literal');
1935    
1936            $self->{state} = DATA_STATE;
1937          !!!next-input-character;          !!!next-input-character;
1938    
1939            $self->{current_token}->{quirks} = 1;
1940            !!!emit ($self->{current_token}); # DOCTYPE
1941    
1942          redo A;          redo A;
1943        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1944            !!!cp (193);
1945          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1946    
1947          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1948          ## reconsume          ## reconsume
1949    
1950          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1951          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1952    
1953          redo A;          redo A;
1954        } else {        } else {
1955            !!!cp (194);
1956          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
1957              .= chr $self->{next_input_character};              .= chr $self->{next_char};
1958          ## Stay in the state          ## Stay in the state
1959          !!!next-input-character;          !!!next-input-character;
1960          redo A;          redo A;
1961        }        }
1962      } elsif ($self->{state} eq 'after DOCTYPE public identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1963        if ({        if ({
1964              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1965              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
1966            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
1967            !!!cp (195);
1968          ## Stay in the state          ## Stay in the state
1969          !!!next-input-character;          !!!next-input-character;
1970          redo A;          redo A;
1971        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1972            !!!cp (196);
1973          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1974          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1975          !!!next-input-character;          !!!next-input-character;
1976          redo A;          redo A;
1977        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1978            !!!cp (197);
1979          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1980          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1981          !!!next-input-character;          !!!next-input-character;
1982          redo A;          redo A;
1983        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1984          $self->{state} = 'data';          !!!cp (198);
1985            $self->{state} = DATA_STATE;
1986          !!!next-input-character;          !!!next-input-character;
1987    
1988          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1989    
1990          redo A;          redo A;
1991        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1992            !!!cp (199);
1993          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1994    
1995          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1996          ## reconsume          ## reconsume
1997    
1998          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1999          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2000    
2001          redo A;          redo A;
2002        } else {        } else {
2003            !!!cp (200);
2004          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
2005          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2006    
2007            $self->{state} = BOGUS_DOCTYPE_STATE;
2008          !!!next-input-character;          !!!next-input-character;
2009          redo A;          redo A;
2010        }        }
2011      } elsif ($self->{state} eq 'before DOCTYPE system identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2012        if ({        if ({
2013              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2014              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2015            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2016            !!!cp (201);
2017          ## Stay in the state          ## Stay in the state
2018          !!!next-input-character;          !!!next-input-character;
2019          redo A;          redo A;
2020        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2021            !!!cp (202);
2022          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2023          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2024          !!!next-input-character;          !!!next-input-character;
2025          redo A;          redo A;
2026        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2027            !!!cp (203);
2028          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2029          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2030          !!!next-input-character;          !!!next-input-character;
2031          redo A;          redo A;
2032        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2033            !!!cp (204);
2034          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
2035          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2036          !!!next-input-character;          !!!next-input-character;
2037    
2038          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2039          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2040    
2041          redo A;          redo A;
2042        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2043            !!!cp (205);
2044          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2045    
2046          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2047          ## reconsume          ## reconsume
2048    
2049          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2050          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2051    
2052          redo A;          redo A;
2053        } else {        } else {
2054            !!!cp (206);
2055          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
2056          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2057    
2058            $self->{state} = BOGUS_DOCTYPE_STATE;
2059          !!!next-input-character;          !!!next-input-character;
2060          redo A;          redo A;
2061        }        }
2062      } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2063        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2064          $self->{state} = 'after DOCTYPE system identifier';          !!!cp (207);
2065            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2066          !!!next-input-character;          !!!next-input-character;
2067          redo A;          redo A;
2068        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2069            !!!cp (208);
2070            !!!parse-error (type => 'unclosed PUBLIC literal');
2071    
2072            $self->{state} = DATA_STATE;
2073            !!!next-input-character;
2074    
2075            $self->{current_token}->{quirks} = 1;
2076            !!!emit ($self->{current_token}); # DOCTYPE
2077    
2078            redo A;
2079          } elsif ($self->{next_char} == -1) {
2080            !!!cp (209);
2081          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2082    
2083          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2084          ## reconsume          ## reconsume
2085    
2086          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2087          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2088    
2089          redo A;          redo A;
2090        } else {        } else {
2091            !!!cp (210);
2092          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2093              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2094          ## Stay in the state          ## Stay in the state
2095          !!!next-input-character;          !!!next-input-character;
2096          redo A;          redo A;
2097        }        }
2098      } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2099        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2100          $self->{state} = 'after DOCTYPE system identifier';          !!!cp (211);
2101            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2102          !!!next-input-character;          !!!next-input-character;
2103          redo A;          redo A;
2104        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2105            !!!cp (212);
2106            !!!parse-error (type => 'unclosed PUBLIC literal');
2107    
2108            $self->{state} = DATA_STATE;
2109            !!!next-input-character;
2110    
2111            $self->{current_token}->{quirks} = 1;
2112            !!!emit ($self->{current_token}); # DOCTYPE
2113    
2114            redo A;
2115          } elsif ($self->{next_char} == -1) {
2116            !!!cp (213);
2117          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2118    
2119          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2120          ## reconsume          ## reconsume
2121    
2122          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2123          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2124    
2125          redo A;          redo A;
2126        } else {        } else {
2127            !!!cp (214);
2128          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2129              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2130          ## Stay in the state          ## Stay in the state
2131          !!!next-input-character;          !!!next-input-character;
2132          redo A;          redo A;
2133        }        }
2134      } elsif ($self->{state} eq 'after DOCTYPE system identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2135        if ({        if ({
2136              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2137              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2138            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2139            !!!cp (215);
2140          ## Stay in the state          ## Stay in the state
2141          !!!next-input-character;          !!!next-input-character;
2142          redo A;          redo A;
2143        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2144          $self->{state} = 'data';          !!!cp (216);
2145            $self->{state} = DATA_STATE;
2146          !!!next-input-character;          !!!next-input-character;
2147    
2148          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2149    
2150          redo A;          redo A;
2151        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2152            !!!cp (217);
2153          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2154    
2155          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2156          ## reconsume          ## reconsume
2157    
2158          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2159          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2160    
2161          redo A;          redo A;
2162        } else {        } else {
2163            !!!cp (218);
2164          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
2165          $self->{state} = 'bogus DOCTYPE';          #$self->{current_token}->{quirks} = 1;
2166    
2167            $self->{state} = BOGUS_DOCTYPE_STATE;
2168          !!!next-input-character;          !!!next-input-character;
2169          redo A;          redo A;
2170        }        }
2171      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2172        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2173          $self->{state} = 'data';          !!!cp (219);
2174            $self->{state} = DATA_STATE;
2175          !!!next-input-character;          !!!next-input-character;
2176    
         delete $self->{current_token}->{correct};  
2177          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2178    
2179          redo A;          redo A;
2180        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2181            !!!cp (220);
2182          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2183          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2184          ## reconsume          ## reconsume
2185    
         delete $self->{current_token}->{correct};  
2186          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2187    
2188          redo A;          redo A;
2189        } else {        } else {
2190            !!!cp (221);
2191          ## Stay in the state          ## Stay in the state
2192          !!!next-input-character;          !!!next-input-character;
2193          redo A;          redo A;
# Line 1606  sub _get_next_token ($) { Line 2200  sub _get_next_token ($) {
2200    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
2201  } # _get_next_token  } # _get_next_token
2202    
2203  sub _tokenize_attempt_to_consume_an_entity ($$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2204    my ($self, $in_attr) = @_;    my ($self, $in_attr, $additional) = @_;
2205    
2206    if ({    if ({
2207         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2208         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
2209        }->{$self->{next_input_character}}) {         $additional => 1,
2210          }->{$self->{next_char}}) {
2211        !!!cp (1001);
2212      ## Don't consume      ## Don't consume
2213      ## No error      ## No error
2214      return undef;      return undef;
2215    } elsif ($self->{next_input_character} == 0x0023) { # #    } elsif ($self->{next_char} == 0x0023) { # #
2216      !!!next-input-character;      !!!next-input-character;
2217      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_char} == 0x0078 or # x
2218          $self->{next_input_character} == 0x0058) { # X          $self->{next_char} == 0x0058) { # X
2219        my $code;        my $code;
2220        X: {        X: {
2221          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_char};
2222          !!!next-input-character;          !!!next-input-character;
2223          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_char} and
2224              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_char} <= 0x0039) { # 0..9
2225              !!!cp (1002);
2226            $code ||= 0;            $code ||= 0;
2227            $code *= 0x10;            $code *= 0x10;
2228            $code += $self->{next_input_character} - 0x0030;            $code += $self->{next_char} - 0x0030;
2229            redo X;            redo X;
2230          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
2231                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_char} <= 0x0066) { # a..f
2232              !!!cp (1003);
2233            $code ||= 0;            $code ||= 0;
2234            $code *= 0x10;            $code *= 0x10;
2235            $code += $self->{next_input_character} - 0x0060 + 9;            $code += $self->{next_char} - 0x0060 + 9;
2236            redo X;            redo X;
2237          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
2238                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_char} <= 0x0046) { # A..F
2239              !!!cp (1004);
2240            $code ||= 0;            $code ||= 0;
2241            $code *= 0x10;            $code *= 0x10;
2242            $code += $self->{next_input_character} - 0x0040 + 9;            $code += $self->{next_char} - 0x0040 + 9;
2243            redo X;            redo X;
2244          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2245              !!!cp (1005);
2246            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro');
2247            !!!back-next-input-character ($x_char, $self->{next_input_character});            !!!back-next-input-character ($x_char, $self->{next_char});
2248            $self->{next_input_character} = 0x0023; # #            $self->{next_char} = 0x0023; # #
2249            return undef;            return undef;
2250          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_char} == 0x003B) { # ;
2251              !!!cp (1006);
2252            !!!next-input-character;            !!!next-input-character;
2253          } else {          } else {
2254              !!!cp (1007);
2255            !!!parse-error (type => 'no refc');            !!!parse-error (type => 'no refc');
2256          }          }
2257    
2258          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2259              !!!cp (1008);
2260            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
2261            $code = 0xFFFD;            $code = 0xFFFD;
2262          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2263              !!!cp (1009);
2264            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
2265            $code = 0xFFFD;            $code = 0xFFFD;
2266          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2267              !!!cp (1010);
2268            !!!parse-error (type => 'CR character reference');            !!!parse-error (type => 'CR character reference');
2269            $code = 0x000A;            $code = 0x000A;
2270          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2271              !!!cp (1011);
2272            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
2273            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2274          }          }
2275    
2276          return {type => 'character', data => chr $code};          return {type => CHARACTER_TOKEN, data => chr $code,
2277                    has_reference => 1};
2278        } # X        } # X
2279      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_char} and
2280               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
2281        my $code = $self->{next_input_character} - 0x0030;        my $code = $self->{next_char} - 0x0030;
2282        !!!next-input-character;        !!!next-input-character;
2283                
2284        while (0x0030 <= $self->{next_input_character} and        while (0x0030 <= $self->{next_char} and
2285                  $self->{next_input_character} <= 0x0039) { # 0..9                  $self->{next_char} <= 0x0039) { # 0..9
2286            !!!cp (1012);
2287          $code *= 10;          $code *= 10;
2288          $code += $self->{next_input_character} - 0x0030;          $code += $self->{next_char} - 0x0030;
2289                    
2290          !!!next-input-character;          !!!next-input-character;
2291        }        }
2292    
2293        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{next_char} == 0x003B) { # ;
2294            !!!cp (1013);
2295          !!!next-input-character;          !!!next-input-character;
2296        } else {        } else {
2297            !!!cp (1014);
2298          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc');
2299        }        }
2300    
2301        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2302            !!!cp (1015);
2303          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
2304          $code = 0xFFFD;          $code = 0xFFFD;
2305        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
2306            !!!cp (1016);
2307          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
2308          $code = 0xFFFD;          $code = 0xFFFD;
2309        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
2310            !!!cp (1017);
2311          !!!parse-error (type => 'CR character reference');          !!!parse-error (type => 'CR character reference');
2312          $code = 0x000A;          $code = 0x000A;
2313        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2314            !!!cp (1018);
2315          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
2316          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2317        }        }
2318                
2319        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};
2320      } else {      } else {
2321          !!!cp (1019);
2322        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero');
2323        !!!back-next-input-character ($self->{next_input_character});        !!!back-next-input-character ($self->{next_char});
2324        $self->{next_input_character} = 0x0023; # #        $self->{next_char} = 0x0023; # #
2325        return undef;        return undef;
2326      }      }
2327    } elsif ((0x0041 <= $self->{next_input_character} and    } elsif ((0x0041 <= $self->{next_char} and
2328              $self->{next_input_character} <= 0x005A) or              $self->{next_char} <= 0x005A) or
2329             (0x0061 <= $self->{next_input_character} and             (0x0061 <= $self->{next_char} and
2330              $self->{next_input_character} <= 0x007A)) {              $self->{next_char} <= 0x007A)) {
2331      my $entity_name = chr $self->{next_input_character};      my $entity_name = chr $self->{next_char};
2332      !!!next-input-character;      !!!next-input-character;
2333    
2334      my $value = $entity_name;      my $value = $entity_name;
# Line 1723  sub _tokenize_attempt_to_consume_an_enti Line 2338  sub _tokenize_attempt_to_consume_an_enti
2338    
2339      while (length $entity_name < 10 and      while (length $entity_name < 10 and
2340             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
2341             ((0x0041 <= $self->{next_input_character} and # a             ((0x0041 <= $self->{next_char} and # a
2342               $self->{next_input_character} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
2343              (0x0061 <= $self->{next_input_character} and # a              (0x0061 <= $self->{next_char} and # a
2344               $self->{next_input_character} <= 0x007A) or # z               $self->{next_char} <= 0x007A) or # z
2345              (0x0030 <= $self->{next_input_character} and # 0              (0x0030 <= $self->{next_char} and # 0
2346               $self->{next_input_character} <= 0x0039) or # 9               $self->{next_char} <= 0x0039) or # 9
2347              $self->{next_input_character} == 0x003B)) { # ;              $self->{next_char} == 0x003B)) { # ;
2348        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_char};
2349        if (defined $EntityChar->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
2350          if ($self->{next_input_character} == 0x003B) { # ;          if ($self->{next_char} == 0x003B) { # ;
2351              !!!cp (1020);
2352            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
2353            $match = 1;            $match = 1;
2354            !!!next-input-character;            !!!next-input-character;
2355            last;            last;
2356          } else {          } else {
2357              !!!cp (1021);
2358            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
2359            $match = -1;            $match = -1;
2360            !!!next-input-character;            !!!next-input-character;
2361          }          }
2362        } else {        } else {
2363          $value .= chr $self->{next_input_character};          !!!cp (1022);
2364            $value .= chr $self->{next_char};
2365          $match *= 2;          $match *= 2;
2366          !!!next-input-character;          !!!next-input-character;
2367        }        }
2368      }      }
2369            
2370      if ($match > 0) {      if ($match > 0) {
2371        return {type => 'character', data => $value};        !!!cp (1023);
2372          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
2373      } elsif ($match < 0) {      } elsif ($match < 0) {
2374        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc');
2375        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
2376          return {type => 'character', data => '&'.$entity_name};          !!!cp (1024);
2377            return {type => CHARACTER_TOKEN, data => '&'.$entity_name};
2378        } else {        } else {
2379          return {type => 'character', data => $value};          !!!cp (1025);
2380            return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
2381        }        }
2382      } else {      } else {
2383          !!!cp (1026);
2384        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero');
2385        ## NOTE: No characters are consumed in the spec.        ## NOTE: "No characters are consumed" in the spec.
2386        return {type => 'character', data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value};
2387      }      }
2388    } else {    } else {
2389        !!!cp (1027);
2390      ## no characters are consumed      ## no characters are consumed
2391      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero');
2392      return undef;      return undef;
# Line 1803  sub _construct_tree ($) { Line 2426  sub _construct_tree ($) {
2426        
2427    !!!next-token;    !!!next-token;
2428    
   $self->{insertion_mode} = 'before head';  
2429    undef $self->{form_element};    undef $self->{form_element};
2430    undef $self->{head_element};    undef $self->{head_element};
2431    $self->{open_elements} = [];    $self->{open_elements} = [];
2432    undef $self->{inner_html_node};    undef $self->{inner_html_node};
2433    
2434      ## NOTE: The "initial" insertion mode.
2435    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
2436    
2437      ## NOTE: The "before html" insertion mode.
2438    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
2439      $self->{insertion_mode} = BEFORE_HEAD_IM;
2440    
2441      ## NOTE: The "before head" insertion mode and so on.
2442    $self->_tree_construction_main;    $self->_tree_construction_main;
2443  } # _construct_tree  } # _construct_tree
2444    
2445  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
2446    my $self = shift;    my $self = shift;
2447    
2448      ## NOTE: "initial" insertion mode
2449    
2450    INITIAL: {    INITIAL: {
2451      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
2452        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
2453        ## error, switch to a conformance checking mode for another        ## error, switch to a conformance checking mode for another
2454        ## language.        ## language.
# Line 1827  sub _tree_construction_initial ($) { Line 2458  sub _tree_construction_initial ($) {
2458        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
2459            defined $token->{public_identifier} or            defined $token->{public_identifier} or
2460            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
2461            !!!cp ('t1');
2462          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5');
2463        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
2464            !!!cp ('t2');
2465          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
2466          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5');
2467          } else {
2468            !!!cp ('t3');
2469        }        }
2470                
2471        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
# Line 1843  sub _tree_construction_initial ($) { Line 2478  sub _tree_construction_initial ($) {
2478        ## ISSUE: internalSubset = null??        ## ISSUE: internalSubset = null??
2479        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
2480                
2481        if (not $token->{correct} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
2482            !!!cp ('t4');
2483          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
2484        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
2485          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
# Line 1897  sub _tree_construction_initial ($) { Line 2533  sub _tree_construction_initial ($) {
2533            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,
2534            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,
2535            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,
2536              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,
2537              "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,
2538              "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,
2539            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,
2540            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,
2541            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,
# Line 1919  sub _tree_construction_initial ($) { Line 2558  sub _tree_construction_initial ($) {
2558            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,
2559            "HTML" => 1,            "HTML" => 1,
2560          }->{$pubid}) {          }->{$pubid}) {
2561              !!!cp ('t5');
2562            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
2563          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or
2564                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {
2565            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
2566                !!!cp ('t6');
2567              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
2568            } else {            } else {
2569                !!!cp ('t7');
2570              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
2571            }            }
2572          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or
2573                   $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {
2574              !!!cp ('t8');
2575            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
2576            } else {
2577              !!!cp ('t9');
2578          }          }
2579          } else {
2580            !!!cp ('t10');
2581        }        }
2582        if (defined $token->{system_identifier}) {        if (defined $token->{system_identifier}) {
2583          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
2584          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
2585          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") {
2586              ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"
2587            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
2588              !!!cp ('t11');
2589            } else {
2590              !!!cp ('t12');
2591          }          }
2592          } else {
2593            !!!cp ('t13');
2594        }        }
2595                
2596        ## Go to the root element phase.        ## Go to the "before html" insertion mode.
2597        !!!next-token;        !!!next-token;
2598        return;        return;
2599      } elsif ({      } elsif ({
2600                'start tag' => 1,                START_TAG_TOKEN, 1,
2601                'end tag' => 1,                END_TAG_TOKEN, 1,
2602                'end-of-file' => 1,                END_OF_FILE_TOKEN, 1,
2603               }->{$token->{type}}) {               }->{$token->{type}}) {
2604          !!!cp ('t14');
2605        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE');
2606        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2607        ## Go to the root element phase        ## Go to the "before html" insertion mode.
2608        ## reprocess        ## reprocess
2609        return;        return;
2610      } elsif ($token->{type} eq 'character') {      } elsif ($token->{type} == CHARACTER_TOKEN) {
2611        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2612          ## Ignore the token          ## Ignore the token
2613    
2614          unless (length $token->{data}) {          unless (length $token->{data}) {
2615            ## Stay in the phase            !!!cp ('t15');
2616              ## Stay in the insertion mode.
2617            !!!next-token;            !!!next-token;
2618            redo INITIAL;            redo INITIAL;
2619            } else {
2620              !!!cp ('t16');
2621          }          }
2622          } else {
2623            !!!cp ('t17');
2624        }        }
2625    
2626        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE');
2627        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2628        ## Go to the root element phase        ## Go to the "before html" insertion mode.
2629        ## reprocess        ## reprocess
2630        return;        return;
2631      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
2632          !!!cp ('t18');
2633        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2634        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
2635                
2636        ## Stay in the phase.        ## Stay in the insertion mode.
2637        !!!next-token;        !!!next-token;
2638        redo INITIAL;        redo INITIAL;
2639      } else {      } else {
2640        die "$0: $token->{type}: Unknown token";        die "$0: $token->{type}: Unknown token type";
2641      }      }
2642    } # INITIAL    } # INITIAL
2643    
2644      die "$0: _tree_construction_initial: This should be never reached";
2645  } # _tree_construction_initial  } # _tree_construction_initial
2646    
2647  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
2648    my $self = shift;    my $self = shift;
2649    
2650      ## NOTE: "before html" insertion mode.
2651        
2652    B: {    B: {
2653        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
2654            !!!cp ('t19');
2655          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE');
2656          ## Ignore the token          ## Ignore the token
2657          ## Stay in the phase          ## Stay in the insertion mode.
2658          !!!next-token;          !!!next-token;
2659          redo B;          redo B;
2660        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
2661            !!!cp ('t20');
2662          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
2663          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2664          ## Stay in the phase          ## Stay in the insertion mode.
2665          !!!next-token;          !!!next-token;
2666          redo B;          redo B;
2667        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
2668          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2669            ## Ignore the token.            ## Ignore the token.
2670    
2671            unless (length $token->{data}) {            unless (length $token->{data}) {
2672              ## Stay in the phase              !!!cp ('t21');
2673                ## Stay in the insertion mode.
2674              !!!next-token;              !!!next-token;
2675              redo B;              redo B;
2676              } else {
2677                !!!cp ('t22');
2678            }            }
2679            } else {
2680              !!!cp ('t23');
2681          }          }
2682    
2683            $self->{application_cache_selection}->(undef);
2684    
2685          #          #
2686          } elsif ($token->{type} == START_TAG_TOKEN) {
2687            if ($token->{tag_name} eq 'html') {
2688              my $root_element;
2689              !!!create-element ($root_element, $token->{tag_name}, $token->{attributes});
2690              $self->{document}->append_child ($root_element);
2691              push @{$self->{open_elements}}, [$root_element, 'html'];
2692    
2693              if ($token->{attributes}->{manifest}) {
2694                !!!cp ('t24');
2695                $self->{application_cache_selection}
2696                    ->($token->{attributes}->{manifest}->{value});
2697                ## ISSUE: No relative reference resolution?
2698              } else {
2699                !!!cp ('t25');
2700                $self->{application_cache_selection}->(undef);
2701              }
2702    
2703              !!!next-token;
2704              return; ## Go to the "before head" insertion mode.
2705            } else {
2706              !!!cp ('t25.1');
2707              #
2708            }
2709        } elsif ({        } elsif ({
2710                  'start tag' => 1,                  END_TAG_TOKEN, 1,
2711                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
2712                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2713          ## ISSUE: There is an issue in the spec          !!!cp ('t26');
2714          #          #
2715        } else {        } else {
2716          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
2717        }        }
2718        my $root_element; !!!create-element ($root_element, 'html');  
2719        $self->{document}->append_child ($root_element);      my $root_element; !!!create-element ($root_element, 'html');
2720        push @{$self->{open_elements}}, [$root_element, 'html'];      $self->{document}->append_child ($root_element);
2721        ## reprocess      push @{$self->{open_elements}}, [$root_element, 'html'];
2722        #redo B;  
2723        return; ## Go to the main phase.      $self->{application_cache_selection}->(undef);
2724    
2725        ## NOTE: Reprocess the token.
2726        return; ## Go to the "before head" insertion mode.
2727    
2728        ## ISSUE: There is an issue in the spec
2729    } # B    } # B
2730    
2731      die "$0: _tree_construction_root_element: This should never be reached";
2732  } # _tree_construction_root_element  } # _tree_construction_root_element
2733    
2734  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 2050  sub _reset_insertion_mode ($) { Line 2753  sub _reset_insertion_mode ($) {
2753          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
2754            if ($self->{inner_html_node}->[1] eq 'td' or            if ($self->{inner_html_node}->[1] eq 'td' or
2755                $self->{inner_html_node}->[1] eq 'th') {                $self->{inner_html_node}->[1] eq 'th') {
2756                !!!cp ('t27');
2757              #              #
2758            } else {            } else {
2759                !!!cp ('t28');
2760              $node = $self->{inner_html_node};              $node = $self->{inner_html_node};
2761            }            }
2762          }          }
# Line 2059  sub _reset_insertion_mode ($) { Line 2764  sub _reset_insertion_mode ($) {
2764            
2765        ## Step 4..13        ## Step 4..13
2766        my $new_mode = {        my $new_mode = {
2767                        select => 'in select',                        select => IN_SELECT_IM,
2768                        td => 'in cell',                        ## NOTE: |option| and |optgroup| do not set
2769                        th => 'in cell',                        ## insertion mode to "in select" by themselves.
2770                        tr => 'in row',                        td => IN_CELL_IM,
2771                        tbody => 'in table body',                        th => IN_CELL_IM,
2772                        thead => 'in table head',                        tr => IN_ROW_IM,
2773                        tfoot => 'in table foot',                        tbody => IN_TABLE_BODY_IM,
2774                        caption => 'in caption',                        thead => IN_TABLE_BODY_IM,
2775                        colgroup => 'in column group',                        tfoot => IN_TABLE_BODY_IM,
2776                        table => 'in table',                        caption => IN_CAPTION_IM,
2777                        head => 'in body', # not in head!                        colgroup => IN_COLUMN_GROUP_IM,
2778                        body => 'in body',                        table => IN_TABLE_IM,
2779                        frameset => 'in frameset',                        head => IN_BODY_IM, # not in head!
2780                          body => IN_BODY_IM,
2781                          frameset => IN_FRAMESET_IM,
2782                       }->{$node->[1]};                       }->{$node->[1]};
2783        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
2784                
2785        ## Step 14        ## Step 14
2786        if ($node->[1] eq 'html') {        if ($node->[1] eq 'html') {
2787          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
2788            $self->{insertion_mode} = 'before head';            !!!cp ('t29');
2789              $self->{insertion_mode} = BEFORE_HEAD_IM;
2790          } else {          } else {
2791            $self->{insertion_mode} = 'after head';            ## ISSUE: Can this state be reached?
2792              !!!cp ('t30');
2793              $self->{insertion_mode} = AFTER_HEAD_IM;
2794          }          }
2795          return;          return;
2796          } else {
2797            !!!cp ('t31');
2798        }        }
2799                
2800        ## Step 15        ## Step 15
2801        $self->{insertion_mode} = 'in body' and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
2802                
2803        ## Step 16        ## Step 16
2804        $i--;        $i--;
# Line 2095  sub _reset_insertion_mode ($) { Line 2807  sub _reset_insertion_mode ($) {
2807        ## Step 17        ## Step 17
2808        redo S3;        redo S3;
2809      } # S3      } # S3
2810    
2811      die "$0: _reset_insertion_mode: This line should never be reached";
2812  } # _reset_insertion_mode  } # _reset_insertion_mode
2813    
2814  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
2815    my $self = shift;    my $self = shift;
2816    
   my $previous_insertion_mode;  
   
2817    my $active_formatting_elements = [];    my $active_formatting_elements = [];
2818    
2819    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 2118  sub _tree_construction_main ($) { Line 2830  sub _tree_construction_main ($) {
2830      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
2831      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
2832        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
2833            !!!cp ('t32');
2834          return;          return;
2835        }        }
2836      }      }
# Line 2132  sub _tree_construction_main ($) { Line 2845  sub _tree_construction_main ($) {
2845    
2846        ## Step 6        ## Step 6
2847        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
2848            !!!cp ('t33_1');
2849          #          #
2850        } else {        } else {
2851          my $in_open_elements;          my $in_open_elements;
2852          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
2853            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
2854                !!!cp ('t33');
2855              $in_open_elements = 1;              $in_open_elements = 1;
2856              last OE;              last OE;
2857            }            }
2858          }          }
2859          if ($in_open_elements) {          if ($in_open_elements) {
2860              !!!cp ('t34');
2861            #            #
2862          } else {          } else {
2863              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
2864              !!!cp ('t35');
2865            redo S4;            redo S4;
2866          }          }
2867        }        }
# Line 2166  sub _tree_construction_main ($) { Line 2884  sub _tree_construction_main ($) {
2884    
2885        ## Step 11        ## Step 11
2886        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
2887            !!!cp ('t36');
2888          ## Step 7'          ## Step 7'
2889          $i++;          $i++;
2890          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
2891                    
2892          redo S7;          redo S7;
2893        }        }
2894    
2895          !!!cp ('t37');
2896      } # S7      } # S7
2897    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
2898    
2899    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
2900      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
2901        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
2902            !!!cp ('t38');
2903          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
2904          return;          return;
2905        }        }
2906      }      }
2907    
2908        !!!cp ('t39');
2909    }; # $clear_up_to_marker    }; # $clear_up_to_marker
2910    
2911    my $parse_rcdata = sub ($$) {    my $parse_rcdata = sub ($$) {
# Line 2196  sub _tree_construction_main ($) { Line 2920  sub _tree_construction_main ($) {
2920      $insert->($el); # /context node/->append_child ($el)      $insert->($el); # /context node/->append_child ($el)
2921    
2922      ## Step 3      ## Step 3
2923      $self->{content_model_flag} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
2924      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
2925    
2926      ## Step 4      ## Step 4
2927      my $text = '';      my $text = '';
2928      !!!next-token;      !!!next-token;
2929      while ($token->{type} eq 'character') { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
2930          !!!cp ('t40');
2931        $text .= $token->{data};        $text .= $token->{data};
2932        !!!next-token;        !!!next-token;
2933      }      }
2934    
2935      ## Step 5      ## Step 5
2936      if (length $text) {      if (length $text) {
2937          !!!cp ('t41');
2938        my $text = $self->{document}->create_text_node ($text);        my $text = $self->{document}->create_text_node ($text);
2939        $el->append_child ($text);        $el->append_child ($text);
2940      }      }
2941    
2942      ## Step 6      ## Step 6
2943      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
2944    
2945      ## Step 7      ## Step 7
2946      if ($token->{type} eq 'end tag' and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} == END_TAG_TOKEN and
2947            $token->{tag_name} eq $start_tag_name) {
2948          !!!cp ('t42');
2949        ## Ignore the token        ## Ignore the token
2950        } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {
2951          !!!cp ('t43');
2952          !!!parse-error (type => 'in CDATA:#'.$token->{type});
2953        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
2954          !!!cp ('t44');
2955          !!!parse-error (type => 'in RCDATA:#'.$token->{type});
2956      } else {      } else {
2957        !!!parse-error (type => 'in '.$content_model_flag.':#'.$token->{type});        die "$0: $content_model_flag in parse_rcdata";
2958      }      }
2959      !!!next-token;      !!!next-token;
2960    }; # $parse_rcdata    }; # $parse_rcdata
# Line 2231  sub _tree_construction_main ($) { Line 2965  sub _tree_construction_main ($) {
2965      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes});
2966      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
2967    
2968      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
2969      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
2970            
2971      my $text = '';      my $text = '';
2972      !!!next-token;      !!!next-token;
2973      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
2974          !!!cp ('t45');
2975        $text .= $token->{data};        $text .= $token->{data};
2976        !!!next-token;        !!!next-token;
2977      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
2978      if (length $text) {      if (length $text) {
2979          !!!cp ('t46');
2980        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
2981      }      }
2982                                
2983      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
2984    
2985      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
2986          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
2987          !!!cp ('t47');
2988        ## Ignore the token        ## Ignore the token
2989      } else {      } else {
2990          !!!cp ('t48');
2991        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type});
2992        ## ISSUE: And ignore?        ## ISSUE: And ignore?
2993        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
2994      }      }
2995            
2996      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
2997          !!!cp ('t49');
2998        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
2999      } else {      } else {
3000          !!!cp ('t50');
3001        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
3002        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
3003    
# Line 2280  sub _tree_construction_main ($) { Line 3020  sub _tree_construction_main ($) {
3020        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3021        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3022          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {
3023              !!!cp ('t51');
3024            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3025            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3026            last AFE;            last AFE;
3027          } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {          } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {
3028              !!!cp ('t52');
3029            last AFE;            last AFE;
3030          }          }
3031        } # AFE        } # AFE
3032        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3033            !!!cp ('t53');
3034          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!parse-error (type => 'unmatched end tag:'.$tag_name);
3035          ## Ignore the token          ## Ignore the token
3036          !!!next-token;          !!!next-token;
# Line 2300  sub _tree_construction_main ($) { Line 3043  sub _tree_construction_main ($) {
3043          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3044          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
3045            if ($in_scope) {            if ($in_scope) {
3046                !!!cp ('t54');
3047              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
3048              last INSCOPE;              last INSCOPE;
3049            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3050                !!!cp ('t55');
3051              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3052              ## Ignore the token              ## Ignore the token
3053              !!!next-token;              !!!next-token;
# Line 2312  sub _tree_construction_main ($) { Line 3057  sub _tree_construction_main ($) {
3057                    table => 1, caption => 1, td => 1, th => 1,                    table => 1, caption => 1, td => 1, th => 1,
3058                    button => 1, marquee => 1, object => 1, html => 1,                    button => 1, marquee => 1, object => 1, html => 1,
3059                   }->{$node->[1]}) {                   }->{$node->[1]}) {
3060              !!!cp ('t56');
3061            $in_scope = 0;            $in_scope = 0;
3062          }          }
3063        } # INSCOPE        } # INSCOPE
3064        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3065            !!!cp ('t57');
3066          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3067          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3068          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3069          return;          return;
3070        }        }
3071        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3072            !!!cp ('t58');
3073          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3074        }        }
3075                
# Line 2334  sub _tree_construction_main ($) { Line 3082  sub _tree_construction_main ($) {
3082              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3083              ($special_category->{$node->[1]} or              ($special_category->{$node->[1]} or
3084               $scoping_category->{$node->[1]})) {               $scoping_category->{$node->[1]})) {
3085              !!!cp ('t59');
3086            $furthest_block = $node;            $furthest_block = $node;
3087            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
3088          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
3089              !!!cp ('t60');
3090            last OE;            last OE;
3091          }          }
3092        } # OE        } # OE
3093                
3094        ## Step 3        ## Step 3
3095        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
3096            !!!cp ('t61');
3097          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
3098          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
3099          !!!next-token;          !!!next-token;
# Line 2355  sub _tree_construction_main ($) { Line 3106  sub _tree_construction_main ($) {
3106        ## Step 5        ## Step 5
3107        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
3108        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
3109            !!!cp ('t62');
3110          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
3111        }        }
3112                
# Line 2377  sub _tree_construction_main ($) { Line 3129  sub _tree_construction_main ($) {
3129          S7S2: {          S7S2: {
3130            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
3131              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
3132                  !!!cp ('t63');
3133                $node_i_in_active = $_;                $node_i_in_active = $_;
3134                last S7S2;                last S7S2;
3135              }              }
# Line 2390  sub _tree_construction_main ($) { Line 3143  sub _tree_construction_main ($) {
3143                    
3144          ## Step 4          ## Step 4
3145          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
3146              !!!cp ('t64');
3147            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
3148          }          }
3149                    
3150          ## Step 5          ## Step 5
3151          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
3152              !!!cp ('t65');
3153            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
3154            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
3155            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2429  sub _tree_construction_main ($) { Line 3184  sub _tree_construction_main ($) {
3184        my $i;        my $i;
3185        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3186          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
3187              !!!cp ('t66');
3188            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
3189            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
3190          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
3191              !!!cp ('t67');
3192            $i = $_;            $i = $_;
3193          }          }
3194        } # AFE        } # AFE
# Line 2441  sub _tree_construction_main ($) { Line 3198  sub _tree_construction_main ($) {
3198        undef $i;        undef $i;
3199        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3200          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
3201              !!!cp ('t68');
3202            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
3203            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
3204          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
3205              !!!cp ('t69');
3206            $i = $_;            $i = $_;
3207          }          }
3208        } # OE        } # OE
# Line 2471  sub _tree_construction_main ($) { Line 3230  sub _tree_construction_main ($) {
3230                             if ($self->{open_elements}->[$_]->[1] eq 'table') {                             if ($self->{open_elements}->[$_]->[1] eq 'table') {
3231                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3232                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3233                                   !!!cp ('t70');
3234                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
3235                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
3236                               } else {                               } else {
3237                                   !!!cp ('t71');
3238                                 $foster_parent_element                                 $foster_parent_element
3239                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
3240                               }                               }
# Line 2485  sub _tree_construction_main ($) { Line 3246  sub _tree_construction_main ($) {
3246                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3247                             ($child, $next_sibling);                             ($child, $next_sibling);
3248                         } else {                         } else {
3249                             !!!cp ('t72');
3250                           $self->{open_elements}->[-1]->[0]->append_child ($child);                           $self->{open_elements}->[-1]->[0]->append_child ($child);
3251                         }                         }
3252    }; # $insert_to_foster    }; # $insert_to_foster
3253    
3254    my $in_body = sub {    my $insert;
     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;  
       }  
     }  
   }; # $in_body  
3255    
3256    B: {    B: {
3257      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
3258          !!!cp ('t73');
3259        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'DOCTYPE in the middle');
3260        ## Ignore the token        ## Ignore the token
3261        ## Stay in the phase        ## Stay in the phase
3262        !!!next-token;        !!!next-token;
3263        redo B;        redo B;
3264      } elsif ($token->{type} eq 'end-of-file') {      } elsif ($token->{type} == END_OF_FILE_TOKEN) {
3265        if ($token->{insertion_mode} ne 'trailing end') {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
3266            !!!cp ('t74');
3267            #
3268          } else {
3269          ## Generate implied end tags          ## Generate implied end tags
3270          if ({          if ({
3271               dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,               dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,
3272               tbody => 1, tfoot=> 1, thead => 1,               tbody => 1, tfoot=> 1, thead => 1,
3273              }->{$self->{open_elements}->[-1]->[1]}) {              }->{$self->{open_elements}->[-1]->[1]}) {
3274              !!!cp ('t75');
3275            !!!back-token;            !!!back-token;
3276            $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};            $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};
3277            redo B;            redo B;
3278          }          }
3279                    
3280          if (@{$self->{open_elements}} > 2 or          if (@{$self->{open_elements}} > 2 or
3281              (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {              (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {
3282              !!!cp ('t76');
3283            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3284          } elsif (defined $self->{inner_html_node} and          } elsif (defined $self->{inner_html_node} and
3285                   @{$self->{open_elements}} > 1 and                   @{$self->{open_elements}} > 1 and
3286                   $self->{open_elements}->[1]->[1] ne 'body') {                   $self->{open_elements}->[1]->[1] ne 'body') {
3287    ## ISSUE: This case is never reached.
3288              !!!cp ('t77');
3289            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3290            } else {
3291              !!!cp ('t78');
3292          }          }
3293    
3294          ## ISSUE: There is an issue in the spec.          ## ISSUE: There is an issue in the spec.
# Line 3367  sub _tree_construction_main ($) { Line 3296  sub _tree_construction_main ($) {
3296    
3297        ## Stop parsing        ## Stop parsing
3298        last B;        last B;
3299      } elsif ($token->{type} eq 'start tag' and      } elsif ($token->{type} == START_TAG_TOKEN and
3300               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
3301        if ($self->{insertion_mode} eq 'trailing end') {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3302          ## Turn into the main phase          !!!cp ('t79');
3303            !!!parse-error (type => 'after html:html');
3304            $self->{insertion_mode} = AFTER_BODY_IM;
3305          } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3306            !!!cp ('t80');
3307          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html');
3308          $self->{insertion_mode} = $previous_insertion_mode;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
3309          } else {
3310            !!!cp ('t81');
3311        }        }
3312    
3313  ## ISSUE: "aa<html>" is not a parse error.        !!!cp ('t82');
3314  ## 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');  
       }  
3315        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
3316        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
3317          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
3318              !!!cp ('t84');
3319            $top_el->set_attribute_ns            $top_el->set_attribute_ns
3320              (undef, [undef, $attr_name],              (undef, [undef, $attr_name],
3321               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
# Line 3390  sub _tree_construction_main ($) { Line 3323  sub _tree_construction_main ($) {
3323        }        }
3324        !!!next-token;        !!!next-token;
3325        redo B;        redo B;
3326      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
3327        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3328        if ($self->{insertion_mode} eq 'trailing end') {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
3329            !!!cp ('t85');
3330          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3331        } elsif ($self->{insertion_mode} eq 'after body') {        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
3332            !!!cp ('t86');
3333          $self->{open_elements}->[0]->[0]->append_child ($comment);          $self->{open_elements}->[0]->[0]->append_child ($comment);
3334        } else {        } else {
3335            !!!cp ('t87');
3336          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
3337        }        }
3338        !!!next-token;        !!!next-token;
3339        redo B;        redo B;
3340      } elsif ($self->{insertion_mode} eq 'before head') {      } elsif ($self->{insertion_mode} & HEAD_IMS) {
3341            if ($token->{type} eq 'character') {        if ($token->{type} == CHARACTER_TOKEN) {
3342              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3343                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3344                unless (length $token->{data}) {            unless (length $token->{data}) {
3345                  !!!next-token;              !!!cp ('t88');
3346                  redo B;              !!!next-token;
3347                }              redo B;
3348              }            }
3349              ## As if <head>          }
3350              !!!create-element ($self->{head_element}, 'head');  
3351              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3352              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            !!!cp ('t89');
3353              $self->{insertion_mode} = 'in head';            ## As if <head>
3354              !!!create-element ($self->{head_element}, 'head');
3355              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3356              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3357    
3358              ## Reprocess in the "in head" insertion mode...
3359              pop @{$self->{open_elements}};
3360    
3361              ## Reprocess in the "after head" insertion mode...
3362            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3363              !!!cp ('t90');
3364              ## As if </noscript>
3365              pop @{$self->{open_elements}};
3366              !!!parse-error (type => 'in noscript:#character');
3367              
3368              ## Reprocess in the "in head" insertion mode...
3369              ## As if </head>
3370              pop @{$self->{open_elements}};
3371    
3372              ## Reprocess in the "after head" insertion mode...
3373            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3374              !!!cp ('t91');
3375              pop @{$self->{open_elements}};
3376    
3377              ## Reprocess in the "after head" insertion mode...
3378            } else {
3379              !!!cp ('t92');
3380            }
3381    
3382                ## "after head" insertion mode
3383                ## As if <body>
3384                !!!insert-element ('body');
3385                $self->{insertion_mode} = IN_BODY_IM;
3386              ## reprocess              ## reprocess
3387              redo B;              redo B;
3388            } 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';  
3389              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
3390                !!!next-token;                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3391              #} elsif ({                  !!!cp ('t93');
3392              #          base => 1, link => 1, meta => 1,                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
3393              #          script => 1, style => 1, title => 1,                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3394              #         }->{$token->{tag_name}}) {                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
3395              #  ## reprocess                  $self->{insertion_mode} = IN_HEAD_IM;
3396              } else {                  !!!next-token;
3397                ## reprocess                  redo B;
3398              }                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3399              redo B;                  !!!cp ('t94');
3400            } elsif ($token->{type} eq 'end tag') {                  #
3401              if ({                } else {
3402                   head => 1, body => 1, html => 1,                  !!!cp ('t95');
3403                   p => 1, br => 1,                  !!!parse-error (type => 'in head:head'); # or in head noscript
3404                  }->{$token->{tag_name}}) {                  ## Ignore the token
3405                    !!!next-token;
3406                    redo B;
3407                  }
3408                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3409                  !!!cp ('t96');
3410                ## As if <head>                ## As if <head>
3411                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head');
3412                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3413                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3414                $self->{insertion_mode} = 'in head';  
3415                ## reprocess                $self->{insertion_mode} = IN_HEAD_IM;
3416                redo B;                ## Reprocess in the "in head" insertion mode...
3417              } else {              } else {
3418                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t97');
               ## Ignore the token ## ISSUE: An issue in the spec.  
               !!!next-token;  
               redo B;  
3419              }              }
3420            } else {  
3421              die "$0: $token->{type}: Unknown type";              if ($token->{tag_name} eq 'base') {
3422            }                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3423          } elsif ($self->{insertion_mode} eq 'in head' or                  !!!cp ('t98');
3424                   $self->{insertion_mode} eq 'in head noscript' or                  ## As if </noscript>
3425                   $self->{insertion_mode} eq 'after head') {                  pop @{$self->{open_elements}};
3426            if ($token->{type} eq 'character') {                  !!!parse-error (type => 'in noscript:base');
3427              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                
3428                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                  $self->{insertion_mode} = IN_HEAD_IM;
3429                unless (length $token->{data}) {                  ## Reprocess in the "in head" insertion mode...
3430                  !!!next-token;                } else {
3431                  redo B;                  !!!cp ('t99');
3432                }                }
3433              }  
               
             #  
           } 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}}) {  
3434                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3435                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3436                    !!!cp ('t100');
3437                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3438                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3439                  } else {
3440                    !!!cp ('t101');
3441                }                }
3442                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3443                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3444                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
3445                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3446                !!!next-token;                !!!next-token;
3447                redo B;                redo B;
3448              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'link') {
3449                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3450                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3451                    !!!cp ('t102');
3452                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3453                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3454                  } else {
3455                    !!!cp ('t103');
3456                }                }
3457                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3458                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3459                  pop @{$self->{open_elements}}
3460                      if $self->{insertion_mode} == AFTER_HEAD_IM;
3461                  !!!next-token;
3462                  redo B;
3463                } elsif ($token->{tag_name} eq 'meta') {
3464                  ## NOTE: There is a "as if in head" code clone.
3465                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3466                    !!!cp ('t104');
3467                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3468                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3469                  } else {
3470                    !!!cp ('t105');
3471                  }
3472                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3473                  my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3474    
3475                unless ($self->{confident}) {                unless ($self->{confident}) {
                 my $charset;  
3476                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
3477                    $charset = $token->{attributes}->{charset}->{value};                    !!!cp ('t106');
3478                  }                    $self->{change_encoding}
3479                  if ($token->{attributes}->{'http-equiv'}) {                        ->($self, $token->{attributes}->{charset}->{value});
3480                      
3481                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3482                          ->set_user_data (manakai_has_reference =>
3483                                               $token->{attributes}->{charset}
3484                                                   ->{has_reference});
3485                    } elsif ($token->{attributes}->{content}) {
3486                    ## 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.
3487                    if ($token->{attributes}->{'http-equiv'}->{value}                    if ($token->{attributes}->{content}->{value}
3488                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
3489                              [\x09-\x0D\x20]*=
3490                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
3491                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
3492                      $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                      !!!cp ('t107');
3493                    } ## TODO: And if supported                      $self->{change_encoding}
3494                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
3495                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3496                            ->set_user_data (manakai_has_reference =>
3497                                                 $token->{attributes}->{content}
3498                                                       ->{has_reference});
3499                      } else {
3500                        !!!cp ('t108');
3501                      }
3502                    }
3503                  } else {
3504                    if ($token->{attributes}->{charset}) {
3505                      !!!cp ('t109');
3506                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3507                          ->set_user_data (manakai_has_reference =>
3508                                               $token->{attributes}->{charset}
3509                                                   ->{has_reference});
3510                    }
3511                    if ($token->{attributes}->{content}) {
3512                      !!!cp ('t110');
3513                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3514                          ->set_user_data (manakai_has_reference =>
3515                                               $token->{attributes}->{content}
3516                                                   ->{has_reference});
3517                  }                  }
                 ## TODO: Change the encoding  
3518                }                }
3519    
               ## TODO: Extracting |charset| from |meta|.  
3520                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
3521                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3522                !!!next-token;                !!!next-token;
3523                redo B;                redo B;
3524              } elsif ($token->{tag_name} eq 'title' and              } elsif ($token->{tag_name} eq 'title') {
3525                       $self->{insertion_mode} eq 'in head') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3526                ## NOTE: There is a "as if in head" code clone.                  !!!cp ('t111');
3527                if ($self->{insertion_mode} eq 'after head') {                  ## As if </noscript>
3528                    pop @{$self->{open_elements}};
3529                    !!!parse-error (type => 'in noscript:title');
3530                  
3531                    $self->{insertion_mode} = IN_HEAD_IM;
3532                    ## Reprocess in the "in head" insertion mode...
3533                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3534                    !!!cp ('t112');
3535                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3536                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3537                  } else {
3538                    !!!cp ('t113');
3539                }                }
3540    
3541                  ## NOTE: There is a "as if in head" code clone.
3542                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
3543                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
3544                $parse_rcdata->('RCDATA', sub { $parent->append_child ($_[0]) });                $parse_rcdata->(RCDATA_CONTENT_MODEL,
3545                                  sub { $parent->append_child ($_[0]) });
3546                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
3547                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3548                redo B;                redo B;
3549              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
3550                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
3551                ## insertion mode 'in head')                ## insertion mode IN_HEAD_IM)
3552                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3553                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3554                    !!!cp ('t114');
3555                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3556                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3557                  } else {
3558                    !!!cp ('t115');
3559                }                }
3560                $parse_rcdata->('CDATA', $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
3561                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
3562                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3563                redo B;                redo B;
3564              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
3565                if ($self->{insertion_mode} eq 'in head') {                if ($self->{insertion_mode} == IN_HEAD_IM) {
3566                    !!!cp ('t116');
3567                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
3568                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3569                  $self->{insertion_mode} = 'in head noscript';                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
3570                  !!!next-token;                  !!!next-token;
3571                  redo B;                  redo B;
3572                } elsif ($self->{insertion_mode} eq 'in head noscript') {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3573                    !!!cp ('t117');
3574                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript:noscript');
3575                  ## Ignore the token                  ## Ignore the token
3576                    !!!next-token;
3577                  redo B;                  redo B;
3578                } else {                } else {
3579                    !!!cp ('t118');
3580                  #                  #
3581                }                }
3582              } elsif ($token->{tag_name} eq 'head' and              } elsif ($token->{tag_name} eq 'script') {
3583                       $self->{insertion_mode} ne 'after head') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3584                !!!parse-error (type => 'in head:head'); # or in head noscript                  !!!cp ('t119');
3585                ## Ignore the token                  ## As if </noscript>
3586                !!!next-token;                  pop @{$self->{open_elements}};
3587                redo B;                  !!!parse-error (type => 'in noscript:script');
3588              } elsif ($self->{insertion_mode} ne 'in head noscript' and                
3589                       $token->{tag_name} eq 'script') {                  $self->{insertion_mode} = IN_HEAD_IM;
3590                if ($self->{insertion_mode} eq 'after head') {                  ## Reprocess in the "in head" insertion mode...
3591                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3592                    !!!cp ('t120');
3593                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3594                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3595                  } else {
3596                    !!!cp ('t121');
3597                }                }
3598    
3599                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3600                $script_start_tag->($insert_to_current);                $script_start_tag->($insert_to_current);
3601                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
3602                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3603                redo B;                redo B;
3604              } 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  
3605                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
3606                !!!insert-element ('frameset', $token->{attributes});                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3607                $self->{insertion_mode} = 'in frameset';                  !!!cp ('t122');
3608                    ## As if </noscript>
3609                    pop @{$self->{open_elements}};
3610                    !!!parse-error (type => 'in noscript:'.$token->{tag_name});
3611                    
3612                    ## Reprocess in the "in head" insertion mode...
3613                    ## As if </head>
3614                    pop @{$self->{open_elements}};
3615                    
3616                    ## Reprocess in the "after head" insertion mode...
3617                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3618                    !!!cp ('t124');
3619                    pop @{$self->{open_elements}};
3620                    
3621                    ## Reprocess in the "after head" insertion mode...
3622                  } else {
3623                    !!!cp ('t125');
3624                  }
3625    
3626                  ## "after head" insertion mode
3627                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3628                  if ($token->{tag_name} eq 'body') {
3629                    !!!cp ('t126');
3630                    $self->{insertion_mode} = IN_BODY_IM;
3631                  } elsif ($token->{tag_name} eq 'frameset') {
3632                    !!!cp ('t127');
3633                    $self->{insertion_mode} = IN_FRAMESET_IM;
3634                  } else {
3635                    die "$0: tag name: $self->{tag_name}";
3636                  }
3637                !!!next-token;                !!!next-token;
3638                redo B;                redo B;
3639              } else {              } else {
3640                  !!!cp ('t128');
3641                #                #
3642              }              }
3643            } elsif ($token->{type} eq 'end tag') {  
3644              if ($self->{insertion_mode} eq 'in head' and              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3645                  $token->{tag_name} eq 'head') {                !!!cp ('t129');
3646                  ## As if </noscript>
3647                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3648                $self->{insertion_mode} = 'after head';                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
3649                !!!next-token;                
3650                redo B;                ## Reprocess in the "in head" insertion mode...
3651              } elsif ($self->{insertion_mode} eq 'in head noscript' and                ## As if </head>
                 $token->{tag_name} eq 'noscript') {  
3652                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3653                $self->{insertion_mode} = 'in head';  
3654                !!!next-token;                ## Reprocess in the "after head" insertion mode...
3655                redo B;              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3656              } elsif ($self->{insertion_mode} eq 'in head' and                !!!cp ('t130');
3657                       {                ## As if </head>
3658                        body => 1, html => 1,                pop @{$self->{open_elements}};
3659                        p => 1, br => 1,  
3660                       }->{$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;  
3661              } else {              } else {
3662                #                !!!cp ('t131');
3663              }              }
           } else {  
             #  
           }  
3664    
3665            ## As if </head> or </noscript> or <body>              ## "after head" insertion mode
3666            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'  
3667              !!!insert-element ('body');              !!!insert-element ('body');
3668              $self->{insertion_mode} = 'in body';              $self->{insertion_mode} = IN_BODY_IM;
3669            }              ## 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);  
3670              redo B;              redo B;
3671            }            } elsif ($token->{type} == END_TAG_TOKEN) {
3672          } elsif ($self->{insertion_mode} eq 'in table') {              if ($token->{tag_name} eq 'head') {
3673            if ($token->{type} eq 'character') {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3674              ## NOTE: There are "character in table" code clones.                  !!!cp ('t132');
3675              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                  ## As if <head>
3676                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                  !!!create-element ($self->{head_element}, 'head');
3677                                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3678                unless (length $token->{data}) {                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3679    
3680                    ## Reprocess in the "in head" insertion mode...
3681                    pop @{$self->{open_elements}};
3682                    $self->{insertion_mode} = AFTER_HEAD_IM;
3683                    !!!next-token;
3684                    redo B;
3685                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3686                    !!!cp ('t133');
3687                    ## As if </noscript>
3688                    pop @{$self->{open_elements}};
3689                    !!!parse-error (type => 'in noscript:/head');
3690                    
3691                    ## Reprocess in the "in head" insertion mode...
3692                    pop @{$self->{open_elements}};
3693                    $self->{insertion_mode} = AFTER_HEAD_IM;
3694                    !!!next-token;
3695                    redo B;
3696                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3697                    !!!cp ('t134');
3698                    pop @{$self->{open_elements}};
3699                    $self->{insertion_mode} = AFTER_HEAD_IM;
3700                  !!!next-token;                  !!!next-token;
3701                  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});  
3702                } else {                } else {
3703                  $foster_parent_element->insert_before                  !!!cp ('t135');
3704                    ($self->{document}->create_text_node ($token->{data}),                  #
                    $next_sibling);  
3705                }                }
3706              } else {              } elsif ($token->{tag_name} eq 'noscript') {
3707                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3708              }                  !!!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]);  
3709                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3710                    $self->{insertion_mode} = IN_HEAD_IM;
3711                    !!!next-token;
3712                    redo B;
3713                  } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3714                    !!!cp ('t137');
3715                    !!!parse-error (type => 'unmatched end tag:noscript');
3716                    ## Ignore the token ## ISSUE: An issue in the spec.
3717                    !!!next-token;
3718                    redo B;
3719                  } else {
3720                    !!!cp ('t138');
3721                    #
3722                }                }
   
               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;  
3723              } elsif ({              } elsif ({
3724                        col => 1,                        body => 1, html => 1,
                       td => 1, th => 1, tr => 1,  
3725                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3726                ## Clear back to table context                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3727                while ($self->{open_elements}->[-1]->[1] ne 'table' and                  !!!cp ('t139');
3728                       $self->{open_elements}->[-1]->[1] ne 'html') {                  ## As if <head>
3729                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!create-element ($self->{head_element}, 'head');
3730                  pop @{$self->{open_elements}};                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3731                }                  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]);  
3732    
3733                ## As if </table>                  $self->{insertion_mode} = IN_HEAD_IM;
3734                ## have a table element in table scope                  ## Reprocess in the "in head" insertion mode...
3735                my $i;                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3736                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  !!!cp ('t140');
3737                  my $node = $self->{open_elements}->[$_];                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3738                  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>  
3739                  !!!next-token;                  !!!next-token;
3740                  redo B;                  redo B;
3741                  } else {
3742                    !!!cp ('t141');
3743                }                }
3744                                
3745                ## generate implied end tags                #
3746                if ({              } elsif ({
3747                     dd => 1, dt => 1, li => 1, p => 1,                        p => 1, br => 1,
3748                     td => 1, th => 1, tr => 1,                       }->{$token->{tag_name}}) {
3749                     tbody => 1, tfoot=> 1, thead => 1,                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3750                    }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t142');
3751                  !!!back-token; # <table>                  ## As if <head>
3752                  $token = {type => 'end tag', tag_name => 'table'};                  !!!create-element ($self->{head_element}, 'head');
3753                  !!!back-token;                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3754                  $token = {type => 'end tag',                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
3755    
3756                if ($self->{open_elements}->[-1]->[1] ne 'table') {                  $self->{insertion_mode} = IN_HEAD_IM;
3757                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  ## Reprocess in the "in head" insertion mode...
3758                  } else {
3759                    !!!cp ('t143');
3760                }                }
3761    
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
3762                #                #
3763              }              } else {
3764            } elsif ($token->{type} eq 'end tag') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3765              if ($token->{tag_name} eq 'table') {                  !!!cp ('t144');
3766                ## have a table element in table scope                  #
3767                my $i;                } else {
3768                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) {  
3769                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3770                  ## Ignore the token                  ## Ignore the token
3771                  !!!next-token;                  !!!next-token;
3772                  redo B;                  redo B;
3773                }                }
3774                              }
               ## 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]);  
               }  
3775    
3776                splice @{$self->{open_elements}}, $i;              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3777                  !!!cp ('t146');
3778                  ## As if </noscript>
3779                  pop @{$self->{open_elements}};
3780                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
3781                  
3782                  ## Reprocess in the "in head" insertion mode...
3783                  ## As if </head>
3784                  pop @{$self->{open_elements}};
3785    
3786                $self->_reset_insertion_mode;                ## Reprocess in the "after head" insertion mode...
3787                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3788                  !!!cp ('t147');
3789                  ## As if </head>
3790                  pop @{$self->{open_elements}};
3791    
3792                !!!next-token;                ## Reprocess in the "after head" insertion mode...
3793                redo B;              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3794              } elsif ({  ## ISSUE: This case cannot be reached?
3795                        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}}) {  
3796                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3797                ## Ignore the token                ## Ignore the token ## ISSUE: An issue in the spec.
3798                !!!next-token;                !!!next-token;
3799                redo B;                redo B;
3800              } else {              } else {
3801                #                !!!cp ('t149');
3802              }              }
3803    
3804                ## "after head" insertion mode
3805                ## As if <body>
3806                !!!insert-element ('body');
3807                $self->{insertion_mode} = IN_BODY_IM;
3808                ## reprocess
3809                redo B;
3810            } else {            } else {
3811              #              die "$0: $token->{type}: Unknown token type";
3812            }            }
3813    
3814            !!!parse-error (type => 'in table:'.$token->{tag_name});            ## ISSUE: An issue in the spec.
3815            $in_body->($insert_to_foster);      } elsif ($self->{insertion_mode} & BODY_IMS) {
3816            redo B;            if ($token->{type} == CHARACTER_TOKEN) {
3817          } elsif ($self->{insertion_mode} eq 'in caption') {              !!!cp ('t150');
3818            if ($token->{type} eq 'character') {              ## NOTE: There is a code clone of "character in body".
             ## NOTE: This is a code clone of "character in body".  
3819              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
3820                            
3821              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3822    
3823              !!!next-token;              !!!next-token;
3824              redo B;              redo B;
3825            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
3826              if ({              if ({
3827                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
3828                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
3829                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3830                !!!parse-error (type => 'not closed:caption');                if ($self->{insertion_mode} == IN_CELL_IM) {
3831                    ## have an element in table scope
3832                    my $tn;
3833                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3834                      my $node = $self->{open_elements}->[$_];
3835                      if ($node->[1] eq 'td' or $node->[1] eq 'th') {
3836                        !!!cp ('t151');
3837                        $tn = $node->[1];
3838                        last INSCOPE;
3839                      } elsif ({
3840                                table => 1, html => 1,
3841                               }->{$node->[1]}) {
3842                        !!!cp ('t152');
3843                        last INSCOPE;
3844                      }
3845                    } # INSCOPE
3846                      unless (defined $tn) {
3847                        !!!cp ('t153');
3848    ## TODO: This error type is wrong.
3849                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3850                        ## Ignore the token
3851                        !!!next-token;
3852                        redo B;
3853                      }
3854                    
3855                    !!!cp ('t154');
3856                    ## Close the cell
3857                    !!!back-token; # <?>
3858                    $token = {type => END_TAG_TOKEN, tag_name => $tn};
3859                    redo B;
3860                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3861                    !!!parse-error (type => 'not closed:caption');
3862                    
3863                    ## As if </caption>
3864                    ## have a table element in table scope
3865                    my $i;
3866                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3867                      my $node = $self->{open_elements}->[$_];
3868                      if ($node->[1] eq 'caption') {
3869                        !!!cp ('t155');
3870                        $i = $_;
3871                        last INSCOPE;
3872                      } elsif ({
3873                                table => 1, html => 1,
3874                               }->{$node->[1]}) {
3875                        !!!cp ('t156');
3876                        last INSCOPE;
3877                      }
3878                    } # INSCOPE
3879                      unless (defined $i) {
3880                        !!!cp ('t157');
3881    ## TODO: this type is wrong.
3882                        !!!parse-error (type => 'unmatched end tag:caption');
3883                        ## Ignore the token
3884                        !!!next-token;
3885                        redo B;
3886                      }
3887                    
3888                    ## generate implied end tags
3889                    if ({
3890                         dd => 1, dt => 1, li => 1, p => 1,
3891    
3892                ## As if </caption>                       ## NOTE: Maybe the following elements never appear here.
3893                ## have a table element in table scope                       td => 1, th => 1, tr => 1,
3894                my $i;                       tbody => 1, tfoot => 1, thead => 1,
3895                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                      }->{$self->{open_elements}->[-1]->[1]}) {
3896                  my $node = $self->{open_elements}->[$_];                    !!!cp ('t158');
3897                  if ($node->[1] eq 'caption') {                    !!!back-token; # <?>
3898                    $i = $_;                    $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3899                    last INSCOPE;                    !!!back-token;
3900                  } elsif ({                    $token = {type => END_TAG_TOKEN,
3901                            table => 1, html => 1,                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3902                           }->{$node->[1]}) {                    redo B;
                   last INSCOPE;  
3903                  }                  }
3904                } # INSCOPE  
3905                unless (defined $i) {                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3906                  !!!parse-error (type => 'unmatched end tag:caption');                    !!!cp ('t159');
3907                  ## Ignore the token                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3908                  !!!next-token;                  } else {
3909                      !!!cp ('t160');
3910                    }
3911                    
3912                    splice @{$self->{open_elements}}, $i;
3913                    
3914                    $clear_up_to_marker->();
3915                    
3916                    $self->{insertion_mode} = IN_TABLE_IM;
3917                    
3918                    ## reprocess
3919                  redo B;                  redo B;
3920                  } else {
3921                    !!!cp ('t161');
3922                    #
3923                }                }
3924                              } else {
3925                ## generate implied end tags                !!!cp ('t162');
3926                if ({                #
3927                     dd => 1, dt => 1, li => 1, p => 1,              }
3928                     td => 1, th => 1, tr => 1,            } elsif ($token->{type} == END_TAG_TOKEN) {
3929                     tbody => 1, tfoot=> 1, thead => 1,              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
3930                    }->{$self->{open_elements}->[-1]->[1]}) {                if ($self->{insertion_mode} == IN_CELL_IM) {
3931                  !!!back-token; # <?>                  ## have an element in table scope
3932                  $token = {type => 'end tag', tag_name => 'caption'};                  my $i;
3933                  !!!back-token;                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3934                  $token = {type => 'end tag',                    my $node = $self->{open_elements}->[$_];
3935                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                    if ($node->[1] eq $token->{tag_name}) {
3936                        !!!cp ('t163');
3937                        $i = $_;
3938                        last INSCOPE;
3939                      } elsif ({
3940                                table => 1, html => 1,
3941                               }->{$node->[1]}) {
3942                        !!!cp ('t164');
3943                        last INSCOPE;
3944                      }
3945                    } # INSCOPE
3946                      unless (defined $i) {
3947                        !!!cp ('t165');
3948                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3949                        ## Ignore the token
3950                        !!!next-token;
3951                        redo B;
3952                      }
3953                    
3954                    ## generate implied end tags
3955                    if ({
3956                         dd => 1, dt => 1, li => 1, p => 1,
3957                         td => ($token->{tag_name} eq 'th'),
3958                         th => ($token->{tag_name} eq 'td'),
3959    
3960                         ## NOTE: Maybe the following elements never appear here.
3961                         tr => 1,
3962                         tbody => 1, tfoot => 1, thead => 1,
3963                        }->{$self->{open_elements}->[-1]->[1]}) {
3964                      !!!cp ('t166');
3965                      !!!back-token;
3966                      $token = {type => END_TAG_TOKEN,
3967                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3968                      redo B;
3969                    }
3970                    
3971                    if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
3972                      !!!cp ('t167');
3973                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3974                    } else {
3975                      !!!cp ('t168');
3976                    }
3977                    
3978                    splice @{$self->{open_elements}}, $i;
3979                    
3980                    $clear_up_to_marker->();
3981                    
3982                    $self->{insertion_mode} = IN_ROW_IM;
3983                    
3984                    !!!next-token;
3985                  redo B;                  redo B;
3986                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3987                    !!!cp ('t169');
3988                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3989                    ## Ignore the token
3990                    !!!next-token;
3991                    redo B;
3992                  } else {
3993                    !!!cp ('t170');
3994                    #
3995                }                }
3996                } elsif ($token->{tag_name} eq 'caption') {
3997                  if ($self->{insertion_mode} == IN_CAPTION_IM) {
3998                    ## have a table element in table scope
3999                    my $i;
4000                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4001                      my $node = $self->{open_elements}->[$_];
4002                      if ($node->[1] eq $token->{tag_name}) {
4003                        !!!cp ('t171');
4004                        $i = $_;
4005                        last INSCOPE;
4006                      } elsif ({
4007                                table => 1, html => 1,
4008                               }->{$node->[1]}) {
4009                        !!!cp ('t172');
4010                        last INSCOPE;
4011                      }
4012                    } # INSCOPE
4013                      unless (defined $i) {
4014                        !!!cp ('t173');
4015                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4016                        ## Ignore the token
4017                        !!!next-token;
4018                        redo B;
4019                      }
4020                    
4021                    ## generate implied end tags
4022                    if ({
4023                         dd => 1, dt => 1, li => 1, p => 1,
4024    
4025                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                       ## NOTE: The following elements never appear here, maybe.
4026                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                       td => 1, th => 1, tr => 1,
4027                         tbody => 1, tfoot => 1, thead => 1,
4028                        }->{$self->{open_elements}->[-1]->[1]}) {
4029                      !!!cp ('t174');
4030                      !!!back-token;
4031                      $token = {type => END_TAG_TOKEN,
4032                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4033                      redo B;
4034                    }
4035                    
4036                    if ($self->{open_elements}->[-1]->[1] ne 'caption') {
4037                      !!!cp ('t175');
4038                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4039                    } else {
4040                      !!!cp ('t176');
4041                    }
4042                    
4043                    splice @{$self->{open_elements}}, $i;
4044                    
4045                    $clear_up_to_marker->();
4046                    
4047                    $self->{insertion_mode} = IN_TABLE_IM;
4048                    
4049                    !!!next-token;
4050                    redo B;
4051                  } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4052                    !!!cp ('t177');
4053                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4054                    ## Ignore the token
4055                    !!!next-token;
4056                    redo B;
4057                  } else {
4058                    !!!cp ('t178');
4059                    #
4060                }                }
4061                } elsif ({
4062                splice @{$self->{open_elements}}, $i;                        table => 1, tbody => 1, tfoot => 1,
4063                          thead => 1, tr => 1,
4064                $clear_up_to_marker->();                       }->{$token->{tag_name}} and
4065                         $self->{insertion_mode} == IN_CELL_IM) {
4066                $self->{insertion_mode} = 'in table';                ## have an element in table scope
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'caption') {  
               ## have a table element in table scope  
4067                my $i;                my $i;
4068                  my $tn;
4069                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4070                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4071                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4072                      !!!cp ('t179');
4073                    $i = $_;                    $i = $_;
4074                    last INSCOPE;                    last INSCOPE;
4075                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {
4076                      !!!cp ('t180');
4077                      $tn = $node->[1];
4078                      ## NOTE: There is exactly one |td| or |th| element
4079                      ## in scope in the stack of open elements by definition.
4080                  } elsif ({                  } elsif ({
4081                            table => 1, html => 1,                            table => 1, html => 1,
4082                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4083                      !!!cp ('t181');
4084                    last INSCOPE;                    last INSCOPE;
4085                  }                  }
4086                } # INSCOPE                } # INSCOPE
4087                unless (defined $i) {                unless (defined $i) {
4088                    !!!cp ('t182');
4089                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4090                  ## Ignore the token                  ## Ignore the token
4091                  !!!next-token;                  !!!next-token;
4092                  redo B;                  redo B;
4093                  } else {
4094                    !!!cp ('t183');
4095                }                }
                 
               ## 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 'caption') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in table';  
4096    
4097                !!!next-token;                ## Close the cell
4098                  !!!back-token; # </?>
4099                  $token = {type => END_TAG_TOKEN, tag_name => $tn};
4100                redo B;                redo B;
4101              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table' and
4102                         $self->{insertion_mode} == IN_CAPTION_IM) {
4103                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption');
4104    
4105                ## As if </caption>                ## As if </caption>
# Line 3987  sub _tree_construction_main ($) { Line 4108  sub _tree_construction_main ($) {
4108                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4109                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4110                  if ($node->[1] eq 'caption') {                  if ($node->[1] eq 'caption') {
4111                      !!!cp ('t184');
4112                    $i = $_;                    $i = $_;
4113                    last INSCOPE;                    last INSCOPE;
4114                  } elsif ({                  } elsif ({
4115                            table => 1, html => 1,                            table => 1, html => 1,
4116                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4117                      !!!cp ('t185');
4118                    last INSCOPE;                    last INSCOPE;
4119                  }                  }
4120                } # INSCOPE                } # INSCOPE
4121                unless (defined $i) {                unless (defined $i) {
4122                    !!!cp ('t186');
4123                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'unmatched end tag:caption');
4124                  ## Ignore the token                  ## Ignore the token
4125                  !!!next-token;                  !!!next-token;
# Line 4005  sub _tree_construction_main ($) { Line 4129  sub _tree_construction_main ($) {
4129                ## generate implied end tags                ## generate implied end tags
4130                if ({                if ({
4131                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
4132    
4133                       ## NOTE: The following elements never appear, maybe.
4134                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
4135                     tbody => 1, tfoot=> 1, thead => 1,                     tbody => 1, tfoot => 1, thead => 1,
4136                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
4137                    !!!cp ('t187');
4138                  !!!back-token; # </table>                  !!!back-token; # </table>
4139                  $token = {type => 'end tag', tag_name => 'caption'};                  $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
4140                  !!!back-token;                  !!!back-token;
4141                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
4142                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4143                  redo B;                  redo B;
4144                }                }
4145    
4146                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                if ($self->{open_elements}->[-1]->[1] ne 'caption') {
4147                    !!!cp ('t188');
4148                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4149                  } else {
4150                    !!!cp ('t189');
4151                }                }
4152    
4153                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4154    
4155                $clear_up_to_marker->();                $clear_up_to_marker->();
4156    
4157                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
4158    
4159                ## reprocess                ## reprocess
4160                redo B;                redo B;
4161              } elsif ({              } elsif ({
4162                        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,  
4163                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4164                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
4165                ## Ignore the token                  !!!cp ('t190');
4166                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');  
4167                  ## Ignore the token                  ## Ignore the token
4168                  !!!next-token;                  !!!next-token;
4169                  redo B;                  redo B;
4170                } else {                } else {
4171                  pop @{$self->{open_elements}}; # colgroup                  !!!cp ('t191');
4172                  $self->{insertion_mode} = 'in table';                  #
                 !!!next-token;  
                 redo B;              
4173                }                }
4174              } elsif ($token->{tag_name} eq 'col') {              } elsif ({
4175                !!!parse-error (type => 'unmatched end tag:col');                        tbody => 1, tfoot => 1,
4176                          thead => 1, tr => 1,
4177                         }->{$token->{tag_name}} and
4178                         $self->{insertion_mode} == IN_CAPTION_IM) {
4179                  !!!cp ('t192');
4180                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4181                ## Ignore the token                ## Ignore the token
4182                !!!next-token;                !!!next-token;
4183                redo B;                redo B;
4184              } else {              } else {
4185                #                !!!cp ('t193');
4186                  #
4187              }              }
4188            } else {        } else {
4189              #          die "$0: $token->{type}: Unknown token type";
4190            }        }
4191    
4192            ## As if </colgroup>        $insert = $insert_to_current;
4193            if ($self->{open_elements}->[-1]->[1] eq 'html') {        #
4194              !!!parse-error (type => 'unmatched end tag:colgroup');      } elsif ($self->{insertion_mode} & TABLE_IMS) {
4195              ## Ignore the token        if ($token->{type} == CHARACTER_TOKEN) {
             !!!next-token;  
             redo B;  
           } else {  
             pop @{$self->{open_elements}}; # colgroup  
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
           }  
         } elsif ($self->{insertion_mode} eq 'in table body') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a "character in table" code clone.  
4196              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4197                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4198                                
4199                unless (length $token->{data}) {                unless (length $token->{data}) {
4200                    !!!cp ('t194');
4201                  !!!next-token;                  !!!next-token;
4202                  redo B;                  redo B;
4203                  } else {
4204                    !!!cp ('t195');
4205                }                }
4206              }              }
4207    
# Line 4121  sub _tree_construction_main ($) { Line 4212  sub _tree_construction_main ($) {
4212              ## into the current node" while characters might not be              ## into the current node" while characters might not be
4213              ## result in a new Text node.              ## result in a new Text node.
4214              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
4215                
4216              if ({              if ({
4217                   table => 1, tbody => 1, tfoot => 1,                   table => 1, tbody => 1, tfoot => 1,
4218                   thead => 1, tr => 1,                   thead => 1, tr => 1,
# Line 4134  sub _tree_construction_main ($) { Line 4225  sub _tree_construction_main ($) {
4225                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] eq 'table') {
4226                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4227                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
4228                        !!!cp ('t196');
4229                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
4230                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
4231                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
4232                    } else {                    } else {
4233                        !!!cp ('t197');
4234                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
4235                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
4236                    }                    }
# Line 4149  sub _tree_construction_main ($) { Line 4242  sub _tree_construction_main ($) {
4242                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
4243                if (defined $prev_sibling and                if (defined $prev_sibling and
4244                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
4245                    !!!cp ('t198');
4246                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
4247                } else {                } else {
4248                    !!!cp ('t199');
4249                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
4250                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
4251                     $next_sibling);                     $next_sibling);
4252                }                }
4253              } else {              } else {
4254                  !!!cp ('t200');
4255                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4256              }              }
4257                            
4258              !!!next-token;              !!!next-token;
4259              redo B;              redo B;
4260            } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4261              if ({              if ({
4262                   tr => 1,                   tr => ($self->{insertion_mode} != IN_ROW_IM),
4263                   th => 1, td => 1,                   th => 1, td => 1,
4264                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4265                unless ($token->{tag_name} eq 'tr') {                if ($self->{insertion_mode} == IN_TABLE_IM) {
4266                  !!!parse-error (type => 'missing start tag:tr');                  ## Clear back to table context
4267                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
4268                           $self->{open_elements}->[-1]->[1] ne 'html') {
4269                      !!!cp ('t201');
4270                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4271                      pop @{$self->{open_elements}};
4272                    }
4273                    
4274                    !!!insert-element ('tbody');
4275                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
4276                    ## reprocess in the "in table body" insertion mode...
4277                }                }
4278    
4279                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4280                    unless ($token->{tag_name} eq 'tr') {
4281                      !!!cp ('t202');
4282                      !!!parse-error (type => 'missing start tag:tr');
4283                    }
4284                    
4285                    ## Clear back to table body context
4286                    while (not {
4287                      tbody => 1, tfoot => 1, thead => 1, html => 1,
4288                    }->{$self->{open_elements}->[-1]->[1]}) {
4289                      !!!cp ('t203');
4290                      ## ISSUE: Can this case be reached?
4291                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4292                      pop @{$self->{open_elements}};
4293                    }
4294                    
4295                    $self->{insertion_mode} = IN_ROW_IM;
4296                    if ($token->{tag_name} eq 'tr') {
4297                      !!!cp ('t204');
4298                      !!!insert-element ($token->{tag_name}, $token->{attributes});
4299                      !!!next-token;
4300                      redo B;
4301                    } else {
4302                      !!!cp ('t205');
4303                      !!!insert-element ('tr');
4304                      ## reprocess in the "in row" insertion mode
4305                    }
4306                  } else {
4307                    !!!cp ('t206');
4308                  }
4309    
4310                  ## Clear back to table row context
4311                while (not {                while (not {
4312                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tr => 1, html => 1,
4313                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4314                    !!!cp ('t207');
4315                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4316                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4317                }                }
4318                                
4319                $self->{insertion_mode} = 'in row';                !!!insert-element ($token->{tag_name}, $token->{attributes});
4320                if ($token->{tag_name} eq 'tr') {                $self->{insertion_mode} = IN_CELL_IM;
4321                  !!!insert-element ($token->{tag_name}, $token->{attributes});  
4322                  !!!next-token;                push @$active_formatting_elements, ['#marker', ''];
4323                } else {                
4324                  !!!insert-element ('tr');                !!!next-token;
                 ## reprocess  
               }  
4325                redo B;                redo B;
4326              } elsif ({              } elsif ({
4327                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
4328                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
4329                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
4330                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4331                ## have an element in table scope                if ($self->{insertion_mode} == IN_ROW_IM) {
4332                my $i;                  ## As if </tr>
4333                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
4334                  my $node = $self->{open_elements}->[$_];                  my $i;
4335                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4336                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
4337                      }->{$node->[1]}) {                    if ($node->[1] eq 'tr') {
4338                    $i = $_;                      !!!cp ('t208');
4339                    last INSCOPE;                      $i = $_;
4340                  } elsif ({                      last INSCOPE;
4341                            table => 1, html => 1,                    } elsif ({
4342                           }->{$node->[1]}) {                              html => 1,
4343                    last INSCOPE;  
4344                                ## NOTE: This element does not appear here, maybe.
4345                                table => 1,
4346                               }->{$node->[1]}) {
4347                        !!!cp ('t209');
4348                        last INSCOPE;
4349                      }
4350                    } # INSCOPE
4351                    unless (defined $i) {
4352                     !!!cp ('t210');
4353    ## TODO: This type is wrong.
4354                     !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});
4355                      ## Ignore the token
4356                      !!!next-token;
4357                      redo B;
4358                    }
4359                    
4360                    ## Clear back to table row context
4361                    while (not {
4362                      tr => 1, html => 1,
4363                    }->{$self->{open_elements}->[-1]->[1]}) {
4364                      !!!cp ('t211');
4365                      ## ISSUE: Can this case be reached?
4366                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4367                      pop @{$self->{open_elements}};
4368                    }
4369                    
4370                    pop @{$self->{open_elements}}; # tr
4371                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
4372                    if ($token->{tag_name} eq 'tr') {
4373                      !!!cp ('t212');
4374                      ## reprocess
4375                      redo B;
4376                    } else {
4377                      !!!cp ('t213');
4378                      ## reprocess in the "in table body" insertion mode...
4379                  }                  }
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
4380                }                }
4381    
4382                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4383                while (not {                  ## have an element in table scope
4384                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  my $i;
4385                }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4386                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    my $node = $self->{open_elements}->[$_];
4387                      if ({
4388                           tbody => 1, thead => 1, tfoot => 1,
4389                          }->{$node->[1]}) {
4390                        !!!cp ('t214');
4391                        $i = $_;
4392                        last INSCOPE;
4393                      } elsif ({
4394                                table => 1, html => 1,
4395                               }->{$node->[1]}) {
4396                        !!!cp ('t215');
4397                        last INSCOPE;
4398                      }
4399                    } # INSCOPE
4400                    unless (defined $i) {
4401                      !!!cp ('t216');
4402    ## TODO: This erorr type ios wrong.
4403                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4404                      ## Ignore the token
4405                      !!!next-token;
4406                      redo B;
4407                    }
4408    
4409                    ## Clear back to table body context
4410                    while (not {
4411                      tbody => 1, tfoot => 1, thead => 1, html => 1,
4412                    }->{$self->{open_elements}->[-1]->[1]}) {
4413                      !!!cp ('t217');
4414                      ## ISSUE: Can this state be reached?
4415                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4416                      pop @{$self->{open_elements}};
4417                    }
4418                    
4419                    ## As if <{current node}>
4420                    ## have an element in table scope
4421                    ## true by definition
4422                    
4423                    ## Clear back to table body context
4424                    ## nop by definition
4425                    
4426                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4427                    $self->{insertion_mode} = IN_TABLE_IM;
4428                    ## reprocess in "in table" insertion mode...
4429                  } else {
4430                    !!!cp ('t218');
4431                }                }
4432    
4433                ## As if <{current node}>                if ($token->{tag_name} eq 'col') {
4434                ## have an element in table scope                  ## Clear back to table context
4435                ## true by definition                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
4436                           $self->{open_elements}->[-1]->[1] ne 'html') {
4437                ## Clear back to table body context                    !!!cp ('t219');
4438                ## nop by definition                    ## ISSUE: Can this state be reached?
4439                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4440                pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4441                $self->{insertion_mode} = 'in table';                  }
4442                ## reprocess                  
4443                redo B;                  !!!insert-element ('colgroup');
4444                    $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
4445                    ## reprocess
4446                    redo B;
4447                  } elsif ({
4448                            caption => 1,
4449                            colgroup => 1,
4450                            tbody => 1, tfoot => 1, thead => 1,
4451                           }->{$token->{tag_name}}) {
4452                    ## Clear back to table context
4453                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
4454                           $self->{open_elements}->[-1]->[1] ne 'html') {
4455                      !!!cp ('t220');
4456                      ## ISSUE: Can this state be reached?
4457                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4458                      pop @{$self->{open_elements}};
4459                    }
4460                    
4461                    push @$active_formatting_elements, ['#marker', '']
4462                        if $token->{tag_name} eq 'caption';
4463                    
4464                    !!!insert-element ($token->{tag_name}, $token->{attributes});
4465                    $self->{insertion_mode} = {
4466                                               caption => IN_CAPTION_IM,
4467                                               colgroup => IN_COLUMN_GROUP_IM,
4468                                               tbody => IN_TABLE_BODY_IM,
4469                                               tfoot => IN_TABLE_BODY_IM,
4470                                               thead => IN_TABLE_BODY_IM,
4471                                              }->{$token->{tag_name}};
4472                    !!!next-token;
4473                    redo B;
4474                  } else {
4475                    die "$0: in table: <>: $token->{tag_name}";
4476                  }
4477              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4478                ## 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');  
4479    
4480                ## As if </table>                ## As if </table>
4481                ## have a table element in table scope                ## have a table element in table scope
# Line 4242  sub _tree_construction_main ($) { Line 4483  sub _tree_construction_main ($) {
4483                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4484                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4485                  if ($node->[1] eq 'table') {                  if ($node->[1] eq 'table') {
4486                      !!!cp ('t221');
4487                    $i = $_;                    $i = $_;
4488                    last INSCOPE;                    last INSCOPE;
4489                  } elsif ({                  } elsif ({
4490                            table => 1, html => 1,                            #table => 1,
4491                              html => 1,
4492                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4493                      !!!cp ('t222');
4494                    last INSCOPE;                    last INSCOPE;
4495                  }                  }
4496                } # INSCOPE                } # INSCOPE
4497                unless (defined $i) {                unless (defined $i) {
4498                    !!!cp ('t223');
4499    ## TODO: The following is wrong, maybe.
4500                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag:table');
4501                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
4502                  !!!next-token;                  !!!next-token;
# Line 4263  sub _tree_construction_main ($) { Line 4509  sub _tree_construction_main ($) {
4509                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
4510                     tbody => 1, tfoot=> 1, thead => 1,                     tbody => 1, tfoot=> 1, thead => 1,
4511                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
4512                    !!!cp ('t224');
4513                  !!!back-token; # <table>                  !!!back-token; # <table>
4514                  $token = {type => 'end tag', tag_name => 'table'};                  $token = {type => END_TAG_TOKEN, tag_name => 'table'};
4515                  !!!back-token;                  !!!back-token;
4516                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
4517                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4518                  redo B;                  redo B;
4519                }                }
4520    
4521                if ($self->{open_elements}->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
4522                    !!!cp ('t225');
4523    ## ISSUE: Can this case be reached?
4524                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4525                  } else {
4526                    !!!cp ('t226');
4527                }                }
4528    
4529                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4530    
4531                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
4532    
4533                ## reprocess                ## reprocess
4534                redo B;                redo B;
4535              } else {          } else {
4536                #            !!!cp ('t227');
4537              }            !!!parse-error (type => 'in table:'.$token->{tag_name});
4538            } elsif ($token->{type} eq 'end tag') {  
4539              if ({            $insert = $insert_to_foster;
4540                   tbody => 1, tfoot => 1, thead => 1,            #
4541                  }->{$token->{tag_name}}) {          }
4542          } elsif ($token->{type} == END_TAG_TOKEN) {
4543                if ($token->{tag_name} eq 'tr' and
4544                    $self->{insertion_mode} == IN_ROW_IM) {
4545                ## have an element in table scope                ## have an element in table scope
4546                my $i;                my $i;
4547                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4548                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4549                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4550                      !!!cp ('t228');
4551                    $i = $_;                    $i = $_;
4552                    last INSCOPE;                    last INSCOPE;
4553                  } elsif ({                  } elsif ({
4554                            table => 1, html => 1,                            table => 1, html => 1,
4555                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4556                      !!!cp ('t229');
4557                    last INSCOPE;                    last INSCOPE;
4558                  }                  }
4559                } # INSCOPE                } # INSCOPE
4560                unless (defined $i) {                unless (defined $i) {
4561                    !!!cp ('t230');
4562                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4563                  ## Ignore the token                  ## Ignore the token
4564                  !!!next-token;                  !!!next-token;
4565                  redo B;                  redo B;
4566                  } else {
4567                    !!!cp ('t232');
4568                }                }
4569    
4570                ## Clear back to table body context                ## Clear back to table row context
4571                while (not {                while (not {
4572                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tr => 1, html => 1,
4573                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4574                    !!!cp ('t231');
4575    ## ISSUE: Can this state be reached?
4576                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4577                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4578                }                }
4579    
4580                pop @{$self->{open_elements}};                pop @{$self->{open_elements}}; # tr
4581                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
4582                !!!next-token;                !!!next-token;
4583                redo B;                redo B;
4584              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4585                ## have an element in table scope                if ($self->{insertion_mode} == IN_ROW_IM) {
4586                my $i;                  ## As if </tr>
4587                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
4588                  my $node = $self->{open_elements}->[$_];                  my $i;
4589                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4590                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
4591                      }->{$node->[1]}) {                    if ($node->[1] eq 'tr') {
4592                    $i = $_;                      !!!cp ('t233');
4593                    last INSCOPE;                      $i = $_;
4594                  } elsif ({                      last INSCOPE;
4595                            table => 1, html => 1,                    } elsif ({
4596                           }->{$node->[1]}) {                              table => 1, html => 1,
4597                    last INSCOPE;                             }->{$node->[1]}) {
4598                        !!!cp ('t234');
4599                        last INSCOPE;
4600                      }
4601                    } # INSCOPE
4602                    unless (defined $i) {
4603                      !!!cp ('t235');
4604    ## TODO: The following is wrong.
4605                      !!!parse-error (type => 'unmatched end tag:'.$token->{type});
4606                      ## Ignore the token
4607                      !!!next-token;
4608                      redo B;
4609                  }                  }
4610                } # INSCOPE                  
4611                unless (defined $i) {                  ## Clear back to table row context
4612                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  while (not {
4613                  ## Ignore the token                    tr => 1, html => 1,
                 !!!next-token;  
                 redo B;  
               }  
   
               ## 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;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           ## As if in table  
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
           $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;  
               }  
             }  
   
             !!!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,  
4614                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4615                # MUST                    !!!cp ('t236');
4616                my $foster_parent_element;  ## ISSUE: Can this state be reached?
4617                my $next_sibling;                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4618                my $prev_sibling;                    pop @{$self->{open_elements}};
4619                OE: for (reverse 0..$#{$self->{open_elements}}) {                  }
4620                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  
4621                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                  pop @{$self->{open_elements}}; # tr
4622                    if (defined $parent and $parent->node_type == 1) {                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4623                      $foster_parent_element = $parent;                  ## reprocess in the "in table body" insertion mode...
4624                      $next_sibling = $self->{open_elements}->[$_]->[0];                }
4625                      $prev_sibling = $next_sibling->previous_sibling;  
4626                    } else {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4627                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                  ## have an element in table scope
4628                      $prev_sibling = $foster_parent_element->last_child;                  my $i;
4629                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4630                      my $node = $self->{open_elements}->[$_];
4631                      if ({
4632                           tbody => 1, thead => 1, tfoot => 1,
4633                          }->{$node->[1]}) {
4634                        !!!cp ('t237');
4635                        $i = $_;
4636                        last INSCOPE;
4637                      } elsif ({
4638                                table => 1, html => 1,
4639                               }->{$node->[1]}) {
4640                        !!!cp ('t238');
4641                        last INSCOPE;
4642                    }                    }
4643                    last OE;                  } # INSCOPE
4644                    unless (defined $i) {
4645                      !!!cp ('t239');
4646                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4647                      ## Ignore the token
4648                      !!!next-token;
4649                      redo B;
4650                  }                  }
4651                } # OE                  
4652                $foster_parent_element = $self->{open_elements}->[0]->[0] and                  ## Clear back to table body context
4653                $prev_sibling = $foster_parent_element->last_child                  while (not {
4654                  unless defined $foster_parent_element;                    tbody => 1, tfoot => 1, thead => 1, html => 1,
4655                if (defined $prev_sibling and                  }->{$self->{open_elements}->[-1]->[1]}) {
4656                    $prev_sibling->node_type == 3) {                    !!!cp ('t240');
4657                  $prev_sibling->manakai_append_text ($token->{data});                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4658                } else {                    pop @{$self->{open_elements}};
                 $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>  
               ## 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;  
4659                  }                  }
4660                } # INSCOPE                  
4661                unless (defined $i) {                  ## As if <{current node}>
4662                  !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                  ## have an element in table scope
4663                  ## Ignore the token                  ## true by definition
4664                  !!!next-token;                  
4665                  redo B;                  ## Clear back to table body context
4666                }                  ## nop by definition
4667                    
               ## 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]);  
4668                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4669                    $self->{insertion_mode} = IN_TABLE_IM;
4670                    ## reprocess in the "in table" insertion mode...
4671                }                }
4672    
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## NOTE: This is a code clone of "table in table"  
               !!!parse-error (type => 'not closed:table');  
   
               ## As if </table>  
4673                ## have a table element in table scope                ## have a table element in table scope
4674                my $i;                my $i;
4675                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4676                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4677                  if ($node->[1] eq 'table') {                  if ($node->[1] eq $token->{tag_name}) {
4678                      !!!cp ('t241');
4679                    $i = $_;                    $i = $_;
4680                    last INSCOPE;                    last INSCOPE;
4681                  } elsif ({                  } elsif ({
4682                            table => 1, html => 1,                            table => 1, html => 1,
4683                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4684                      !!!cp ('t242');
4685                    last INSCOPE;                    last INSCOPE;
4686                  }                  }
4687                } # INSCOPE                } # INSCOPE
4688                unless (defined $i) {                unless (defined $i) {
4689                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t243');
4690                  ## Ignore tokens </table><table>                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4691                    ## Ignore the token
4692                  !!!next-token;                  !!!next-token;
4693                  redo B;                  redo B;
4694                }                }
4695                  
4696                ## generate implied end tags                ## generate implied end tags
4697                if ({                if ({
4698                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
4699                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
4700                     tbody => 1, tfoot=> 1, thead => 1,                     tbody => 1, tfoot=> 1, thead => 1,
4701                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
4702                  !!!back-token; # <table>                  !!!cp ('t244');
4703                  $token = {type => 'end tag', tag_name => 'table'};  ## ISSUE: Can this case be reached?
4704                  !!!back-token;                  !!!back-token;
4705                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
4706                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4707                  redo B;                  redo B;
4708                }                }
4709                  
4710                if ($self->{open_elements}->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
4711                    !!!cp ('t245');
4712                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4713                  } else {
4714                    !!!cp ('t246');
4715                }                }
4716                    
4717                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4718                  
4719                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
4720                  
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq '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 $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});  
                 ## 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';  
4721                !!!next-token;                !!!next-token;
4722                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;  
4723              } elsif ({              } elsif ({
4724                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
4725                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}} and
4726                ## have an element in table scope                       $self->{insertion_mode} & ROW_IMS) {
4727                my $i;                if ($self->{insertion_mode} == IN_ROW_IM) {
4728                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
4729                  my $node = $self->{open_elements}->[$_];                  my $i;
4730                  if ($node->[1] eq $token->{tag_name}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4731                    $i = $_;                    my $node = $self->{open_elements}->[$_];
4732                    last INSCOPE;                    if ($node->[1] eq $token->{tag_name}) {
4733                  } elsif ({                      !!!cp ('t247');
4734                            table => 1, html => 1,                      $i = $_;
4735                           }->{$node->[1]}) {                      last INSCOPE;
4736                    last INSCOPE;                    } elsif ({
4737                                table => 1, html => 1,
4738                               }->{$node->[1]}) {
4739                        !!!cp ('t248');
4740                        last INSCOPE;
4741                      }
4742                    } # INSCOPE
4743                      unless (defined $i) {
4744                        !!!cp ('t249');
4745                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4746                        ## Ignore the token
4747                        !!!next-token;
4748                        redo B;
4749                      }
4750                    
4751                    ## As if </tr>
4752                    ## have an element in table scope
4753                    my $i;
4754                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4755                      my $node = $self->{open_elements}->[$_];
4756                      if ($node->[1] eq 'tr') {
4757                        !!!cp ('t250');
4758                        $i = $_;
4759                        last INSCOPE;
4760                      } elsif ({
4761                                table => 1, html => 1,
4762                               }->{$node->[1]}) {
4763                        !!!cp ('t251');
4764                        last INSCOPE;
4765                      }
4766                    } # INSCOPE
4767                      unless (defined $i) {
4768                        !!!cp ('t252');
4769                        !!!parse-error (type => 'unmatched end tag:tr');
4770                        ## Ignore the token
4771                        !!!next-token;
4772                        redo B;
4773                      }
4774                    
4775                    ## Clear back to table row context
4776                    while (not {
4777                      tr => 1, html => 1,
4778                    }->{$self->{open_elements}->[-1]->[1]}) {
4779                      !!!cp ('t253');
4780    ## ISSUE: Can this case be reached?
4781                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4782                      pop @{$self->{open_elements}};
4783                  }                  }
4784                } # INSCOPE                  
4785                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
4786                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4787                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
4788                }                }
4789    
               ## As if </tr>  
4790                ## have an element in table scope                ## have an element in table scope
4791                my $i;                my $i;
4792                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4793                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4794                  if ($node->[1] eq 'tr') {                  if ($node->[1] eq $token->{tag_name}) {
4795                      !!!cp ('t254');
4796                    $i = $_;                    $i = $_;
4797                    last INSCOPE;                    last INSCOPE;
4798                  } elsif ({                  } elsif ({
4799                            table => 1, html => 1,                            table => 1, html => 1,
4800                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4801                      !!!cp ('t255');
4802                    last INSCOPE;                    last INSCOPE;
4803                  }                  }
4804                } # INSCOPE                } # INSCOPE
4805                unless (defined $i) {                unless (defined $i) {
4806                  !!!parse-error (type => 'unmatched end tag:tr');                  !!!cp ('t256');
4807                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4808                  ## Ignore the token                  ## Ignore the token
4809                  !!!next-token;                  !!!next-token;
4810                  redo B;                  redo B;
4811                }                }
4812    
4813                ## Clear back to table row context                ## Clear back to table body context
4814                while (not {                while (not {
4815                  tr => 1, html => 1,                  tbody => 1, tfoot => 1, thead => 1, html => 1,
4816                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4817                    !!!cp ('t257');
4818    ## ISSUE: Can this case be reached?
4819                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4820                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4821                }                }
4822    
4823                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}};
4824                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_IM;
4825                ## reprocess                !!!next-token;
4826                redo B;                redo B;
4827              } elsif ({              } elsif ({
4828                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
4829                        colgroup => 1, html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
4830                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
4831                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
4832                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4833                  !!!cp ('t258');
4834                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4835                ## Ignore the token                ## Ignore the token
4836                !!!next-token;                !!!next-token;
4837                redo B;                redo B;
4838              } else {          } else {
4839                #            !!!cp ('t259');
4840              }            !!!parse-error (type => 'in table:/'.$token->{tag_name});
           } else {  
             #  
           }  
   
           ## As if in table  
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
           $in_body->($insert_to_foster);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in cell') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This 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});  
4841    
4842              !!!next-token;            $insert = $insert_to_foster;
4843              redo B;            #
4844            } elsif ($token->{type} eq 'start tag') {          }
4845              if ({        } else {
4846                   caption => 1, col => 1, colgroup => 1,          die "$0: $token->{type}: Unknown token type";
4847                   tbody => 1, td => 1, tfoot => 1, th => 1,        }
4848                   thead => 1, tr => 1,      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
4849                  }->{$token->{tag_name}}) {            if ($token->{type} == CHARACTER_TOKEN) {
4850                ## have an element in table scope              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4851                my $tn;                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4852                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                unless (length $token->{data}) {
4853                  my $node = $self->{open_elements}->[$_];                  !!!cp ('t260');
                 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  
4854                  !!!next-token;                  !!!next-token;
4855                  redo B;                  redo B;
4856                }                }
4857                }
4858                ## Close the cell              
4859                !!!back-token; # <?>              !!!cp ('t261');
4860                $token = {type => 'end tag', tag_name => $tn};              #
4861              } elsif ($token->{type} == START_TAG_TOKEN) {
4862                if ($token->{tag_name} eq 'col') {
4863                  !!!cp ('t262');
4864                  !!!insert-element ($token->{tag_name}, $token->{attributes});
4865                  pop @{$self->{open_elements}};
4866                  !!!next-token;
4867                redo B;                redo B;
4868              } else {              } else {
4869                  !!!cp ('t263');
4870                #                #
4871              }              }
4872            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
4873              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'colgroup') {
4874                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] eq 'html') {
4875                my $i;                  !!!cp ('t264');
4876                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});  
4877                  ## Ignore the token                  ## Ignore the token
4878                  !!!next-token;                  !!!next-token;
4879                  redo B;                  redo B;
4880                  } else {
4881                    !!!cp ('t265');
4882                    pop @{$self->{open_elements}}; # colgroup
4883                    $self->{insertion_mode} = IN_TABLE_IM;
4884                    !!!next-token;
4885                    redo B;            
4886                }                }
4887                              } elsif ($token->{tag_name} eq 'col') {
4888                ## generate implied end tags                !!!cp ('t266');
4889                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});  
4890                ## Ignore the token                ## Ignore the token
4891                !!!next-token;                !!!next-token;
4892                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;  
4893              } else {              } else {
4894                #                !!!cp ('t267');
4895                  #
4896              }              }
4897            } else {            } else {
4898              #              die "$0: $token->{type}: Unknown token type";
4899            }            }
4900              
4901            $in_body->($insert_to_current);            ## As if </colgroup>
4902            redo B;            if ($self->{open_elements}->[-1]->[1] eq 'html') {
4903          } elsif ($self->{insertion_mode} eq 'in select') {              !!!cp ('t269');
4904            if ($token->{type} eq 'character') {              !!!parse-error (type => 'unmatched end tag:colgroup');
4905              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              ## Ignore the token
4906              !!!next-token;              !!!next-token;
4907              redo B;              redo B;
4908            } elsif ($token->{type} eq 'start tag') {            } else {
4909                !!!cp ('t270');
4910                pop @{$self->{open_elements}}; # colgroup
4911                $self->{insertion_mode} = IN_TABLE_IM;
4912                ## reprocess
4913                redo B;
4914              }
4915        } elsif ($self->{insertion_mode} == IN_SELECT_IM) {
4916          if ($token->{type} == CHARACTER_TOKEN) {
4917            !!!cp ('t271');
4918            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4919            !!!next-token;
4920            redo B;
4921          } elsif ($token->{type} == START_TAG_TOKEN) {
4922              if ($token->{tag_name} eq 'option') {              if ($token->{tag_name} eq 'option') {
4923                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
4924                    !!!cp ('t272');
4925                  ## As if </option>                  ## As if </option>
4926                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4927                  } else {
4928                    !!!cp ('t273');
4929                }                }
4930    
4931                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
# Line 4847  sub _tree_construction_main ($) { Line 4933  sub _tree_construction_main ($) {
4933                redo B;                redo B;
4934              } elsif ($token->{tag_name} eq 'optgroup') {              } elsif ($token->{tag_name} eq 'optgroup') {
4935                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
4936                    !!!cp ('t274');
4937                  ## As if </option>                  ## As if </option>
4938                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4939                  } else {
4940                    !!!cp ('t275');
4941                }                }
4942    
4943                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
4944                    !!!cp ('t276');
4945                  ## As if </optgroup>                  ## As if </optgroup>
4946                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4947                  } else {
4948                    !!!cp ('t277');
4949                }                }
4950    
4951                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
4952                !!!next-token;                !!!next-token;
4953                redo B;                redo B;
4954              } elsif ($token->{tag_name} eq 'select') {              } elsif ($token->{tag_name} eq 'select') {
4955    ## TODO: The type below is not good - <select> is replaced by </select>
4956                !!!parse-error (type => 'not closed:select');                !!!parse-error (type => 'not closed:select');
4957                ## As if </select> instead                ## As if </select> instead
4958                ## have an element in table scope                ## have an element in table scope
# Line 4867  sub _tree_construction_main ($) { Line 4960  sub _tree_construction_main ($) {
4960                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4961                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4962                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4963                      !!!cp ('t278');
4964                    $i = $_;                    $i = $_;
4965                    last INSCOPE;                    last INSCOPE;
4966                  } elsif ({                  } elsif ({
4967                            table => 1, html => 1,                            table => 1, html => 1,
4968                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4969                      !!!cp ('t279');
4970                    last INSCOPE;                    last INSCOPE;
4971                  }                  }
4972                } # INSCOPE                } # INSCOPE
4973                unless (defined $i) {                unless (defined $i) {
4974                    !!!cp ('t280');
4975                  !!!parse-error (type => 'unmatched end tag:select');                  !!!parse-error (type => 'unmatched end tag:select');
4976                  ## Ignore the token                  ## Ignore the token
4977                  !!!next-token;                  !!!next-token;
4978                  redo B;                  redo B;
4979                }                }
4980                                
4981                  !!!cp ('t281');
4982                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4983    
4984                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
4985    
4986                !!!next-token;                !!!next-token;
4987                redo B;                redo B;
4988              } else {          } else {
4989                #            !!!cp ('t282');
4990              }            !!!parse-error (type => 'in select:'.$token->{tag_name});
4991            } elsif ($token->{type} eq 'end tag') {            ## Ignore the token
4992              !!!next-token;
4993              redo B;
4994            }
4995          } elsif ($token->{type} == END_TAG_TOKEN) {
4996              if ($token->{tag_name} eq 'optgroup') {              if ($token->{tag_name} eq 'optgroup') {
4997                if ($self->{open_elements}->[-1]->[1] eq 'option' and                if ($self->{open_elements}->[-1]->[1] eq 'option' and
4998                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {
4999                    !!!cp ('t283');
5000                  ## As if </option>                  ## As if </option>
5001                  splice @{$self->{open_elements}}, -2;                  splice @{$self->{open_elements}}, -2;
5002                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
5003                    !!!cp ('t284');
5004                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5005                } else {                } else {
5006                    !!!cp ('t285');
5007                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5008                  ## Ignore the token                  ## Ignore the token
5009                }                }
# Line 4907  sub _tree_construction_main ($) { Line 5011  sub _tree_construction_main ($) {
5011                redo B;                redo B;
5012              } elsif ($token->{tag_name} eq 'option') {              } elsif ($token->{tag_name} eq 'option') {
5013                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
5014                    !!!cp ('t286');
5015                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5016                } else {                } else {
5017                    !!!cp ('t287');
5018                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5019                  ## Ignore the token                  ## Ignore the token
5020                }                }
# Line 4920  sub _tree_construction_main ($) { Line 5026  sub _tree_construction_main ($) {
5026                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5027                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5028                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
5029                      !!!cp ('t288');
5030                    $i = $_;                    $i = $_;
5031                    last INSCOPE;                    last INSCOPE;
5032                  } elsif ({                  } elsif ({
5033                            table => 1, html => 1,                            table => 1, html => 1,
5034                           }->{$node->[1]}) {                           }->{$node->[1]}) {
5035                      !!!cp ('t289');
5036                    last INSCOPE;                    last INSCOPE;
5037                  }                  }
5038                } # INSCOPE                } # INSCOPE
5039                unless (defined $i) {                unless (defined $i) {
5040                    !!!cp ('t290');
5041                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5042                  ## Ignore the token                  ## Ignore the token
5043                  !!!next-token;                  !!!next-token;
5044                  redo B;                  redo B;
5045                }                }
5046                                
5047                  !!!cp ('t291');
5048                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5049    
5050                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
# Line 4945  sub _tree_construction_main ($) { Line 5055  sub _tree_construction_main ($) {
5055                        caption => 1, table => 1, tbody => 1,                        caption => 1, table => 1, tbody => 1,
5056                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5057                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5058    ## TODO: The following is wrong?
5059                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5060                                
5061                ## have an element in table scope                ## have an element in table scope
# Line 4952  sub _tree_construction_main ($) { Line 5063  sub _tree_construction_main ($) {
5063                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5064                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5065                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
5066                      !!!cp ('t292');
5067                    $i = $_;                    $i = $_;
5068                    last INSCOPE;                    last INSCOPE;
5069                  } elsif ({                  } elsif ({
5070                            table => 1, html => 1,                            table => 1, html => 1,
5071                           }->{$node->[1]}) {                           }->{$node->[1]}) {
5072                      !!!cp ('t293');
5073                    last INSCOPE;                    last INSCOPE;
5074                  }                  }
5075                } # INSCOPE                } # INSCOPE
5076                unless (defined $i) {                unless (defined $i) {
5077                    !!!cp ('t294');
5078                  ## Ignore the token                  ## Ignore the token
5079                  !!!next-token;                  !!!next-token;
5080                  redo B;                  redo B;
# Line 4972  sub _tree_construction_main ($) { Line 5086  sub _tree_construction_main ($) {
5086                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5087                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5088                  if ($node->[1] eq 'select') {                  if ($node->[1] eq 'select') {
5089                      !!!cp ('t295');
5090                    $i = $_;                    $i = $_;
5091                    last INSCOPE;                    last INSCOPE;
5092                  } elsif ({                  } elsif ({
5093                            table => 1, html => 1,                            table => 1, html => 1,
5094                           }->{$node->[1]}) {                           }->{$node->[1]}) {
5095    ## ISSUE: Can this state be reached?
5096                      !!!cp ('t296');
5097                    last INSCOPE;                    last INSCOPE;
5098                  }                  }
5099                } # INSCOPE                } # INSCOPE
5100                unless (defined $i) {                unless (defined $i) {
5101                    !!!cp ('t297');
5102    ## TODO: The following error type is correct?
5103                  !!!parse-error (type => 'unmatched end tag:select');                  !!!parse-error (type => 'unmatched end tag:select');
5104                  ## Ignore the </select> token                  ## Ignore the </select> token
5105                  !!!next-token; ## TODO: ok?                  !!!next-token; ## TODO: ok?
5106                  redo B;                  redo B;
5107                }                }
5108                                
5109                  !!!cp ('t298');
5110                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5111    
5112                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5113    
5114                ## reprocess                ## reprocess
5115                redo B;                redo B;
5116              } else {          } else {
5117                #            !!!cp ('t299');
5118              }            !!!parse-error (type => 'in select:/'.$token->{tag_name});
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in select:'.$token->{tag_name});  
5119            ## Ignore the token            ## Ignore the token
5120            !!!next-token;            !!!next-token;
5121            redo B;            redo B;
5122          } elsif ($self->{insertion_mode} eq 'after body') {          }
5123            if ($token->{type} eq 'character') {        } else {
5124              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          die "$0: $token->{type}: Unknown token type";
5125                my $data = $1;        }
5126                ## As if in body      } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
5127                $reconstruct_active_formatting_elements->($insert_to_current);        if ($token->{type} == CHARACTER_TOKEN) {
5128            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5129              my $data = $1;
5130              ## As if in body
5131              $reconstruct_active_formatting_elements->($insert_to_current);
5132                                
5133                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5134              
5135              unless (length $token->{data}) {
5136                !!!cp ('t300');
5137                !!!next-token;
5138                redo B;
5139              }
5140            }
5141            
5142            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5143              !!!cp ('t301');
5144              !!!parse-error (type => 'after html:#character');
5145    
5146                unless (length $token->{data}) {            ## Reprocess in the "after body" insertion mode.
5147                  !!!next-token;          } else {
5148                  redo B;            !!!cp ('t302');
5149                }          }
5150              }          
5151                        ## "after body" insertion mode
5152              #          !!!parse-error (type => 'after body:#character');
5153              !!!parse-error (type => 'after body:#character');  
5154            } elsif ($token->{type} eq 'start tag') {          $self->{insertion_mode} = IN_BODY_IM;
5155              !!!parse-error (type => 'after body:'.$token->{tag_name});          ## reprocess
5156              #          redo B;
5157            } elsif ($token->{type} eq 'end tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
5158              if ($token->{tag_name} eq 'html') {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5159                if (defined $self->{inner_html_node}) {            !!!cp ('t303');
5160                  !!!parse-error (type => 'unmatched end tag:html');            !!!parse-error (type => 'after html:'.$token->{tag_name});
5161                  ## Ignore the token            
5162                  !!!next-token;            ## Reprocess in the "after body" insertion mode.
5163                  redo B;          } else {
5164                } else {            !!!cp ('t304');
5165                  $previous_insertion_mode = $self->{insertion_mode};          }
5166                  $self->{insertion_mode} = 'trailing end';  
5167                  !!!next-token;          ## "after body" insertion mode
5168                  redo B;          !!!parse-error (type => 'after body:'.$token->{tag_name});
5169                }  
5170              } else {          $self->{insertion_mode} = IN_BODY_IM;
5171                !!!parse-error (type => 'after body:/'.$token->{tag_name});          ## reprocess
5172              }          redo B;
5173          } elsif ($token->{type} == END_TAG_TOKEN) {
5174            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5175              !!!cp ('t305');
5176              !!!parse-error (type => 'after html:/'.$token->{tag_name});
5177              
5178              $self->{insertion_mode} = AFTER_BODY_IM;
5179              ## Reprocess in the "after body" insertion mode.
5180            } else {
5181              !!!cp ('t306');
5182            }
5183    
5184            ## "after body" insertion mode
5185            if ($token->{tag_name} eq 'html') {
5186              if (defined $self->{inner_html_node}) {
5187                !!!cp ('t307');
5188                !!!parse-error (type => 'unmatched end tag:html');
5189                ## Ignore the token
5190                !!!next-token;
5191                redo B;
5192            } else {            } else {
5193              die "$0: $token->{type}: Unknown token type";              !!!cp ('t308');
5194                $self->{insertion_mode} = AFTER_HTML_BODY_IM;
5195                !!!next-token;
5196                redo B;
5197            }            }
5198            } else {
5199              !!!cp ('t309');
5200              !!!parse-error (type => 'after body:/'.$token->{tag_name});
5201    
5202            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = IN_BODY_IM;
5203            ## reprocess            ## reprocess
5204            redo B;            redo B;
5205      } elsif ($self->{insertion_mode} eq 'in frameset') {          }
5206        if ($token->{type} eq 'character') {        } else {
5207            die "$0: $token->{type}: Unknown token type";
5208          }
5209        } elsif ($self->{insertion_mode} & FRAME_IMS) {
5210          if ($token->{type} == CHARACTER_TOKEN) {
5211          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5212            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5213              
5214            unless (length $token->{data}) {            unless (length $token->{data}) {
5215                !!!cp ('t310');
5216              !!!next-token;              !!!next-token;
5217              redo B;              redo B;
5218            }            }
5219          }          }
5220            
5221          !!!parse-error (type => 'in frameset:#character');          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
5222          ## Ignore the token            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5223          !!!next-token;              !!!cp ('t311');
5224          redo B;              !!!parse-error (type => 'in frameset:#character');
5225        } elsif ($token->{type} eq 'start tag') {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
5226          if ($token->{tag_name} eq 'frameset') {              !!!cp ('t312');
5227                !!!parse-error (type => 'after frameset:#character');
5228              } else { # "after html frameset"
5229                !!!cp ('t313');
5230                !!!parse-error (type => 'after html:#character');
5231    
5232                $self->{insertion_mode} = AFTER_FRAMESET_IM;
5233                ## Reprocess in the "after frameset" insertion mode.
5234                !!!parse-error (type => 'after frameset:#character');
5235              }
5236              
5237              ## Ignore the token.
5238              if (length $token->{data}) {
5239                !!!cp ('t314');
5240                ## reprocess the rest of characters
5241              } else {
5242                !!!cp ('t315');
5243                !!!next-token;
5244              }
5245              redo B;
5246            }
5247            
5248            die qq[$0: Character "$token->{data}"];
5249          } elsif ($token->{type} == START_TAG_TOKEN) {
5250            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5251              !!!cp ('t316');
5252              !!!parse-error (type => 'after html:'.$token->{tag_name});
5253    
5254              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5255              ## Process in the "after frameset" insertion mode.
5256            } else {
5257              !!!cp ('t317');
5258            }
5259    
5260            if ($token->{tag_name} eq 'frameset' and
5261                $self->{insertion_mode} == IN_FRAMESET_IM) {
5262              !!!cp ('t318');
5263            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes});
5264            !!!next-token;            !!!next-token;
5265            redo B;            redo B;
5266          } elsif ($token->{tag_name} eq 'frame') {          } elsif ($token->{tag_name} eq 'frame' and
5267                     $self->{insertion_mode} == IN_FRAMESET_IM) {
5268              !!!cp ('t319');
5269            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes});
5270            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
5271            !!!next-token;            !!!next-token;
5272            redo B;            redo B;
5273          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
5274            $in_body->($insert_to_current);            !!!cp ('t320');
5275              ## NOTE: As if in body.
5276              $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
5277            redo B;            redo B;
5278          } else {          } else {
5279            !!!parse-error (type => 'in frameset:'.$token->{tag_name});            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5280                !!!cp ('t321');
5281                !!!parse-error (type => 'in frameset:'.$token->{tag_name});
5282              } else {
5283                !!!cp ('t322');
5284                !!!parse-error (type => 'after frameset:'.$token->{tag_name});
5285              }
5286            ## Ignore the token            ## Ignore the token
5287            !!!next-token;            !!!next-token;
5288            redo B;            redo B;
5289          }          }
5290        } elsif ($token->{type} eq 'end tag') {        } elsif ($token->{type} == END_TAG_TOKEN) {
5291          if ($token->{tag_name} eq 'frameset') {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5292              !!!cp ('t323');
5293              !!!parse-error (type => 'after html:/'.$token->{tag_name});
5294    
5295              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5296              ## Process in the "after frameset" insertion mode.
5297            } else {
5298              !!!cp ('t324');
5299            }
5300    
5301            if ($token->{tag_name} eq 'frameset' and
5302                $self->{insertion_mode} == IN_FRAMESET_IM) {
5303            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] eq 'html' and
5304                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
5305                !!!cp ('t325');
5306              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5307              ## Ignore the token              ## Ignore the token
5308              !!!next-token;              !!!next-token;
5309            } else {            } else {
5310                !!!cp ('t326');
5311              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5312              !!!next-token;              !!!next-token;
5313            }            }
5314    
5315            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
5316                $self->{open_elements}->[-1]->[1] ne 'frameset') {                $self->{open_elements}->[-1]->[1] ne 'frameset') {
5317              $self->{insertion_mode} = 'after frameset';              !!!cp ('t327');
5318                $self->{insertion_mode} = AFTER_FRAMESET_IM;
5319              } else {
5320                !!!cp ('t328');
5321            }            }
5322            redo B;            redo B;
5323            } elsif ($token->{tag_name} eq 'html' and
5324                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
5325              !!!cp ('t329');
5326              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
5327              !!!next-token;
5328              redo B;
5329          } else {          } else {
5330            !!!parse-error (type => 'in frameset:/'.$token->{tag_name});            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5331                !!!cp ('t330');
5332                !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
5333              } else {
5334                !!!cp ('t331');
5335                !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
5336              }
5337            ## Ignore the token            ## Ignore the token
5338            !!!next-token;            !!!next-token;
5339            redo B;            redo B;
# Line 5107  sub _tree_construction_main ($) { Line 5341  sub _tree_construction_main ($) {
5341        } else {        } else {
5342          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5343        }        }
     } 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);  
5344    
5345                unless (length $token->{data}) {        ## ISSUE: An issue in spec here
5346                  !!!next-token;      } else {
5347                  redo B;        die "$0: $self->{insertion_mode}: Unknown insertion mode";
5348                }      }
             }  
5349    
5350              if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {      ## "in body" insertion mode
5351                !!!parse-error (type => 'after frameset:#character');      if ($token->{type} == START_TAG_TOKEN) {
5352          if ($token->{tag_name} eq 'script') {
5353            !!!cp ('t332');
5354            ## NOTE: This is an "as if in head" code clone
5355            $script_start_tag->($insert);
5356            redo B;
5357          } elsif ($token->{tag_name} eq 'style') {
5358            !!!cp ('t333');
5359            ## NOTE: This is an "as if in head" code clone
5360            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
5361            redo B;
5362          } elsif ({
5363                    base => 1, link => 1,
5364                   }->{$token->{tag_name}}) {
5365            !!!cp ('t334');
5366            ## NOTE: This is an "as if in head" code clone, only "-t" differs
5367            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5368            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
5369            !!!next-token;
5370            redo B;
5371          } elsif ($token->{tag_name} eq 'meta') {
5372            ## NOTE: This is an "as if in head" code clone, only "-t" differs
5373            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5374            my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
5375    
5376                ## Ignore the token.          unless ($self->{confident}) {
5377                if (length $token->{data}) {            if ($token->{attributes}->{charset}) { ## TODO: And if supported
5378                  ## reprocess the rest of characters              !!!cp ('t335');
5379                } else {              $self->{change_encoding}
5380                  !!!next-token;                  ->($self, $token->{attributes}->{charset}->{value});
5381                }              
5382                redo B;              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
5383                    ->set_user_data (manakai_has_reference =>
5384                                         $token->{attributes}->{charset}
5385                                             ->{has_reference});
5386              } elsif ($token->{attributes}->{content}) {
5387                ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
5388                if ($token->{attributes}->{content}->{value}
5389                    =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
5390                        [\x09-\x0D\x20]*=
5391                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
5392                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
5393                  !!!cp ('t336');
5394                  $self->{change_encoding}
5395                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
5396                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
5397                      ->set_user_data (manakai_has_reference =>
5398                                           $token->{attributes}->{content}
5399                                                 ->{has_reference});
5400              }              }
5401              }
         die qq[$0: Character "$token->{data}"];  
       } elsif ($token->{type} eq 'start tag') {  
         if ($token->{tag_name} eq 'noframes') {  
           $in_body->($insert_to_current);  
           redo B;  
5402          } else {          } else {
5403            !!!parse-error (type => 'after frameset:'.$token->{tag_name});            if ($token->{attributes}->{charset}) {
5404                !!!cp ('t337');
5405                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
5406                    ->set_user_data (manakai_has_reference =>
5407                                         $token->{attributes}->{charset}
5408                                             ->{has_reference});
5409              }
5410              if ($token->{attributes}->{content}) {
5411                !!!cp ('t338');
5412                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
5413                    ->set_user_data (manakai_has_reference =>
5414                                         $token->{attributes}->{content}
5415                                             ->{has_reference});
5416              }
5417            }
5418    
5419            !!!next-token;
5420            redo B;
5421          } elsif ($token->{tag_name} eq 'title') {
5422            !!!cp ('t341');
5423            !!!parse-error (type => 'in body:title');
5424            ## NOTE: This is an "as if in head" code clone
5425            $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {
5426              if (defined $self->{head_element}) {
5427                !!!cp ('t339');
5428                $self->{head_element}->append_child ($_[0]);
5429              } else {
5430                !!!cp ('t340');
5431                $insert->($_[0]);
5432              }
5433            });
5434            redo B;
5435          } elsif ($token->{tag_name} eq 'body') {
5436            !!!parse-error (type => 'in body:body');
5437                  
5438            if (@{$self->{open_elements}} == 1 or
5439                $self->{open_elements}->[1]->[1] ne 'body') {
5440              !!!cp ('t342');
5441            ## Ignore the token            ## Ignore the token
5442            } else {
5443              my $body_el = $self->{open_elements}->[1]->[0];
5444              for my $attr_name (keys %{$token->{attributes}}) {
5445                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
5446                  !!!cp ('t343');
5447                  $body_el->set_attribute_ns
5448                    (undef, [undef, $attr_name],
5449                     $token->{attributes}->{$attr_name}->{value});
5450                }
5451              }
5452            }
5453            !!!next-token;
5454            redo B;
5455          } elsif ({
5456                    address => 1, blockquote => 1, center => 1, dir => 1,
5457                    div => 1, dl => 1, fieldset => 1, listing => 1,
5458                    menu => 1, ol => 1, p => 1, ul => 1,
5459                    pre => 1,
5460                   }->{$token->{tag_name}}) {
5461            ## has a p element in scope
5462            INSCOPE: for (reverse @{$self->{open_elements}}) {
5463              if ($_->[1] eq 'p') {
5464                !!!cp ('t344');
5465                !!!back-token;
5466                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5467                redo B;
5468              } elsif ({
5469                        table => 1, caption => 1, td => 1, th => 1,
5470                        button => 1, marquee => 1, object => 1, html => 1,
5471                       }->{$_->[1]}) {
5472                !!!cp ('t345');
5473                last INSCOPE;
5474              }
5475            } # INSCOPE
5476              
5477            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5478            if ($token->{tag_name} eq 'pre') {
5479              !!!next-token;
5480              if ($token->{type} == CHARACTER_TOKEN) {
5481                $token->{data} =~ s/^\x0A//;
5482                unless (length $token->{data}) {
5483                  !!!cp ('t346');
5484                  !!!next-token;
5485                } else {
5486                  !!!cp ('t349');
5487                }
5488              } else {
5489                !!!cp ('t348');
5490              }
5491            } else {
5492              !!!cp ('t347');
5493            !!!next-token;            !!!next-token;
           redo B;  
5494          }          }
5495        } elsif ($token->{type} eq 'end tag') {          redo B;
5496          if ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'form') {
5497            $previous_insertion_mode = $self->{insertion_mode};          if (defined $self->{form_element}) {
5498            $self->{insertion_mode} = 'trailing end';            !!!cp ('t350');
5499              !!!parse-error (type => 'in form:form');
5500              ## Ignore the token
5501            !!!next-token;            !!!next-token;
5502            redo B;            redo B;
5503          } else {          } else {
5504            !!!parse-error (type => 'after frameset:/'.$token->{tag_name});            ## has a p element in scope
5505            ## Ignore the token            INSCOPE: for (reverse @{$self->{open_elements}}) {
5506                if ($_->[1] eq 'p') {
5507                  !!!cp ('t351');
5508                  !!!back-token;
5509                  $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5510                  redo B;
5511                } elsif ({
5512                          table => 1, caption => 1, td => 1, th => 1,
5513                          button => 1, marquee => 1, object => 1, html => 1,
5514                         }->{$_->[1]}) {
5515                  !!!cp ('t352');
5516                  last INSCOPE;
5517                }
5518              } # INSCOPE
5519                
5520              !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5521              $self->{form_element} = $self->{open_elements}->[-1]->[0];
5522            !!!next-token;            !!!next-token;
5523            redo B;            redo B;
5524          }          }
5525        } else {        } elsif ($token->{tag_name} eq 'li') {
5526          die "$0: $token->{type}: Unknown token type";          ## has a p element in scope
5527        }          INSCOPE: for (reverse @{$self->{open_elements}}) {
5528              if ($_->[1] eq 'p') {
5529                !!!cp ('t353');
5530                !!!back-token;
5531                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5532                redo B;
5533              } elsif ({
5534                        table => 1, caption => 1, td => 1, th => 1,
5535                        button => 1, marquee => 1, object => 1, html => 1,
5536                       }->{$_->[1]}) {
5537                !!!cp ('t354');
5538                last INSCOPE;
5539              }
5540            } # INSCOPE
5541              
5542            ## Step 1
5543            my $i = -1;
5544            my $node = $self->{open_elements}->[$i];
5545            LI: {
5546              ## Step 2
5547              if ($node->[1] eq 'li') {
5548                if ($i != -1) {
5549                  !!!cp ('t355');
5550                  !!!parse-error (type => 'end tag missing:'.
5551                                  $self->{open_elements}->[-1]->[1]);
5552                } else {
5553                  !!!cp ('t356');
5554                }
5555                splice @{$self->{open_elements}}, $i;
5556                last LI;
5557              } else {
5558                !!!cp ('t357');
5559              }
5560              
5561              ## Step 3
5562              if (not $formatting_category->{$node->[1]} and
5563                  #not $phrasing_category->{$node->[1]} and
5564                  ($special_category->{$node->[1]} or
5565                   $scoping_category->{$node->[1]}) and
5566                  $node->[1] ne 'address' and $node->[1] ne 'div') {
5567                !!!cp ('t358');
5568                last LI;
5569              }
5570              
5571              !!!cp ('t359');
5572              ## Step 4
5573              $i--;
5574              $node = $self->{open_elements}->[$i];
5575              redo LI;
5576            } # LI
5577              
5578            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5579            !!!next-token;
5580            redo B;
5581          } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {
5582            ## has a p element in scope
5583            INSCOPE: for (reverse @{$self->{open_elements}}) {
5584              if ($_->[1] eq 'p') {
5585                !!!cp ('t360');
5586                !!!back-token;
5587                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5588                redo B;
5589              } elsif ({
5590                        table => 1, caption => 1, td => 1, th => 1,
5591                        button => 1, marquee => 1, object => 1, html => 1,
5592                       }->{$_->[1]}) {
5593                !!!cp ('t361');
5594                last INSCOPE;
5595              }
5596            } # INSCOPE
5597              
5598            ## Step 1
5599            my $i = -1;
5600            my $node = $self->{open_elements}->[$i];
5601            LI: {
5602              ## Step 2
5603              if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {
5604                if ($i != -1) {
5605                  !!!cp ('t362');
5606                  !!!parse-error (type => 'end tag missing:'.
5607                                  $self->{open_elements}->[-1]->[1]);
5608                } else {
5609                  !!!cp ('t363');
5610                }
5611                splice @{$self->{open_elements}}, $i;
5612                last LI;
5613              } else {
5614                !!!cp ('t364');
5615              }
5616              
5617              ## Step 3
5618              if (not $formatting_category->{$node->[1]} and
5619                  #not $phrasing_category->{$node->[1]} and
5620                  ($special_category->{$node->[1]} or
5621                   $scoping_category->{$node->[1]}) and
5622                  $node->[1] ne 'address' and $node->[1] ne 'div') {
5623                !!!cp ('t365');
5624                last LI;
5625              }
5626              
5627              !!!cp ('t366');
5628              ## Step 4
5629              $i--;
5630              $node = $self->{open_elements}->[$i];
5631              redo LI;
5632            } # LI
5633              
5634            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5635            !!!next-token;
5636            redo B;
5637          } elsif ($token->{tag_name} eq 'plaintext') {
5638            ## has a p element in scope
5639            INSCOPE: for (reverse @{$self->{open_elements}}) {
5640              if ($_->[1] eq 'p') {
5641                !!!cp ('t367');
5642                !!!back-token;
5643                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5644                redo B;
5645              } elsif ({
5646                        table => 1, caption => 1, td => 1, th => 1,
5647                        button => 1, marquee => 1, object => 1, html => 1,
5648                       }->{$_->[1]}) {
5649                !!!cp ('t368');
5650                last INSCOPE;
5651              }
5652            } # INSCOPE
5653              
5654            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5655              
5656            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
5657              
5658            !!!next-token;
5659            redo B;
5660          } elsif ({
5661                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5662                   }->{$token->{tag_name}}) {
5663            ## has a p element in scope
5664            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5665              my $node = $self->{open_elements}->[$_];
5666              if ($node->[1] eq 'p') {
5667                !!!cp ('t369');
5668                !!!back-token;
5669                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5670                redo B;
5671              } elsif ({
5672                        table => 1, caption => 1, td => 1, th => 1,
5673                        button => 1, marquee => 1, object => 1, html => 1,
5674                       }->{$node->[1]}) {
5675                !!!cp ('t370');
5676                last INSCOPE;
5677              }
5678            } # INSCOPE
5679              
5680            ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>
5681            ## has an element in scope
5682            #my $i;
5683            #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5684            #  my $node = $self->{open_elements}->[$_];
5685            #  if ({
5686            #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5687            #      }->{$node->[1]}) {
5688            #    $i = $_;
5689            #    last INSCOPE;
5690            #  } elsif ({
5691            #            table => 1, caption => 1, td => 1, th => 1,
5692            #            button => 1, marquee => 1, object => 1, html => 1,
5693            #           }->{$node->[1]}) {
5694            #    last INSCOPE;
5695            #  }
5696            #} # INSCOPE
5697            #  
5698            #if (defined $i) {
5699            #  !!! parse-error (type => 'in hn:hn');
5700            #  splice @{$self->{open_elements}}, $i;
5701            #}
5702              
5703            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5704              
5705            !!!next-token;
5706            redo B;
5707          } elsif ($token->{tag_name} eq 'a') {
5708            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
5709              my $node = $active_formatting_elements->[$i];
5710              if ($node->[1] eq 'a') {
5711                !!!cp ('t371');
5712                !!!parse-error (type => 'in a:a');
5713                
5714                !!!back-token;
5715                $token = {type => END_TAG_TOKEN, tag_name => 'a'};
5716                $formatting_end_tag->($token->{tag_name});
5717                
5718                AFE2: for (reverse 0..$#$active_formatting_elements) {
5719                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
5720                    !!!cp ('t372');
5721                    splice @$active_formatting_elements, $_, 1;
5722                    last AFE2;
5723                  }
5724                } # AFE2
5725                OE: for (reverse 0..$#{$self->{open_elements}}) {
5726                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
5727                    !!!cp ('t373');
5728                    splice @{$self->{open_elements}}, $_, 1;
5729                    last OE;
5730                  }
5731                } # OE
5732                last AFE;
5733              } elsif ($node->[0] eq '#marker') {
5734                !!!cp ('t374');
5735                last AFE;
5736              }
5737            } # AFE
5738              
5739            $reconstruct_active_formatting_elements->($insert_to_current);
5740    
5741        ## ISSUE: An issue in spec here          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5742      } elsif ($self->{insertion_mode} eq 'trailing end') {          push @$active_formatting_elements, $self->{open_elements}->[-1];
5743        ## states in the main stage is preserved yet # MUST  
5744                  !!!next-token;
5745        if ($token->{type} eq 'character') {          redo B;
5746          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        } elsif ({
5747            my $data = $1;                  b => 1, big => 1, em => 1, font => 1, i => 1,
5748            ## As if in the main phase.                  s => 1, small => 1, strile => 1,
5749            ## NOTE: The insertion mode in the main phase                  strong => 1, tt => 1, u => 1,
5750            ## just before the phase has been changed to the trailing                 }->{$token->{tag_name}}) {
5751            ## end phase is either "after body" or "after frameset".          !!!cp ('t375');
5752            $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5753            
5754            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5755            push @$active_formatting_elements, $self->{open_elements}->[-1];
5756            
5757            !!!next-token;
5758            redo B;
5759          } elsif ($token->{tag_name} eq 'nobr') {
5760            $reconstruct_active_formatting_elements->($insert_to_current);
5761    
5762            ## has a |nobr| element in scope
5763            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5764              my $node = $self->{open_elements}->[$_];
5765              if ($node->[1] eq 'nobr') {
5766                !!!cp ('t376');
5767                !!!parse-error (type => 'in nobr:nobr');
5768                !!!back-token;
5769                $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};
5770                redo B;
5771              } elsif ({
5772                        table => 1, caption => 1, td => 1, th => 1,
5773                        button => 1, marquee => 1, object => 1, html => 1,
5774                       }->{$node->[1]}) {
5775                !!!cp ('t377');
5776                last INSCOPE;
5777              }
5778            } # INSCOPE
5779            
5780            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5781            push @$active_formatting_elements, $self->{open_elements}->[-1];
5782            
5783            !!!next-token;
5784            redo B;
5785          } elsif ($token->{tag_name} eq 'button') {
5786            ## has a button element in scope
5787            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5788              my $node = $self->{open_elements}->[$_];
5789              if ($node->[1] eq 'button') {
5790                !!!cp ('t378');
5791                !!!parse-error (type => 'in button:button');
5792                !!!back-token;
5793                $token = {type => END_TAG_TOKEN, tag_name => 'button'};
5794                redo B;
5795              } elsif ({
5796                        table => 1, caption => 1, td => 1, th => 1,
5797                        button => 1, marquee => 1, object => 1, html => 1,
5798                       }->{$node->[1]}) {
5799                !!!cp ('t379');
5800                last INSCOPE;
5801              }
5802            } # INSCOPE
5803                        
5804            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);          $reconstruct_active_formatting_elements->($insert_to_current);
5805                        
5806            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5807            push @$active_formatting_elements, ['#marker', ''];
5808    
5809            !!!next-token;
5810            redo B;
5811          } elsif ($token->{tag_name} eq 'marquee' or
5812                   $token->{tag_name} eq 'object') {
5813            !!!cp ('t380');
5814            $reconstruct_active_formatting_elements->($insert_to_current);
5815            
5816            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5817            push @$active_formatting_elements, ['#marker', ''];
5818            
5819            !!!next-token;
5820            redo B;
5821          } elsif ($token->{tag_name} eq 'xmp') {
5822            !!!cp ('t381');
5823            $reconstruct_active_formatting_elements->($insert_to_current);
5824            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
5825            redo B;
5826          } elsif ($token->{tag_name} eq 'table') {
5827            ## has a p element in scope
5828            INSCOPE: for (reverse @{$self->{open_elements}}) {
5829              if ($_->[1] eq 'p') {
5830                !!!cp ('t382');
5831                !!!back-token;
5832                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5833                redo B;
5834              } elsif ({
5835                        table => 1, caption => 1, td => 1, th => 1,
5836                        button => 1, marquee => 1, object => 1, html => 1,
5837                       }->{$_->[1]}) {
5838                !!!cp ('t383');
5839                last INSCOPE;
5840              }
5841            } # INSCOPE
5842              
5843            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5844              
5845            $self->{insertion_mode} = IN_TABLE_IM;
5846              
5847            !!!next-token;
5848            redo B;
5849          } elsif ({
5850                    area => 1, basefont => 1, bgsound => 1, br => 1,
5851                    embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
5852                    image => 1,
5853                   }->{$token->{tag_name}}) {
5854            if ($token->{tag_name} eq 'image') {
5855              !!!cp ('t384');
5856              !!!parse-error (type => 'image');
5857              $token->{tag_name} = 'img';
5858            } else {
5859              !!!cp ('t385');
5860            }
5861    
5862            ## NOTE: There is an "as if <br>" code clone.
5863            $reconstruct_active_formatting_elements->($insert_to_current);
5864            
5865            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5866            pop @{$self->{open_elements}};
5867            
5868            !!!next-token;
5869            redo B;
5870          } elsif ($token->{tag_name} eq 'hr') {
5871            ## has a p element in scope
5872            INSCOPE: for (reverse @{$self->{open_elements}}) {
5873              if ($_->[1] eq 'p') {
5874                !!!cp ('t386');
5875                !!!back-token;
5876                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5877                redo B;
5878              } elsif ({
5879                        table => 1, caption => 1, td => 1, th => 1,
5880                        button => 1, marquee => 1, object => 1, html => 1,
5881                       }->{$_->[1]}) {
5882                !!!cp ('t387');
5883                last INSCOPE;
5884              }
5885            } # INSCOPE
5886              
5887            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5888            pop @{$self->{open_elements}};
5889              
5890            !!!next-token;
5891            redo B;
5892          } elsif ($token->{tag_name} eq 'input') {
5893            !!!cp ('t388');
5894            $reconstruct_active_formatting_elements->($insert_to_current);
5895            
5896            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5897            ## TODO: associate with $self->{form_element} if defined
5898            pop @{$self->{open_elements}};
5899            
5900            !!!next-token;
5901            redo B;
5902          } elsif ($token->{tag_name} eq 'isindex') {
5903            !!!parse-error (type => 'isindex');
5904            
5905            if (defined $self->{form_element}) {
5906              !!!cp ('t389');
5907              ## Ignore the token
5908              !!!next-token;
5909              redo B;
5910            } else {
5911              my $at = $token->{attributes};
5912              my $form_attrs;
5913              $form_attrs->{action} = $at->{action} if $at->{action};
5914              my $prompt_attr = $at->{prompt};
5915              $at->{name} = {name => 'name', value => 'isindex'};
5916              delete $at->{action};
5917              delete $at->{prompt};
5918              my @tokens = (
5919                            {type => START_TAG_TOKEN, tag_name => 'form',
5920                             attributes => $form_attrs},
5921                            {type => START_TAG_TOKEN, tag_name => 'hr'},
5922                            {type => START_TAG_TOKEN, tag_name => 'p'},
5923                            {type => START_TAG_TOKEN, tag_name => 'label'},
5924                           );
5925              if ($prompt_attr) {
5926                !!!cp ('t390');
5927                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};
5928              } else {
5929                !!!cp ('t391');
5930                push @tokens, {type => CHARACTER_TOKEN,
5931                               data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
5932                ## TODO: make this configurable
5933              }
5934              push @tokens,
5935                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},
5936                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
5937                            {type => END_TAG_TOKEN, tag_name => 'label'},
5938                            {type => END_TAG_TOKEN, tag_name => 'p'},
5939                            {type => START_TAG_TOKEN, tag_name => 'hr'},
5940                            {type => END_TAG_TOKEN, tag_name => 'form'};
5941              $token = shift @tokens;
5942              !!!back-token (@tokens);
5943              redo B;
5944            }
5945          } elsif ($token->{tag_name} eq 'textarea') {
5946            my $tag_name = $token->{tag_name};
5947            my $el;
5948            !!!create-element ($el, $token->{tag_name}, $token->{attributes});
5949            
5950            ## TODO: $self->{form_element} if defined
5951            $self->{content_model} = RCDATA_CONTENT_MODEL;
5952            delete $self->{escape}; # MUST
5953            
5954            $insert->($el);
5955            
5956            my $text = '';
5957            !!!next-token;
5958            if ($token->{type} == CHARACTER_TOKEN) {
5959              $token->{data} =~ s/^\x0A//;
5960            unless (length $token->{data}) {            unless (length $token->{data}) {
5961                !!!cp ('t392');
5962              !!!next-token;              !!!next-token;
5963              redo B;            } else {
5964                !!!cp ('t393');
5965            }            }
5966            } else {
5967              !!!cp ('t394');
5968            }
5969            while ($token->{type} == CHARACTER_TOKEN) {
5970              !!!cp ('t395');
5971              $text .= $token->{data};
5972              !!!next-token;
5973          }          }
5974            if (length $text) {
5975              !!!cp ('t396');
5976              $el->manakai_append_text ($text);
5977            }
5978            
5979            $self->{content_model} = PCDATA_CONTENT_MODEL;
5980            
5981            if ($token->{type} == END_TAG_TOKEN and
5982                $token->{tag_name} eq $tag_name) {
5983              !!!cp ('t397');
5984              ## Ignore the token
5985            } else {
5986              !!!cp ('t398');
5987              !!!parse-error (type => 'in RCDATA:#'.$token->{type});
5988            }
5989            !!!next-token;
5990            redo B;
5991          } elsif ({
5992                    iframe => 1,
5993                    noembed => 1,
5994                    noframes => 1,
5995                    noscript => 0, ## TODO: 1 if scripting is enabled
5996                   }->{$token->{tag_name}}) {
5997            !!!cp ('t399');
5998            ## NOTE: There is an "as if in body" code clone.
5999            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
6000            redo B;
6001          } elsif ($token->{tag_name} eq 'select') {
6002            !!!cp ('t400');
6003            $reconstruct_active_formatting_elements->($insert_to_current);
6004            
6005            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
6006            
6007            $self->{insertion_mode} = IN_SELECT_IM;
6008            !!!next-token;
6009            redo B;
6010          } elsif ({
6011                    caption => 1, col => 1, colgroup => 1, frame => 1,
6012                    frameset => 1, head => 1, option => 1, optgroup => 1,
6013                    tbody => 1, td => 1, tfoot => 1, th => 1,
6014                    thead => 1, tr => 1,
6015                   }->{$token->{tag_name}}) {
6016            !!!cp ('t401');
6017            !!!parse-error (type => 'in body:'.$token->{tag_name});
6018            ## Ignore the token
6019            !!!next-token;
6020            redo B;
6021            
6022            ## ISSUE: An issue on HTML5 new elements in the spec.
6023          } else {
6024            !!!cp ('t402');
6025            $reconstruct_active_formatting_elements->($insert_to_current);
6026            
6027            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
6028            
6029            !!!next-token;
6030            redo B;
6031          }
6032        } elsif ($token->{type} == END_TAG_TOKEN) {
6033          if ($token->{tag_name} eq 'body') {
6034            if (@{$self->{open_elements}} > 1 and
6035                $self->{open_elements}->[1]->[1] eq 'body') {
6036              for (@{$self->{open_elements}}) {
6037                unless ({
6038                           dd => 1, dt => 1, li => 1, p => 1, td => 1,
6039                           th => 1, tr => 1, body => 1, html => 1,
6040                         tbody => 1, tfoot => 1, thead => 1,
6041                        }->{$_->[1]}) {
6042                  !!!cp ('t403');
6043                  !!!parse-error (type => 'not closed:'.$_->[1]);
6044                } else {
6045                  !!!cp ('t404');
6046                }
6047              }
6048    
6049          !!!parse-error (type => 'after html:#character');            $self->{insertion_mode} = AFTER_BODY_IM;
6050          $self->{insertion_mode} = $previous_insertion_mode;            !!!next-token;
6051          ## reprocess            redo B;
6052            } else {
6053              !!!cp ('t405');
6054              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6055              ## Ignore the token
6056              !!!next-token;
6057              redo B;
6058            }
6059          } elsif ($token->{tag_name} eq 'html') {
6060            if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {
6061              ## ISSUE: There is an issue in the spec.
6062              if ($self->{open_elements}->[-1]->[1] ne 'body') {
6063                !!!cp ('t406');
6064                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
6065              } else {
6066                !!!cp ('t407');
6067              }
6068              $self->{insertion_mode} = AFTER_BODY_IM;
6069              ## reprocess
6070              redo B;
6071            } else {
6072              !!!cp ('t408');
6073              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6074              ## Ignore the token
6075              !!!next-token;
6076              redo B;
6077            }
6078          } elsif ({
6079                    address => 1, blockquote => 1, center => 1, dir => 1,
6080                    div => 1, dl => 1, fieldset => 1, listing => 1,
6081                    menu => 1, ol => 1, pre => 1, ul => 1,
6082                    p => 1,
6083                    dd => 1, dt => 1, li => 1,
6084                    button => 1, marquee => 1, object => 1,
6085                   }->{$token->{tag_name}}) {
6086            ## has an element in scope
6087            my $i;
6088            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6089              my $node = $self->{open_elements}->[$_];
6090              if ($node->[1] eq $token->{tag_name}) {
6091                ## generate implied end tags
6092                if ({
6093                     dd => ($token->{tag_name} ne 'dd'),
6094                     dt => ($token->{tag_name} ne 'dt'),
6095                     li => ($token->{tag_name} ne 'li'),
6096                     p => ($token->{tag_name} ne 'p'),
6097                     td => 1, th => 1, tr => 1,
6098                     tbody => 1, tfoot=> 1, thead => 1,
6099                    }->{$self->{open_elements}->[-1]->[1]}) {
6100                  !!!cp ('t409');
6101                  !!!back-token;
6102                  $token = {type => END_TAG_TOKEN,
6103                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
6104                  redo B;
6105                }
6106                
6107                !!!cp ('t410');
6108                $i = $_;
6109                last INSCOPE unless $token->{tag_name} eq 'p';
6110              } elsif ({
6111                        table => 1, caption => 1, td => 1, th => 1,
6112                        button => 1, marquee => 1, object => 1, html => 1,
6113                       }->{$node->[1]}) {
6114                !!!cp ('t411');
6115                last INSCOPE;
6116              }
6117            } # INSCOPE
6118            
6119            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6120              if (defined $i) {
6121                !!!cp ('t412');
6122                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
6123              } else {
6124                !!!cp ('t413');
6125                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6126              }
6127            }
6128            
6129            if (defined $i) {
6130              !!!cp ('t414');
6131              splice @{$self->{open_elements}}, $i;
6132            } elsif ($token->{tag_name} eq 'p') {
6133              !!!cp ('t415');
6134              ## As if <p>, then reprocess the current token
6135              my $el;
6136              !!!create-element ($el, 'p');
6137              $insert->($el);
6138            } else {
6139              !!!cp ('t416');
6140            }
6141            $clear_up_to_marker->()
6142              if {
6143                button => 1, marquee => 1, object => 1,
6144              }->{$token->{tag_name}};
6145            !!!next-token;
6146          redo B;          redo B;
6147        } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{tag_name} eq 'form') {
6148          !!!parse-error (type => 'after html:'.$token->{tag_name});          ## has an element in scope
6149          $self->{insertion_mode} = $previous_insertion_mode;          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6150          ## reprocess            my $node = $self->{open_elements}->[$_];
6151              if ($node->[1] eq $token->{tag_name}) {
6152                ## generate implied end tags
6153                if ({
6154                     dd => 1, dt => 1, li => 1, p => 1,
6155    
6156                     ## NOTE: The following elements never appear here, maybe.
6157                     td => 1, th => 1, tr => 1,
6158                     tbody => 1, tfoot => 1, thead => 1,
6159                    }->{$self->{open_elements}->[-1]->[1]}) {
6160                  !!!cp ('t417');
6161                  !!!back-token;
6162                  $token = {type => END_TAG_TOKEN,
6163                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
6164                  redo B;
6165                }
6166                
6167                !!!cp ('t418');
6168                last INSCOPE;
6169              } elsif ({
6170                        table => 1, caption => 1, td => 1, th => 1,
6171                        button => 1, marquee => 1, object => 1, html => 1,
6172                       }->{$node->[1]}) {
6173                !!!cp ('t419');
6174                last INSCOPE;
6175              }
6176            } # INSCOPE
6177            
6178            if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
6179              !!!cp ('t420');
6180              pop @{$self->{open_elements}};
6181            } else {
6182              !!!cp ('t421');
6183              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6184            }
6185    
6186            undef $self->{form_element};
6187            !!!next-token;
6188          redo B;          redo B;
6189        } elsif ($token->{type} eq 'end tag') {        } elsif ({
6190          !!!parse-error (type => 'after html:/'.$token->{tag_name});                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6191          $self->{insertion_mode} = $previous_insertion_mode;                 }->{$token->{tag_name}}) {
6192          ## reprocess          ## has an element in scope
6193            my $i;
6194            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6195              my $node = $self->{open_elements}->[$_];
6196              if ({
6197                   h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6198                  }->{$node->[1]}) {
6199                ## generate implied end tags
6200                if ({
6201                     dd => 1, dt => 1, li => 1, p => 1,
6202                     td => 1, th => 1, tr => 1,
6203                     tbody => 1, tfoot=> 1, thead => 1,
6204                    }->{$self->{open_elements}->[-1]->[1]}) {
6205                  !!!cp ('t422');
6206                  !!!back-token;
6207                  $token = {type => END_TAG_TOKEN,
6208                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
6209                  redo B;
6210                }
6211    
6212                !!!cp ('t423');
6213                $i = $_;
6214                last INSCOPE;
6215              } elsif ({
6216                        table => 1, caption => 1, td => 1, th => 1,
6217                        button => 1, marquee => 1, object => 1, html => 1,
6218                       }->{$node->[1]}) {
6219                !!!cp ('t424');
6220                last INSCOPE;
6221              }
6222            } # INSCOPE
6223            
6224            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6225              !!!cp ('t425');
6226              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6227            } else {
6228              !!!cp ('t426');
6229            }
6230            
6231            splice @{$self->{open_elements}}, $i if defined $i;
6232            !!!next-token;
6233            redo B;
6234          } elsif ({
6235                    a => 1,
6236                    b => 1, big => 1, em => 1, font => 1, i => 1,
6237                    nobr => 1, s => 1, small => 1, strile => 1,
6238                    strong => 1, tt => 1, u => 1,
6239                   }->{$token->{tag_name}}) {
6240            !!!cp ('t427');
6241            $formatting_end_tag->($token->{tag_name});
6242            redo B;
6243          } elsif ($token->{tag_name} eq 'br') {
6244            !!!cp ('t428');
6245            !!!parse-error (type => 'unmatched end tag:br');
6246    
6247            ## As if <br>
6248            $reconstruct_active_formatting_elements->($insert_to_current);
6249            
6250            my $el;
6251            !!!create-element ($el, 'br');
6252            $insert->($el);
6253            
6254            ## Ignore the token.
6255            !!!next-token;
6256          redo B;          redo B;
6257          } elsif ({
6258                    caption => 1, col => 1, colgroup => 1, frame => 1,
6259                    frameset => 1, head => 1, option => 1, optgroup => 1,
6260                    tbody => 1, td => 1, tfoot => 1, th => 1,
6261                    thead => 1, tr => 1,
6262                    area => 1, basefont => 1, bgsound => 1,
6263                    embed => 1, hr => 1, iframe => 1, image => 1,
6264                    img => 1, input => 1, isindex => 1, noembed => 1,
6265                    noframes => 1, param => 1, select => 1, spacer => 1,
6266                    table => 1, textarea => 1, wbr => 1,
6267                    noscript => 0, ## TODO: if scripting is enabled
6268                   }->{$token->{tag_name}}) {
6269            !!!cp ('t429');
6270            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6271            ## Ignore the token
6272            !!!next-token;
6273            redo B;
6274            
6275            ## ISSUE: Issue on HTML5 new elements in spec
6276            
6277        } else {        } else {
6278          die "$0: $token->{type}: Unknown token";          ## Step 1
6279            my $node_i = -1;
6280            my $node = $self->{open_elements}->[$node_i];
6281    
6282            ## Step 2
6283            S2: {
6284              if ($node->[1] eq $token->{tag_name}) {
6285                ## Step 1
6286                ## generate implied end tags
6287                if ({
6288                     dd => 1, dt => 1, li => 1, p => 1,
6289                     td => 1, th => 1, tr => 1,
6290                     tbody => 1, tfoot => 1, thead => 1,
6291                    }->{$self->{open_elements}->[-1]->[1]}) {
6292                  !!!cp ('t430');
6293                  ## ISSUE: Can this case be reached?
6294                  !!!back-token;
6295                  $token = {type => END_TAG_TOKEN,
6296                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
6297                  redo B;
6298                }
6299            
6300                ## Step 2
6301                if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
6302                  !!!cp ('t431');
6303                  ## NOTE: <x><y></x>
6304                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
6305                } else {
6306                  !!!cp ('t432');
6307                }
6308                
6309                ## Step 3
6310                splice @{$self->{open_elements}}, $node_i;
6311    
6312                !!!next-token;
6313                last S2;
6314              } else {
6315                ## Step 3
6316                if (not $formatting_category->{$node->[1]} and
6317                    #not $phrasing_category->{$node->[1]} and
6318                    ($special_category->{$node->[1]} or
6319                     $scoping_category->{$node->[1]})) {
6320                  !!!cp ('t433');
6321                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6322                  ## Ignore the token
6323                  !!!next-token;
6324                  last S2;
6325                }
6326    
6327                !!!cp ('t434');
6328              }
6329              
6330              ## Step 4
6331              $node_i--;
6332              $node = $self->{open_elements}->[$node_i];
6333              
6334              ## Step 5;
6335              redo S2;
6336            } # S2
6337            redo B;
6338        }        }
     } else {  
       die "$0: $self->{insertion_mode}: Unknown insertion mode";  
6339      }      }
6340        redo B;
6341    } # B    } # B
6342    
6343    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 5211  sub set_inner_html ($$$) { Line 6351  sub set_inner_html ($$$) {
6351    my $s = \$_[0];    my $s = \$_[0];
6352    my $onerror = $_[1];    my $onerror = $_[1];
6353    
6354      ## ISSUE: Should {confident} be true?
6355    
6356    my $nt = $node->node_type;    my $nt = $node->node_type;
6357    if ($nt == 9) {    if ($nt == 9) {
6358      # MUST      # MUST
# Line 5239  sub set_inner_html ($$$) { Line 6381  sub set_inner_html ($$$) {
6381      my $p = $class->new;      my $p = $class->new;
6382      $p->{document} = $doc;      $p->{document} = $doc;
6383    
6384      ## Step 9 # MUST      ## Step 8 # MUST
6385      my $i = 0;      my $i = 0;
6386      my $line = 1;      my $line = 1;
6387      my $column = 0;      my $column = 0;
6388      $p->{set_next_input_character} = sub {      $p->{set_next_char} = sub {
6389        my $self = shift;        my $self = shift;
6390    
6391        pop @{$self->{prev_input_character}};        pop @{$self->{prev_char}};
6392        unshift @{$self->{prev_input_character}}, $self->{next_input_character};        unshift @{$self->{prev_char}}, $self->{next_char};
6393    
6394        $self->{next_input_character} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
6395        $self->{next_input_character} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
6396        $column++;        $column++;
6397    
6398        if ($self->{next_input_character} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
6399          $line++;          $line++;
6400          $column = 0;          $column = 0;
6401        } elsif ($self->{next_input_character} == 0x000D) { # CR          !!!cp ('i1');
6402          } elsif ($self->{next_char} == 0x000D) { # CR
6403          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
6404          $self->{next_input_character} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
6405          $line++;          $line++;
6406          $column = 0;          $column = 0;
6407        } elsif ($self->{next_input_character} > 0x10FFFF) {          !!!cp ('i2');
6408          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        } elsif ($self->{next_char} > 0x10FFFF) {
6409        } elsif ($self->{next_input_character} == 0x0000) { # NULL          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
6410            !!!cp ('i3');
6411          } elsif ($self->{next_char} == 0x0000) { # NULL
6412            !!!cp ('i4');
6413          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
6414          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
6415        }        }
6416      };      };
6417      $p->{prev_input_character} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
6418      $p->{next_input_character} = -1;      $p->{next_char} = -1;
6419            
6420      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
6421        my (%opt) = @_;        my (%opt) = @_;
# Line 5283  sub set_inner_html ($$$) { Line 6429  sub set_inner_html ($$$) {
6429      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
6430    
6431      ## Step 2      ## Step 2
6432      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
6433      $p->{content_model_flag} = {      $p->{content_model} = {
6434        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
6435        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
6436        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
6437        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
6438        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
6439        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
6440        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
6441        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
6442        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
6443        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
6444      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
6445         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
6446            unless defined $p->{content_model};
6447            ## ISSUE: What is "the name of the element"? local name?
6448    
6449      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $node_ln];
6450    
6451      ## Step 4      ## Step 3
6452      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
6453        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
6454    
6455      ## Step 5 # MUST      ## Step 4 # MUST
6456      $doc->append_child ($root);      $doc->append_child ($root);
6457    
6458      ## Step 6 # MUST      ## Step 5 # MUST
6459      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, 'html'];
6460    
6461      undef $p->{head_element};      undef $p->{head_element};
6462    
6463      ## Step 7 # MUST      ## Step 6 # MUST
6464      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
6465    
6466      ## Step 8 # MUST      ## Step 7 # MUST
6467      my $anode = $node;      my $anode = $node;
6468      AN: while (defined $anode) {      AN: while (defined $anode) {
6469        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
6470          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
6471          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
6472            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
6473                !!!cp ('i5');
6474              $p->{form_element} = $anode;              $p->{form_element} = $anode;
6475              last AN;              last AN;
6476            }            }
# Line 5330  sub set_inner_html ($$$) { Line 6479  sub set_inner_html ($$$) {
6479        $anode = $anode->parent_node;        $anode = $anode->parent_node;
6480      } # AN      } # AN
6481            
6482      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
6483      {      {
6484        my $self = $p;        my $self = $p;
6485        !!!next-token;        !!!next-token;
6486      }      }
6487      $p->_tree_construction_main;      $p->_tree_construction_main;
6488    
6489      ## Step 11 # MUST      ## Step 10 # MUST
6490      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
6491      for (@cn) {      for (@cn) {
6492        $node->remove_child ($_);        $node->remove_child ($_);
6493      }      }
6494      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
6495    
6496      ## Step 12 # MUST      ## Step 11 # MUST
6497      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
6498      for (@cn) {      for (@cn) {
6499        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);
# Line 5361  sub set_inner_html ($$$) { Line 6509  sub set_inner_html ($$$) {
6509    
6510  } # tree construction stage  } # tree construction stage
6511    
6512  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
6513    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  
6514    
6515  1;  1;
6516  # $Date$  # $Date$

Legend:
Removed from v.1.38  
changed lines
  Added in v.1.84

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24