/[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.64 by wakaba, Sun Nov 11 08:39:42 2007 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);
# Line 84  my $formatting_category = { Line 85  my $formatting_category = {
85  };  };
86  # $phrasing_category: all other elements  # $phrasing_category: all other elements
87    
88    sub parse_byte_string ($$$$;$) {
89      my $self = ref $_[0] ? shift : shift->new;
90      my $charset = shift;
91      my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);
92      my $s;
93      
94      if (defined $charset) {
95        require Encode; ## TODO: decode(utf8) don't delete BOM
96        $s = \ (Encode::decode ($charset, $$bytes_s));
97        $self->{input_encoding} = lc $charset; ## TODO: normalize name
98        $self->{confident} = 1;
99      } else {
100        $charset = 'windows-1252'; ## TODO: for now.
101        $s = \ (Encode::decode ($charset, $$bytes_s));
102        $self->{input_encoding} = $charset;
103        $self->{confident} = 0;
104      }
105    
106      $self->{change_encoding} = sub {
107        my $self = shift;
108        my $charset = lc shift;
109        ## TODO: if $charset is supported
110        ## TODO: normalize charset name
111    
112        ## "Change the encoding" algorithm:
113    
114        ## Step 1    
115        if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?
116          $charset = 'utf-8';
117        }
118    
119        ## Step 2
120        if (defined $self->{input_encoding} and
121            $self->{input_encoding} eq $charset) {
122          $self->{confident} = 1;
123          return;
124        }
125    
126        !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
127            ':'.$charset, level => 'w');
128    
129        ## Step 3
130        # if (can) {
131          ## change the encoding on the fly.
132          #$self->{confident} = 1;
133          #return;
134        # }
135    
136        ## Step 4
137        throw Whatpm::HTML::RestartParser (charset => $charset);
138      }; # $self->{change_encoding}
139    
140      my @args = @_; shift @args; # $s
141      my $return;
142      try {
143        $return = $self->parse_char_string ($s, @args);  
144      } catch Whatpm::HTML::RestartParser with {
145        my $charset = shift->{charset};
146        $s = \ (Encode::decode ($charset, $$bytes_s));    
147        $self->{input_encoding} = $charset; ## TODO: normalize
148        $self->{confident} = 1;
149        $return = $self->parse_char_string ($s, @args);
150      };
151      return $return;
152    } # parse_byte_string
153    
154    *parse_char_string = \&parse_string;
155    
156  sub parse_string ($$$;$) {  sub parse_string ($$$;$) {
157    my $self = shift->new;    my $self = ref $_[0] ? shift : shift->new;
158    my $s = \$_[0];    my $s = ref $_[0] ? $_[0] : \($_[0]);
159    $self->{document} = $_[1];    $self->{document} = $_[1];
160      @{$self->{document}->child_nodes} = ();
161    
162    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
163    
164      $self->{confident} = 1 unless exists $self->{confident};
165      $self->{document}->input_encoding ($self->{input_encoding})
166          if defined $self->{input_encoding};
167    
168    my $i = 0;    my $i = 0;
169    my $line = 1;    my $line = 1;
170    my $column = 0;    my $column = 0;
# Line 147  sub new ($) { Line 221  sub new ($) {
221    $self->{parse_error} = sub {    $self->{parse_error} = sub {
222      #      #
223    };    };
224      $self->{change_encoding} = sub {
225        # if ($_[0] is a supported encoding) {
226        #   run "change the encoding" algorithm;
227        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
228        # }
229      };
230      $self->{application_cache_selection} = sub {
231        #
232      };
233    return $self;    return $self;
234  } # new  } # new
235    
236    sub CM_ENTITY () { 0b001 } # & markup in data
237    sub CM_LIMITED_MARKUP () { 0b010 } # < markup in data (limited)
238    sub CM_FULL_MARKUP () { 0b100 } # < markup in data (any)
239    
240    sub PLAINTEXT_CONTENT_MODEL () { 0 }
241    sub CDATA_CONTENT_MODEL () { CM_LIMITED_MARKUP }
242    sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
243    sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
244    
245    sub DATA_STATE () { 0 }
246    sub ENTITY_DATA_STATE () { 1 }
247    sub TAG_OPEN_STATE () { 2 }
248    sub CLOSE_TAG_OPEN_STATE () { 3 }
249    sub TAG_NAME_STATE () { 4 }
250    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
251    sub ATTRIBUTE_NAME_STATE () { 6 }
252    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
253    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
254    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
255    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
256    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
257    sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
258    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
259    sub COMMENT_START_STATE () { 14 }
260    sub COMMENT_START_DASH_STATE () { 15 }
261    sub COMMENT_STATE () { 16 }
262    sub COMMENT_END_STATE () { 17 }
263    sub COMMENT_END_DASH_STATE () { 18 }
264    sub BOGUS_COMMENT_STATE () { 19 }
265    sub DOCTYPE_STATE () { 20 }
266    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
267    sub DOCTYPE_NAME_STATE () { 22 }
268    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
269    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
270    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
271    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
272    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
273    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
274    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
275    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
276    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
277    sub BOGUS_DOCTYPE_STATE () { 32 }
278    
279    sub DOCTYPE_TOKEN () { 1 }
280    sub COMMENT_TOKEN () { 2 }
281    sub START_TAG_TOKEN () { 3 }
282    sub END_TAG_TOKEN () { 4 }
283    sub END_OF_FILE_TOKEN () { 5 }
284    sub CHARACTER_TOKEN () { 6 }
285    
286    sub AFTER_HTML_IMS () { 0b100 }
287    sub HEAD_IMS ()       { 0b1000 }
288    sub BODY_IMS ()       { 0b10000 }
289    sub BODY_TABLE_IMS () { 0b100000 }
290    sub TABLE_IMS ()      { 0b1000000 }
291    sub ROW_IMS ()        { 0b10000000 }
292    sub BODY_AFTER_IMS () { 0b100000000 }
293    sub FRAME_IMS ()      { 0b1000000000 }
294    
295    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
296    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
297    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
298    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
299    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
300    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
301    sub IN_BODY_IM () { BODY_IMS }
302    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
303    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
304    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
305    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
306    sub IN_TABLE_IM () { TABLE_IMS }
307    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
308    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
309    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
310    sub IN_SELECT_IM () { 0b01 }
311    sub IN_COLUMN_GROUP_IM () { 0b10 }
312    
313  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
314    
315  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
316    my $self = shift;    my $self = shift;
317    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
318    $self->{content_model_flag} = 'PCDATA'; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
319    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
320    undef $self->{current_attribute};    undef $self->{current_attribute};
321    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
# Line 168  sub _initialize_tokenizer ($) { Line 328  sub _initialize_tokenizer ($) {
328  } # _initialize_tokenizer  } # _initialize_tokenizer
329    
330  ## A token has:  ## A token has:
331  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
332  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
333  ##   ->{name} (DOCTYPE, start tag (tag name), end tag (tag name))  ##   ->{name} (DOCTYPE_TOKEN)
334  ##   ->{public_identifier} (DOCTYPE)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
335  ##   ->{system_identifier} (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
336  ##   ->{correct} == 1 or 0 (DOCTYPE)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
337  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)
338  ##   ->{data} (comment, character)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
339    ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
340    
341  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
342    
# Line 185  sub _initialize_tokenizer ($) { Line 346  sub _initialize_tokenizer ($) {
346  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
347  ## and removed from the list.  ## and removed from the list.
348    
349    ## NOTE: HTML5 "Writing HTML documents" section, applied to
350    ## documents and not to user agents and conformance checkers,
351    ## contains some requirements that are not detected by the
352    ## parsing algorithm:
353    ## - Some requirements on character encoding declarations. ## TODO
354    ## - "Elements MUST NOT contain content that their content model disallows."
355    ##   ... Some are parse error, some are not (will be reported by c.c.).
356    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
357    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
358    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
359    
360    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
361    ## be detected by the HTML5 parsing algorithm:
362    ## - Text,
363    
364  sub _get_next_token ($) {  sub _get_next_token ($) {
365    my $self = shift;    my $self = shift;
366    if (@{$self->{token}}) {    if (@{$self->{token}}) {
# Line 192  sub _get_next_token ($) { Line 368  sub _get_next_token ($) {
368    }    }
369    
370    A: {    A: {
371      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
372        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_input_character} == 0x0026) { # &
373          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA
374              $self->{content_model_flag} eq 'RCDATA') {            $self->{state} = ENTITY_DATA_STATE;
           $self->{state} = 'entity data';  
375            !!!next-input-character;            !!!next-input-character;
376            redo A;            redo A;
377          } else {          } else {
378            #            #
379          }          }
380        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_input_character} == 0x002D) { # -
381          if ($self->{content_model_flag} eq 'RCDATA' or          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
             $self->{content_model_flag} eq 'CDATA') {  
382            unless ($self->{escape}) {            unless ($self->{escape}) {
383              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_input_character}->[0] == 0x002D and # -
384                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_input_character}->[1] == 0x0021 and # !
# Line 216  sub _get_next_token ($) { Line 390  sub _get_next_token ($) {
390                    
391          #          #
392        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_input_character} == 0x003C) { # <
393          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
394              (($self->{content_model_flag} eq 'CDATA' or              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
               $self->{content_model_flag} eq 'RCDATA') and  
395               not $self->{escape})) {               not $self->{escape})) {
396            $self->{state} = 'tag open';            $self->{state} = TAG_OPEN_STATE;
397            !!!next-input-character;            !!!next-input-character;
398            redo A;            redo A;
399          } else {          } else {
# Line 228  sub _get_next_token ($) { Line 401  sub _get_next_token ($) {
401          }          }
402        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
403          if ($self->{escape} and          if ($self->{escape} and
404              ($self->{content_model_flag} eq 'RCDATA' or              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
              $self->{content_model_flag} eq 'CDATA')) {  
405            if ($self->{prev_input_character}->[0] == 0x002D and # -            if ($self->{prev_input_character}->[0] == 0x002D and # -
406                $self->{prev_input_character}->[1] == 0x002D) { # -                $self->{prev_input_character}->[1] == 0x002D) { # -
407              delete $self->{escape};              delete $self->{escape};
# Line 238  sub _get_next_token ($) { Line 410  sub _get_next_token ($) {
410                    
411          #          #
412        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
413          !!!emit ({type => 'end-of-file'});          !!!emit ({type => END_OF_FILE_TOKEN});
414          last A; ## TODO: ok?          last A; ## TODO: ok?
415        }        }
416        # Anything else        # Anything else
417        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
418                     data => chr $self->{next_input_character}};                     data => chr $self->{next_input_character}};
419        ## Stay in the data state        ## Stay in the data state
420        !!!next-input-character;        !!!next-input-character;
# Line 250  sub _get_next_token ($) { Line 422  sub _get_next_token ($) {
422        !!!emit ($token);        !!!emit ($token);
423    
424        redo A;        redo A;
425      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
426        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
427                
428        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);
429    
430        $self->{state} = 'data';        $self->{state} = DATA_STATE;
431        # next-input-character is already done        # next-input-character is already done
432    
433        unless (defined $token) {        unless (defined $token) {
434          !!!emit ({type => 'character', data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&'});
435        } else {        } else {
436          !!!emit ($token);          !!!emit ($token);
437        }        }
438    
439        redo A;        redo A;
440      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
441        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
           $self->{content_model_flag} eq 'CDATA') {  
442          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_input_character} == 0x002F) { # /
443            !!!next-input-character;            !!!next-input-character;
444            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
445            redo A;            redo A;
446          } else {          } else {
447            ## reconsume            ## reconsume
448            $self->{state} = 'data';            $self->{state} = DATA_STATE;
449    
450            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
451    
452            redo A;            redo A;
453          }          }
454        } elsif ($self->{content_model_flag} eq 'PCDATA') {        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
455          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_input_character} == 0x0021) { # !
456            $self->{state} = 'markup declaration open';            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
457            !!!next-input-character;            !!!next-input-character;
458            redo A;            redo A;
459          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_input_character} == 0x002F) { # /
460            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
461            !!!next-input-character;            !!!next-input-character;
462            redo A;            redo A;
463          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_input_character} and
464                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_input_character} <= 0x005A) { # A..Z
465            $self->{current_token}            $self->{current_token}
466              = {type => 'start tag',              = {type => START_TAG_TOKEN,
467                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_input_character} + 0x0020)};
468            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
469            !!!next-input-character;            !!!next-input-character;
470            redo A;            redo A;
471          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_input_character} and
472                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_input_character} <= 0x007A) { # a..z
473            $self->{current_token} = {type => 'start tag',            $self->{current_token} = {type => START_TAG_TOKEN,
474                              tag_name => chr ($self->{next_input_character})};                              tag_name => chr ($self->{next_input_character})};
475            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
476            !!!next-input-character;            !!!next-input-character;
477            redo A;            redo A;
478          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_input_character} == 0x003E) { # >
479            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag');
480            $self->{state} = 'data';            $self->{state} = DATA_STATE;
481            !!!next-input-character;            !!!next-input-character;
482    
483            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});
484    
485            redo A;            redo A;
486          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_input_character} == 0x003F) { # ?
487            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio');
488            $self->{state} = 'bogus comment';            $self->{state} = BOGUS_COMMENT_STATE;
489            ## $self->{next_input_character} is intentionally left as is            ## $self->{next_input_character} is intentionally left as is
490            redo A;            redo A;
491          } else {          } else {
492            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago');
493            $self->{state} = 'data';            $self->{state} = DATA_STATE;
494            ## reconsume            ## reconsume
495    
496            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
497    
498            redo A;            redo A;
499          }          }
500        } else {        } else {
501          die "$0: $self->{content_model_flag}: Unknown content model flag";          die "$0: $self->{content_model} in tag open";
502        }        }
503      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
504        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
           $self->{content_model_flag} eq 'CDATA') {  
505          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
506            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
507            my @next_char;            my @next_char;
# Line 345  sub _get_next_token ($) { Line 515  sub _get_next_token ($) {
515              } else {              } else {
516                $self->{next_input_character} = shift @next_char; # reconsume                $self->{next_input_character} = shift @next_char; # reconsume
517                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
518                $self->{state} = 'data';                $self->{state} = DATA_STATE;
519    
520                !!!emit ({type => 'character', data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</'});
521        
522                redo A;                redo A;
523              }              }
# Line 364  sub _get_next_token ($) { Line 534  sub _get_next_token ($) {
534                    $self->{next_input_character} == -1) {                    $self->{next_input_character} == -1) {
535              $self->{next_input_character} = shift @next_char; # reconsume              $self->{next_input_character} = shift @next_char; # reconsume
536              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
537              $self->{state} = 'data';              $self->{state} = DATA_STATE;
538              !!!emit ({type => 'character', data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</'});
539              redo A;              redo A;
540            } else {            } else {
541              $self->{next_input_character} = shift @next_char;              $self->{next_input_character} = shift @next_char;
# Line 375  sub _get_next_token ($) { Line 545  sub _get_next_token ($) {
545          } else {          } else {
546            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
547            # next-input-character is already done            # next-input-character is already done
548            $self->{state} = 'data';            $self->{state} = DATA_STATE;
549            !!!emit ({type => 'character', data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</'});
550            redo A;            redo A;
551          }          }
552        }        }
553                
554        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_input_character} and
555            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_input_character} <= 0x005A) { # A..Z
556          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
557                            tag_name => chr ($self->{next_input_character} + 0x0020)};                            tag_name => chr ($self->{next_input_character} + 0x0020)};
558          $self->{state} = 'tag name';          $self->{state} = TAG_NAME_STATE;
559          !!!next-input-character;          !!!next-input-character;
560          redo A;          redo A;
561        } elsif (0x0061 <= $self->{next_input_character} and        } elsif (0x0061 <= $self->{next_input_character} and
562                 $self->{next_input_character} <= 0x007A) { # a..z                 $self->{next_input_character} <= 0x007A) { # a..z
563          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
564                            tag_name => chr ($self->{next_input_character})};                            tag_name => chr ($self->{next_input_character})};
565          $self->{state} = 'tag name';          $self->{state} = TAG_NAME_STATE;
566          !!!next-input-character;          !!!next-input-character;
567          redo A;          redo A;
568        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
569          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag');
570          $self->{state} = 'data';          $self->{state} = DATA_STATE;
571          !!!next-input-character;          !!!next-input-character;
572          redo A;          redo A;
573        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
574          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
575          $self->{state} = 'data';          $self->{state} = DATA_STATE;
576          # reconsume          # reconsume
577    
578          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</'});
579    
580          redo A;          redo A;
581        } else {        } else {
582          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
583          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
584          ## $self->{next_input_character} is intentionally left as is          ## $self->{next_input_character} is intentionally left as is
585          redo A;          redo A;
586        }        }
587      } elsif ($self->{state} eq 'tag name') {      } elsif ($self->{state} == TAG_NAME_STATE) {
588        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
589            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
590            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
591            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
592            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
593          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
594          !!!next-input-character;          !!!next-input-character;
595          redo A;          redo A;
596        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
597          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
598            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
599                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
600            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
601          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
602            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
603            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
604              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
605            }            }
606          } else {          } else {
607            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
608          }          }
609          $self->{state} = 'data';          $self->{state} = DATA_STATE;
610          !!!next-input-character;          !!!next-input-character;
611    
612          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 451  sub _get_next_token ($) { Line 621  sub _get_next_token ($) {
621          redo A;          redo A;
622        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
623          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
624          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
625            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
626                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
627            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
628          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
629            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
630            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
631              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
632            }            }
633          } else {          } else {
634            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
635          }          }
636          $self->{state} = 'data';          $self->{state} = DATA_STATE;
637          # reconsume          # reconsume
638    
639          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 472  sub _get_next_token ($) { Line 642  sub _get_next_token ($) {
642        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
643          !!!next-input-character;          !!!next-input-character;
644          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
645              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
646              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
647            # permitted slash            # permitted slash
648            #            #
649          } else {          } else {
650            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
651          }          }
652          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
653          # next-input-character is already done          # next-input-character is already done
654          redo A;          redo A;
655        } else {        } else {
# Line 489  sub _get_next_token ($) { Line 659  sub _get_next_token ($) {
659          !!!next-input-character;          !!!next-input-character;
660          redo A;          redo A;
661        }        }
662      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
663        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
664            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
665            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 499  sub _get_next_token ($) { Line 669  sub _get_next_token ($) {
669          !!!next-input-character;          !!!next-input-character;
670          redo A;          redo A;
671        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
672          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
673            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
674                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
675            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
676          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
677            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
678            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
679              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
680            }            }
681          } else {          } else {
682            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
683          }          }
684          $self->{state} = 'data';          $self->{state} = DATA_STATE;
685          !!!next-input-character;          !!!next-input-character;
686    
687          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 521  sub _get_next_token ($) { Line 691  sub _get_next_token ($) {
691                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_input_character} <= 0x005A) { # A..Z
692          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),
693                                value => ''};                                value => ''};
694          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
695          !!!next-input-character;          !!!next-input-character;
696          redo A;          redo A;
697        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
698          !!!next-input-character;          !!!next-input-character;
699          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
700              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
701              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
702            # permitted slash            # permitted slash
703            #            #
# Line 539  sub _get_next_token ($) { Line 709  sub _get_next_token ($) {
709          redo A;          redo A;
710        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
711          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
712          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
713            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
714                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
715            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
716          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
717            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
718            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
719              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
720            }            }
721          } else {          } else {
722            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
723          }          }
724          $self->{state} = 'data';          $self->{state} = DATA_STATE;
725          # reconsume          # reconsume
726    
727          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 560  sub _get_next_token ($) { Line 730  sub _get_next_token ($) {
730        } else {        } else {
731          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute} = {name => chr ($self->{next_input_character}),
732                                value => ''};                                value => ''};
733          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
734          !!!next-input-character;          !!!next-input-character;
735          redo A;          redo A;
736        }        }
737      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
738        my $before_leave = sub {        my $before_leave = sub {
739          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
740              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
741            !!!parse-error (type => 'duplicate attribute');            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});
742            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
743          } else {          } else {
744            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
# Line 582  sub _get_next_token ($) { Line 752  sub _get_next_token ($) {
752            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
753            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
754          $before_leave->();          $before_leave->();
755          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
756          !!!next-input-character;          !!!next-input-character;
757          redo A;          redo A;
758        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_input_character} == 0x003D) { # =
759          $before_leave->();          $before_leave->();
760          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
761          !!!next-input-character;          !!!next-input-character;
762          redo A;          redo A;
763        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
764          $before_leave->();          $before_leave->();
765          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
766            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
767                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
768            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
769          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
770            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
771            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
772              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
773            }            }
774          } else {          } else {
775            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
776          }          }
777          $self->{state} = 'data';          $self->{state} = DATA_STATE;
778          !!!next-input-character;          !!!next-input-character;
779    
780          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 620  sub _get_next_token ($) { Line 790  sub _get_next_token ($) {
790          $before_leave->();          $before_leave->();
791          !!!next-input-character;          !!!next-input-character;
792          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
793              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
794              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
795            # permitted slash            # permitted slash
796            #            #
797          } else {          } else {
798            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
799          }          }
800          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
801          # next-input-character is already done          # next-input-character is already done
802          redo A;          redo A;
803        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
804          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
805          $before_leave->();          $before_leave->();
806          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
807            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
808                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
809            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
810          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
811            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
812            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
813              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
814            }            }
815          } else {          } else {
816            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
817          }          }
818          $self->{state} = 'data';          $self->{state} = DATA_STATE;
819          # reconsume          # reconsume
820    
821          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 657  sub _get_next_token ($) { Line 827  sub _get_next_token ($) {
827          !!!next-input-character;          !!!next-input-character;
828          redo A;          redo A;
829        }        }
830      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
831        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
832            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
833            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 667  sub _get_next_token ($) { Line 837  sub _get_next_token ($) {
837          !!!next-input-character;          !!!next-input-character;
838          redo A;          redo A;
839        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_input_character} == 0x003D) { # =
840          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
841          !!!next-input-character;          !!!next-input-character;
842          redo A;          redo A;
843        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
844          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
845            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
846                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
847            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
848          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
849            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
850            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
851              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
852            }            }
853          } else {          } else {
854            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
855          }          }
856          $self->{state} = 'data';          $self->{state} = DATA_STATE;
857          !!!next-input-character;          !!!next-input-character;
858    
859          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 693  sub _get_next_token ($) { Line 863  sub _get_next_token ($) {
863                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_input_character} <= 0x005A) { # A..Z
864          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),
865                                value => ''};                                value => ''};
866          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
867          !!!next-input-character;          !!!next-input-character;
868          redo A;          redo A;
869        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
870          !!!next-input-character;          !!!next-input-character;
871          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
872              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
873              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
874            # permitted slash            # permitted slash
875            #            #
# Line 707  sub _get_next_token ($) { Line 877  sub _get_next_token ($) {
877            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
878            ## TODO: Different error type for <aa / bb> than <aa/>            ## TODO: Different error type for <aa / bb> than <aa/>
879          }          }
880          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
881          # next-input-character is already done          # next-input-character is already done
882          redo A;          redo A;
883        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
884          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
885          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
886            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
887                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
888            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
889          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
890            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
891            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
892              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
893            }            }
894          } else {          } else {
895            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
896          }          }
897          $self->{state} = 'data';          $self->{state} = DATA_STATE;
898          # reconsume          # reconsume
899    
900          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 733  sub _get_next_token ($) { Line 903  sub _get_next_token ($) {
903        } else {        } else {
904          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute} = {name => chr ($self->{next_input_character}),
905                                value => ''};                                value => ''};
906          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
907          !!!next-input-character;          !!!next-input-character;
908          redo A;                  redo A;        
909        }        }
910      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
911        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
912            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
913            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 747  sub _get_next_token ($) { Line 917  sub _get_next_token ($) {
917          !!!next-input-character;          !!!next-input-character;
918          redo A;          redo A;
919        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
920          $self->{state} = 'attribute value (double-quoted)';          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
921          !!!next-input-character;          !!!next-input-character;
922          redo A;          redo A;
923        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
924          $self->{state} = 'attribute value (unquoted)';          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
925          ## reconsume          ## reconsume
926          redo A;          redo A;
927        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
928          $self->{state} = 'attribute value (single-quoted)';          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
929          !!!next-input-character;          !!!next-input-character;
930          redo A;          redo A;
931        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
932          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
933            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
934                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
935            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
936          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
937            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
938            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
939              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
940            }            }
941          } else {          } else {
942            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
943          }          }
944          $self->{state} = 'data';          $self->{state} = DATA_STATE;
945          !!!next-input-character;          !!!next-input-character;
946    
947          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 779  sub _get_next_token ($) { Line 949  sub _get_next_token ($) {
949          redo A;          redo A;
950        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
951          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
952          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
953            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
954                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
955            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
956          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
957            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
958            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
959              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
960            }            }
961          } else {          } else {
962            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
963          }          }
964          $self->{state} = 'data';          $self->{state} = DATA_STATE;
965          ## reconsume          ## reconsume
966    
967          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 799  sub _get_next_token ($) { Line 969  sub _get_next_token ($) {
969          redo A;          redo A;
970        } else {        } else {
971          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});
972          $self->{state} = 'attribute value (unquoted)';          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
973          !!!next-input-character;          !!!next-input-character;
974          redo A;          redo A;
975        }        }
976      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
977        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
978          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
979          !!!next-input-character;          !!!next-input-character;
980          redo A;          redo A;
981        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
982          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          $self->{last_attribute_value_state} = $self->{state};
983          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
984          !!!next-input-character;          !!!next-input-character;
985          redo A;          redo A;
986        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
987          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
988          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
989            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
990                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
991            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
992          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
993            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
994            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
995              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
996            }            }
997          } else {          } else {
998            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
999          }          }
1000          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1001          ## reconsume          ## reconsume
1002    
1003          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 839  sub _get_next_token ($) { Line 1009  sub _get_next_token ($) {
1009          !!!next-input-character;          !!!next-input-character;
1010          redo A;          redo A;
1011        }        }
1012      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1013        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1014          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1015          !!!next-input-character;          !!!next-input-character;
1016          redo A;          redo A;
1017        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
1018          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          $self->{last_attribute_value_state} = $self->{state};
1019          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1020          !!!next-input-character;          !!!next-input-character;
1021          redo A;          redo A;
1022        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1023          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1024          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1025            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1026                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1027            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1028          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1029            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1030            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1031              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1032            }            }
1033          } else {          } else {
1034            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1035          }          }
1036          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1037          ## reconsume          ## reconsume
1038    
1039          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 875  sub _get_next_token ($) { Line 1045  sub _get_next_token ($) {
1045          !!!next-input-character;          !!!next-input-character;
1046          redo A;          redo A;
1047        }        }
1048      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1049        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1050            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1051            $self->{next_input_character} == 0x000B or # HT            $self->{next_input_character} == 0x000B or # HT
1052            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1053            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1054          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1055          !!!next-input-character;          !!!next-input-character;
1056          redo A;          redo A;
1057        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
1058          $self->{last_attribute_value_state} = 'attribute value (unquoted)';          $self->{last_attribute_value_state} = $self->{state};
1059          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1060          !!!next-input-character;          !!!next-input-character;
1061          redo A;          redo A;
1062        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1063          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1064            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1065                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1066            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1067          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1068            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1069            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1070              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1071            }            }
1072          } else {          } else {
1073            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1074          }          }
1075          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1076          !!!next-input-character;          !!!next-input-character;
1077    
1078          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 910  sub _get_next_token ($) { Line 1080  sub _get_next_token ($) {
1080          redo A;          redo A;
1081        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1082          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1083          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1084            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1085                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1086            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1087          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1088            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1089            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1090              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1091            }            }
1092          } else {          } else {
1093            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1094          }          }
1095          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1096          ## reconsume          ## reconsume
1097    
1098          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 934  sub _get_next_token ($) { Line 1104  sub _get_next_token ($) {
1104          !!!next-input-character;          !!!next-input-character;
1105          redo A;          redo A;
1106        }        }
1107      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1108        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);
1109    
1110        unless (defined $token) {        unless (defined $token) {
# Line 947  sub _get_next_token ($) { Line 1117  sub _get_next_token ($) {
1117        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1118        # next-input-character is already done        # next-input-character is already done
1119        redo A;        redo A;
1120      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1121        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1122                
1123        my $token = {type => 'comment', data => ''};        my $token = {type => COMMENT_TOKEN, data => ''};
1124    
1125        BC: {        BC: {
1126          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_input_character} == 0x003E) { # >
1127            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1128            !!!next-input-character;            !!!next-input-character;
1129    
1130            !!!emit ($token);            !!!emit ($token);
1131    
1132            redo A;            redo A;
1133          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_input_character} == -1) {
1134            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1135            ## reconsume            ## reconsume
1136    
1137            !!!emit ($token);            !!!emit ($token);
# Line 973  sub _get_next_token ($) { Line 1143  sub _get_next_token ($) {
1143            redo BC;            redo BC;
1144          }          }
1145        } # BC        } # BC
1146      } elsif ($self->{state} eq 'markup declaration open') {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1147        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1148    
1149        my @next_char;        my @next_char;
# Line 983  sub _get_next_token ($) { Line 1153  sub _get_next_token ($) {
1153          !!!next-input-character;          !!!next-input-character;
1154          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_input_character};
1155          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_input_character} == 0x002D) { # -
1156            $self->{current_token} = {type => 'comment', data => ''};            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};
1157            $self->{state} = 'comment start';            $self->{state} = COMMENT_START_STATE;
1158            !!!next-input-character;            !!!next-input-character;
1159            redo A;            redo A;
1160          }          }
# Line 1015  sub _get_next_token ($) { Line 1185  sub _get_next_token ($) {
1185                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_input_character} == 0x0045 or # E
1186                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_input_character} == 0x0065) { # e
1187                      ## ISSUE: What a stupid code this is!                      ## ISSUE: What a stupid code this is!
1188                      $self->{state} = 'DOCTYPE';                      $self->{state} = DOCTYPE_STATE;
1189                      !!!next-input-character;                      !!!next-input-character;
1190                      redo A;                      redo A;
1191                    }                    }
# Line 1029  sub _get_next_token ($) { Line 1199  sub _get_next_token ($) {
1199        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment');
1200        $self->{next_input_character} = shift @next_char;        $self->{next_input_character} = shift @next_char;
1201        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1202        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
1203        redo A;        redo A;
1204                
1205        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1206        ## 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?
1207      } elsif ($self->{state} eq 'comment start') {      } elsif ($self->{state} == COMMENT_START_STATE) {
1208        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1209          $self->{state} = 'comment start dash';          $self->{state} = COMMENT_START_DASH_STATE;
1210          !!!next-input-character;          !!!next-input-character;
1211          redo A;          redo A;
1212        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1213          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1214          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1215          !!!next-input-character;          !!!next-input-character;
1216    
1217          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1049  sub _get_next_token ($) { Line 1219  sub _get_next_token ($) {
1219          redo A;          redo A;
1220        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1221          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1222          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1223          ## reconsume          ## reconsume
1224    
1225          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1058  sub _get_next_token ($) { Line 1228  sub _get_next_token ($) {
1228        } else {        } else {
1229          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1230              .= chr ($self->{next_input_character});              .= chr ($self->{next_input_character});
1231          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1232          !!!next-input-character;          !!!next-input-character;
1233          redo A;          redo A;
1234        }        }
1235      } elsif ($self->{state} eq 'comment start dash') {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
1236        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1237          $self->{state} = 'comment end';          $self->{state} = COMMENT_END_STATE;
1238          !!!next-input-character;          !!!next-input-character;
1239          redo A;          redo A;
1240        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1241          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1242          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1243          !!!next-input-character;          !!!next-input-character;
1244    
1245          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1077  sub _get_next_token ($) { Line 1247  sub _get_next_token ($) {
1247          redo A;          redo A;
1248        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1249          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1250          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1251          ## reconsume          ## reconsume
1252    
1253          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1086  sub _get_next_token ($) { Line 1256  sub _get_next_token ($) {
1256        } else {        } else {
1257          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1258              .= '-' . chr ($self->{next_input_character});              .= '-' . chr ($self->{next_input_character});
1259          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1260          !!!next-input-character;          !!!next-input-character;
1261          redo A;          redo A;
1262        }        }
1263      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_STATE) {
1264        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1265          $self->{state} = 'comment end dash';          $self->{state} = COMMENT_END_DASH_STATE;
1266          !!!next-input-character;          !!!next-input-character;
1267          redo A;          redo A;
1268        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1269          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1270          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1271          ## reconsume          ## reconsume
1272    
1273          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1109  sub _get_next_token ($) { Line 1279  sub _get_next_token ($) {
1279          !!!next-input-character;          !!!next-input-character;
1280          redo A;          redo A;
1281        }        }
1282      } elsif ($self->{state} eq 'comment end dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
1283        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1284          $self->{state} = 'comment end';          $self->{state} = COMMENT_END_STATE;
1285          !!!next-input-character;          !!!next-input-character;
1286          redo A;          redo A;
1287        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1288          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1289          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1290          ## reconsume          ## reconsume
1291    
1292          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1124  sub _get_next_token ($) { Line 1294  sub _get_next_token ($) {
1294          redo A;          redo A;
1295        } else {        } else {
1296          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment
1297          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1298          !!!next-input-character;          !!!next-input-character;
1299          redo A;          redo A;
1300        }        }
1301      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
1302        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_input_character} == 0x003E) { # >
1303          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1304          !!!next-input-character;          !!!next-input-character;
1305    
1306          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1144  sub _get_next_token ($) { Line 1314  sub _get_next_token ($) {
1314          redo A;          redo A;
1315        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1316          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1317          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1318          ## reconsume          ## reconsume
1319    
1320          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1153  sub _get_next_token ($) { Line 1323  sub _get_next_token ($) {
1323        } else {        } else {
1324          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment');
1325          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment
1326          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1327          !!!next-input-character;          !!!next-input-character;
1328          redo A;          redo A;
1329        }        }
1330      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
1331        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1332            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1333            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1334            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1335            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1336          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1337          !!!next-input-character;          !!!next-input-character;
1338          redo A;          redo A;
1339        } else {        } else {
1340          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
1341          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1342          ## reconsume          ## reconsume
1343          redo A;          redo A;
1344        }        }
1345      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
1346        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1347            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1348            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 1183  sub _get_next_token ($) { Line 1353  sub _get_next_token ($) {
1353          redo A;          redo A;
1354        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1355          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1356          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1357          !!!next-input-character;          !!!next-input-character;
1358    
1359          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1360    
1361          redo A;          redo A;
1362        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1363          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1364          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1365          ## reconsume          ## reconsume
1366    
1367          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1368    
1369          redo A;          redo A;
1370        } else {        } else {
1371          $self->{current_token}          $self->{current_token}
1372              = {type => 'DOCTYPE',              = {type => DOCTYPE_TOKEN,
1373                 name => chr ($self->{next_input_character}),                 name => chr ($self->{next_input_character}),
1374                 correct => 1};                 correct => 1};
1375  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
1376          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
1377          !!!next-input-character;          !!!next-input-character;
1378          redo A;          redo A;
1379        }        }
1380      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
1381  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
1382        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1383            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1384            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1385            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1386            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1387          $self->{state} = 'after DOCTYPE name';          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
1388          !!!next-input-character;          !!!next-input-character;
1389          redo A;          redo A;
1390        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1391          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1392          !!!next-input-character;          !!!next-input-character;
1393    
1394          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1226  sub _get_next_token ($) { Line 1396  sub _get_next_token ($) {
1396          redo A;          redo A;
1397        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1398          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1399          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1400          ## reconsume          ## reconsume
1401    
1402          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1240  sub _get_next_token ($) { Line 1410  sub _get_next_token ($) {
1410          !!!next-input-character;          !!!next-input-character;
1411          redo A;          redo A;
1412        }        }
1413      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
1414        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1415            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1416            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 1250  sub _get_next_token ($) { Line 1420  sub _get_next_token ($) {
1420          !!!next-input-character;          !!!next-input-character;
1421          redo A;          redo A;
1422        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1423          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1424          !!!next-input-character;          !!!next-input-character;
1425    
1426          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1258  sub _get_next_token ($) { Line 1428  sub _get_next_token ($) {
1428          redo A;          redo A;
1429        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1430          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1431          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1432          ## reconsume          ## reconsume
1433    
1434          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1282  sub _get_next_token ($) { Line 1452  sub _get_next_token ($) {
1452                  !!!next-input-character;                  !!!next-input-character;
1453                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_input_character} == 0x0043 or # C
1454                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_input_character} == 0x0063) { # c
1455                    $self->{state} = 'before DOCTYPE public identifier';                    $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1456                    !!!next-input-character;                    !!!next-input-character;
1457                    redo A;                    redo A;
1458                  }                  }
# Line 1309  sub _get_next_token ($) { Line 1479  sub _get_next_token ($) {
1479                  !!!next-input-character;                  !!!next-input-character;
1480                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_input_character} == 0x004D or # M
1481                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_input_character} == 0x006D) { # m
1482                    $self->{state} = 'before DOCTYPE system identifier';                    $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1483                    !!!next-input-character;                    !!!next-input-character;
1484                    redo A;                    redo A;
1485                  }                  }
# Line 1325  sub _get_next_token ($) { Line 1495  sub _get_next_token ($) {
1495        }        }
1496    
1497        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
1498        $self->{state} = 'bogus DOCTYPE';        $self->{state} = BOGUS_DOCTYPE_STATE;
1499        # next-input-character is already done        # next-input-character is already done
1500        redo A;        redo A;
1501      } elsif ($self->{state} eq 'before DOCTYPE public identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1502        if ({        if ({
1503              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1504              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1338  sub _get_next_token ($) { Line 1508  sub _get_next_token ($) {
1508          redo A;          redo A;
1509        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_input_character} eq 0x0022) { # "
1510          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1511          $self->{state} = 'DOCTYPE public identifier (double-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
1512          !!!next-input-character;          !!!next-input-character;
1513          redo A;          redo A;
1514        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_input_character} eq 0x0027) { # '
1515          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1516          $self->{state} = 'DOCTYPE public identifier (single-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
1517          !!!next-input-character;          !!!next-input-character;
1518          redo A;          redo A;
1519        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_input_character} eq 0x003E) { # >
1520          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
1521    
1522          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1523          !!!next-input-character;          !!!next-input-character;
1524    
1525          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1359  sub _get_next_token ($) { Line 1529  sub _get_next_token ($) {
1529        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1530          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1531    
1532          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1533          ## reconsume          ## reconsume
1534    
1535          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1368  sub _get_next_token ($) { Line 1538  sub _get_next_token ($) {
1538          redo A;          redo A;
1539        } else {        } else {
1540          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
1541          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1542          !!!next-input-character;          !!!next-input-character;
1543          redo A;          redo A;
1544        }        }
1545      } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1546        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
1547          $self->{state} = 'after DOCTYPE public identifier';          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1548          !!!next-input-character;          !!!next-input-character;
1549          redo A;          redo A;
1550        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1551          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1552    
1553          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1554          ## reconsume          ## reconsume
1555    
1556          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1394  sub _get_next_token ($) { Line 1564  sub _get_next_token ($) {
1564          !!!next-input-character;          !!!next-input-character;
1565          redo A;          redo A;
1566        }        }
1567      } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
1568        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1569          $self->{state} = 'after DOCTYPE public identifier';          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1570          !!!next-input-character;          !!!next-input-character;
1571          redo A;          redo A;
1572        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1573          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1574    
1575          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1576          ## reconsume          ## reconsume
1577    
1578          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1416  sub _get_next_token ($) { Line 1586  sub _get_next_token ($) {
1586          !!!next-input-character;          !!!next-input-character;
1587          redo A;          redo A;
1588        }        }
1589      } elsif ($self->{state} eq 'after DOCTYPE public identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1590        if ({        if ({
1591              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1592              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1426  sub _get_next_token ($) { Line 1596  sub _get_next_token ($) {
1596          redo A;          redo A;
1597        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
1598          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1599          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1600          !!!next-input-character;          !!!next-input-character;
1601          redo A;          redo A;
1602        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
1603          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1604          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1605          !!!next-input-character;          !!!next-input-character;
1606          redo A;          redo A;
1607        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1608          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1609          !!!next-input-character;          !!!next-input-character;
1610    
1611          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1444  sub _get_next_token ($) { Line 1614  sub _get_next_token ($) {
1614        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1615          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1616    
1617          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1618          ## reconsume          ## reconsume
1619    
1620          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1453  sub _get_next_token ($) { Line 1623  sub _get_next_token ($) {
1623          redo A;          redo A;
1624        } else {        } else {
1625          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
1626          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1627          !!!next-input-character;          !!!next-input-character;
1628          redo A;          redo A;
1629        }        }
1630      } elsif ($self->{state} eq 'before DOCTYPE system identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1631        if ({        if ({
1632              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1633              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1467  sub _get_next_token ($) { Line 1637  sub _get_next_token ($) {
1637          redo A;          redo A;
1638        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
1639          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1640          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1641          !!!next-input-character;          !!!next-input-character;
1642          redo A;          redo A;
1643        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
1644          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1645          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1646          !!!next-input-character;          !!!next-input-character;
1647          redo A;          redo A;
1648        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1649          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
1650          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1651          !!!next-input-character;          !!!next-input-character;
1652    
1653          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1487  sub _get_next_token ($) { Line 1657  sub _get_next_token ($) {
1657        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1658          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1659    
1660          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1661          ## reconsume          ## reconsume
1662    
1663          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1496  sub _get_next_token ($) { Line 1666  sub _get_next_token ($) {
1666          redo A;          redo A;
1667        } else {        } else {
1668          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
1669          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1670          !!!next-input-character;          !!!next-input-character;
1671          redo A;          redo A;
1672        }        }
1673      } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1674        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
1675          $self->{state} = 'after DOCTYPE system identifier';          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1676          !!!next-input-character;          !!!next-input-character;
1677          redo A;          redo A;
1678        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1679          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
1680    
1681          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1682          ## reconsume          ## reconsume
1683    
1684          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1522  sub _get_next_token ($) { Line 1692  sub _get_next_token ($) {
1692          !!!next-input-character;          !!!next-input-character;
1693          redo A;          redo A;
1694        }        }
1695      } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
1696        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1697          $self->{state} = 'after DOCTYPE system identifier';          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1698          !!!next-input-character;          !!!next-input-character;
1699          redo A;          redo A;
1700        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1701          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
1702    
1703          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1704          ## reconsume          ## reconsume
1705    
1706          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1544  sub _get_next_token ($) { Line 1714  sub _get_next_token ($) {
1714          !!!next-input-character;          !!!next-input-character;
1715          redo A;          redo A;
1716        }        }
1717      } elsif ($self->{state} eq 'after DOCTYPE system identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1718        if ({        if ({
1719              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1720              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1553  sub _get_next_token ($) { Line 1723  sub _get_next_token ($) {
1723          !!!next-input-character;          !!!next-input-character;
1724          redo A;          redo A;
1725        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1726          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1727          !!!next-input-character;          !!!next-input-character;
1728    
1729          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1562  sub _get_next_token ($) { Line 1732  sub _get_next_token ($) {
1732        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1733          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1734    
1735          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1736          ## reconsume          ## reconsume
1737    
1738          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1571  sub _get_next_token ($) { Line 1741  sub _get_next_token ($) {
1741          redo A;          redo A;
1742        } else {        } else {
1743          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
1744          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1745          !!!next-input-character;          !!!next-input-character;
1746          redo A;          redo A;
1747        }        }
1748      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
1749        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_input_character} == 0x003E) { # >
1750          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1751          !!!next-input-character;          !!!next-input-character;
1752    
1753          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1586  sub _get_next_token ($) { Line 1756  sub _get_next_token ($) {
1756          redo A;          redo A;
1757        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1758          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1759          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1760          ## reconsume          ## reconsume
1761    
1762          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1667  sub _tokenize_attempt_to_consume_an_enti Line 1837  sub _tokenize_attempt_to_consume_an_enti
1837            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
1838          }          }
1839    
1840          return {type => 'character', data => chr $code};          return {type => CHARACTER_TOKEN, data => chr $code};
1841        } # X        } # X
1842      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_input_character} and
1843               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_input_character} <= 0x0039) { # 0..9
# Line 1702  sub _tokenize_attempt_to_consume_an_enti Line 1872  sub _tokenize_attempt_to_consume_an_enti
1872          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
1873        }        }
1874                
1875        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code};
1876      } else {      } else {
1877        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero');
1878        !!!back-next-input-character ($self->{next_input_character});        !!!back-next-input-character ($self->{next_input_character});
# Line 1750  sub _tokenize_attempt_to_consume_an_enti Line 1920  sub _tokenize_attempt_to_consume_an_enti
1920      }      }
1921            
1922      if ($match > 0) {      if ($match > 0) {
1923        return {type => 'character', data => $value};        return {type => CHARACTER_TOKEN, data => $value};
1924      } elsif ($match < 0) {      } elsif ($match < 0) {
1925        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc');
1926        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
1927          return {type => 'character', data => '&'.$entity_name};          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};
1928        } else {        } else {
1929          return {type => 'character', data => $value};          return {type => CHARACTER_TOKEN, data => $value};
1930        }        }
1931      } else {      } else {
1932        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero');
1933        ## NOTE: No characters are consumed in the spec.        ## NOTE: No characters are consumed in the spec.
1934        return {type => 'character', data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value};
1935      }      }
1936    } else {    } else {
1937      ## no characters are consumed      ## no characters are consumed
# Line 1803  sub _construct_tree ($) { Line 1973  sub _construct_tree ($) {
1973        
1974    !!!next-token;    !!!next-token;
1975    
1976    $self->{insertion_mode} = 'before head';    $self->{insertion_mode} = BEFORE_HEAD_IM;
1977    undef $self->{form_element};    undef $self->{form_element};
1978    undef $self->{head_element};    undef $self->{head_element};
1979    $self->{open_elements} = [];    $self->{open_elements} = [];
# Line 1817  sub _construct_tree ($) { Line 1987  sub _construct_tree ($) {
1987  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
1988    my $self = shift;    my $self = shift;
1989    INITIAL: {    INITIAL: {
1990      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
1991        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
1992        ## error, switch to a conformance checking mode for another        ## error, switch to a conformance checking mode for another
1993        ## language.        ## language.
# Line 1944  sub _tree_construction_initial ($) { Line 2114  sub _tree_construction_initial ($) {
2114        !!!next-token;        !!!next-token;
2115        return;        return;
2116      } elsif ({      } elsif ({
2117                'start tag' => 1,                START_TAG_TOKEN, 1,
2118                'end tag' => 1,                END_TAG_TOKEN, 1,
2119                'end-of-file' => 1,                END_OF_FILE_TOKEN, 1,
2120               }->{$token->{type}}) {               }->{$token->{type}}) {
2121        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE');
2122        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2123        ## Go to the root element phase        ## Go to the root element phase
2124        ## reprocess        ## reprocess
2125        return;        return;
2126      } elsif ($token->{type} eq 'character') {      } elsif ($token->{type} == CHARACTER_TOKEN) {
2127        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2128          ## Ignore the token          ## Ignore the token
2129    
# Line 1969  sub _tree_construction_initial ($) { Line 2139  sub _tree_construction_initial ($) {
2139        ## Go to the root element phase        ## Go to the root element phase
2140        ## reprocess        ## reprocess
2141        return;        return;
2142      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
2143        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2144        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
2145                
# Line 1977  sub _tree_construction_initial ($) { Line 2147  sub _tree_construction_initial ($) {
2147        !!!next-token;        !!!next-token;
2148        redo INITIAL;        redo INITIAL;
2149      } else {      } else {
2150        die "$0: $token->{type}: Unknown token";        die "$0: $token->{type}: Unknown token type";
2151      }      }
2152    } # INITIAL    } # INITIAL
2153  } # _tree_construction_initial  } # _tree_construction_initial
# Line 1986  sub _tree_construction_root_element ($) Line 2156  sub _tree_construction_root_element ($)
2156    my $self = shift;    my $self = shift;
2157        
2158    B: {    B: {
2159        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
2160          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE');
2161          ## Ignore the token          ## Ignore the token
2162          ## Stay in the phase          ## Stay in the phase
2163          !!!next-token;          !!!next-token;
2164          redo B;          redo B;
2165        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
2166          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
2167          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2168          ## Stay in the phase          ## Stay in the phase
2169          !!!next-token;          !!!next-token;
2170          redo B;          redo B;
2171        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
2172          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2173            ## Ignore the token.            ## Ignore the token.
2174    
# Line 2008  sub _tree_construction_root_element ($) Line 2178  sub _tree_construction_root_element ($)
2178              redo B;              redo B;
2179            }            }
2180          }          }
2181    
2182            $self->{application_cache_selection}->(undef);
2183    
2184            #
2185          } elsif ($token->{type} == START_TAG_TOKEN) {
2186            if ($token->{tag_name} eq 'html' and
2187                $token->{attributes}->{manifest}) { ## ISSUE: Spec spells as "application"
2188              $self->{application_cache_selection}
2189                   ->($token->{attributes}->{manifest}->{value});
2190              ## ISSUE: No relative reference resolution?
2191            } else {
2192              $self->{application_cache_selection}->(undef);
2193            }
2194    
2195            ## ISSUE: There is an issue in the spec
2196          #          #
2197        } elsif ({        } elsif ({
2198                  'start tag' => 1,                  END_TAG_TOKEN, 1,
2199                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
2200                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2201            $self->{application_cache_selection}->(undef);
2202    
2203          ## ISSUE: There is an issue in the spec          ## ISSUE: There is an issue in the spec
2204          #          #
2205        } else {        } else {
2206          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
2207        }        }
2208    
2209        my $root_element; !!!create-element ($root_element, 'html');        my $root_element; !!!create-element ($root_element, 'html');
2210        $self->{document}->append_child ($root_element);        $self->{document}->append_child ($root_element);
2211        push @{$self->{open_elements}}, [$root_element, 'html'];        push @{$self->{open_elements}}, [$root_element, 'html'];
# Line 2059  sub _reset_insertion_mode ($) { Line 2246  sub _reset_insertion_mode ($) {
2246            
2247        ## Step 4..13        ## Step 4..13
2248        my $new_mode = {        my $new_mode = {
2249                        select => 'in select',                        select => IN_SELECT_IM,
2250                        td => 'in cell',                        td => IN_CELL_IM,
2251                        th => 'in cell',                        th => IN_CELL_IM,
2252                        tr => 'in row',                        tr => IN_ROW_IM,
2253                        tbody => 'in table body',                        tbody => IN_TABLE_BODY_IM,
2254                        thead => 'in table head',                        thead => IN_TABLE_BODY_IM,
2255                        tfoot => 'in table foot',                        tfoot => IN_TABLE_BODY_IM,
2256                        caption => 'in caption',                        caption => IN_CAPTION_IM,
2257                        colgroup => 'in column group',                        colgroup => IN_COLUMN_GROUP_IM,
2258                        table => 'in table',                        table => IN_TABLE_IM,
2259                        head => 'in body', # not in head!                        head => IN_BODY_IM, # not in head!
2260                        body => 'in body',                        body => IN_BODY_IM,
2261                        frameset => 'in frameset',                        frameset => IN_FRAMESET_IM,
2262                       }->{$node->[1]};                       }->{$node->[1]};
2263        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
2264                
2265        ## Step 14        ## Step 14
2266        if ($node->[1] eq 'html') {        if ($node->[1] eq 'html') {
2267          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
2268            $self->{insertion_mode} = 'before head';            $self->{insertion_mode} = BEFORE_HEAD_IM;
2269          } else {          } else {
2270            $self->{insertion_mode} = 'after head';            $self->{insertion_mode} = AFTER_HEAD_IM;
2271          }          }
2272          return;          return;
2273        }        }
2274                
2275        ## Step 15        ## Step 15
2276        $self->{insertion_mode} = 'in body' and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
2277                
2278        ## Step 16        ## Step 16
2279        $i--;        $i--;
# Line 2100  sub _reset_insertion_mode ($) { Line 2287  sub _reset_insertion_mode ($) {
2287  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
2288    my $self = shift;    my $self = shift;
2289    
   my $previous_insertion_mode;  
   
2290    my $active_formatting_elements = [];    my $active_formatting_elements = [];
2291    
2292    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 2196  sub _tree_construction_main ($) { Line 2381  sub _tree_construction_main ($) {
2381      $insert->($el); # /context node/->append_child ($el)      $insert->($el); # /context node/->append_child ($el)
2382    
2383      ## Step 3      ## Step 3
2384      $self->{content_model_flag} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
2385      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
2386    
2387      ## Step 4      ## Step 4
2388      my $text = '';      my $text = '';
2389      !!!next-token;      !!!next-token;
2390      while ($token->{type} eq 'character') { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
2391        $text .= $token->{data};        $text .= $token->{data};
2392        !!!next-token;        !!!next-token;
2393      }      }
# Line 2214  sub _tree_construction_main ($) { Line 2399  sub _tree_construction_main ($) {
2399      }      }
2400    
2401      ## Step 6      ## Step 6
2402      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
2403    
2404      ## Step 7      ## Step 7
2405      if ($token->{type} eq 'end tag' and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} == END_TAG_TOKEN and $token->{tag_name} eq $start_tag_name) {
2406        ## Ignore the token        ## Ignore the token
2407        } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {
2408          !!!parse-error (type => 'in CDATA:#'.$token->{type});
2409        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
2410          !!!parse-error (type => 'in RCDATA:#'.$token->{type});
2411      } else {      } else {
2412        !!!parse-error (type => 'in '.$content_model_flag.':#'.$token->{type});        die "$0: $content_model_flag in parse_rcdata";
2413      }      }
2414      !!!next-token;      !!!next-token;
2415    }; # $parse_rcdata    }; # $parse_rcdata
# Line 2231  sub _tree_construction_main ($) { Line 2420  sub _tree_construction_main ($) {
2420      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes});
2421      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
2422    
2423      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
2424      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
2425            
2426      my $text = '';      my $text = '';
2427      !!!next-token;      !!!next-token;
2428      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
2429        $text .= $token->{data};        $text .= $token->{data};
2430        !!!next-token;        !!!next-token;
2431      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
# Line 2244  sub _tree_construction_main ($) { Line 2433  sub _tree_construction_main ($) {
2433        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
2434      }      }
2435                                
2436      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
2437    
2438      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
2439          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
2440        ## Ignore the token        ## Ignore the token
2441      } else {      } else {
# Line 2489  sub _tree_construction_main ($) { Line 2678  sub _tree_construction_main ($) {
2678                         }                         }
2679    }; # $insert_to_foster    }; # $insert_to_foster
2680    
2681    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  
2682    
2683    B: {    B: {
2684      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
2685        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'DOCTYPE in the middle');
2686        ## Ignore the token        ## Ignore the token
2687        ## Stay in the phase        ## Stay in the phase
2688        !!!next-token;        !!!next-token;
2689        redo B;        redo B;
2690      } elsif ($token->{type} eq 'end-of-file') {      } elsif ($token->{type} == END_OF_FILE_TOKEN) {
2691        if ($token->{insertion_mode} ne 'trailing end') {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
2692            #
2693          } else {
2694          ## Generate implied end tags          ## Generate implied end tags
2695          if ({          if ({
2696               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,
2697               tbody => 1, tfoot=> 1, thead => 1,               tbody => 1, tfoot=> 1, thead => 1,
2698              }->{$self->{open_elements}->[-1]->[1]}) {              }->{$self->{open_elements}->[-1]->[1]}) {
2699            !!!back-token;            !!!back-token;
2700            $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};            $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};
2701            redo B;            redo B;
2702          }          }
2703                    
# Line 3367  sub _tree_construction_main ($) { Line 2715  sub _tree_construction_main ($) {
2715    
2716        ## Stop parsing        ## Stop parsing
2717        last B;        last B;
2718      } elsif ($token->{type} eq 'start tag' and      } elsif ($token->{type} == START_TAG_TOKEN and
2719               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
2720        if ($self->{insertion_mode} eq 'trailing end') {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
2721            ## Turn into the main phase
2722            !!!parse-error (type => 'after html:html');
2723            $self->{insertion_mode} = AFTER_BODY_IM;
2724          } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
2725          ## Turn into the main phase          ## Turn into the main phase
2726          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html');
2727          $self->{insertion_mode} = $previous_insertion_mode;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
2728        }        }
2729    
2730  ## ISSUE: "aa<html>" is not a parse error.  ## ISSUE: "aa<html>" is not a parse error.
# Line 3390  sub _tree_construction_main ($) { Line 2742  sub _tree_construction_main ($) {
2742        }        }
2743        !!!next-token;        !!!next-token;
2744        redo B;        redo B;
2745      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
2746        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2747        if ($self->{insertion_mode} eq 'trailing end') {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
2748          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2749        } elsif ($self->{insertion_mode} eq 'after body') {        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
2750          $self->{open_elements}->[0]->[0]->append_child ($comment);          $self->{open_elements}->[0]->[0]->append_child ($comment);
2751        } else {        } else {
2752          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
2753        }        }
2754        !!!next-token;        !!!next-token;
2755        redo B;        redo B;
2756      } elsif ($self->{insertion_mode} eq 'before head') {      } elsif ($self->{insertion_mode} & HEAD_IMS) {
2757            if ($token->{type} eq 'character') {        if ($token->{type} == CHARACTER_TOKEN) {
2758              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
2759                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
2760                unless (length $token->{data}) {            unless (length $token->{data}) {
2761                  !!!next-token;              !!!next-token;
2762                  redo B;              redo B;
2763                }            }
2764              }          }
2765              ## As if <head>  
2766              !!!create-element ($self->{head_element}, 'head');          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2767              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            ## As if <head>
2768              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            !!!create-element ($self->{head_element}, 'head');
2769              $self->{insertion_mode} = 'in head';            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2770              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2771    
2772              ## Reprocess in the "in head" insertion mode...
2773              pop @{$self->{open_elements}};
2774    
2775              ## Reprocess in the "after head" insertion mode...
2776            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2777              ## As if </noscript>
2778              pop @{$self->{open_elements}};
2779              !!!parse-error (type => 'in noscript:#character');
2780              
2781              ## Reprocess in the "in head" insertion mode...
2782              ## As if </head>
2783              pop @{$self->{open_elements}};
2784    
2785              ## Reprocess in the "after head" insertion mode...
2786            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2787              pop @{$self->{open_elements}};
2788    
2789              ## Reprocess in the "after head" insertion mode...
2790            }
2791    
2792                ## "after head" insertion mode
2793                ## As if <body>
2794                !!!insert-element ('body');
2795                $self->{insertion_mode} = IN_BODY_IM;
2796              ## reprocess              ## reprocess
2797              redo B;              redo B;
2798            } 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';  
2799              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
2800                !!!next-token;                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2801              #} elsif ({                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
2802              #          base => 1, link => 1, meta => 1,                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2803              #          script => 1, style => 1, title => 1,                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
2804              #         }->{$token->{tag_name}}) {                  $self->{insertion_mode} = IN_HEAD_IM;
2805              #  ## reprocess                  !!!next-token;
2806              } else {                  redo B;
2807                ## reprocess                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2808              }                  #
2809              redo B;                } else {
2810            } elsif ($token->{type} eq 'end tag') {                  !!!parse-error (type => 'in head:head'); # or in head noscript
2811              if ({                  ## Ignore the token
2812                   head => 1, body => 1, html => 1,                  !!!next-token;
2813                   p => 1, br => 1,                  redo B;
2814                  }->{$token->{tag_name}}) {                }
2815                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2816                ## As if <head>                ## As if <head>
2817                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head');
2818                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2819                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2820                $self->{insertion_mode} = 'in head';  
2821                ## reprocess                $self->{insertion_mode} = IN_HEAD_IM;
2822                redo B;                ## Reprocess in the "in head" insertion mode...
             } else {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token ## ISSUE: An issue in the spec.  
               !!!next-token;  
               redo B;  
2823              }              }
2824            } else {  
2825              die "$0: $token->{type}: Unknown type";              if ($token->{tag_name} eq 'base') {
2826            }                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2827          } elsif ($self->{insertion_mode} eq 'in head' or                  ## As if </noscript>
2828                   $self->{insertion_mode} eq 'in head noscript' or                  pop @{$self->{open_elements}};
2829                   $self->{insertion_mode} eq 'after head') {                  !!!parse-error (type => 'in noscript:base');
2830            if ($token->{type} eq 'character') {                
2831              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                  $self->{insertion_mode} = IN_HEAD_IM;
2832                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                  ## Reprocess in the "in head" insertion mode...
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
2833                }                }
2834              }  
               
             #  
           } 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}}) {  
2835                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2836                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2837                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2838                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2839                }                }
2840                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
2841                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2842                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2843                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2844                  !!!next-token;
2845                  redo B;
2846                } elsif ($token->{tag_name} eq 'link') {
2847                  ## NOTE: There is a "as if in head" code clone.
2848                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2849                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2850                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2851                  }
2852                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2853                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2854                  pop @{$self->{open_elements}}
2855                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2856                !!!next-token;                !!!next-token;
2857                redo B;                redo B;
2858              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
2859                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2860                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2861                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2862                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2863                }                }
# Line 3493  sub _tree_construction_main ($) { Line 2865  sub _tree_construction_main ($) {
2865                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2866    
2867                unless ($self->{confident}) {                unless ($self->{confident}) {
                 my $charset;  
2868                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
2869                    $charset = $token->{attributes}->{charset}->{value};                    $self->{change_encoding}
2870                  }                        ->($self, $token->{attributes}->{charset}->{value});
2871                  if ($token->{attributes}->{'http-equiv'}) {                  } elsif ($token->{attributes}->{content}) {
2872                    ## 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.
2873                    if ($token->{attributes}->{'http-equiv'}->{value}                    if ($token->{attributes}->{content}->{value}
2874                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
2875                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
2876                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
2877                      $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                      $self->{change_encoding}
2878                    } ## TODO: And if supported                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
2879                      }
2880                  }                  }
                 ## TODO: Change the encoding  
2881                }                }
2882    
               ## TODO: Extracting |charset| from |meta|.  
2883                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2884                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2885                !!!next-token;                !!!next-token;
2886                redo B;                redo B;
2887              } elsif ($token->{tag_name} eq 'title' and              } elsif ($token->{tag_name} eq 'title') {
2888                       $self->{insertion_mode} eq 'in head') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2889                ## NOTE: There is a "as if in head" code clone.                  ## As if </noscript>
2890                if ($self->{insertion_mode} eq 'after head') {                  pop @{$self->{open_elements}};
2891                    !!!parse-error (type => 'in noscript:title');
2892                  
2893                    $self->{insertion_mode} = IN_HEAD_IM;
2894                    ## Reprocess in the "in head" insertion mode...
2895                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2896                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2897                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2898                }                }
2899    
2900                  ## NOTE: There is a "as if in head" code clone.
2901                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
2902                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
2903                $parse_rcdata->('RCDATA', sub { $parent->append_child ($_[0]) });                $parse_rcdata->(RCDATA_CONTENT_MODEL,
2904                                  sub { $parent->append_child ($_[0]) });
2905                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2906                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2907                redo B;                redo B;
2908              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
2909                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
2910                ## insertion mode 'in head')                ## insertion mode IN_HEAD_IM)
2911                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2912                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2913                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2914                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2915                }                }
2916                $parse_rcdata->('CDATA', $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
2917                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2918                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2919                redo B;                redo B;
2920              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
2921                if ($self->{insertion_mode} eq 'in head') {                if ($self->{insertion_mode} == IN_HEAD_IM) {
2922                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
2923                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2924                  $self->{insertion_mode} = 'in head noscript';                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
2925                  !!!next-token;                  !!!next-token;
2926                  redo B;                  redo B;
2927                } elsif ($self->{insertion_mode} eq 'in head noscript') {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2928                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript:noscript');
2929                  ## Ignore the token                  ## Ignore the token
2930                    !!!next-token;
2931                  redo B;                  redo B;
2932                } else {                } else {
2933                  #                  #
2934                }                }
2935              } elsif ($token->{tag_name} eq 'head' and              } elsif ($token->{tag_name} eq 'script') {
2936                       $self->{insertion_mode} ne 'after head') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2937                !!!parse-error (type => 'in head:head'); # or in head noscript                  ## As if </noscript>
2938                ## Ignore the token                  pop @{$self->{open_elements}};
2939                !!!next-token;                  !!!parse-error (type => 'in noscript:script');
2940                redo B;                
2941              } elsif ($self->{insertion_mode} ne 'in head noscript' and                  $self->{insertion_mode} = IN_HEAD_IM;
2942                       $token->{tag_name} eq 'script') {                  ## Reprocess in the "in head" insertion mode...
2943                if ($self->{insertion_mode} eq 'after head') {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2944                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2945                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2946                }                }
2947    
2948                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2949                $script_start_tag->($insert_to_current);                $script_start_tag->($insert_to_current);
2950                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2951                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
               redo B;  
             } elsif ($self->{insertion_mode} eq 'after head' and  
                      $token->{tag_name} eq 'body') {  
               !!!insert-element ('body', $token->{attributes});  
               $self->{insertion_mode} = 'in body';  
               !!!next-token;  
2952                redo B;                redo B;
2953              } elsif ($self->{insertion_mode} eq 'after head' and              } elsif ($token->{tag_name} eq 'body' or
2954                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
2955                !!!insert-element ('frameset', $token->{attributes});                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2956                $self->{insertion_mode} = 'in frameset';                  ## As if </noscript>
2957                    pop @{$self->{open_elements}};
2958                    !!!parse-error (type => 'in noscript:'.$token->{tag_name});
2959                    
2960                    ## Reprocess in the "in head" insertion mode...
2961                    ## As if </head>
2962                    pop @{$self->{open_elements}};
2963                    
2964                    ## Reprocess in the "after head" insertion mode...
2965                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2966                    pop @{$self->{open_elements}};
2967                    
2968                    ## Reprocess in the "after head" insertion mode...
2969                  }
2970    
2971                  ## "after head" insertion mode
2972                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2973                  if ($token->{tag_name} eq 'body') {
2974                    $self->{insertion_mode} = IN_BODY_IM;
2975                  } elsif ($token->{tag_name} eq 'frameset') {
2976                    $self->{insertion_mode} = IN_FRAMESET_IM;
2977                  } else {
2978                    die "$0: tag name: $self->{tag_name}";
2979                  }
2980                !!!next-token;                !!!next-token;
2981                redo B;                redo B;
2982              } else {              } else {
2983                #                #
2984              }              }
2985            } elsif ($token->{type} eq 'end tag') {  
2986              if ($self->{insertion_mode} eq 'in head' and              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2987                  $token->{tag_name} eq 'head') {                ## As if </noscript>
2988                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
2989                $self->{insertion_mode} = 'after head';                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
2990                !!!next-token;                
2991                redo B;                ## Reprocess in the "in head" insertion mode...
2992              } elsif ($self->{insertion_mode} eq 'in head noscript' and                ## As if </head>
                 $token->{tag_name} eq 'noscript') {  
2993                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
               $self->{insertion_mode} = 'in head';  
               !!!next-token;  
               redo B;  
             } elsif ($self->{insertion_mode} eq 'in head' and  
                      {  
                       body => 1, html => 1,  
                       p => 1, br => 1,  
                      }->{$token->{tag_name}}) {  
               #  
             } 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;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
2994    
2995            ## As if </head> or </noscript> or <body>                ## Reprocess in the "after head" insertion mode...
2996            if ($self->{insertion_mode} eq 'in head') {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2997              pop @{$self->{open_elements}};                ## As if </head>
2998              $self->{insertion_mode} = 'after head';                pop @{$self->{open_elements}};
           } 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'  
             !!!insert-element ('body');  
             $self->{insertion_mode} = 'in body';  
           }  
           ## reprocess  
           redo B;  
2999    
3000            ## ISSUE: An issue in the spec.                ## Reprocess in the "after head" insertion mode...
3001          } 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});  
3002    
3003              !!!next-token;              ## "after head" insertion mode
3004              redo B;              ## As if <body>
3005            } else {              !!!insert-element ('body');
3006              $in_body->($insert_to_current);              $self->{insertion_mode} = IN_BODY_IM;
3007                ## reprocess
3008              redo B;              redo B;
3009            }            } elsif ($token->{type} == END_TAG_TOKEN) {
3010          } elsif ($self->{insertion_mode} eq 'in table') {              if ($token->{tag_name} eq 'head') {
3011            if ($token->{type} eq 'character') {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3012              ## NOTE: There are "character in table" code clones.                  ## As if <head>
3013              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                  !!!create-element ($self->{head_element}, 'head');
3014                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3015                                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3016                unless (length $token->{data}) {  
3017                    ## Reprocess in the "in head" insertion mode...
3018                    pop @{$self->{open_elements}};
3019                    $self->{insertion_mode} = AFTER_HEAD_IM;
3020                    !!!next-token;
3021                    redo B;
3022                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3023                    ## As if </noscript>
3024                    pop @{$self->{open_elements}};
3025                    !!!parse-error (type => 'in noscript:script');
3026                    
3027                    ## Reprocess in the "in head" insertion mode...
3028                    pop @{$self->{open_elements}};
3029                    $self->{insertion_mode} = AFTER_HEAD_IM;
3030                    !!!next-token;
3031                    redo B;
3032                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3033                    pop @{$self->{open_elements}};
3034                    $self->{insertion_mode} = AFTER_HEAD_IM;
3035                  !!!next-token;                  !!!next-token;
3036                  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});  
3037                } else {                } else {
3038                  $foster_parent_element->insert_before                  #
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
3039                }                }
3040              } else {              } elsif ($token->{tag_name} eq 'noscript') {
3041                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
             }  
               
             !!!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]);  
3042                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3043                    $self->{insertion_mode} = IN_HEAD_IM;
3044                    !!!next-token;
3045                    redo B;
3046                  } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3047                    !!!parse-error (type => 'unmatched end tag:noscript');
3048                    ## Ignore the token ## ISSUE: An issue in the spec.
3049                    !!!next-token;
3050                    redo B;
3051                  } else {
3052                    #
3053                }                }
   
               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;  
3054              } elsif ({              } elsif ({
3055                        col => 1,                        body => 1, html => 1,
                       td => 1, th => 1, tr => 1,  
3056                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3057                ## Clear back to table context                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3058                while ($self->{open_elements}->[-1]->[1] ne 'table' and                  ## As if <head>
3059                       $self->{open_elements}->[-1]->[1] ne 'html') {                  !!!create-element ($self->{head_element}, 'head');
3060                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3061                  pop @{$self->{open_elements}};                  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]);  
3062    
3063                ## As if </table>                  $self->{insertion_mode} = IN_HEAD_IM;
3064                ## have a table element in table scope                  ## Reprocess in the "in head" insertion mode...
3065                my $i;                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3066                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3067                  my $node = $self->{open_elements}->[$_];                  ## Ignore the token
                 if ($node->[1] eq 'table') {  
                   $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>  
3068                  !!!next-token;                  !!!next-token;
3069                  redo B;                  redo B;
3070                }                }
3071                                
3072                ## generate implied end tags                #
3073                if ({              } elsif ({
3074                     dd => 1, dt => 1, li => 1, p => 1,                        p => 1, br => 1,
3075                     td => 1, th => 1, tr => 1,                       }->{$token->{tag_name}}) {
3076                     tbody => 1, tfoot=> 1, thead => 1,                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3077                    }->{$self->{open_elements}->[-1]->[1]}) {                  ## As if <head>
3078                  !!!back-token; # <table>                  !!!create-element ($self->{head_element}, 'head');
3079                  $token = {type => 'end tag', tag_name => 'table'};                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3080                  !!!back-token;                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
3081    
3082                if ($self->{open_elements}->[-1]->[1] ne 'table') {                  $self->{insertion_mode} = IN_HEAD_IM;
3083                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  ## Reprocess in the "in head" insertion mode...
3084                }                }
3085    
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
3086                #                #
3087              }              } else {
3088            } elsif ($token->{type} eq 'end tag') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3089              if ($token->{tag_name} eq 'table') {                  #
3090                ## have a table element in table scope                } else {
               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) {  
3091                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3092                  ## Ignore the token                  ## Ignore the token
3093                  !!!next-token;                  !!!next-token;
3094                  redo B;                  redo B;
3095                }                }
3096                              }
               ## 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]);  
               }  
3097    
3098                splice @{$self->{open_elements}}, $i;              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3099                  ## As if </noscript>
3100                  pop @{$self->{open_elements}};
3101                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
3102                  
3103                  ## Reprocess in the "in head" insertion mode...
3104                  ## As if </head>
3105                  pop @{$self->{open_elements}};
3106    
3107                $self->_reset_insertion_mode;                ## Reprocess in the "after head" insertion mode...
3108                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3109                  ## As if </head>
3110                  pop @{$self->{open_elements}};
3111    
3112                !!!next-token;                ## Reprocess in the "after head" insertion mode...
3113                redo B;              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
             } elsif ({  
                       body => 1, caption => 1, col => 1, colgroup => 1,  
                       html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,  
                       thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
3114                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3115                ## Ignore the token                ## Ignore the token ## ISSUE: An issue in the spec.
3116                !!!next-token;                !!!next-token;
3117                redo B;                redo B;
             } else {  
               #  
3118              }              }
3119    
3120                ## "after head" insertion mode
3121                ## As if <body>
3122                !!!insert-element ('body');
3123                $self->{insertion_mode} = IN_BODY_IM;
3124                ## reprocess
3125                redo B;
3126            } else {            } else {
3127              #              die "$0: $token->{type}: Unknown token type";
3128            }            }
3129    
3130            !!!parse-error (type => 'in table:'.$token->{tag_name});            ## ISSUE: An issue in the spec.
3131            $in_body->($insert_to_foster);      } elsif ($self->{insertion_mode} & BODY_IMS) {
3132            redo B;            if ($token->{type} == CHARACTER_TOKEN) {
3133          } elsif ($self->{insertion_mode} eq 'in caption') {              ## NOTE: There is a code clone of "character in body".
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a code clone of "character in body".  
3134              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
3135                            
3136              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3137    
3138              !!!next-token;              !!!next-token;
3139              redo B;              redo B;
3140            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
3141              if ({              if ({
3142                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
3143                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
3144                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3145                !!!parse-error (type => 'not closed:caption');                if ($self->{insertion_mode} == IN_CELL_IM) {
3146                    ## have an element in table scope
3147                    my $tn;
3148                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3149                      my $node = $self->{open_elements}->[$_];
3150                      if ($node->[1] eq 'td' or $node->[1] eq 'th') {
3151                        $tn = $node->[1];
3152                        last INSCOPE;
3153                      } elsif ({
3154                                table => 1, html => 1,
3155                               }->{$node->[1]}) {
3156                        last INSCOPE;
3157                      }
3158                    } # INSCOPE
3159                      unless (defined $tn) {
3160                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3161                        ## Ignore the token
3162                        !!!next-token;
3163                        redo B;
3164                      }
3165                    
3166                    ## Close the cell
3167                    !!!back-token; # <?>
3168                    $token = {type => END_TAG_TOKEN, tag_name => $tn};
3169                    redo B;
3170                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3171                    !!!parse-error (type => 'not closed:caption');
3172                    
3173                    ## As if </caption>
3174                    ## have a table element in table scope
3175                    my $i;
3176                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3177                      my $node = $self->{open_elements}->[$_];
3178                      if ($node->[1] eq 'caption') {
3179                        $i = $_;
3180                        last INSCOPE;
3181                      } elsif ({
3182                                table => 1, html => 1,
3183                               }->{$node->[1]}) {
3184                        last INSCOPE;
3185                      }
3186                    } # INSCOPE
3187                      unless (defined $i) {
3188                        !!!parse-error (type => 'unmatched end tag:caption');
3189                        ## Ignore the token
3190                        !!!next-token;
3191                        redo B;
3192                      }
3193                    
3194                    ## generate implied end tags
3195                    if ({
3196                         dd => 1, dt => 1, li => 1, p => 1,
3197                         td => 1, th => 1, tr => 1,
3198                         tbody => 1, tfoot=> 1, thead => 1,
3199                        }->{$self->{open_elements}->[-1]->[1]}) {
3200                      !!!back-token; # <?>
3201                      $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3202                      !!!back-token;
3203                      $token = {type => END_TAG_TOKEN,
3204                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3205                      redo B;
3206                    }
3207    
3208                ## As if </caption>                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3209                ## have a table element in table scope                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'caption') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
3210                  }                  }
3211                } # INSCOPE                  
3212                unless (defined $i) {                  splice @{$self->{open_elements}}, $i;
3213                  !!!parse-error (type => 'unmatched end tag:caption');                  
3214                    $clear_up_to_marker->();
3215                    
3216                    $self->{insertion_mode} = IN_TABLE_IM;
3217                    
3218                    ## reprocess
3219                    redo B;
3220                  } else {
3221                    #
3222                  }
3223                } else {
3224                  #
3225                }
3226              } elsif ($token->{type} == END_TAG_TOKEN) {
3227                if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
3228                  if ($self->{insertion_mode} == IN_CELL_IM) {
3229                    ## have an element in table scope
3230                    my $i;
3231                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3232                      my $node = $self->{open_elements}->[$_];
3233                      if ($node->[1] eq $token->{tag_name}) {
3234                        $i = $_;
3235                        last INSCOPE;
3236                      } elsif ({
3237                                table => 1, html => 1,
3238                               }->{$node->[1]}) {
3239                        last INSCOPE;
3240                      }
3241                    } # INSCOPE
3242                      unless (defined $i) {
3243                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3244                        ## Ignore the token
3245                        !!!next-token;
3246                        redo B;
3247                      }
3248                    
3249                    ## generate implied end tags
3250                    if ({
3251                         dd => 1, dt => 1, li => 1, p => 1,
3252                         td => ($token->{tag_name} eq 'th'),
3253                         th => ($token->{tag_name} eq 'td'),
3254                         tr => 1,
3255                         tbody => 1, tfoot=> 1, thead => 1,
3256                        }->{$self->{open_elements}->[-1]->[1]}) {
3257                      !!!back-token;
3258                      $token = {type => END_TAG_TOKEN,
3259                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3260                      redo B;
3261                    }
3262                    
3263                    if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
3264                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3265                    }
3266                    
3267                    splice @{$self->{open_elements}}, $i;
3268                    
3269                    $clear_up_to_marker->();
3270                    
3271                    $self->{insertion_mode} = IN_ROW_IM;
3272                    
3273                    !!!next-token;
3274                    redo B;
3275                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3276                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3277                  ## Ignore the token                  ## Ignore the token
3278                  !!!next-token;                  !!!next-token;
3279                  redo B;                  redo B;
3280                  } else {
3281                    #
3282                }                }
3283                              } elsif ($token->{tag_name} eq 'caption') {
3284                ## generate implied end tags                if ($self->{insertion_mode} == IN_CAPTION_IM) {
3285                if ({                  ## have a table element in table scope
3286                     dd => 1, dt => 1, li => 1, p => 1,                  my $i;
3287                     td => 1, th => 1, tr => 1,                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3288                     tbody => 1, tfoot=> 1, thead => 1,                    my $node = $self->{open_elements}->[$_];
3289                    }->{$self->{open_elements}->[-1]->[1]}) {                    if ($node->[1] eq $token->{tag_name}) {
3290                  !!!back-token; # <?>                      $i = $_;
3291                  $token = {type => 'end tag', tag_name => 'caption'};                      last INSCOPE;
3292                  !!!back-token;                    } elsif ({
3293                  $token = {type => 'end tag',                              table => 1, html => 1,
3294                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                             }->{$node->[1]}) {
3295                        last INSCOPE;
3296                      }
3297                    } # INSCOPE
3298                      unless (defined $i) {
3299                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3300                        ## Ignore the token
3301                        !!!next-token;
3302                        redo B;
3303                      }
3304                    
3305                    ## generate implied end tags
3306                    if ({
3307                         dd => 1, dt => 1, li => 1, p => 1,
3308                         td => 1, th => 1, tr => 1,
3309                         tbody => 1, tfoot=> 1, thead => 1,
3310                        }->{$self->{open_elements}->[-1]->[1]}) {
3311                      !!!back-token;
3312                      $token = {type => END_TAG_TOKEN,
3313                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3314                      redo B;
3315                    }
3316                    
3317                    if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3318                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3319                    }
3320                    
3321                    splice @{$self->{open_elements}}, $i;
3322                    
3323                    $clear_up_to_marker->();
3324                    
3325                    $self->{insertion_mode} = IN_TABLE_IM;
3326                    
3327                    !!!next-token;
3328                  redo B;                  redo B;
3329                  } elsif ($self->{insertion_mode} == IN_CELL_IM) {
3330                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3331                    ## Ignore the token
3332                    !!!next-token;
3333                    redo B;
3334                  } else {
3335                    #
3336                }                }
3337                } elsif ({
3338                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                        table => 1, tbody => 1, tfoot => 1,
3339                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                        thead => 1, tr => 1,
3340                }                       }->{$token->{tag_name}} and
3341                         $self->{insertion_mode} == IN_CELL_IM) {
3342                splice @{$self->{open_elements}}, $i;                ## have an element in table scope
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in table';  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'caption') {  
               ## have a table element in table scope  
3343                my $i;                my $i;
3344                  my $tn;
3345                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3346                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3347                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
3348                    $i = $_;                    $i = $_;
3349                    last INSCOPE;                    last INSCOPE;
3350                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {
3351                      $tn = $node->[1];
3352                      ## NOTE: There is exactly one |td| or |th| element
3353                      ## in scope in the stack of open elements by definition.
3354                  } elsif ({                  } elsif ({
3355                            table => 1, html => 1,                            table => 1, html => 1,
3356                           }->{$node->[1]}) {                           }->{$node->[1]}) {
# Line 3953  sub _tree_construction_main ($) { Line 3363  sub _tree_construction_main ($) {
3363                  !!!next-token;                  !!!next-token;
3364                  redo B;                  redo B;
3365                }                }
                 
               ## 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->();  
3366    
3367                $self->{insertion_mode} = 'in table';                ## Close the cell
3368                  !!!back-token; # </?>
3369                !!!next-token;                $token = {type => END_TAG_TOKEN, tag_name => $tn};
3370                redo B;                redo B;
3371              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table' and
3372                         $self->{insertion_mode} == IN_CAPTION_IM) {
3373                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption');
3374    
3375                ## As if </caption>                ## As if </caption>
# Line 4009  sub _tree_construction_main ($) { Line 3400  sub _tree_construction_main ($) {
3400                     tbody => 1, tfoot=> 1, thead => 1,                     tbody => 1, tfoot=> 1, thead => 1,
3401                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3402                  !!!back-token; # </table>                  !!!back-token; # </table>
3403                  $token = {type => 'end tag', tag_name => 'caption'};                  $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3404                  !!!back-token;                  !!!back-token;
3405                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3406                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3407                  redo B;                  redo B;
3408                }                }
# Line 4024  sub _tree_construction_main ($) { Line 3415  sub _tree_construction_main ($) {
3415    
3416                $clear_up_to_marker->();                $clear_up_to_marker->();
3417    
3418                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
3419    
3420                ## reprocess                ## reprocess
3421                redo B;                redo B;
3422              } elsif ({              } elsif ({
3423                        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,  
3424                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3425                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
3426                ## Ignore the token                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
                 
           $in_body->($insert_to_current);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in column group') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'col') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}};  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'colgroup') {  
               if ($self->{open_elements}->[-1]->[1] eq 'html') {  
                 !!!parse-error (type => 'unmatched end tag:colgroup');  
3427                  ## Ignore the token                  ## Ignore the token
3428                  !!!next-token;                  !!!next-token;
3429                  redo B;                  redo B;
3430                } else {                } else {
3431                  pop @{$self->{open_elements}}; # colgroup                  #
                 $self->{insertion_mode} = 'in table';  
                 !!!next-token;  
                 redo B;              
3432                }                }
3433              } elsif ($token->{tag_name} eq 'col') {              } elsif ({
3434                !!!parse-error (type => 'unmatched end tag:col');                        tbody => 1, tfoot => 1,
3435                          thead => 1, tr => 1,
3436                         }->{$token->{tag_name}} and
3437                         $self->{insertion_mode} == IN_CAPTION_IM) {
3438                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3439                ## Ignore the token                ## Ignore the token
3440                !!!next-token;                !!!next-token;
3441                redo B;                redo B;
3442              } else {              } else {
3443                #                #
3444              }              }
3445            } else {        } else {
3446              #          die "$0: $token->{type}: Unknown token type";
3447            }        }
3448    
3449            ## As if </colgroup>        $insert = $insert_to_current;
3450            if ($self->{open_elements}->[-1]->[1] eq 'html') {        #
3451              !!!parse-error (type => 'unmatched end tag:colgroup');      } elsif ($self->{insertion_mode} & TABLE_IMS) {
3452              ## 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.  
3453              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3454                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3455                                
# Line 4121  sub _tree_construction_main ($) { Line 3466  sub _tree_construction_main ($) {
3466              ## into the current node" while characters might not be              ## into the current node" while characters might not be
3467              ## result in a new Text node.              ## result in a new Text node.
3468              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
3469                
3470              if ({              if ({
3471                   table => 1, tbody => 1, tfoot => 1,                   table => 1, tbody => 1, tfoot => 1,
3472                   thead => 1, tr => 1,                   thead => 1, tr => 1,
# Line 4161  sub _tree_construction_main ($) { Line 3506  sub _tree_construction_main ($) {
3506                            
3507              !!!next-token;              !!!next-token;
3508              redo B;              redo B;
3509            } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
3510              if ({              if ({
3511                   tr => 1,                   tr => ($self->{insertion_mode} != IN_ROW_IM),
3512                   th => 1, td => 1,                   th => 1, td => 1,
3513                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3514                unless ($token->{tag_name} eq 'tr') {                if ($self->{insertion_mode} == IN_TABLE_IM) {
3515                  !!!parse-error (type => 'missing start tag:tr');                  ## Clear back to table context
3516                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3517                           $self->{open_elements}->[-1]->[1] ne 'html') {
3518                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3519                      pop @{$self->{open_elements}};
3520                    }
3521                    
3522                    !!!insert-element ('tbody');
3523                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3524                    ## reprocess in the "in table body" insertion mode...
3525                }                }
3526    
3527                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3528                    unless ($token->{tag_name} eq 'tr') {
3529                      !!!parse-error (type => 'missing start tag:tr');
3530                    }
3531                    
3532                    ## Clear back to table body context
3533                    while (not {
3534                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3535                    }->{$self->{open_elements}->[-1]->[1]}) {
3536                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3537                      pop @{$self->{open_elements}};
3538                    }
3539                    
3540                    $self->{insertion_mode} = IN_ROW_IM;
3541                    if ($token->{tag_name} eq 'tr') {
3542                      !!!insert-element ($token->{tag_name}, $token->{attributes});
3543                      !!!next-token;
3544                      redo B;
3545                    } else {
3546                      !!!insert-element ('tr');
3547                      ## reprocess in the "in row" insertion mode
3548                    }
3549                  }
3550    
3551                  ## Clear back to table row context
3552                while (not {                while (not {
3553                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tr => 1, html => 1,
3554                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
3555                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3556                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3557                }                }
3558                                
3559                $self->{insertion_mode} = 'in row';                !!!insert-element ($token->{tag_name}, $token->{attributes});
3560                if ($token->{tag_name} eq 'tr') {                $self->{insertion_mode} = IN_CELL_IM;
3561                  !!!insert-element ($token->{tag_name}, $token->{attributes});  
3562                  !!!next-token;                push @$active_formatting_elements, ['#marker', ''];
3563                } else {                
3564                  !!!insert-element ('tr');                !!!next-token;
                 ## reprocess  
               }  
3565                redo B;                redo B;
3566              } elsif ({              } elsif ({
3567                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
3568                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
3569                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3570                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3571                ## have an element in table scope                if ($self->{insertion_mode} == IN_ROW_IM) {
3572                my $i;                  ## As if </tr>
3573                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
3574                  my $node = $self->{open_elements}->[$_];                  my $i;
3575                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3576                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
3577                      }->{$node->[1]}) {                    if ($node->[1] eq 'tr') {
3578                    $i = $_;                      $i = $_;
3579                    last INSCOPE;                      last INSCOPE;
3580                  } elsif ({                    } elsif ({
3581                            table => 1, html => 1,                              table => 1, html => 1,
3582                           }->{$node->[1]}) {                             }->{$node->[1]}) {
3583                    last INSCOPE;                      last INSCOPE;
3584                      }
3585                    } # INSCOPE
3586                    unless (defined $i) {
3587                      !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});
3588                      ## Ignore the token
3589                      !!!next-token;
3590                      redo B;
3591                    }
3592                    
3593                    ## Clear back to table row context
3594                    while (not {
3595                      tr => 1, html => 1,
3596                    }->{$self->{open_elements}->[-1]->[1]}) {
3597                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3598                      pop @{$self->{open_elements}};
3599                    }
3600                    
3601                    pop @{$self->{open_elements}}; # tr
3602                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3603                    if ($token->{tag_name} eq 'tr') {
3604                      ## reprocess
3605                      redo B;
3606                    } else {
3607                      ## reprocess in the "in table body" insertion mode...
3608                  }                  }
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
3609                }                }
3610    
3611                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3612                while (not {                  ## have an element in table scope
3613                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  my $i;
3614                }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3615                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    my $node = $self->{open_elements}->[$_];
3616                      if ({
3617                           tbody => 1, thead => 1, tfoot => 1,
3618                          }->{$node->[1]}) {
3619                        $i = $_;
3620                        last INSCOPE;
3621                      } elsif ({
3622                                table => 1, html => 1,
3623                               }->{$node->[1]}) {
3624                        last INSCOPE;
3625                      }
3626                    } # INSCOPE
3627                    unless (defined $i) {
3628                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3629                      ## Ignore the token
3630                      !!!next-token;
3631                      redo B;
3632                    }
3633    
3634                    ## Clear back to table body context
3635                    while (not {
3636                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3637                    }->{$self->{open_elements}->[-1]->[1]}) {
3638                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3639                      pop @{$self->{open_elements}};
3640                    }
3641                    
3642                    ## As if <{current node}>
3643                    ## have an element in table scope
3644                    ## true by definition
3645                    
3646                    ## Clear back to table body context
3647                    ## nop by definition
3648                    
3649                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3650                    $self->{insertion_mode} = IN_TABLE_IM;
3651                    ## reprocess in "in table" insertion mode...
3652                }                }
3653    
3654                ## As if <{current node}>                if ($token->{tag_name} eq 'col') {
3655                ## have an element in table scope                  ## Clear back to table context
3656                ## true by definition                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
3657                           $self->{open_elements}->[-1]->[1] ne 'html') {
3658                ## Clear back to table body context                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3659                ## nop by definition                    pop @{$self->{open_elements}};
3660                    }
3661                pop @{$self->{open_elements}};                  
3662                $self->{insertion_mode} = 'in table';                  !!!insert-element ('colgroup');
3663                ## reprocess                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
3664                redo B;                  ## reprocess
3665                    redo B;
3666                  } elsif ({
3667                            caption => 1,
3668                            colgroup => 1,
3669                            tbody => 1, tfoot => 1, thead => 1,
3670                           }->{$token->{tag_name}}) {
3671                    ## Clear back to table context
3672                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3673                           $self->{open_elements}->[-1]->[1] ne 'html') {
3674                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3675                      pop @{$self->{open_elements}};
3676                    }
3677                    
3678                    push @$active_formatting_elements, ['#marker', '']
3679                        if $token->{tag_name} eq 'caption';
3680                    
3681                    !!!insert-element ($token->{tag_name}, $token->{attributes});
3682                    $self->{insertion_mode} = {
3683                                               caption => IN_CAPTION_IM,
3684                                               colgroup => IN_COLUMN_GROUP_IM,
3685                                               tbody => IN_TABLE_BODY_IM,
3686                                               tfoot => IN_TABLE_BODY_IM,
3687                                               thead => IN_TABLE_BODY_IM,
3688                                              }->{$token->{tag_name}};
3689                    !!!next-token;
3690                    redo B;
3691                  } else {
3692                    die "$0: in table: <>: $token->{tag_name}";
3693                  }
3694              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
3695                ## 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');  
3696    
3697                ## As if </table>                ## As if </table>
3698                ## have a table element in table scope                ## have a table element in table scope
# Line 4264  sub _tree_construction_main ($) { Line 3722  sub _tree_construction_main ($) {
3722                     tbody => 1, tfoot=> 1, thead => 1,                     tbody => 1, tfoot=> 1, thead => 1,
3723                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3724                  !!!back-token; # <table>                  !!!back-token; # <table>
3725                  $token = {type => 'end tag', tag_name => 'table'};                  $token = {type => END_TAG_TOKEN, tag_name => 'table'};
3726                  !!!back-token;                  !!!back-token;
3727                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3728                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3729                  redo B;                  redo B;
3730                }                }
# Line 4277  sub _tree_construction_main ($) { Line 3735  sub _tree_construction_main ($) {
3735    
3736                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
3737    
3738                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
3739    
3740                ## reprocess                ## reprocess
3741                redo B;                redo B;
3742              } else {          } else {
3743                #            !!!parse-error (type => 'in table:'.$token->{tag_name});
3744              }  
3745            } elsif ($token->{type} eq 'end tag') {            $insert = $insert_to_foster;
3746              if ({            #
3747                   tbody => 1, tfoot => 1, thead => 1,          }
3748                  }->{$token->{tag_name}}) {        } elsif ($token->{type} == END_TAG_TOKEN) {
3749                if ($token->{tag_name} eq 'tr' and
3750                    $self->{insertion_mode} == IN_ROW_IM) {
3751                ## have an element in table scope                ## have an element in table scope
3752                my $i;                my $i;
3753                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 4308  sub _tree_construction_main ($) { Line 3768  sub _tree_construction_main ($) {
3768                  redo B;                  redo B;
3769                }                }
3770    
3771                ## Clear back to table body context                ## Clear back to table row context
3772                while (not {                while (not {
3773                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tr => 1, html => 1,
3774                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
3775                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3776                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3777                }                }
3778    
3779                pop @{$self->{open_elements}};                pop @{$self->{open_elements}}; # tr
3780                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
3781                !!!next-token;                !!!next-token;
3782                redo B;                redo B;
3783              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
3784                ## have an element in table scope                if ($self->{insertion_mode} == IN_ROW_IM) {
3785                my $i;                  ## As if </tr>
3786                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
3787                  my $node = $self->{open_elements}->[$_];                  my $i;
3788                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3789                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
3790                      }->{$node->[1]}) {                    if ($node->[1] eq 'tr') {
3791                    $i = $_;                      $i = $_;
3792                    last INSCOPE;                      last INSCOPE;
3793                  } elsif ({                    } elsif ({
3794                            table => 1, html => 1,                              table => 1, html => 1,
3795                           }->{$node->[1]}) {                             }->{$node->[1]}) {
3796                    last INSCOPE;                      last INSCOPE;
3797                      }
3798                    } # INSCOPE
3799                    unless (defined $i) {
3800                      !!!parse-error (type => 'unmatched end tag:'.$token->{type});
3801                      ## Ignore the token
3802                      !!!next-token;
3803                      redo B;
3804                  }                  }
3805                } # INSCOPE                  
3806                unless (defined $i) {                  ## Clear back to table row context
3807                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  while (not {
3808                  ## 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,  
3809                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
3810                # MUST                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3811                my $foster_parent_element;                    pop @{$self->{open_elements}};
3812                my $next_sibling;                  }
3813                my $prev_sibling;                  
3814                OE: for (reverse 0..$#{$self->{open_elements}}) {                  pop @{$self->{open_elements}}; # tr
3815                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3816                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                  ## reprocess in the "in table body" insertion mode...
3817                    if (defined $parent and $parent->node_type == 1) {                }
3818                      $foster_parent_element = $parent;  
3819                      $next_sibling = $self->{open_elements}->[$_]->[0];                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3820                      $prev_sibling = $next_sibling->previous_sibling;                  ## have an element in table scope
3821                    } else {                  my $i;
3822                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3823                      $prev_sibling = $foster_parent_element->last_child;                    my $node = $self->{open_elements}->[$_];
3824                      if ({
3825                           tbody => 1, thead => 1, tfoot => 1,
3826                          }->{$node->[1]}) {
3827                        $i = $_;
3828                        last INSCOPE;
3829                      } elsif ({
3830                                table => 1, html => 1,
3831                               }->{$node->[1]}) {
3832                        last INSCOPE;
3833                    }                    }
3834                    last OE;                  } # INSCOPE
3835                    unless (defined $i) {
3836                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3837                      ## Ignore the token
3838                      !!!next-token;
3839                      redo B;
3840                  }                  }
3841                } # OE                  
3842                $foster_parent_element = $self->{open_elements}->[0]->[0] and                  ## Clear back to table body context
3843                $prev_sibling = $foster_parent_element->last_child                  while (not {
3844                  unless defined $foster_parent_element;                    tbody => 1, tfoot => 1, thead => 1, html => 1,
3845                if (defined $prev_sibling and                  }->{$self->{open_elements}->[-1]->[1]}) {
3846                    $prev_sibling->node_type == 3) {                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3847                  $prev_sibling->manakai_append_text ($token->{data});                    pop @{$self->{open_elements}};
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'th' or  
                 $token->{tag_name} eq 'td') {  
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
                 
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = 'in cell';  
   
               push @$active_formatting_elements, ['#marker', ''];  
                 
               !!!next-token;  
               redo B;  
             } elsif ({  
                       caption => 1, col => 1, colgroup => 1,  
                       tbody => 1, tfoot => 1, thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## As if </tr>  
               ## 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;  
3848                  }                  }
3849                } # INSCOPE                  
3850                unless (defined $i) {                  ## As if <{current node}>
3851                  !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                  ## have an element in table scope
3852                  ## Ignore the token                  ## true by definition
3853                  !!!next-token;                  
3854                  redo B;                  ## Clear back to table body context
3855                }                  ## nop by definition
3856                    
               ## 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]);  
3857                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3858                    $self->{insertion_mode} = IN_TABLE_IM;
3859                    ## reprocess in the "in table" insertion mode...
3860                }                }
3861    
               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>  
3862                ## have a table element in table scope                ## have a table element in table scope
3863                my $i;                my $i;
3864                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3865                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3866                  if ($node->[1] eq 'table') {                  if ($node->[1] eq $token->{tag_name}) {
3867                    $i = $_;                    $i = $_;
3868                    last INSCOPE;                    last INSCOPE;
3869                  } elsif ({                  } elsif ({
# Line 4514  sub _tree_construction_main ($) { Line 3873  sub _tree_construction_main ($) {
3873                  }                  }
3874                } # INSCOPE                } # INSCOPE
3875                unless (defined $i) {                unless (defined $i) {
3876                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3877                  ## Ignore tokens </table><table>                  ## Ignore the token
3878                  !!!next-token;                  !!!next-token;
3879                  redo B;                  redo B;
3880                }                }
3881                  
3882                ## generate implied end tags                ## generate implied end tags
3883                if ({                if ({
3884                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3885                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3886                     tbody => 1, tfoot=> 1, thead => 1,                     tbody => 1, tfoot=> 1, thead => 1,
3887                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
3888                  !!!back-token;                  !!!back-token;
3889                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3890                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3891                  redo B;                  redo B;
3892                }                }
3893                  
3894                if ($self->{open_elements}->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
3895                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3896                }                }
3897                    
3898                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
3899                  
3900                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
3901                  
               ## 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';  
3902                !!!next-token;                !!!next-token;
3903                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;  
3904              } elsif ({              } elsif ({
3905                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
3906                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}} and
3907                ## have an element in table scope                       $self->{insertion_mode} & ROW_IMS) {
3908                my $i;                if ($self->{insertion_mode} == IN_ROW_IM) {
3909                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
3910                  my $node = $self->{open_elements}->[$_];                  my $i;
3911                  if ($node->[1] eq $token->{tag_name}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3912                    $i = $_;                    my $node = $self->{open_elements}->[$_];
3913                    last INSCOPE;                    if ($node->[1] eq $token->{tag_name}) {
3914                  } elsif ({                      $i = $_;
3915                            table => 1, html => 1,                      last INSCOPE;
3916                           }->{$node->[1]}) {                    } elsif ({
3917                    last INSCOPE;                              table => 1, html => 1,
3918                               }->{$node->[1]}) {
3919                        last INSCOPE;
3920                      }
3921                    } # INSCOPE
3922                      unless (defined $i) {
3923                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3924                        ## Ignore the token
3925                        !!!next-token;
3926                        redo B;
3927                      }
3928                    
3929                    ## As if </tr>
3930                    ## have an element in table scope
3931                    my $i;
3932                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3933                      my $node = $self->{open_elements}->[$_];
3934                      if ($node->[1] eq 'tr') {
3935                        $i = $_;
3936                        last INSCOPE;
3937                      } elsif ({
3938                                table => 1, html => 1,
3939                               }->{$node->[1]}) {
3940                        last INSCOPE;
3941                      }
3942                    } # INSCOPE
3943                      unless (defined $i) {
3944                        !!!parse-error (type => 'unmatched end tag:tr');
3945                        ## Ignore the token
3946                        !!!next-token;
3947                        redo B;
3948                      }
3949                    
3950                    ## Clear back to table row context
3951                    while (not {
3952                      tr => 1, html => 1,
3953                    }->{$self->{open_elements}->[-1]->[1]}) {
3954                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3955                      pop @{$self->{open_elements}};
3956                  }                  }
3957                } # INSCOPE                  
3958                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
3959                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3960                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
3961                }                }
3962    
               ## As if </tr>  
3963                ## have an element in table scope                ## have an element in table scope
3964                my $i;                my $i;
3965                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3966                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3967                  if ($node->[1] eq 'tr') {                  if ($node->[1] eq $token->{tag_name}) {
3968                    $i = $_;                    $i = $_;
3969                    last INSCOPE;                    last INSCOPE;
3970                  } elsif ({                  } elsif ({
# Line 4653  sub _tree_construction_main ($) { Line 3974  sub _tree_construction_main ($) {
3974                  }                  }
3975                } # INSCOPE                } # INSCOPE
3976                unless (defined $i) {                unless (defined $i) {
3977                  !!!parse-error (type => 'unmatched end tag:tr');                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3978                  ## Ignore the token                  ## Ignore the token
3979                  !!!next-token;                  !!!next-token;
3980                  redo B;                  redo B;
3981                }                }
3982    
3983                ## Clear back to table row context                ## Clear back to table body context
3984                while (not {                while (not {
3985                  tr => 1, html => 1,                  tbody => 1, tfoot => 1, thead => 1, html => 1,
3986                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
3987                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3988                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3989                }                }
3990    
3991                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}};
3992                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_IM;
3993                ## reprocess                !!!next-token;
3994                redo B;                redo B;
3995              } elsif ({              } elsif ({
3996                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
3997                        colgroup => 1, html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
3998                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3999                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
4000                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4001                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4002                ## Ignore the token                ## Ignore the token
4003                !!!next-token;                !!!next-token;
4004                redo B;                redo B;
4005              } else {          } else {
4006                #            !!!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});  
4007    
4008              !!!next-token;            $insert = $insert_to_foster;
4009              redo B;            #
4010            } elsif ($token->{type} eq 'start tag') {          }
4011              if ({        } else {
4012                   caption => 1, col => 1, colgroup => 1,          die "$0: $token->{type}: Unknown token type";
4013                   tbody => 1, td => 1, tfoot => 1, th => 1,        }
4014                   thead => 1, tr => 1,      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
4015                  }->{$token->{tag_name}}) {            if ($token->{type} == CHARACTER_TOKEN) {
4016                ## have an element in table scope              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4017                my $tn;                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4018                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                unless (length $token->{data}) {
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'td' or $node->[1] eq 'th') {  
                   $tn = $node->[1];  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $tn) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
4019                  !!!next-token;                  !!!next-token;
4020                  redo B;                  redo B;
4021                }                }
4022                }
4023                ## Close the cell              
4024                !!!back-token; # <?>              #
4025                $token = {type => 'end tag', tag_name => $tn};            } elsif ($token->{type} == START_TAG_TOKEN) {
4026                if ($token->{tag_name} eq 'col') {
4027                  !!!insert-element ($token->{tag_name}, $token->{attributes});
4028                  pop @{$self->{open_elements}};
4029                  !!!next-token;
4030                redo B;                redo B;
4031              } else {              } else {
4032                #                #
4033              }              }
4034            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
4035              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'colgroup') {
4036                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] eq 'html') {
4037                my $i;                  !!!parse-error (type => 'unmatched end tag:colgroup');
               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});  
4038                  ## Ignore the token                  ## Ignore the token
4039                  !!!next-token;                  !!!next-token;
4040                  redo B;                  redo B;
4041                  } else {
4042                    pop @{$self->{open_elements}}; # colgroup
4043                    $self->{insertion_mode} = IN_TABLE_IM;
4044                    !!!next-token;
4045                    redo B;            
4046                }                }
4047                              } elsif ($token->{tag_name} eq 'col') {
4048                ## generate implied end tags                !!!parse-error (type => 'unmatched end tag:col');
               if ({  
                    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});  
4049                ## Ignore the token                ## Ignore the token
4050                !!!next-token;                !!!next-token;
4051                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;  
4052              } else {              } else {
4053                #                #
4054              }              }
4055            } else {            } else {
4056              #              #
4057            }            }
4058              
4059            $in_body->($insert_to_current);            ## As if </colgroup>
4060            redo B;            if ($self->{open_elements}->[-1]->[1] eq 'html') {
4061          } elsif ($self->{insertion_mode} eq 'in select') {              !!!parse-error (type => 'unmatched end tag:colgroup');
4062            if ($token->{type} eq 'character') {              ## Ignore the token
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
4063              !!!next-token;              !!!next-token;
4064              redo B;              redo B;
4065            } elsif ($token->{type} eq 'start tag') {            } else {
4066                pop @{$self->{open_elements}}; # colgroup
4067                $self->{insertion_mode} = IN_TABLE_IM;
4068                ## reprocess
4069                redo B;
4070              }
4071        } elsif ($self->{insertion_mode} == IN_SELECT_IM) {
4072          if ($token->{type} == CHARACTER_TOKEN) {
4073            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4074            !!!next-token;
4075            redo B;
4076          } elsif ($token->{type} == START_TAG_TOKEN) {
4077              if ($token->{tag_name} eq 'option') {              if ($token->{tag_name} eq 'option') {
4078                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
4079                  ## As if </option>                  ## As if </option>
# Line 4888  sub _tree_construction_main ($) { Line 4126  sub _tree_construction_main ($) {
4126    
4127                !!!next-token;                !!!next-token;
4128                redo B;                redo B;
4129              } else {          } else {
4130                #            !!!parse-error (type => 'in select:'.$token->{tag_name});
4131              }            ## Ignore the token
4132            } elsif ($token->{type} eq 'end tag') {            !!!next-token;
4133              redo B;
4134            }
4135          } elsif ($token->{type} == END_TAG_TOKEN) {
4136              if ($token->{tag_name} eq 'optgroup') {              if ($token->{tag_name} eq 'optgroup') {
4137                if ($self->{open_elements}->[-1]->[1] eq 'option' and                if ($self->{open_elements}->[-1]->[1] eq 'option' and
4138                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {
# Line 4993  sub _tree_construction_main ($) { Line 4234  sub _tree_construction_main ($) {
4234    
4235                ## reprocess                ## reprocess
4236                redo B;                redo B;
4237              } else {          } else {
4238                #            !!!parse-error (type => 'in select:/'.$token->{tag_name});
             }  
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in select:'.$token->{tag_name});  
4239            ## Ignore the token            ## Ignore the token
4240            !!!next-token;            !!!next-token;
4241            redo B;            redo B;
4242          } elsif ($self->{insertion_mode} eq 'after body') {          }
4243            if ($token->{type} eq 'character') {        } else {
4244              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          die "$0: $token->{type}: Unknown token type";
4245                my $data = $1;        }
4246                ## As if in body      } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
4247                $reconstruct_active_formatting_elements->($insert_to_current);        if ($token->{type} == CHARACTER_TOKEN) {
4248            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4249              my $data = $1;
4250              ## As if in body
4251              $reconstruct_active_formatting_elements->($insert_to_current);
4252                                
4253                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4254              
4255              unless (length $token->{data}) {
4256                !!!next-token;
4257                redo B;
4258              }
4259            }
4260            
4261            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4262              !!!parse-error (type => 'after html:#character');
4263    
4264                unless (length $token->{data}) {            ## Reprocess in the "main" phase, "after body" insertion mode...
4265                  !!!next-token;          }
4266                  redo B;          
4267                }          ## "after body" insertion mode
4268              }          !!!parse-error (type => 'after body:#character');
4269                
4270              #          $self->{insertion_mode} = IN_BODY_IM;
4271              !!!parse-error (type => 'after body:#character');          ## reprocess
4272            } elsif ($token->{type} eq 'start tag') {          redo B;
4273              !!!parse-error (type => 'after body:'.$token->{tag_name});        } elsif ($token->{type} == START_TAG_TOKEN) {
4274              #          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4275            } elsif ($token->{type} eq 'end tag') {            !!!parse-error (type => 'after html:'.$token->{tag_name});
4276              if ($token->{tag_name} eq 'html') {            
4277                if (defined $self->{inner_html_node}) {            ## Reprocess in the "main" phase, "after body" insertion mode...
4278                  !!!parse-error (type => 'unmatched end tag:html');          }
4279                  ## Ignore the token  
4280                  !!!next-token;          ## "after body" insertion mode
4281                  redo B;          !!!parse-error (type => 'after body:'.$token->{tag_name});
4282                } else {  
4283                  $previous_insertion_mode = $self->{insertion_mode};          $self->{insertion_mode} = IN_BODY_IM;
4284                  $self->{insertion_mode} = 'trailing end';          ## reprocess
4285                  !!!next-token;          redo B;
4286                  redo B;        } elsif ($token->{type} == END_TAG_TOKEN) {
4287                }          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4288              } else {            !!!parse-error (type => 'after html:/'.$token->{tag_name});
4289                !!!parse-error (type => 'after body:/'.$token->{tag_name});            
4290              }            $self->{insertion_mode} = AFTER_BODY_IM;
4291              ## Reprocess in the "main" phase, "after body" insertion mode...
4292            }
4293    
4294            ## "after body" insertion mode
4295            if ($token->{tag_name} eq 'html') {
4296              if (defined $self->{inner_html_node}) {
4297                !!!parse-error (type => 'unmatched end tag:html');
4298                ## Ignore the token
4299                !!!next-token;
4300                redo B;
4301            } else {            } else {
4302              die "$0: $token->{type}: Unknown token type";              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
4303                !!!next-token;
4304                redo B;
4305            }            }
4306            } else {
4307              !!!parse-error (type => 'after body:/'.$token->{tag_name});
4308    
4309            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = IN_BODY_IM;
4310            ## reprocess            ## reprocess
4311            redo B;            redo B;
4312      } elsif ($self->{insertion_mode} eq 'in frameset') {          }
4313        if ($token->{type} eq 'character') {        } else {
4314            die "$0: $token->{type}: Unknown token type";
4315          }
4316        } elsif ($self->{insertion_mode} & FRAME_IMS) {
4317          if ($token->{type} == CHARACTER_TOKEN) {
4318          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4319            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4320              
4321            unless (length $token->{data}) {            unless (length $token->{data}) {
4322              !!!next-token;              !!!next-token;
4323              redo B;              redo B;
4324            }            }
4325          }          }
4326            
4327            if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
4328              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4329                !!!parse-error (type => 'in frameset:#character');
4330              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
4331                !!!parse-error (type => 'after frameset:#character');
4332              } else { # "after html frameset"
4333                !!!parse-error (type => 'after html:#character');
4334    
4335                $self->{insertion_mode} = AFTER_FRAMESET_IM;
4336                ## Reprocess in the "main" phase, "after frameset"...
4337                !!!parse-error (type => 'after frameset:#character');
4338              }
4339              
4340              ## Ignore the token.
4341              if (length $token->{data}) {
4342                ## reprocess the rest of characters
4343              } else {
4344                !!!next-token;
4345              }
4346              redo B;
4347            }
4348            
4349            die qq[$0: Character "$token->{data}"];
4350          } elsif ($token->{type} == START_TAG_TOKEN) {
4351            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4352              !!!parse-error (type => 'after html:'.$token->{tag_name});
4353    
4354          !!!parse-error (type => 'in frameset:#character');            $self->{insertion_mode} = AFTER_FRAMESET_IM;
4355          ## Ignore the token            ## Process in the "main" phase, "after frameset" insertion mode...
4356          !!!next-token;          }
4357          redo B;  
4358        } elsif ($token->{type} eq 'start tag') {          if ($token->{tag_name} eq 'frameset' and
4359          if ($token->{tag_name} eq 'frameset') {              $self->{insertion_mode} == IN_FRAMESET_IM) {
4360            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes});
4361            !!!next-token;            !!!next-token;
4362            redo B;            redo B;
4363          } elsif ($token->{tag_name} eq 'frame') {          } elsif ($token->{tag_name} eq 'frame' and
4364                     $self->{insertion_mode} == IN_FRAMESET_IM) {
4365            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes});
4366            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4367            !!!next-token;            !!!next-token;
4368            redo B;            redo B;
4369          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
4370            $in_body->($insert_to_current);            ## NOTE: As if in body.
4371              $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
4372            redo B;            redo B;
4373          } else {          } else {
4374            !!!parse-error (type => 'in frameset:'.$token->{tag_name});            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4375                !!!parse-error (type => 'in frameset:'.$token->{tag_name});
4376              } else {
4377                !!!parse-error (type => 'after frameset:'.$token->{tag_name});
4378              }
4379            ## Ignore the token            ## Ignore the token
4380            !!!next-token;            !!!next-token;
4381            redo B;            redo B;
4382          }          }
4383        } elsif ($token->{type} eq 'end tag') {        } elsif ($token->{type} == END_TAG_TOKEN) {
4384          if ($token->{tag_name} eq 'frameset') {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4385              !!!parse-error (type => 'after html:/'.$token->{tag_name});
4386    
4387              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4388              ## Process in the "main" phase, "after frameset" insertion mode...
4389            }
4390    
4391            if ($token->{tag_name} eq 'frameset' and
4392                $self->{insertion_mode} == IN_FRAMESET_IM) {
4393            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] eq 'html' and
4394                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
4395              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
# Line 5095  sub _tree_construction_main ($) { Line 4402  sub _tree_construction_main ($) {
4402    
4403            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
4404                $self->{open_elements}->[-1]->[1] ne 'frameset') {                $self->{open_elements}->[-1]->[1] ne 'frameset') {
4405              $self->{insertion_mode} = 'after frameset';              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4406            }            }
4407            redo B;            redo B;
4408            } elsif ($token->{tag_name} eq 'html' and
4409                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
4410              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
4411              !!!next-token;
4412              redo B;
4413          } else {          } else {
4414            !!!parse-error (type => 'in frameset:/'.$token->{tag_name});            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4415                !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
4416              } else {
4417                !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
4418              }
4419            ## Ignore the token            ## Ignore the token
4420            !!!next-token;            !!!next-token;
4421            redo B;            redo B;
# Line 5107  sub _tree_construction_main ($) { Line 4423  sub _tree_construction_main ($) {
4423        } else {        } else {
4424          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4425        }        }
     } 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);  
4426    
4427                unless (length $token->{data}) {        ## ISSUE: An issue in spec here
4428                  !!!next-token;      } else {
4429                  redo B;        die "$0: $self->{insertion_mode}: Unknown insertion mode";
4430                }      }
             }  
4431    
4432              if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {      ## "in body" insertion mode
4433                !!!parse-error (type => 'after frameset:#character');      if ($token->{type} == START_TAG_TOKEN) {
4434          if ($token->{tag_name} eq 'script') {
4435            ## NOTE: This is an "as if in head" code clone
4436            $script_start_tag->($insert);
4437            redo B;
4438          } elsif ($token->{tag_name} eq 'style') {
4439            ## NOTE: This is an "as if in head" code clone
4440            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4441            redo B;
4442          } elsif ({
4443                    base => 1, link => 1,
4444                   }->{$token->{tag_name}}) {
4445            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4446            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4447            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4448            !!!next-token;
4449            redo B;
4450          } elsif ($token->{tag_name} eq 'meta') {
4451            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4452            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4453            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4454    
4455                ## Ignore the token.          unless ($self->{confident}) {
4456                if (length $token->{data}) {            if ($token->{attributes}->{charset}) { ## TODO: And if supported
4457                  ## reprocess the rest of characters              $self->{change_encoding}
4458                } else {                  ->($self, $token->{attributes}->{charset}->{value});
4459                  !!!next-token;            } elsif ($token->{attributes}->{content}) {
4460                }              ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
4461                redo B;              if ($token->{attributes}->{content}->{value}
4462                    =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
4463                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4464                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4465                  $self->{change_encoding}
4466                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
4467              }              }
4468              }
4469            }
4470    
4471          die qq[$0: Character "$token->{data}"];          !!!next-token;
4472        } elsif ($token->{type} eq 'start tag') {          redo B;
4473          if ($token->{tag_name} eq 'noframes') {        } elsif ($token->{tag_name} eq 'title') {
4474            $in_body->($insert_to_current);          !!!parse-error (type => 'in body:title');
4475            redo B;          ## NOTE: This is an "as if in head" code clone
4476          } else {          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {
4477            !!!parse-error (type => 'after frameset:'.$token->{tag_name});            if (defined $self->{head_element}) {
4478                $self->{head_element}->append_child ($_[0]);
4479              } else {
4480                $insert->($_[0]);
4481              }
4482            });
4483            redo B;
4484          } elsif ($token->{tag_name} eq 'body') {
4485            !!!parse-error (type => 'in body:body');
4486                  
4487            if (@{$self->{open_elements}} == 1 or
4488                $self->{open_elements}->[1]->[1] ne 'body') {
4489            ## Ignore the token            ## Ignore the token
4490            } else {
4491              my $body_el = $self->{open_elements}->[1]->[0];
4492              for my $attr_name (keys %{$token->{attributes}}) {
4493                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
4494                  $body_el->set_attribute_ns
4495                    (undef, [undef, $attr_name],
4496                     $token->{attributes}->{$attr_name}->{value});
4497                }
4498              }
4499            }
4500            !!!next-token;
4501            redo B;
4502          } elsif ({
4503                    address => 1, blockquote => 1, center => 1, dir => 1,
4504                    div => 1, dl => 1, fieldset => 1, listing => 1,
4505                    menu => 1, ol => 1, p => 1, ul => 1,
4506                    pre => 1,
4507                   }->{$token->{tag_name}}) {
4508            ## has a p element in scope
4509            INSCOPE: for (reverse @{$self->{open_elements}}) {
4510              if ($_->[1] eq 'p') {
4511                !!!back-token;
4512                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4513                redo B;
4514              } elsif ({
4515                        table => 1, caption => 1, td => 1, th => 1,
4516                        button => 1, marquee => 1, object => 1, html => 1,
4517                       }->{$_->[1]}) {
4518                last INSCOPE;
4519              }
4520            } # INSCOPE
4521              
4522            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4523            if ($token->{tag_name} eq 'pre') {
4524              !!!next-token;
4525              if ($token->{type} == CHARACTER_TOKEN) {
4526                $token->{data} =~ s/^\x0A//;
4527                unless (length $token->{data}) {
4528                  !!!next-token;
4529                }
4530              }
4531            } else {
4532            !!!next-token;            !!!next-token;
           redo B;  
4533          }          }
4534        } elsif ($token->{type} eq 'end tag') {          redo B;
4535          if ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'form') {
4536            $previous_insertion_mode = $self->{insertion_mode};          if (defined $self->{form_element}) {
4537            $self->{insertion_mode} = 'trailing end';            !!!parse-error (type => 'in form:form');
4538              ## Ignore the token
4539            !!!next-token;            !!!next-token;
4540            redo B;            redo B;
4541          } else {          } else {
4542            !!!parse-error (type => 'after frameset:/'.$token->{tag_name});            ## has a p element in scope
4543            ## Ignore the token            INSCOPE: for (reverse @{$self->{open_elements}}) {
4544                if ($_->[1] eq 'p') {
4545                  !!!back-token;
4546                  $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4547                  redo B;
4548                } elsif ({
4549                          table => 1, caption => 1, td => 1, th => 1,
4550                          button => 1, marquee => 1, object => 1, html => 1,
4551                         }->{$_->[1]}) {
4552                  last INSCOPE;
4553                }
4554              } # INSCOPE
4555                
4556              !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4557              $self->{form_element} = $self->{open_elements}->[-1]->[0];
4558            !!!next-token;            !!!next-token;
4559            redo B;            redo B;
4560          }          }
4561        } else {        } elsif ($token->{tag_name} eq 'li') {
4562          die "$0: $token->{type}: Unknown token type";          ## has a p element in scope
4563        }          INSCOPE: for (reverse @{$self->{open_elements}}) {
4564              if ($_->[1] eq 'p') {
4565                !!!back-token;
4566                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4567                redo B;
4568              } elsif ({
4569                        table => 1, caption => 1, td => 1, th => 1,
4570                        button => 1, marquee => 1, object => 1, html => 1,
4571                       }->{$_->[1]}) {
4572                last INSCOPE;
4573              }
4574            } # INSCOPE
4575              
4576            ## Step 1
4577            my $i = -1;
4578            my $node = $self->{open_elements}->[$i];
4579            LI: {
4580              ## Step 2
4581              if ($node->[1] eq 'li') {
4582                if ($i != -1) {
4583                  !!!parse-error (type => 'end tag missing:'.
4584                                  $self->{open_elements}->[-1]->[1]);
4585                }
4586                splice @{$self->{open_elements}}, $i;
4587                last LI;
4588              }
4589              
4590              ## Step 3
4591              if (not $formatting_category->{$node->[1]} and
4592                  #not $phrasing_category->{$node->[1]} and
4593                  ($special_category->{$node->[1]} or
4594                   $scoping_category->{$node->[1]}) and
4595                  $node->[1] ne 'address' and $node->[1] ne 'div') {
4596                last LI;
4597              }
4598              
4599              ## Step 4
4600              $i--;
4601              $node = $self->{open_elements}->[$i];
4602              redo LI;
4603            } # LI
4604              
4605            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4606            !!!next-token;
4607            redo B;
4608          } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {
4609            ## has a p element in scope
4610            INSCOPE: for (reverse @{$self->{open_elements}}) {
4611              if ($_->[1] eq 'p') {
4612                !!!back-token;
4613                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4614                redo B;
4615              } elsif ({
4616                        table => 1, caption => 1, td => 1, th => 1,
4617                        button => 1, marquee => 1, object => 1, html => 1,
4618                       }->{$_->[1]}) {
4619                last INSCOPE;
4620              }
4621            } # INSCOPE
4622              
4623            ## Step 1
4624            my $i = -1;
4625            my $node = $self->{open_elements}->[$i];
4626            LI: {
4627              ## Step 2
4628              if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {
4629                if ($i != -1) {
4630                  !!!parse-error (type => 'end tag missing:'.
4631                                  $self->{open_elements}->[-1]->[1]);
4632                }
4633                splice @{$self->{open_elements}}, $i;
4634                last LI;
4635              }
4636              
4637              ## Step 3
4638              if (not $formatting_category->{$node->[1]} and
4639                  #not $phrasing_category->{$node->[1]} and
4640                  ($special_category->{$node->[1]} or
4641                   $scoping_category->{$node->[1]}) and
4642                  $node->[1] ne 'address' and $node->[1] ne 'div') {
4643                last LI;
4644              }
4645              
4646              ## Step 4
4647              $i--;
4648              $node = $self->{open_elements}->[$i];
4649              redo LI;
4650            } # LI
4651              
4652            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4653            !!!next-token;
4654            redo B;
4655          } elsif ($token->{tag_name} eq 'plaintext') {
4656            ## has a p element in scope
4657            INSCOPE: for (reverse @{$self->{open_elements}}) {
4658              if ($_->[1] eq 'p') {
4659                !!!back-token;
4660                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4661                redo B;
4662              } elsif ({
4663                        table => 1, caption => 1, td => 1, th => 1,
4664                        button => 1, marquee => 1, object => 1, html => 1,
4665                       }->{$_->[1]}) {
4666                last INSCOPE;
4667              }
4668            } # INSCOPE
4669              
4670            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4671              
4672            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
4673              
4674            !!!next-token;
4675            redo B;
4676          } elsif ({
4677                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4678                   }->{$token->{tag_name}}) {
4679            ## has a p element in scope
4680            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4681              my $node = $self->{open_elements}->[$_];
4682              if ($node->[1] eq 'p') {
4683                !!!back-token;
4684                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4685                redo B;
4686              } elsif ({
4687                        table => 1, caption => 1, td => 1, th => 1,
4688                        button => 1, marquee => 1, object => 1, html => 1,
4689                       }->{$node->[1]}) {
4690                last INSCOPE;
4691              }
4692            } # INSCOPE
4693              
4694            ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>
4695            ## has an element in scope
4696            #my $i;
4697            #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4698            #  my $node = $self->{open_elements}->[$_];
4699            #  if ({
4700            #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4701            #      }->{$node->[1]}) {
4702            #    $i = $_;
4703            #    last INSCOPE;
4704            #  } elsif ({
4705            #            table => 1, caption => 1, td => 1, th => 1,
4706            #            button => 1, marquee => 1, object => 1, html => 1,
4707            #           }->{$node->[1]}) {
4708            #    last INSCOPE;
4709            #  }
4710            #} # INSCOPE
4711            #  
4712            #if (defined $i) {
4713            #  !!! parse-error (type => 'in hn:hn');
4714            #  splice @{$self->{open_elements}}, $i;
4715            #}
4716              
4717            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4718              
4719            !!!next-token;
4720            redo B;
4721          } elsif ($token->{tag_name} eq 'a') {
4722            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
4723              my $node = $active_formatting_elements->[$i];
4724              if ($node->[1] eq 'a') {
4725                !!!parse-error (type => 'in a:a');
4726                
4727                !!!back-token;
4728                $token = {type => END_TAG_TOKEN, tag_name => 'a'};
4729                $formatting_end_tag->($token->{tag_name});
4730                
4731                AFE2: for (reverse 0..$#$active_formatting_elements) {
4732                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
4733                    splice @$active_formatting_elements, $_, 1;
4734                    last AFE2;
4735                  }
4736                } # AFE2
4737                OE: for (reverse 0..$#{$self->{open_elements}}) {
4738                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
4739                    splice @{$self->{open_elements}}, $_, 1;
4740                    last OE;
4741                  }
4742                } # OE
4743                last AFE;
4744              } elsif ($node->[0] eq '#marker') {
4745                last AFE;
4746              }
4747            } # AFE
4748              
4749            $reconstruct_active_formatting_elements->($insert_to_current);
4750    
4751        ## ISSUE: An issue in spec here          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4752      } elsif ($self->{insertion_mode} eq 'trailing end') {          push @$active_formatting_elements, $self->{open_elements}->[-1];
4753        ## states in the main stage is preserved yet # MUST  
4754                  !!!next-token;
4755        if ($token->{type} eq 'character') {          redo B;
4756          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        } elsif ({
4757            my $data = $1;                  b => 1, big => 1, em => 1, font => 1, i => 1,
4758            ## As if in the main phase.                  s => 1, small => 1, strile => 1,
4759            ## NOTE: The insertion mode in the main phase                  strong => 1, tt => 1, u => 1,
4760            ## just before the phase has been changed to the trailing                 }->{$token->{tag_name}}) {
4761            ## end phase is either "after body" or "after frameset".          $reconstruct_active_formatting_elements->($insert_to_current);
4762            $reconstruct_active_formatting_elements->($insert_to_current);          
4763            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4764            push @$active_formatting_elements, $self->{open_elements}->[-1];
4765            
4766            !!!next-token;
4767            redo B;
4768          } elsif ($token->{tag_name} eq 'nobr') {
4769            $reconstruct_active_formatting_elements->($insert_to_current);
4770    
4771            ## has a |nobr| element in scope
4772            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4773              my $node = $self->{open_elements}->[$_];
4774              if ($node->[1] eq 'nobr') {
4775                !!!parse-error (type => 'in nobr:nobr');
4776                !!!back-token;
4777                $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};
4778                redo B;
4779              } elsif ({
4780                        table => 1, caption => 1, td => 1, th => 1,
4781                        button => 1, marquee => 1, object => 1, html => 1,
4782                       }->{$node->[1]}) {
4783                last INSCOPE;
4784              }
4785            } # INSCOPE
4786            
4787            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4788            push @$active_formatting_elements, $self->{open_elements}->[-1];
4789            
4790            !!!next-token;
4791            redo B;
4792          } elsif ($token->{tag_name} eq 'button') {
4793            ## has a button element in scope
4794            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4795              my $node = $self->{open_elements}->[$_];
4796              if ($node->[1] eq 'button') {
4797                !!!parse-error (type => 'in button:button');
4798                !!!back-token;
4799                $token = {type => END_TAG_TOKEN, tag_name => 'button'};
4800                redo B;
4801              } elsif ({
4802                        table => 1, caption => 1, td => 1, th => 1,
4803                        button => 1, marquee => 1, object => 1, html => 1,
4804                       }->{$node->[1]}) {
4805                last INSCOPE;
4806              }
4807            } # INSCOPE
4808                        
4809            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);          $reconstruct_active_formatting_elements->($insert_to_current);
4810                        
4811            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4812            push @$active_formatting_elements, ['#marker', ''];
4813    
4814            !!!next-token;
4815            redo B;
4816          } elsif ($token->{tag_name} eq 'marquee' or
4817                   $token->{tag_name} eq 'object') {
4818            $reconstruct_active_formatting_elements->($insert_to_current);
4819            
4820            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4821            push @$active_formatting_elements, ['#marker', ''];
4822            
4823            !!!next-token;
4824            redo B;
4825          } elsif ($token->{tag_name} eq 'xmp') {
4826            $reconstruct_active_formatting_elements->($insert_to_current);
4827            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4828            redo B;
4829          } elsif ($token->{tag_name} eq 'table') {
4830            ## has a p element in scope
4831            INSCOPE: for (reverse @{$self->{open_elements}}) {
4832              if ($_->[1] eq 'p') {
4833                !!!back-token;
4834                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4835                redo B;
4836              } elsif ({
4837                        table => 1, caption => 1, td => 1, th => 1,
4838                        button => 1, marquee => 1, object => 1, html => 1,
4839                       }->{$_->[1]}) {
4840                last INSCOPE;
4841              }
4842            } # INSCOPE
4843              
4844            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4845              
4846            $self->{insertion_mode} = IN_TABLE_IM;
4847              
4848            !!!next-token;
4849            redo B;
4850          } elsif ({
4851                    area => 1, basefont => 1, bgsound => 1, br => 1,
4852                    embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
4853                    image => 1,
4854                   }->{$token->{tag_name}}) {
4855            if ($token->{tag_name} eq 'image') {
4856              !!!parse-error (type => 'image');
4857              $token->{tag_name} = 'img';
4858            }
4859    
4860            ## NOTE: There is an "as if <br>" code clone.
4861            $reconstruct_active_formatting_elements->($insert_to_current);
4862            
4863            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4864            pop @{$self->{open_elements}};
4865            
4866            !!!next-token;
4867            redo B;
4868          } elsif ($token->{tag_name} eq 'hr') {
4869            ## has a p element in scope
4870            INSCOPE: for (reverse @{$self->{open_elements}}) {
4871              if ($_->[1] eq 'p') {
4872                !!!back-token;
4873                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4874                redo B;
4875              } elsif ({
4876                        table => 1, caption => 1, td => 1, th => 1,
4877                        button => 1, marquee => 1, object => 1, html => 1,
4878                       }->{$_->[1]}) {
4879                last INSCOPE;
4880              }
4881            } # INSCOPE
4882              
4883            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4884            pop @{$self->{open_elements}};
4885              
4886            !!!next-token;
4887            redo B;
4888          } elsif ($token->{tag_name} eq 'input') {
4889            $reconstruct_active_formatting_elements->($insert_to_current);
4890            
4891            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4892            ## TODO: associate with $self->{form_element} if defined
4893            pop @{$self->{open_elements}};
4894            
4895            !!!next-token;
4896            redo B;
4897          } elsif ($token->{tag_name} eq 'isindex') {
4898            !!!parse-error (type => 'isindex');
4899            
4900            if (defined $self->{form_element}) {
4901              ## Ignore the token
4902              !!!next-token;
4903              redo B;
4904            } else {
4905              my $at = $token->{attributes};
4906              my $form_attrs;
4907              $form_attrs->{action} = $at->{action} if $at->{action};
4908              my $prompt_attr = $at->{prompt};
4909              $at->{name} = {name => 'name', value => 'isindex'};
4910              delete $at->{action};
4911              delete $at->{prompt};
4912              my @tokens = (
4913                            {type => START_TAG_TOKEN, tag_name => 'form',
4914                             attributes => $form_attrs},
4915                            {type => START_TAG_TOKEN, tag_name => 'hr'},
4916                            {type => START_TAG_TOKEN, tag_name => 'p'},
4917                            {type => START_TAG_TOKEN, tag_name => 'label'},
4918                           );
4919              if ($prompt_attr) {
4920                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};
4921              } else {
4922                push @tokens, {type => CHARACTER_TOKEN,
4923                               data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
4924                ## TODO: make this configurable
4925              }
4926              push @tokens,
4927                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},
4928                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
4929                            {type => END_TAG_TOKEN, tag_name => 'label'},
4930                            {type => END_TAG_TOKEN, tag_name => 'p'},
4931                            {type => START_TAG_TOKEN, tag_name => 'hr'},
4932                            {type => END_TAG_TOKEN, tag_name => 'form'};
4933              $token = shift @tokens;
4934              !!!back-token (@tokens);
4935              redo B;
4936            }
4937          } elsif ($token->{tag_name} eq 'textarea') {
4938            my $tag_name = $token->{tag_name};
4939            my $el;
4940            !!!create-element ($el, $token->{tag_name}, $token->{attributes});
4941            
4942            ## TODO: $self->{form_element} if defined
4943            $self->{content_model} = RCDATA_CONTENT_MODEL;
4944            delete $self->{escape}; # MUST
4945            
4946            $insert->($el);
4947            
4948            my $text = '';
4949            !!!next-token;
4950            if ($token->{type} == CHARACTER_TOKEN) {
4951              $token->{data} =~ s/^\x0A//;
4952            unless (length $token->{data}) {            unless (length $token->{data}) {
4953              !!!next-token;              !!!next-token;
             redo B;  
4954            }            }
4955          }          }
4956            while ($token->{type} == CHARACTER_TOKEN) {
4957              $text .= $token->{data};
4958              !!!next-token;
4959            }
4960            if (length $text) {
4961              $el->manakai_append_text ($text);
4962            }
4963            
4964            $self->{content_model} = PCDATA_CONTENT_MODEL;
4965            
4966            if ($token->{type} == END_TAG_TOKEN and
4967                $token->{tag_name} eq $tag_name) {
4968              ## Ignore the token
4969            } else {
4970              !!!parse-error (type => 'in RCDATA:#'.$token->{type});
4971            }
4972            !!!next-token;
4973            redo B;
4974          } elsif ({
4975                    iframe => 1,
4976                    noembed => 1,
4977                    noframes => 1,
4978                    noscript => 0, ## TODO: 1 if scripting is enabled
4979                   }->{$token->{tag_name}}) {
4980            ## NOTE: There is an "as if in body" code clone.
4981            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4982            redo B;
4983          } elsif ($token->{tag_name} eq 'select') {
4984            $reconstruct_active_formatting_elements->($insert_to_current);
4985            
4986            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4987            
4988            $self->{insertion_mode} = IN_SELECT_IM;
4989            !!!next-token;
4990            redo B;
4991          } elsif ({
4992                    caption => 1, col => 1, colgroup => 1, frame => 1,
4993                    frameset => 1, head => 1, option => 1, optgroup => 1,
4994                    tbody => 1, td => 1, tfoot => 1, th => 1,
4995                    thead => 1, tr => 1,
4996                   }->{$token->{tag_name}}) {
4997            !!!parse-error (type => 'in body:'.$token->{tag_name});
4998            ## Ignore the token
4999            !!!next-token;
5000            redo B;
5001            
5002            ## ISSUE: An issue on HTML5 new elements in the spec.
5003          } else {
5004            $reconstruct_active_formatting_elements->($insert_to_current);
5005            
5006            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5007            
5008            !!!next-token;
5009            redo B;
5010          }
5011        } elsif ($token->{type} == END_TAG_TOKEN) {
5012          if ($token->{tag_name} eq 'body') {
5013            if (@{$self->{open_elements}} > 1 and
5014                $self->{open_elements}->[1]->[1] eq 'body') {
5015              for (@{$self->{open_elements}}) {
5016                unless ({
5017                           dd => 1, dt => 1, li => 1, p => 1, td => 1,
5018                           th => 1, tr => 1, body => 1, html => 1,
5019                         tbody => 1, tfoot => 1, thead => 1,
5020                        }->{$_->[1]}) {
5021                  !!!parse-error (type => 'not closed:'.$_->[1]);
5022                }
5023              }
5024    
5025          !!!parse-error (type => 'after html:#character');            $self->{insertion_mode} = AFTER_BODY_IM;
5026          $self->{insertion_mode} = $previous_insertion_mode;            !!!next-token;
5027          ## reprocess            redo B;
5028            } else {
5029              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5030              ## Ignore the token
5031              !!!next-token;
5032              redo B;
5033            }
5034          } elsif ($token->{tag_name} eq 'html') {
5035            if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {
5036              ## ISSUE: There is an issue in the spec.
5037              if ($self->{open_elements}->[-1]->[1] ne 'body') {
5038                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
5039              }
5040              $self->{insertion_mode} = AFTER_BODY_IM;
5041              ## reprocess
5042              redo B;
5043            } else {
5044              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5045              ## Ignore the token
5046              !!!next-token;
5047              redo B;
5048            }
5049          } elsif ({
5050                    address => 1, blockquote => 1, center => 1, dir => 1,
5051                    div => 1, dl => 1, fieldset => 1, listing => 1,
5052                    menu => 1, ol => 1, pre => 1, ul => 1,
5053                    p => 1,
5054                    dd => 1, dt => 1, li => 1,
5055                    button => 1, marquee => 1, object => 1,
5056                   }->{$token->{tag_name}}) {
5057            ## has an element in scope
5058            my $i;
5059            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5060              my $node = $self->{open_elements}->[$_];
5061              if ($node->[1] eq $token->{tag_name}) {
5062                ## generate implied end tags
5063                if ({
5064                     dd => ($token->{tag_name} ne 'dd'),
5065                     dt => ($token->{tag_name} ne 'dt'),
5066                     li => ($token->{tag_name} ne 'li'),
5067                     p => ($token->{tag_name} ne 'p'),
5068                     td => 1, th => 1, tr => 1,
5069                     tbody => 1, tfoot=> 1, thead => 1,
5070                    }->{$self->{open_elements}->[-1]->[1]}) {
5071                  !!!back-token;
5072                  $token = {type => END_TAG_TOKEN,
5073                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5074                  redo B;
5075                }
5076                $i = $_;
5077                last INSCOPE unless $token->{tag_name} eq 'p';
5078              } elsif ({
5079                        table => 1, caption => 1, td => 1, th => 1,
5080                        button => 1, marquee => 1, object => 1, html => 1,
5081                       }->{$node->[1]}) {
5082                last INSCOPE;
5083              }
5084            } # INSCOPE
5085            
5086            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5087              if (defined $i) {
5088                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5089              } else {
5090                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5091              }
5092            }
5093            
5094            if (defined $i) {
5095              splice @{$self->{open_elements}}, $i;
5096            } elsif ($token->{tag_name} eq 'p') {
5097              ## As if <p>, then reprocess the current token
5098              my $el;
5099              !!!create-element ($el, 'p');
5100              $insert->($el);
5101            }
5102            $clear_up_to_marker->()
5103              if {
5104                button => 1, marquee => 1, object => 1,
5105              }->{$token->{tag_name}};
5106            !!!next-token;
5107          redo B;          redo B;
5108        } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{tag_name} eq 'form') {
5109          !!!parse-error (type => 'after html:'.$token->{tag_name});          ## has an element in scope
5110          $self->{insertion_mode} = $previous_insertion_mode;          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5111          ## reprocess            my $node = $self->{open_elements}->[$_];
5112              if ($node->[1] eq $token->{tag_name}) {
5113                ## generate implied end tags
5114                if ({
5115                     dd => 1, dt => 1, li => 1, p => 1,
5116                     td => 1, th => 1, tr => 1,
5117                     tbody => 1, tfoot=> 1, thead => 1,
5118                    }->{$self->{open_elements}->[-1]->[1]}) {
5119                  !!!back-token;
5120                  $token = {type => END_TAG_TOKEN,
5121                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5122                  redo B;
5123                }
5124                last INSCOPE;
5125              } elsif ({
5126                        table => 1, caption => 1, td => 1, th => 1,
5127                        button => 1, marquee => 1, object => 1, html => 1,
5128                       }->{$node->[1]}) {
5129                last INSCOPE;
5130              }
5131            } # INSCOPE
5132            
5133            if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
5134              pop @{$self->{open_elements}};
5135            } else {
5136              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5137            }
5138    
5139            undef $self->{form_element};
5140            !!!next-token;
5141          redo B;          redo B;
5142        } elsif ($token->{type} eq 'end tag') {        } elsif ({
5143          !!!parse-error (type => 'after html:/'.$token->{tag_name});                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5144          $self->{insertion_mode} = $previous_insertion_mode;                 }->{$token->{tag_name}}) {
5145          ## reprocess          ## has an element in scope
5146            my $i;
5147            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5148              my $node = $self->{open_elements}->[$_];
5149              if ({
5150                   h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5151                  }->{$node->[1]}) {
5152                ## generate implied end tags
5153                if ({
5154                     dd => 1, dt => 1, li => 1, p => 1,
5155                     td => 1, th => 1, tr => 1,
5156                     tbody => 1, tfoot=> 1, thead => 1,
5157                    }->{$self->{open_elements}->[-1]->[1]}) {
5158                  !!!back-token;
5159                  $token = {type => END_TAG_TOKEN,
5160                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5161                  redo B;
5162                }
5163                $i = $_;
5164                last INSCOPE;
5165              } elsif ({
5166                        table => 1, caption => 1, td => 1, th => 1,
5167                        button => 1, marquee => 1, object => 1, html => 1,
5168                       }->{$node->[1]}) {
5169                last INSCOPE;
5170              }
5171            } # INSCOPE
5172            
5173            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5174              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5175            }
5176            
5177            splice @{$self->{open_elements}}, $i if defined $i;
5178            !!!next-token;
5179          redo B;          redo B;
5180          } elsif ({
5181                    a => 1,
5182                    b => 1, big => 1, em => 1, font => 1, i => 1,
5183                    nobr => 1, s => 1, small => 1, strile => 1,
5184                    strong => 1, tt => 1, u => 1,
5185                   }->{$token->{tag_name}}) {
5186            $formatting_end_tag->($token->{tag_name});
5187            redo B;
5188          } elsif ($token->{tag_name} eq 'br') {
5189            !!!parse-error (type => 'unmatched end tag:br');
5190    
5191            ## As if <br>
5192            $reconstruct_active_formatting_elements->($insert_to_current);
5193            
5194            my $el;
5195            !!!create-element ($el, 'br');
5196            $insert->($el);
5197            
5198            ## Ignore the token.
5199            !!!next-token;
5200            redo B;
5201          } elsif ({
5202                    caption => 1, col => 1, colgroup => 1, frame => 1,
5203                    frameset => 1, head => 1, option => 1, optgroup => 1,
5204                    tbody => 1, td => 1, tfoot => 1, th => 1,
5205                    thead => 1, tr => 1,
5206                    area => 1, basefont => 1, bgsound => 1,
5207                    embed => 1, hr => 1, iframe => 1, image => 1,
5208                    img => 1, input => 1, isindex => 1, noembed => 1,
5209                    noframes => 1, param => 1, select => 1, spacer => 1,
5210                    table => 1, textarea => 1, wbr => 1,
5211                    noscript => 0, ## TODO: if scripting is enabled
5212                   }->{$token->{tag_name}}) {
5213            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5214            ## Ignore the token
5215            !!!next-token;
5216            redo B;
5217            
5218            ## ISSUE: Issue on HTML5 new elements in spec
5219            
5220        } else {        } else {
5221          die "$0: $token->{type}: Unknown token";          ## Step 1
5222            my $node_i = -1;
5223            my $node = $self->{open_elements}->[$node_i];
5224    
5225            ## Step 2
5226            S2: {
5227              if ($node->[1] eq $token->{tag_name}) {
5228                ## Step 1
5229                ## generate implied end tags
5230                if ({
5231                     dd => 1, dt => 1, li => 1, p => 1,
5232                     td => 1, th => 1, tr => 1,
5233                     tbody => 1, tfoot => 1, thead => 1,
5234                    }->{$self->{open_elements}->[-1]->[1]}) {
5235                  !!!back-token;
5236                  $token = {type => END_TAG_TOKEN,
5237                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5238                  redo B;
5239                }
5240            
5241                ## Step 2
5242                if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
5243                  ## NOTE: <x><y></x>
5244                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5245                }
5246                
5247                ## Step 3
5248                splice @{$self->{open_elements}}, $node_i;
5249    
5250                !!!next-token;
5251                last S2;
5252              } else {
5253                ## Step 3
5254                if (not $formatting_category->{$node->[1]} and
5255                    #not $phrasing_category->{$node->[1]} and
5256                    ($special_category->{$node->[1]} or
5257                     $scoping_category->{$node->[1]})) {
5258                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5259                  ## Ignore the token
5260                  !!!next-token;
5261                  last S2;
5262                }
5263              }
5264              
5265              ## Step 4
5266              $node_i--;
5267              $node = $self->{open_elements}->[$node_i];
5268              
5269              ## Step 5;
5270              redo S2;
5271            } # S2
5272            redo B;
5273        }        }
     } else {  
       die "$0: $self->{insertion_mode}: Unknown insertion mode";  
5274      }      }
5275        redo B;
5276    } # B    } # B
5277    
5278      ## NOTE: The "trailing end" phase in HTML5 is split into
5279      ## two insertion modes: "after html body" and "after html frameset".
5280      ## NOTE: States in the main stage is preserved while
5281      ## the parser stays in the trailing end phase. # MUST
5282    
5283    ## Stop parsing # MUST    ## Stop parsing # MUST
5284        
5285    ## TODO: script stuffs    ## TODO: script stuffs
# Line 5211  sub set_inner_html ($$$) { Line 5291  sub set_inner_html ($$$) {
5291    my $s = \$_[0];    my $s = \$_[0];
5292    my $onerror = $_[1];    my $onerror = $_[1];
5293    
5294      ## ISSUE: Should {confident} be true?
5295    
5296    my $nt = $node->node_type;    my $nt = $node->node_type;
5297    if ($nt == 9) {    if ($nt == 9) {
5298      # MUST      # MUST
# Line 5284  sub set_inner_html ($$$) { Line 5366  sub set_inner_html ($$$) {
5366    
5367      ## Step 2      ## Step 2
5368      my $node_ln = $node->local_name;      my $node_ln = $node->local_name;
5369      $p->{content_model_flag} = {      $p->{content_model} = {
5370        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
5371        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
5372        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
5373        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
5374        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
5375        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
5376        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
5377        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
5378        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
5379        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
5380      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
5381         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
5382            unless defined $p->{content_model};
5383            ## ISSUE: What is "the name of the element"? local name?
5384    
5385      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $node_ln];
5386    
# Line 5361  sub set_inner_html ($$$) { Line 5445  sub set_inner_html ($$$) {
5445    
5446  } # tree construction stage  } # tree construction stage
5447    
5448  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
5449    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  
5450    
5451  1;  1;
5452  # $Date$  # $Date$

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24