/[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.20 by wakaba, Sat Jun 23 14:25:05 2007 UTC revision 1.70 by wakaba, Sat Mar 1 00:42:52 2008 UTC
# Line 1  Line 1 
1  package Whatpm::HTML;  package Whatpm::HTML;
2  use strict;  use strict;
3  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
4    use Error qw(:try);
5    
6  ## ISSUE:  ## ISSUE:
7  ## var doc = implementation.createDocument (null, null, null);  ## var doc = implementation.createDocument (null, null, null);
8  ## doc.write ('');  ## doc.write ('');
9  ## alert (doc.compatMode);  ## alert (doc.compatMode);
10    
11    ## ISSUE: HTML5 revision 967 says that the encoding layer MUST NOT
12    ## strip BOM and the HTML layer MUST ignore it.  Whether we can do it
13    ## is not yet clear.
14    ## "{U+FEFF}..." in UTF-16BE/UTF-16LE is three or four characters?
15    ## "{U+FEFF}..." in GB18030?
16    
17    ## TODO: Control charcters and noncharacters are not allowed (HTML5 revision 1263)
18    ## TODO: 1252 parse error (revision 1264)
19    ## TODO: 8859-11 = 874 (revision 1271)
20    
21  my $permitted_slash_tag_name = {  my $permitted_slash_tag_name = {
22    base => 1,    base => 1,
23    link => 1,    link => 1,
# Line 78  my $formatting_category = { Line 89  my $formatting_category = {
89  };  };
90  # $phrasing_category: all other elements  # $phrasing_category: all other elements
91    
92    sub parse_byte_string ($$$$;$) {
93      my $self = ref $_[0] ? shift : shift->new;
94      my $charset = shift;
95      my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);
96      my $s;
97      
98      if (defined $charset) {
99        require Encode; ## TODO: decode(utf8) don't delete BOM
100        $s = \ (Encode::decode ($charset, $$bytes_s));
101        $self->{input_encoding} = lc $charset; ## TODO: normalize name
102        $self->{confident} = 1;
103      } else {
104        ## TODO: Implement HTML5 detection algorithm
105        require Whatpm::Charset::UniversalCharDet;
106        $charset = Whatpm::Charset::UniversalCharDet->detect_byte_string
107            (substr ($$bytes_s, 0, 1024));
108        $charset ||= 'windows-1252';
109        $s = \ (Encode::decode ($charset, $$bytes_s));
110        $self->{input_encoding} = $charset;
111        $self->{confident} = 0;
112      }
113    
114      $self->{change_encoding} = sub {
115        my $self = shift;
116        my $charset = lc shift;
117        ## TODO: if $charset is supported
118        ## TODO: normalize charset name
119    
120        ## "Change the encoding" algorithm:
121    
122        ## Step 1    
123        if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?
124          $charset = 'utf-8';
125        }
126    
127        ## Step 2
128        if (defined $self->{input_encoding} and
129            $self->{input_encoding} eq $charset) {
130          $self->{confident} = 1;
131          return;
132        }
133    
134        !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
135            ':'.$charset, level => 'w');
136    
137        ## Step 3
138        # if (can) {
139          ## change the encoding on the fly.
140          #$self->{confident} = 1;
141          #return;
142        # }
143    
144        ## Step 4
145        throw Whatpm::HTML::RestartParser (charset => $charset);
146      }; # $self->{change_encoding}
147    
148      my @args = @_; shift @args; # $s
149      my $return;
150      try {
151        $return = $self->parse_char_string ($s, @args);  
152      } catch Whatpm::HTML::RestartParser with {
153        my $charset = shift->{charset};
154        $s = \ (Encode::decode ($charset, $$bytes_s));    
155        $self->{input_encoding} = $charset; ## TODO: normalize
156        $self->{confident} = 1;
157        $return = $self->parse_char_string ($s, @args);
158      };
159      return $return;
160    } # parse_byte_string
161    
162    *parse_char_string = \&parse_string;
163    
164  sub parse_string ($$$;$) {  sub parse_string ($$$;$) {
165    my $self = shift->new;    my $self = ref $_[0] ? shift : shift->new;
166    my $s = \$_[0];    my $s = ref $_[0] ? $_[0] : \($_[0]);
167    $self->{document} = $_[1];    $self->{document} = $_[1];
168      @{$self->{document}->child_nodes} = ();
169    
170    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
171    
172      $self->{confident} = 1 unless exists $self->{confident};
173      $self->{document}->input_encoding ($self->{input_encoding})
174          if defined $self->{input_encoding};
175    
176    my $i = 0;    my $i = 0;
177    my $line = 1;    my $line = 1;
178    my $column = 0;    my $column = 0;
# Line 141  sub new ($) { Line 229  sub new ($) {
229    $self->{parse_error} = sub {    $self->{parse_error} = sub {
230      #      #
231    };    };
232      $self->{change_encoding} = sub {
233        # if ($_[0] is a supported encoding) {
234        #   run "change the encoding" algorithm;
235        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
236        # }
237      };
238      $self->{application_cache_selection} = sub {
239        #
240      };
241    return $self;    return $self;
242  } # new  } # new
243    
244    sub CM_ENTITY () { 0b001 } # & markup in data
245    sub CM_LIMITED_MARKUP () { 0b010 } # < markup in data (limited)
246    sub CM_FULL_MARKUP () { 0b100 } # < markup in data (any)
247    
248    sub PLAINTEXT_CONTENT_MODEL () { 0 }
249    sub CDATA_CONTENT_MODEL () { CM_LIMITED_MARKUP }
250    sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
251    sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
252    
253    sub DATA_STATE () { 0 }
254    sub ENTITY_DATA_STATE () { 1 }
255    sub TAG_OPEN_STATE () { 2 }
256    sub CLOSE_TAG_OPEN_STATE () { 3 }
257    sub TAG_NAME_STATE () { 4 }
258    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
259    sub ATTRIBUTE_NAME_STATE () { 6 }
260    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
261    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
262    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
263    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
264    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
265    sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
266    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
267    sub COMMENT_START_STATE () { 14 }
268    sub COMMENT_START_DASH_STATE () { 15 }
269    sub COMMENT_STATE () { 16 }
270    sub COMMENT_END_STATE () { 17 }
271    sub COMMENT_END_DASH_STATE () { 18 }
272    sub BOGUS_COMMENT_STATE () { 19 }
273    sub DOCTYPE_STATE () { 20 }
274    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
275    sub DOCTYPE_NAME_STATE () { 22 }
276    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
277    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
278    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
279    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
280    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
281    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
282    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
283    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
284    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
285    sub BOGUS_DOCTYPE_STATE () { 32 }
286    
287    sub DOCTYPE_TOKEN () { 1 }
288    sub COMMENT_TOKEN () { 2 }
289    sub START_TAG_TOKEN () { 3 }
290    sub END_TAG_TOKEN () { 4 }
291    sub END_OF_FILE_TOKEN () { 5 }
292    sub CHARACTER_TOKEN () { 6 }
293    
294    sub AFTER_HTML_IMS () { 0b100 }
295    sub HEAD_IMS ()       { 0b1000 }
296    sub BODY_IMS ()       { 0b10000 }
297    sub BODY_TABLE_IMS () { 0b100000 }
298    sub TABLE_IMS ()      { 0b1000000 }
299    sub ROW_IMS ()        { 0b10000000 }
300    sub BODY_AFTER_IMS () { 0b100000000 }
301    sub FRAME_IMS ()      { 0b1000000000 }
302    
303    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
304    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
305    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
306    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
307    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
308    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
309    sub IN_BODY_IM () { BODY_IMS }
310    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
311    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
312    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
313    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
314    sub IN_TABLE_IM () { TABLE_IMS }
315    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
316    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
317    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
318    sub IN_SELECT_IM () { 0b01 }
319    sub IN_COLUMN_GROUP_IM () { 0b10 }
320    
321  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
322    
323  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
324    my $self = shift;    my $self = shift;
325    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
326    $self->{content_model_flag} = 'PCDATA'; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
327    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
328    undef $self->{current_attribute};    undef $self->{current_attribute};
329    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
# Line 162  sub _initialize_tokenizer ($) { Line 336  sub _initialize_tokenizer ($) {
336  } # _initialize_tokenizer  } # _initialize_tokenizer
337    
338  ## A token has:  ## A token has:
339  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
340  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
341  ##   ->{name} (DOCTYPE, start tag (tag name), end tag (tag name))  ##   ->{name} (DOCTYPE_TOKEN)
342  ##   ->{public_identifier} (DOCTYPE)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
343  ##   ->{system_identifier} (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
344  ##   ->{correct} == 1 or 0 (DOCTYPE)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
345  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)
346  ##   ->{data} (comment, character)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
347    ##        ->{name}
348    ##        ->{value}
349    ##        ->{has_reference} == 1 or 0
350    ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
351    
352  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
353    
# Line 179  sub _initialize_tokenizer ($) { Line 357  sub _initialize_tokenizer ($) {
357  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
358  ## and removed from the list.  ## and removed from the list.
359    
360    ## NOTE: HTML5 "Writing HTML documents" section, applied to
361    ## documents and not to user agents and conformance checkers,
362    ## contains some requirements that are not detected by the
363    ## parsing algorithm:
364    ## - Some requirements on character encoding declarations. ## TODO
365    ## - "Elements MUST NOT contain content that their content model disallows."
366    ##   ... Some are parse error, some are not (will be reported by c.c.).
367    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
368    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
369    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
370    
371    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
372    ## be detected by the HTML5 parsing algorithm:
373    ## - Text,
374    
375  sub _get_next_token ($) {  sub _get_next_token ($) {
376    my $self = shift;    my $self = shift;
377    if (@{$self->{token}}) {    if (@{$self->{token}}) {
# Line 186  sub _get_next_token ($) { Line 379  sub _get_next_token ($) {
379    }    }
380    
381    A: {    A: {
382      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
383        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_input_character} == 0x0026) { # &
384          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA
385              $self->{content_model_flag} eq 'RCDATA') {            $self->{state} = ENTITY_DATA_STATE;
           $self->{state} = 'entity data';  
386            !!!next-input-character;            !!!next-input-character;
387            redo A;            redo A;
388          } else {          } else {
389            #            #
390          }          }
391        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_input_character} == 0x002D) { # -
392          if ($self->{content_model_flag} eq 'RCDATA' or          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
             $self->{content_model_flag} eq 'CDATA') {  
393            unless ($self->{escape}) {            unless ($self->{escape}) {
394              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_input_character}->[0] == 0x002D and # -
395                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_input_character}->[1] == 0x0021 and # !
# Line 210  sub _get_next_token ($) { Line 401  sub _get_next_token ($) {
401                    
402          #          #
403        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_input_character} == 0x003C) { # <
404          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
405              (($self->{content_model_flag} eq 'CDATA' or              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
               $self->{content_model_flag} eq 'RCDATA') and  
406               not $self->{escape})) {               not $self->{escape})) {
407            $self->{state} = 'tag open';            $self->{state} = TAG_OPEN_STATE;
408            !!!next-input-character;            !!!next-input-character;
409            redo A;            redo A;
410          } else {          } else {
# Line 222  sub _get_next_token ($) { Line 412  sub _get_next_token ($) {
412          }          }
413        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
414          if ($self->{escape} and          if ($self->{escape} and
415              ($self->{content_model_flag} eq 'RCDATA' or              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
              $self->{content_model_flag} eq 'CDATA')) {  
416            if ($self->{prev_input_character}->[0] == 0x002D and # -            if ($self->{prev_input_character}->[0] == 0x002D and # -
417                $self->{prev_input_character}->[1] == 0x002D) { # -                $self->{prev_input_character}->[1] == 0x002D) { # -
418              delete $self->{escape};              delete $self->{escape};
# Line 232  sub _get_next_token ($) { Line 421  sub _get_next_token ($) {
421                    
422          #          #
423        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
424          !!!emit ({type => 'end-of-file'});          !!!emit ({type => END_OF_FILE_TOKEN});
425          last A; ## TODO: ok?          last A; ## TODO: ok?
426        }        }
427        # Anything else        # Anything else
428        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
429                     data => chr $self->{next_input_character}};                     data => chr $self->{next_input_character}};
430        ## Stay in the data state        ## Stay in the data state
431        !!!next-input-character;        !!!next-input-character;
# Line 244  sub _get_next_token ($) { Line 433  sub _get_next_token ($) {
433        !!!emit ($token);        !!!emit ($token);
434    
435        redo A;        redo A;
436      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
437        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
438                
439        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);
440    
441        $self->{state} = 'data';        $self->{state} = DATA_STATE;
442        # next-input-character is already done        # next-input-character is already done
443    
444        unless (defined $token) {        unless (defined $token) {
445          !!!emit ({type => 'character', data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&'});
446        } else {        } else {
447          !!!emit ($token);          !!!emit ($token);
448        }        }
449    
450        redo A;        redo A;
451      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
452        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
           $self->{content_model_flag} eq 'CDATA') {  
453          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_input_character} == 0x002F) { # /
454            !!!next-input-character;            !!!next-input-character;
455            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
456            redo A;            redo A;
457          } else {          } else {
458            ## reconsume            ## reconsume
459            $self->{state} = 'data';            $self->{state} = DATA_STATE;
460    
461            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
462    
463            redo A;            redo A;
464          }          }
465        } elsif ($self->{content_model_flag} eq 'PCDATA') {        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
466          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_input_character} == 0x0021) { # !
467            $self->{state} = 'markup declaration open';            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
468            !!!next-input-character;            !!!next-input-character;
469            redo A;            redo A;
470          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_input_character} == 0x002F) { # /
471            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
472            !!!next-input-character;            !!!next-input-character;
473            redo A;            redo A;
474          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_input_character} and
475                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_input_character} <= 0x005A) { # A..Z
476            $self->{current_token}            $self->{current_token}
477              = {type => 'start tag',              = {type => START_TAG_TOKEN,
478                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_input_character} + 0x0020)};
479            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
480            !!!next-input-character;            !!!next-input-character;
481            redo A;            redo A;
482          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_input_character} and
483                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_input_character} <= 0x007A) { # a..z
484            $self->{current_token} = {type => 'start tag',            $self->{current_token} = {type => START_TAG_TOKEN,
485                              tag_name => chr ($self->{next_input_character})};                              tag_name => chr ($self->{next_input_character})};
486            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
487            !!!next-input-character;            !!!next-input-character;
488            redo A;            redo A;
489          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_input_character} == 0x003E) { # >
490            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag');
491            $self->{state} = 'data';            $self->{state} = DATA_STATE;
492            !!!next-input-character;            !!!next-input-character;
493    
494            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});
495    
496            redo A;            redo A;
497          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_input_character} == 0x003F) { # ?
498            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio');
499            $self->{state} = 'bogus comment';            $self->{state} = BOGUS_COMMENT_STATE;
500            ## $self->{next_input_character} is intentionally left as is            ## $self->{next_input_character} is intentionally left as is
501            redo A;            redo A;
502          } else {          } else {
503            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago');
504            $self->{state} = 'data';            $self->{state} = DATA_STATE;
505            ## reconsume            ## reconsume
506    
507            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
508    
509            redo A;            redo A;
510          }          }
511        } else {        } else {
512          die "$0: $self->{content_model_flag}: Unknown content model flag";          die "$0: $self->{content_model} in tag open";
513        }        }
514      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
515        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
516            $self->{content_model_flag} eq 'CDATA') {          if (defined $self->{last_emitted_start_tag_name}) {
517          my @next_char;            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
518          TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            my @next_char;
519              TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
520                push @next_char, $self->{next_input_character};
521                my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
522                my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
523                if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {
524                  !!!next-input-character;
525                  next TAGNAME;
526                } else {
527                  $self->{next_input_character} = shift @next_char; # reconsume
528                  !!!back-next-input-character (@next_char);
529                  $self->{state} = DATA_STATE;
530    
531                  !!!emit ({type => CHARACTER_TOKEN, data => '</'});
532      
533                  redo A;
534                }
535              }
536            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_input_character};
537            my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);        
538            my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;            unless ($self->{next_input_character} == 0x0009 or # HT
539            if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {                    $self->{next_input_character} == 0x000A or # LF
540              !!!next-input-character;                    $self->{next_input_character} == 0x000B or # VT
541              next TAGNAME;                    $self->{next_input_character} == 0x000C or # FF
542            } else {                    $self->{next_input_character} == 0x0020 or # SP
543                      $self->{next_input_character} == 0x003E or # >
544                      $self->{next_input_character} == 0x002F or # /
545                      $self->{next_input_character} == -1) {
546              $self->{next_input_character} = shift @next_char; # reconsume              $self->{next_input_character} = shift @next_char; # reconsume
547              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
548              $self->{state} = 'data';              $self->{state} = DATA_STATE;
549                !!!emit ({type => CHARACTER_TOKEN, data => '</'});
             !!!emit ({type => 'character', data => '</'});  
   
550              redo A;              redo A;
551              } else {
552                $self->{next_input_character} = shift @next_char;
553                !!!back-next-input-character (@next_char);
554                # and consume...
555            }            }
         }  
         push @next_char, $self->{next_input_character};  
       
         unless ($self->{next_input_character} == 0x0009 or # HT  
                 $self->{next_input_character} == 0x000A or # LF  
                 $self->{next_input_character} == 0x000B or # VT  
                 $self->{next_input_character} == 0x000C or # FF  
                 $self->{next_input_character} == 0x0020 or # SP  
                 $self->{next_input_character} == 0x003E or # >  
                 $self->{next_input_character} == 0x002F or # /  
                 $self->{next_input_character} == -1) {  
           $self->{next_input_character} = shift @next_char; # reconsume  
           !!!back-next-input-character (@next_char);  
           $self->{state} = 'data';  
   
           !!!emit ({type => 'character', data => '</'});  
   
           redo A;  
556          } else {          } else {
557            $self->{next_input_character} = shift @next_char;            ## No start tag token has ever been emitted
558            !!!back-next-input-character (@next_char);            # next-input-character is already done
559            # and consume...            $self->{state} = DATA_STATE;
560              !!!emit ({type => CHARACTER_TOKEN, data => '</'});
561              redo A;
562          }          }
563        }        }
564                
565        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_input_character} and
566            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_input_character} <= 0x005A) { # A..Z
567          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
568                            tag_name => chr ($self->{next_input_character} + 0x0020)};                            tag_name => chr ($self->{next_input_character} + 0x0020)};
569          $self->{state} = 'tag name';          $self->{state} = TAG_NAME_STATE;
570          !!!next-input-character;          !!!next-input-character;
571          redo A;          redo A;
572        } elsif (0x0061 <= $self->{next_input_character} and        } elsif (0x0061 <= $self->{next_input_character} and
573                 $self->{next_input_character} <= 0x007A) { # a..z                 $self->{next_input_character} <= 0x007A) { # a..z
574          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
575                            tag_name => chr ($self->{next_input_character})};                            tag_name => chr ($self->{next_input_character})};
576          $self->{state} = 'tag name';          $self->{state} = TAG_NAME_STATE;
577          !!!next-input-character;          !!!next-input-character;
578          redo A;          redo A;
579        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
580          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag');
581          $self->{state} = 'data';          $self->{state} = DATA_STATE;
582          !!!next-input-character;          !!!next-input-character;
583          redo A;          redo A;
584        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
585          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
586          $self->{state} = 'data';          $self->{state} = DATA_STATE;
587          # reconsume          # reconsume
588    
589          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</'});
590    
591          redo A;          redo A;
592        } else {        } else {
593          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
594          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
595          ## $self->{next_input_character} is intentionally left as is          ## $self->{next_input_character} is intentionally left as is
596          redo A;          redo A;
597        }        }
598      } elsif ($self->{state} eq 'tag name') {      } elsif ($self->{state} == TAG_NAME_STATE) {
599        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
600            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
601            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
602            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
603            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
604          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
605          !!!next-input-character;          !!!next-input-character;
606          redo A;          redo A;
607        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
608          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
609              $self->{current_token}->{first_start_tag}
610                  = not defined $self->{last_emitted_start_tag_name};
611            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
612          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
613            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
614            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
615              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
616            }            }
617          } else {          } else {
618            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
619          }          }
620          $self->{state} = 'data';          $self->{state} = DATA_STATE;
621          !!!next-input-character;          !!!next-input-character;
622    
623          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
624    
625          redo A;          redo A;
626        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 437  sub _get_next_token ($) { Line 632  sub _get_next_token ($) {
632          redo A;          redo A;
633        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
634          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
635          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
636              $self->{current_token}->{first_start_tag}
637                  = not defined $self->{last_emitted_start_tag_name};
638            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
639          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
640            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
641            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
642              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
643            }            }
644          } else {          } else {
645            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
646          }          }
647          $self->{state} = 'data';          $self->{state} = DATA_STATE;
648          # reconsume          # reconsume
649    
650          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
651    
652          redo A;          redo A;
653        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
654          !!!next-input-character;          !!!next-input-character;
655          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
656              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
657              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
658            # permitted slash            # permitted slash
659            #            #
660          } else {          } else {
661            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
662          }          }
663          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
664          # next-input-character is already done          # next-input-character is already done
665          redo A;          redo A;
666        } else {        } else {
# Line 474  sub _get_next_token ($) { Line 670  sub _get_next_token ($) {
670          !!!next-input-character;          !!!next-input-character;
671          redo A;          redo A;
672        }        }
673      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
674        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
675            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
676            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 484  sub _get_next_token ($) { Line 680  sub _get_next_token ($) {
680          !!!next-input-character;          !!!next-input-character;
681          redo A;          redo A;
682        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
683          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
684              $self->{current_token}->{first_start_tag}
685                  = not defined $self->{last_emitted_start_tag_name};
686            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
687          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
688            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
689            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
690              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
691            }            }
692          } else {          } else {
693            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
694          }          }
695          $self->{state} = 'data';          $self->{state} = DATA_STATE;
696          !!!next-input-character;          !!!next-input-character;
697    
698          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
699    
700          redo A;          redo A;
701        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
702                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_input_character} <= 0x005A) { # A..Z
703          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),
704                                value => ''};                                value => ''};
705          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
706          !!!next-input-character;          !!!next-input-character;
707          redo A;          redo A;
708        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
709          !!!next-input-character;          !!!next-input-character;
710          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
711              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
712              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
713            # permitted slash            # permitted slash
714            #            #
# Line 523  sub _get_next_token ($) { Line 720  sub _get_next_token ($) {
720          redo A;          redo A;
721        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
722          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
723          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
724              $self->{current_token}->{first_start_tag}
725                  = not defined $self->{last_emitted_start_tag_name};
726            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
727          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
728            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
729            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
730              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
731            }            }
732          } else {          } else {
733            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
734          }          }
735          $self->{state} = 'data';          $self->{state} = DATA_STATE;
736          # reconsume          # reconsume
737    
738          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
739    
740          redo A;          redo A;
741        } else {        } else {
742          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute} = {name => chr ($self->{next_input_character}),
743                                value => ''};                                value => ''};
744          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
745          !!!next-input-character;          !!!next-input-character;
746          redo A;          redo A;
747        }        }
748      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
749        my $before_leave = sub {        my $before_leave = sub {
750          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
751              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
752            !!!parse-error (type => 'dupulicate attribute');            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});
753            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
754          } else {          } else {
755            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
# Line 565  sub _get_next_token ($) { Line 763  sub _get_next_token ($) {
763            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
764            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
765          $before_leave->();          $before_leave->();
766          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
767          !!!next-input-character;          !!!next-input-character;
768          redo A;          redo A;
769        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_input_character} == 0x003D) { # =
770          $before_leave->();          $before_leave->();
771          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
772          !!!next-input-character;          !!!next-input-character;
773          redo A;          redo A;
774        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
775          $before_leave->();          $before_leave->();
776          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
777              $self->{current_token}->{first_start_tag}
778                  = not defined $self->{last_emitted_start_tag_name};
779            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
780          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
781            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
782            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
783              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
784            }            }
785          } else {          } else {
786            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
787          }          }
788          $self->{state} = 'data';          $self->{state} = DATA_STATE;
789          !!!next-input-character;          !!!next-input-character;
790    
791          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
792    
793          redo A;          redo A;
794        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 602  sub _get_next_token ($) { Line 801  sub _get_next_token ($) {
801          $before_leave->();          $before_leave->();
802          !!!next-input-character;          !!!next-input-character;
803          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
804              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
805              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
806            # permitted slash            # permitted slash
807            #            #
808          } else {          } else {
809            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
810          }          }
811          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
812          # next-input-character is already done          # next-input-character is already done
813          redo A;          redo A;
814        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
815          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
816          $before_leave->();          $before_leave->();
817          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
818              $self->{current_token}->{first_start_tag}
819                  = not defined $self->{last_emitted_start_tag_name};
820            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
821          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
822            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
823            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
824              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
825            }            }
826          } else {          } else {
827            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
828          }          }
829          $self->{state} = 'data';          $self->{state} = DATA_STATE;
830          # reconsume          # reconsume
831    
832          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
833    
834          redo A;          redo A;
835        } else {        } else {
# Line 638  sub _get_next_token ($) { Line 838  sub _get_next_token ($) {
838          !!!next-input-character;          !!!next-input-character;
839          redo A;          redo A;
840        }        }
841      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
842        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
843            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
844            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 648  sub _get_next_token ($) { Line 848  sub _get_next_token ($) {
848          !!!next-input-character;          !!!next-input-character;
849          redo A;          redo A;
850        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_input_character} == 0x003D) { # =
851          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
852          !!!next-input-character;          !!!next-input-character;
853          redo A;          redo A;
854        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
855          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
856              $self->{current_token}->{first_start_tag}
857                  = not defined $self->{last_emitted_start_tag_name};
858            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
859          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
860            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
861            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
862              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
863            }            }
864          } else {          } else {
865            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
866          }          }
867          $self->{state} = 'data';          $self->{state} = DATA_STATE;
868          !!!next-input-character;          !!!next-input-character;
869    
870          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
871    
872          redo A;          redo A;
873        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
874                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_input_character} <= 0x005A) { # A..Z
875          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),
876                                value => ''};                                value => ''};
877          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
878          !!!next-input-character;          !!!next-input-character;
879          redo A;          redo A;
880        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
881          !!!next-input-character;          !!!next-input-character;
882          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
883              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
884              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
885            # permitted slash            # permitted slash
886            #            #
887          } else {          } else {
888            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
889              ## TODO: Different error type for <aa / bb> than <aa/>
890          }          }
891          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
892          # next-input-character is already done          # next-input-character is already done
893          redo A;          redo A;
894        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
895          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
896          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
897              $self->{current_token}->{first_start_tag}
898                  = not defined $self->{last_emitted_start_tag_name};
899            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
900          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
901            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
902            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
903              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
904            }            }
905          } else {          } else {
906            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
907          }          }
908          $self->{state} = 'data';          $self->{state} = DATA_STATE;
909          # reconsume          # reconsume
910    
911          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
912    
913          redo A;          redo A;
914        } else {        } else {
915          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute} = {name => chr ($self->{next_input_character}),
916                                value => ''};                                value => ''};
917          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
918          !!!next-input-character;          !!!next-input-character;
919          redo A;                  redo A;        
920        }        }
921      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
922        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
923            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
924            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 725  sub _get_next_token ($) { Line 928  sub _get_next_token ($) {
928          !!!next-input-character;          !!!next-input-character;
929          redo A;          redo A;
930        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
931          $self->{state} = 'attribute value (double-quoted)';          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
932          !!!next-input-character;          !!!next-input-character;
933          redo A;          redo A;
934        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
935          $self->{state} = 'attribute value (unquoted)';          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
936          ## reconsume          ## reconsume
937          redo A;          redo A;
938        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
939          $self->{state} = 'attribute value (single-quoted)';          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
940          !!!next-input-character;          !!!next-input-character;
941          redo A;          redo A;
942        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
943          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
944              $self->{current_token}->{first_start_tag}
945                  = not defined $self->{last_emitted_start_tag_name};
946            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
947          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
948            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
949            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
950              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
951            }            }
952          } else {          } else {
953            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
954          }          }
955          $self->{state} = 'data';          $self->{state} = DATA_STATE;
956          !!!next-input-character;          !!!next-input-character;
957    
958          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
959    
960          redo A;          redo A;
961        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
962          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
963          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
964              $self->{current_token}->{first_start_tag}
965                  = not defined $self->{last_emitted_start_tag_name};
966            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
967          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
968            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
969            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
970              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
971            }            }
972          } else {          } else {
973            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
974          }          }
975          $self->{state} = 'data';          $self->{state} = DATA_STATE;
976          ## reconsume          ## reconsume
977    
978          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
979    
980          redo A;          redo A;
981        } else {        } else {
982          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});
983          $self->{state} = 'attribute value (unquoted)';          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
984          !!!next-input-character;          !!!next-input-character;
985          redo A;          redo A;
986        }        }
987      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
988        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
989          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
990          !!!next-input-character;          !!!next-input-character;
991          redo A;          redo A;
992        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
993          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          $self->{last_attribute_value_state} = $self->{state};
994          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
995          !!!next-input-character;          !!!next-input-character;
996          redo A;          redo A;
997        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
998          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
999          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1000              $self->{current_token}->{first_start_tag}
1001                  = not defined $self->{last_emitted_start_tag_name};
1002            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1003          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1004            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1005            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1006              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1007            }            }
1008          } else {          } else {
1009            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1010          }          }
1011          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1012          ## reconsume          ## reconsume
1013    
1014          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1015    
1016          redo A;          redo A;
1017        } else {        } else {
# Line 814  sub _get_next_token ($) { Line 1020  sub _get_next_token ($) {
1020          !!!next-input-character;          !!!next-input-character;
1021          redo A;          redo A;
1022        }        }
1023      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1024        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1025          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1026          !!!next-input-character;          !!!next-input-character;
1027          redo A;          redo A;
1028        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
1029          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          $self->{last_attribute_value_state} = $self->{state};
1030          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1031          !!!next-input-character;          !!!next-input-character;
1032          redo A;          redo A;
1033        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1034          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1035          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1036              $self->{current_token}->{first_start_tag}
1037                  = not defined $self->{last_emitted_start_tag_name};
1038            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1039          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1040            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1041            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1042              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1043            }            }
1044          } else {          } else {
1045            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1046          }          }
1047          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1048          ## reconsume          ## reconsume
1049    
1050          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1051    
1052          redo A;          redo A;
1053        } else {        } else {
# Line 849  sub _get_next_token ($) { Line 1056  sub _get_next_token ($) {
1056          !!!next-input-character;          !!!next-input-character;
1057          redo A;          redo A;
1058        }        }
1059      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1060        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1061            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1062            $self->{next_input_character} == 0x000B or # HT            $self->{next_input_character} == 0x000B or # HT
1063            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1064            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1065          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1066          !!!next-input-character;          !!!next-input-character;
1067          redo A;          redo A;
1068        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
1069          $self->{last_attribute_value_state} = 'attribute value (unquoted)';          $self->{last_attribute_value_state} = $self->{state};
1070          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1071          !!!next-input-character;          !!!next-input-character;
1072          redo A;          redo A;
1073        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1074          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1075              $self->{current_token}->{first_start_tag}
1076                  = not defined $self->{last_emitted_start_tag_name};
1077            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1078          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1079            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1080            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1081              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1082            }            }
1083          } else {          } else {
1084            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1085          }          }
1086          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1087          !!!next-input-character;          !!!next-input-character;
1088    
1089          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1090    
1091          redo A;          redo A;
1092        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1093          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1094          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1095              $self->{current_token}->{first_start_tag}
1096                  = not defined $self->{last_emitted_start_tag_name};
1097            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1098          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1099            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1100            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1101              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1102            }            }
1103          } else {          } else {
1104            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1105          }          }
1106          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1107          ## reconsume          ## reconsume
1108    
1109          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1110    
1111          redo A;          redo A;
1112        } else {        } else {
# Line 906  sub _get_next_token ($) { Line 1115  sub _get_next_token ($) {
1115          !!!next-input-character;          !!!next-input-character;
1116          redo A;          redo A;
1117        }        }
1118      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1119        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);
1120    
1121        unless (defined $token) {        unless (defined $token) {
1122          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1123        } else {        } else {
1124          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1125            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1126          ## ISSUE: spec says "append the returned character token to the current attribute's value"          ## ISSUE: spec says "append the returned character token to the current attribute's value"
1127        }        }
1128    
1129        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1130        # next-input-character is already done        # next-input-character is already done
1131        redo A;        redo A;
1132      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1133        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1134                
1135        my $token = {type => 'comment', data => ''};        my $token = {type => COMMENT_TOKEN, data => ''};
1136    
1137        BC: {        BC: {
1138          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_input_character} == 0x003E) { # >
1139            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1140            !!!next-input-character;            !!!next-input-character;
1141    
1142            !!!emit ($token);            !!!emit ($token);
1143    
1144            redo A;            redo A;
1145          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_input_character} == -1) {
1146            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1147            ## reconsume            ## reconsume
1148    
1149            !!!emit ($token);            !!!emit ($token);
# Line 945  sub _get_next_token ($) { Line 1155  sub _get_next_token ($) {
1155            redo BC;            redo BC;
1156          }          }
1157        } # BC        } # BC
1158      } elsif ($self->{state} eq 'markup declaration open') {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1159        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1160    
1161        my @next_char;        my @next_char;
# Line 955  sub _get_next_token ($) { Line 1165  sub _get_next_token ($) {
1165          !!!next-input-character;          !!!next-input-character;
1166          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_input_character};
1167          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_input_character} == 0x002D) { # -
1168            $self->{current_token} = {type => 'comment', data => ''};            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};
1169            $self->{state} = 'comment';            $self->{state} = COMMENT_START_STATE;
1170            !!!next-input-character;            !!!next-input-character;
1171            redo A;            redo A;
1172          }          }
# Line 987  sub _get_next_token ($) { Line 1197  sub _get_next_token ($) {
1197                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_input_character} == 0x0045 or # E
1198                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_input_character} == 0x0065) { # e
1199                      ## ISSUE: What a stupid code this is!                      ## ISSUE: What a stupid code this is!
1200                      $self->{state} = 'DOCTYPE';                      $self->{state} = DOCTYPE_STATE;
1201                      !!!next-input-character;                      !!!next-input-character;
1202                      redo A;                      redo A;
1203                    }                    }
# Line 998  sub _get_next_token ($) { Line 1208  sub _get_next_token ($) {
1208          }          }
1209        }        }
1210    
1211        !!!parse-error (type => 'bogus comment open');        !!!parse-error (type => 'bogus comment');
1212        $self->{next_input_character} = shift @next_char;        $self->{next_input_character} = shift @next_char;
1213        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1214        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
1215        redo A;        redo A;
1216                
1217        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1218        ## 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?
1219      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_START_STATE) {
1220        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1221          $self->{state} = 'comment dash';          $self->{state} = COMMENT_START_DASH_STATE;
1222            !!!next-input-character;
1223            redo A;
1224          } elsif ($self->{next_input_character} == 0x003E) { # >
1225            !!!parse-error (type => 'bogus comment');
1226            $self->{state} = DATA_STATE;
1227          !!!next-input-character;          !!!next-input-character;
1228    
1229            !!!emit ($self->{current_token}); # comment
1230    
1231          redo A;          redo A;
1232        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1233          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1234          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1235            ## reconsume
1236    
1237            !!!emit ($self->{current_token}); # comment
1238    
1239            redo A;
1240          } else {
1241            $self->{current_token}->{data} # comment
1242                .= chr ($self->{next_input_character});
1243            $self->{state} = COMMENT_STATE;
1244            !!!next-input-character;
1245            redo A;
1246          }
1247        } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
1248          if ($self->{next_input_character} == 0x002D) { # -
1249            $self->{state} = COMMENT_END_STATE;
1250            !!!next-input-character;
1251            redo A;
1252          } elsif ($self->{next_input_character} == 0x003E) { # >
1253            !!!parse-error (type => 'bogus comment');
1254            $self->{state} = DATA_STATE;
1255            !!!next-input-character;
1256    
1257            !!!emit ($self->{current_token}); # comment
1258    
1259            redo A;
1260          } elsif ($self->{next_input_character} == -1) {
1261            !!!parse-error (type => 'unclosed comment');
1262            $self->{state} = DATA_STATE;
1263            ## reconsume
1264    
1265            !!!emit ($self->{current_token}); # comment
1266    
1267            redo A;
1268          } else {
1269            $self->{current_token}->{data} # comment
1270                .= '-' . chr ($self->{next_input_character});
1271            $self->{state} = COMMENT_STATE;
1272            !!!next-input-character;
1273            redo A;
1274          }
1275        } elsif ($self->{state} == COMMENT_STATE) {
1276          if ($self->{next_input_character} == 0x002D) { # -
1277            $self->{state} = COMMENT_END_DASH_STATE;
1278            !!!next-input-character;
1279            redo A;
1280          } elsif ($self->{next_input_character} == -1) {
1281            !!!parse-error (type => 'unclosed comment');
1282            $self->{state} = DATA_STATE;
1283          ## reconsume          ## reconsume
1284    
1285          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1286    
1287          redo A;          redo A;
1288        } else {        } else {
# Line 1026  sub _get_next_token ($) { Line 1291  sub _get_next_token ($) {
1291          !!!next-input-character;          !!!next-input-character;
1292          redo A;          redo A;
1293        }        }
1294      } elsif ($self->{state} eq 'comment dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
1295        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1296          $self->{state} = 'comment end';          $self->{state} = COMMENT_END_STATE;
1297          !!!next-input-character;          !!!next-input-character;
1298          redo A;          redo A;
1299        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1300          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1301          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1302          ## reconsume          ## reconsume
1303    
1304          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1305    
1306          redo A;          redo A;
1307        } else {        } else {
1308          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment
1309          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1310          !!!next-input-character;          !!!next-input-character;
1311          redo A;          redo A;
1312        }        }
1313      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
1314        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_input_character} == 0x003E) { # >
1315          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1316          !!!next-input-character;          !!!next-input-character;
1317    
1318          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1319    
1320          redo A;          redo A;
1321        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_input_character} == 0x002D) { # -
# Line 1063  sub _get_next_token ($) { Line 1326  sub _get_next_token ($) {
1326          redo A;          redo A;
1327        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1328          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1329          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1330          ## reconsume          ## reconsume
1331    
1332          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1333    
1334          redo A;          redo A;
1335        } else {        } else {
1336          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment');
1337          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment
1338          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1339          !!!next-input-character;          !!!next-input-character;
1340          redo A;          redo A;
1341        }        }
1342      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
1343        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1344            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1345            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1346            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1347            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1348          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1349          !!!next-input-character;          !!!next-input-character;
1350          redo A;          redo A;
1351        } else {        } else {
1352          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
1353          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1354          ## reconsume          ## reconsume
1355          redo A;          redo A;
1356        }        }
1357      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
1358        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1359            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1360            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 1103  sub _get_next_token ($) { Line 1365  sub _get_next_token ($) {
1365          redo A;          redo A;
1366        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1367          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1368          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1369          !!!next-input-character;          !!!next-input-character;
1370    
1371          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1372    
1373          redo A;          redo A;
1374        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1375          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1376          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1377          ## reconsume          ## reconsume
1378    
1379          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1380    
1381          redo A;          redo A;
1382        } else {        } else {
1383          $self->{current_token}          $self->{current_token}
1384              = {type => 'DOCTYPE',              = {type => DOCTYPE_TOKEN,
1385                 name => chr ($self->{next_input_character}),                 name => chr ($self->{next_input_character}),
1386                 correct => 1};                 correct => 1};
1387  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
1388          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
1389          !!!next-input-character;          !!!next-input-character;
1390          redo A;          redo A;
1391        }        }
1392      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
1393  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
1394        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1395            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1396            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1397            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1398            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1399          $self->{state} = 'after DOCTYPE name';          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
1400          !!!next-input-character;          !!!next-input-character;
1401          redo A;          redo A;
1402        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1403          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1404          !!!next-input-character;          !!!next-input-character;
1405    
1406          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1407    
1408          redo A;          redo A;
1409        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1410          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1411          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1412          ## reconsume          ## reconsume
1413    
1414          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1415          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1416    
1417          redo A;          redo A;
1418        } else {        } else {
# Line 1162  sub _get_next_token ($) { Line 1422  sub _get_next_token ($) {
1422          !!!next-input-character;          !!!next-input-character;
1423          redo A;          redo A;
1424        }        }
1425      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
1426        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1427            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1428            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 1172  sub _get_next_token ($) { Line 1432  sub _get_next_token ($) {
1432          !!!next-input-character;          !!!next-input-character;
1433          redo A;          redo A;
1434        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1435          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1436          !!!next-input-character;          !!!next-input-character;
1437    
1438          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1439    
1440          redo A;          redo A;
1441        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1442          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1443          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1444          ## reconsume          ## reconsume
1445    
1446          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1447          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1448    
1449          redo A;          redo A;
1450        } elsif ($self->{next_input_character} == 0x0050 or # P        } elsif ($self->{next_input_character} == 0x0050 or # P
# Line 1206  sub _get_next_token ($) { Line 1464  sub _get_next_token ($) {
1464                  !!!next-input-character;                  !!!next-input-character;
1465                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_input_character} == 0x0043 or # C
1466                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_input_character} == 0x0063) { # c
1467                    $self->{state} = 'before DOCTYPE public identifier';                    $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1468                    !!!next-input-character;                    !!!next-input-character;
1469                    redo A;                    redo A;
1470                  }                  }
# Line 1233  sub _get_next_token ($) { Line 1491  sub _get_next_token ($) {
1491                  !!!next-input-character;                  !!!next-input-character;
1492                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_input_character} == 0x004D or # M
1493                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_input_character} == 0x006D) { # m
1494                    $self->{state} = 'before DOCTYPE system identifier';                    $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1495                    !!!next-input-character;                    !!!next-input-character;
1496                    redo A;                    redo A;
1497                  }                  }
# Line 1249  sub _get_next_token ($) { Line 1507  sub _get_next_token ($) {
1507        }        }
1508    
1509        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
1510        $self->{state} = 'bogus DOCTYPE';        $self->{state} = BOGUS_DOCTYPE_STATE;
1511        # next-input-character is already done        # next-input-character is already done
1512        redo A;        redo A;
1513      } elsif ($self->{state} eq 'before DOCTYPE public identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1514        if ({        if ({
1515              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1516              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1262  sub _get_next_token ($) { Line 1520  sub _get_next_token ($) {
1520          redo A;          redo A;
1521        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_input_character} eq 0x0022) { # "
1522          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1523          $self->{state} = 'DOCTYPE public identifier (double-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
1524          !!!next-input-character;          !!!next-input-character;
1525          redo A;          redo A;
1526        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_input_character} eq 0x0027) { # '
1527          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1528          $self->{state} = 'DOCTYPE public identifier (single-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
1529          !!!next-input-character;          !!!next-input-character;
1530          redo A;          redo A;
1531        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_input_character} eq 0x003E) { # >
1532          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
1533    
1534          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1535          !!!next-input-character;          !!!next-input-character;
1536    
1537          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1538          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1539    
1540          redo A;          redo A;
1541        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1542          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1543    
1544          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1545          ## reconsume          ## reconsume
1546    
1547          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1548          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1549    
1550          redo A;          redo A;
1551        } else {        } else {
1552          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
1553          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1554          !!!next-input-character;          !!!next-input-character;
1555          redo A;          redo A;
1556        }        }
1557      } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1558        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
1559          $self->{state} = 'after DOCTYPE public identifier';          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1560          !!!next-input-character;          !!!next-input-character;
1561          redo A;          redo A;
1562          } elsif ($self->{next_input_character} == 0x003E) { # >
1563            !!!parse-error (type => 'unclosed PUBLIC literal');
1564    
1565            $self->{state} = DATA_STATE;
1566            !!!next-input-character;
1567    
1568            delete $self->{current_token}->{correct};
1569            !!!emit ($self->{current_token}); # DOCTYPE
1570    
1571            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};
1579          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1580    
1581          redo A;          redo A;
1582        } else {        } else {
# Line 1321  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 'DOCTYPE public identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
1590        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1591          $self->{state} = 'after DOCTYPE public identifier';          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1592          !!!next-input-character;          !!!next-input-character;
1593          redo A;          redo A;
1594          } elsif ($self->{next_input_character} == 0x003E) { # >
1595            !!!parse-error (type => 'unclosed PUBLIC literal');
1596    
1597            $self->{state} = DATA_STATE;
1598            !!!next-input-character;
1599    
1600            delete $self->{current_token}->{correct};
1601            !!!emit ($self->{current_token}); # DOCTYPE
1602    
1603            redo A;
1604        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1605          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1606    
1607          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1608          ## reconsume          ## reconsume
1609    
1610          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1611          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1612    
1613          redo A;          redo A;
1614        } else {        } else {
# Line 1344  sub _get_next_token ($) { Line 1618  sub _get_next_token ($) {
1618          !!!next-input-character;          !!!next-input-character;
1619          redo A;          redo A;
1620        }        }
1621      } elsif ($self->{state} eq 'after DOCTYPE public identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1622        if ({        if ({
1623              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1624              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1354  sub _get_next_token ($) { Line 1628  sub _get_next_token ($) {
1628          redo A;          redo A;
1629        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
1630          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1631          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1632          !!!next-input-character;          !!!next-input-character;
1633          redo A;          redo A;
1634        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
1635          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1636          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1637          !!!next-input-character;          !!!next-input-character;
1638          redo A;          redo A;
1639        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1640          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1641          !!!next-input-character;          !!!next-input-character;
1642    
1643          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1644    
1645          redo A;          redo A;
1646        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1647          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1648    
1649          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1650          ## recomsume          ## reconsume
1651    
1652          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1653          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1654    
1655          redo A;          redo A;
1656        } else {        } else {
1657          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
1658          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1659          !!!next-input-character;          !!!next-input-character;
1660          redo A;          redo A;
1661        }        }
1662      } elsif ($self->{state} eq 'before DOCTYPE system identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1663        if ({        if ({
1664              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1665              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1397  sub _get_next_token ($) { Line 1669  sub _get_next_token ($) {
1669          redo A;          redo A;
1670        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
1671          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1672          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1673          !!!next-input-character;          !!!next-input-character;
1674          redo A;          redo A;
1675        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
1676          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1677          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1678          !!!next-input-character;          !!!next-input-character;
1679          redo A;          redo A;
1680        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1681          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
1682          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1683          !!!next-input-character;          !!!next-input-character;
1684    
1685          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1686          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1687    
1688          redo A;          redo A;
1689        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1690          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1691    
1692          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1693          ## recomsume          ## reconsume
1694    
1695          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1696          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1697    
1698          redo A;          redo A;
1699        } else {        } else {
1700          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after SYSTEM');
1701          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1702          !!!next-input-character;          !!!next-input-character;
1703          redo A;          redo A;
1704        }        }
1705      } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1706        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
1707          $self->{state} = 'after DOCTYPE system identifier';          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1708          !!!next-input-character;          !!!next-input-character;
1709          redo A;          redo A;
1710          } elsif ($self->{next_input_character} == 0x003E) { # >
1711            !!!parse-error (type => 'unclosed PUBLIC literal');
1712    
1713            $self->{state} = DATA_STATE;
1714            !!!next-input-character;
1715    
1716            delete $self->{current_token}->{correct};
1717            !!!emit ($self->{current_token}); # DOCTYPE
1718    
1719            redo A;
1720        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1721          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
1722    
1723          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1724          ## reconsume          ## reconsume
1725    
1726          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1727          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1728    
1729          redo A;          redo A;
1730        } else {        } else {
# Line 1455  sub _get_next_token ($) { Line 1734  sub _get_next_token ($) {
1734          !!!next-input-character;          !!!next-input-character;
1735          redo A;          redo A;
1736        }        }
1737      } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
1738        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1739          $self->{state} = 'after DOCTYPE system identifier';          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1740          !!!next-input-character;          !!!next-input-character;
1741          redo A;          redo A;
1742          } elsif ($self->{next_input_character} == 0x003E) { # >
1743            !!!parse-error (type => 'unclosed PUBLIC literal');
1744    
1745            $self->{state} = DATA_STATE;
1746            !!!next-input-character;
1747    
1748            delete $self->{current_token}->{correct};
1749            !!!emit ($self->{current_token}); # DOCTYPE
1750    
1751            redo A;
1752        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1753          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
1754    
1755          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1756          ## reconsume          ## reconsume
1757    
1758          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1759          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1760    
1761          redo A;          redo A;
1762        } else {        } else {
# Line 1478  sub _get_next_token ($) { Line 1766  sub _get_next_token ($) {
1766          !!!next-input-character;          !!!next-input-character;
1767          redo A;          redo A;
1768        }        }
1769      } elsif ($self->{state} eq 'after DOCTYPE system identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1770        if ({        if ({
1771              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1772              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1487  sub _get_next_token ($) { Line 1775  sub _get_next_token ($) {
1775          !!!next-input-character;          !!!next-input-character;
1776          redo A;          redo A;
1777        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1778          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1779          !!!next-input-character;          !!!next-input-character;
1780    
1781          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1782    
1783          redo A;          redo A;
1784        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1785          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1786    
1787          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1788          ## recomsume          ## reconsume
1789    
1790          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1791          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1792    
1793          redo A;          redo A;
1794        } else {        } else {
1795          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
1796          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1797          !!!next-input-character;          !!!next-input-character;
1798          redo A;          redo A;
1799        }        }
1800      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
1801        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_input_character} == 0x003E) { # >
1802          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1803          !!!next-input-character;          !!!next-input-character;
1804    
1805          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1806          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1807    
1808          redo A;          redo A;
1809        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1810          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1811          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1812          ## reconsume          ## reconsume
1813    
1814          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1815          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1816    
1817          redo A;          redo A;
1818        } else {        } else {
# Line 1544  sub _get_next_token ($) { Line 1828  sub _get_next_token ($) {
1828    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
1829  } # _get_next_token  } # _get_next_token
1830    
1831  sub _tokenize_attempt_to_consume_an_entity ($) {  sub _tokenize_attempt_to_consume_an_entity ($$) {
1832    my $self = shift;    my ($self, $in_attr) = @_;
1833    
1834    if ({    if ({
1835         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
# Line 1558  sub _tokenize_attempt_to_consume_an_enti Line 1842  sub _tokenize_attempt_to_consume_an_enti
1842      !!!next-input-character;      !!!next-input-character;
1843      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_input_character} == 0x0078 or # x
1844          $self->{next_input_character} == 0x0058) { # X          $self->{next_input_character} == 0x0058) { # X
1845        my $num;        my $code;
1846        X: {        X: {
1847          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_input_character};
1848          !!!next-input-character;          !!!next-input-character;
1849          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_input_character} and
1850              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_input_character} <= 0x0039) { # 0..9
1851            $num ||= 0;            $code ||= 0;
1852            $num *= 0x10;            $code *= 0x10;
1853            $num += $self->{next_input_character} - 0x0030;            $code += $self->{next_input_character} - 0x0030;
1854            redo X;            redo X;
1855          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_input_character} and
1856                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_input_character} <= 0x0066) { # a..f
1857            ## ISSUE: the spec says U+0078, which is apparently incorrect            $code ||= 0;
1858            $num ||= 0;            $code *= 0x10;
1859            $num *= 0x10;            $code += $self->{next_input_character} - 0x0060 + 9;
           $num += $self->{next_input_character} - 0x0060 + 9;  
1860            redo X;            redo X;
1861          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_input_character} and
1862                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_input_character} <= 0x0046) { # A..F
1863            ## ISSUE: the spec says U+0058, which is apparently incorrect            $code ||= 0;
1864            $num ||= 0;            $code *= 0x10;
1865            $num *= 0x10;            $code += $self->{next_input_character} - 0x0040 + 9;
           $num += $self->{next_input_character} - 0x0040 + 9;  
1866            redo X;            redo X;
1867          } elsif (not defined $num) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
1868            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro');
1869              !!!back-next-input-character ($x_char, $self->{next_input_character});
1870            $self->{next_input_character} = 0x0023; # #            $self->{next_input_character} = 0x0023; # #
           !!!back-next-input-character ($x_char);  
1871            return undef;            return undef;
1872          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_input_character} == 0x003B) { # ;
1873            !!!next-input-character;            !!!next-input-character;
# Line 1593  sub _tokenize_attempt_to_consume_an_enti Line 1875  sub _tokenize_attempt_to_consume_an_enti
1875            !!!parse-error (type => 'no refc');            !!!parse-error (type => 'no refc');
1876          }          }
1877    
1878          ## TODO: check the definition for |a valid Unicode character|.          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
1879          ## <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8189>            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
1880          if ($num > 1114111 or $num == 0) {            $code = 0xFFFD;
1881            $num = 0xFFFD; # REPLACEMENT CHARACTER          } elsif ($code > 0x10FFFF) {
1882            ## ISSUE: Why this is not an error?            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
1883          } elsif (0x80 <= $num and $num <= 0x9F) {            $code = 0xFFFD;
1884            !!!parse-error (type => sprintf 'c1 entity:U+%04X', $num);          } elsif ($code == 0x000D) {
1885            $num = $c1_entity_char->{$num};            !!!parse-error (type => 'CR character reference');
1886              $code = 0x000A;
1887            } elsif (0x80 <= $code and $code <= 0x9F) {
1888              !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
1889              $code = $c1_entity_char->{$code};
1890          }          }
1891    
1892          return {type => 'character', data => chr $num};          return {type => CHARACTER_TOKEN, data => chr $code,
1893                    has_reference => 1};
1894        } # X        } # X
1895      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_input_character} and
1896               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_input_character} <= 0x0039) { # 0..9
# Line 1624  sub _tokenize_attempt_to_consume_an_enti Line 1911  sub _tokenize_attempt_to_consume_an_enti
1911          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc');
1912        }        }
1913    
1914        ## TODO: check the definition for |a valid Unicode character|.        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
1915        if ($code > 1114111 or $code == 0) {          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
1916          $code = 0xFFFD; # REPLACEMENT CHARACTER          $code = 0xFFFD;
1917          ## ISSUE: Why this is not an error?        } elsif ($code > 0x10FFFF) {
1918            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
1919            $code = 0xFFFD;
1920          } elsif ($code == 0x000D) {
1921            !!!parse-error (type => 'CR character reference');
1922            $code = 0x000A;
1923        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
1924          !!!parse-error (type => sprintf 'c1 entity:U+%04X', $code);          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
1925          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
1926        }        }
1927                
1928        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};
1929      } else {      } else {
1930        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero');
1931        !!!back-next-input-character ($self->{next_input_character});        !!!back-next-input-character ($self->{next_input_character});
# Line 1648  sub _tokenize_attempt_to_consume_an_enti Line 1940  sub _tokenize_attempt_to_consume_an_enti
1940      !!!next-input-character;      !!!next-input-character;
1941    
1942      my $value = $entity_name;      my $value = $entity_name;
1943      my $match;      my $match = 0;
1944      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
1945      our $EntityChar;      our $EntityChar;
1946    
# Line 1663  sub _tokenize_attempt_to_consume_an_enti Line 1955  sub _tokenize_attempt_to_consume_an_enti
1955              $self->{next_input_character} == 0x003B)) { # ;              $self->{next_input_character} == 0x003B)) { # ;
1956        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_input_character};
1957        if (defined $EntityChar->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
         $value = $EntityChar->{$entity_name};  
1958          if ($self->{next_input_character} == 0x003B) { # ;          if ($self->{next_input_character} == 0x003B) { # ;
1959              $value = $EntityChar->{$entity_name};
1960            $match = 1;            $match = 1;
1961            !!!next-input-character;            !!!next-input-character;
1962            last;            last;
1963          } else {          } else {
1964              $value = $EntityChar->{$entity_name};
1965            $match = -1;            $match = -1;
1966              !!!next-input-character;
1967          }          }
1968        } else {        } else {
1969          $value .= chr $self->{next_input_character};          $value .= chr $self->{next_input_character};
1970            $match *= 2;
1971            !!!next-input-character;
1972        }        }
       !!!next-input-character;  
1973      }      }
1974            
1975      if ($match > 0) {      if ($match > 0) {
1976        return {type => 'character', data => $value};        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
1977      } elsif ($match < 0) {      } elsif ($match < 0) {
1978        !!!parse-error (type => 'refc');        !!!parse-error (type => 'no refc');
1979        return {type => 'character', data => $value};        if ($in_attr and $match < -1) {
1980            return {type => CHARACTER_TOKEN, data => '&'.$entity_name};
1981          } else {
1982            return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
1983          }
1984      } else {      } else {
1985        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero');
1986        ## NOTE: No characters are consumed in the spec.        ## NOTE: "No characters are consumed" in the spec.
1987        !!!back-token ({type => 'character', data => $value});        return {type => CHARACTER_TOKEN, data => '&'.$value};
       return undef;  
1988      }      }
1989    } else {    } else {
1990      ## no characters are consumed      ## no characters are consumed
# Line 1728  sub _construct_tree ($) { Line 2026  sub _construct_tree ($) {
2026        
2027    !!!next-token;    !!!next-token;
2028    
2029    $self->{insertion_mode} = 'before head';    $self->{insertion_mode} = BEFORE_HEAD_IM;
2030    undef $self->{form_element};    undef $self->{form_element};
2031    undef $self->{head_element};    undef $self->{head_element};
2032    $self->{open_elements} = [];    $self->{open_elements} = [];
# Line 1742  sub _construct_tree ($) { Line 2040  sub _construct_tree ($) {
2040  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
2041    my $self = shift;    my $self = shift;
2042    INITIAL: {    INITIAL: {
2043      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
2044        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
2045        ## error, switch to a conformance checking mode for another        ## error, switch to a conformance checking mode for another
2046        ## language.        ## language.
# Line 1869  sub _tree_construction_initial ($) { Line 2167  sub _tree_construction_initial ($) {
2167        !!!next-token;        !!!next-token;
2168        return;        return;
2169      } elsif ({      } elsif ({
2170                'start tag' => 1,                START_TAG_TOKEN, 1,
2171                'end tag' => 1,                END_TAG_TOKEN, 1,
2172                'end-of-file' => 1,                END_OF_FILE_TOKEN, 1,
2173               }->{$token->{type}}) {               }->{$token->{type}}) {
2174        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE');
2175        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2176        ## Go to the root element phase        ## Go to the root element phase
2177        ## reprocess        ## reprocess
2178        return;        return;
2179      } elsif ($token->{type} eq 'character') {      } elsif ($token->{type} == CHARACTER_TOKEN) {
2180        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2181          ## Ignore the token          ## Ignore the token
2182    
2183          unless (length $token->{data}) {          unless (length $token->{data}) {
2184            ## Stay in the phase            ## Stay in the phase
2185            !!!next-token;            !!!next-token;
# Line 1893  sub _tree_construction_initial ($) { Line 2192  sub _tree_construction_initial ($) {
2192        ## Go to the root element phase        ## Go to the root element phase
2193        ## reprocess        ## reprocess
2194        return;        return;
2195      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
2196        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2197        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
2198                
# Line 1901  sub _tree_construction_initial ($) { Line 2200  sub _tree_construction_initial ($) {
2200        !!!next-token;        !!!next-token;
2201        redo INITIAL;        redo INITIAL;
2202      } else {      } else {
2203        die "$0: $token->{type}: Unknown token";        die "$0: $token->{type}: Unknown token type";
2204      }      }
2205    } # INITIAL    } # INITIAL
2206  } # _tree_construction_initial  } # _tree_construction_initial
# Line 1910  sub _tree_construction_root_element ($) Line 2209  sub _tree_construction_root_element ($)
2209    my $self = shift;    my $self = shift;
2210        
2211    B: {    B: {
2212        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
2213          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE');
2214          ## Ignore the token          ## Ignore the token
2215          ## Stay in the phase          ## Stay in the phase
2216          !!!next-token;          !!!next-token;
2217          redo B;          redo B;
2218        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
2219          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
2220          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2221          ## Stay in the phase          ## Stay in the phase
2222          !!!next-token;          !!!next-token;
2223          redo B;          redo B;
2224        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
2225          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2226            $self->{document}->manakai_append_text ($1);            ## Ignore the token.
2227            ## ISSUE: DOM3 Core does not allow Document > Text  
2228            unless (length $token->{data}) {            unless (length $token->{data}) {
2229              ## Stay in the phase              ## Stay in the phase
2230              !!!next-token;              !!!next-token;
2231              redo B;              redo B;
2232            }            }
2233          }          }
2234    
2235            $self->{application_cache_selection}->(undef);
2236    
2237            #
2238          } elsif ($token->{type} == START_TAG_TOKEN) {
2239            if ($token->{tag_name} eq 'html' and
2240                $token->{attributes}->{manifest}) {
2241              $self->{application_cache_selection}
2242                   ->($token->{attributes}->{manifest}->{value});
2243              ## ISSUE: No relative reference resolution?
2244            } else {
2245              $self->{application_cache_selection}->(undef);
2246            }
2247    
2248            ## ISSUE: There is an issue in the spec
2249          #          #
2250        } elsif ({        } elsif ({
2251                  'start tag' => 1,                  END_TAG_TOKEN, 1,
2252                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
2253                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2254            $self->{application_cache_selection}->(undef);
2255    
2256          ## ISSUE: There is an issue in the spec          ## ISSUE: There is an issue in the spec
2257          #          #
2258        } else {        } else {
2259          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
2260        }        }
2261    
2262        my $root_element; !!!create-element ($root_element, 'html');        my $root_element; !!!create-element ($root_element, 'html');
2263        $self->{document}->append_child ($root_element);        $self->{document}->append_child ($root_element);
2264        push @{$self->{open_elements}}, [$root_element, 'html'];        push @{$self->{open_elements}}, [$root_element, 'html'];
       #$phase = 'main';  
2265        ## reprocess        ## reprocess
2266        #redo B;        #redo B;
2267        return;        return; ## Go to the main phase.
2268    } # B    } # B
2269  } # _tree_construction_root_element  } # _tree_construction_root_element
2270    
# Line 1965  sub _reset_insertion_mode ($) { Line 2280  sub _reset_insertion_mode ($) {
2280            
2281      ## Step 3      ## Step 3
2282      S3: {      S3: {
2283        $last = 1 if $self->{open_elements}->[0]->[0] eq $node->[0];        ## ISSUE: Oops! "If node is the first node in the stack of open
2284        if (defined $self->{inner_html_node}) {        ## elements, then set last to true. If the context element of the
2285          if ($self->{inner_html_node}->[1] eq 'td' or        ## HTML fragment parsing algorithm is neither a td element nor a
2286              $self->{inner_html_node}->[1] eq 'th') {        ## th element, then set node to the context element. (fragment case)":
2287            #        ## The second "if" is in the scope of the first "if"!?
2288          } else {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
2289            $node = $self->{inner_html_node};          $last = 1;
2290            if (defined $self->{inner_html_node}) {
2291              if ($self->{inner_html_node}->[1] eq 'td' or
2292                  $self->{inner_html_node}->[1] eq 'th') {
2293                #
2294              } else {
2295                $node = $self->{inner_html_node};
2296              }
2297          }          }
2298        }        }
2299            
2300        ## Step 4..13        ## Step 4..13
2301        my $new_mode = {        my $new_mode = {
2302                        select => 'in select',                        select => IN_SELECT_IM,
2303                        td => 'in cell',                        td => IN_CELL_IM,
2304                        th => 'in cell',                        th => IN_CELL_IM,
2305                        tr => 'in row',                        tr => IN_ROW_IM,
2306                        tbody => 'in table body',                        tbody => IN_TABLE_BODY_IM,
2307                        thead => 'in table head',                        thead => IN_TABLE_BODY_IM,
2308                        tfoot => 'in table foot',                        tfoot => IN_TABLE_BODY_IM,
2309                        caption => 'in caption',                        caption => IN_CAPTION_IM,
2310                        colgroup => 'in column group',                        colgroup => IN_COLUMN_GROUP_IM,
2311                        table => 'in table',                        table => IN_TABLE_IM,
2312                        head => 'in body', # not in head!                        head => IN_BODY_IM, # not in head!
2313                        body => 'in body',                        body => IN_BODY_IM,
2314                        frameset => 'in frameset',                        frameset => IN_FRAMESET_IM,
2315                       }->{$node->[1]};                       }->{$node->[1]};
2316        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
2317                
2318        ## Step 14        ## Step 14
2319        if ($node->[1] eq 'html') {        if ($node->[1] eq 'html') {
2320          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
2321            $self->{insertion_mode} = 'before head';            $self->{insertion_mode} = BEFORE_HEAD_IM;
2322          } else {          } else {
2323            $self->{insertion_mode} = 'after head';            $self->{insertion_mode} = AFTER_HEAD_IM;
2324          }          }
2325          return;          return;
2326        }        }
2327                
2328        ## Step 15        ## Step 15
2329        $self->{insertion_mode} = 'in body' and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
2330                
2331        ## Step 16        ## Step 16
2332        $i--;        $i--;
# Line 2018  sub _reset_insertion_mode ($) { Line 2340  sub _reset_insertion_mode ($) {
2340  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
2341    my $self = shift;    my $self = shift;
2342    
   my $phase = 'main';  
   
2343    my $active_formatting_elements = [];    my $active_formatting_elements = [];
2344    
2345    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 2102  sub _tree_construction_main ($) { Line 2422  sub _tree_construction_main ($) {
2422      }      }
2423    }; # $clear_up_to_marker    }; # $clear_up_to_marker
2424    
2425    my $style_start_tag = sub {    my $parse_rcdata = sub ($$) {
2426      my $style_el; !!!create-element ($style_el, 'style', $token->{attributes});      my ($content_model_flag, $insert) = @_;
2427      ## $self->{insertion_mode} eq 'in head' and ... (always true)  
2428      (($self->{insertion_mode} eq 'in head' and defined $self->{head_element})      ## Step 1
2429       ? $self->{head_element} : $self->{open_elements}->[-1]->[0])      my $start_tag_name = $token->{tag_name};
2430        ->append_child ($style_el);      my $el;
2431      $self->{content_model_flag} = 'CDATA';      !!!create-element ($el, $start_tag_name, $token->{attributes});
2432    
2433        ## Step 2
2434        $insert->($el); # /context node/->append_child ($el)
2435    
2436        ## Step 3
2437        $self->{content_model} = $content_model_flag; # CDATA or RCDATA
2438      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
2439                  
2440        ## Step 4
2441      my $text = '';      my $text = '';
2442      !!!next-token;      !!!next-token;
2443      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
2444        $text .= $token->{data};        $text .= $token->{data};
2445        !!!next-token;        !!!next-token;
2446      } # stop if non-character token or tokenizer stops tokenising      }
2447    
2448        ## Step 5
2449      if (length $text) {      if (length $text) {
2450        $style_el->manakai_append_text ($text);        my $text = $self->{document}->create_text_node ($text);
2451          $el->append_child ($text);
2452      }      }
2453        
2454      $self->{content_model_flag} = 'PCDATA';      ## Step 6
2455                      $self->{content_model} = PCDATA_CONTENT_MODEL;
2456      if ($token->{type} eq 'end tag' and $token->{tag_name} eq 'style') {  
2457        ## Step 7
2458        if ($token->{type} == END_TAG_TOKEN and $token->{tag_name} eq $start_tag_name) {
2459        ## Ignore the token        ## Ignore the token
2460      } else {      } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {
2461        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type});
2462        ## ISSUE: And ignore?      } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
2463          !!!parse-error (type => 'in RCDATA:#'.$token->{type});
2464        } else {
2465          die "$0: $content_model_flag in parse_rcdata";
2466      }      }
2467      !!!next-token;      !!!next-token;
2468    }; # $style_start_tag    }; # $parse_rcdata
2469    
2470    my $script_start_tag = sub {    my $script_start_tag = sub ($) {
2471        my $insert = $_[0];
2472      my $script_el;      my $script_el;
2473      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes});
2474      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
2475    
2476      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
2477      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
2478            
2479      my $text = '';      my $text = '';
2480      !!!next-token;      !!!next-token;
2481      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
2482        $text .= $token->{data};        $text .= $token->{data};
2483        !!!next-token;        !!!next-token;
2484      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
# Line 2150  sub _tree_construction_main ($) { Line 2486  sub _tree_construction_main ($) {
2486        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
2487      }      }
2488                                
2489      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
2490    
2491      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
2492          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
2493        ## Ignore the token        ## Ignore the token
2494      } else {      } else {
# Line 2166  sub _tree_construction_main ($) { Line 2502  sub _tree_construction_main ($) {
2502      } else {      } else {
2503        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
2504        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
2505          
2506        (($self->{insertion_mode} eq 'in head' and defined $self->{head_element})        $insert->($script_el);
        ? $self->{head_element} : $self->{open_elements}->[-1]->[0])->append_child ($script_el);  
2507                
2508        ## TODO: insertion point = $old_insertion_point (might be "undefined")        ## TODO: insertion point = $old_insertion_point (might be "undefined")
2509                
# Line 2362  sub _tree_construction_main ($) { Line 2697  sub _tree_construction_main ($) {
2697    }; # $formatting_end_tag    }; # $formatting_end_tag
2698    
2699    my $insert_to_current = sub {    my $insert_to_current = sub {
2700      $self->{open_elements}->[-1]->[0]->append_child (shift);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
2701    }; # $insert_to_current    }; # $insert_to_current
2702    
2703    my $insert_to_foster = sub {    my $insert_to_foster = sub {
# Line 2396  sub _tree_construction_main ($) { Line 2731  sub _tree_construction_main ($) {
2731                         }                         }
2732    }; # $insert_to_foster    }; # $insert_to_foster
2733    
2734    my $in_body = sub {    my $insert;
     my $insert = shift;  
     if ($token->{type} eq 'start tag') {  
       if ($token->{tag_name} eq 'script') {  
         $script_start_tag->();  
         return;  
       } elsif ($token->{tag_name} eq 'style') {  
         $style_start_tag->();  
         return;  
       } elsif ({  
                 base => 1, link => 1, meta => 1,  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'in body:'.$token->{tag_name});  
         ## NOTE: This is an "as if in head" code clone  
         my $el;  
         !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
         if (defined $self->{head_element}) {  
           $self->{head_element}->append_child ($el);  
         } else {  
           $insert->($el);  
         }  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'title') {  
         !!!parse-error (type => 'in body:title');  
         ## NOTE: There is an "as if in head" code clone  
         my $title_el;  
         !!!create-element ($title_el, 'title', $token->{attributes});  
         (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])  
           ->append_child ($title_el);  
         $self->{content_model_flag} = 'RCDATA';  
         delete $self->{escape}; # MUST  
           
         my $text = '';  
         !!!next-token;  
         while ($token->{type} eq 'character') {  
           $text .= $token->{data};  
           !!!next-token;  
         }  
         if (length $text) {  
           $title_el->manakai_append_text ($text);  
         }  
           
         $self->{content_model_flag} = 'PCDATA';  
           
         if ($token->{type} eq 'end tag' and  
             $token->{tag_name} eq 'title') {  
           ## Ignore the token  
         } else {  
           !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
           ## ISSUE: And ignore?  
         }  
         !!!next-token;  
         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]);  
               ## TODO: test  
             }  
             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]);  
               ## TODO: test  
             }  
             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  
             
         ## 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') {  
             !!!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);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         $self->{content_model_flag} = 'CDATA';  
         delete $self->{escape}; # MUST  
           
         !!!next-token;  
         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';  
         }  
           
         $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};  
           $at->{name} = {name => 'name', value => 'isindex'};  
           my @tokens = (  
                         {type => 'start tag', tag_name => 'form'},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'start tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'label'},  
                         {type => 'character',  
                          data => 'This is a searchable index. Insert your search keywords here: '}, # SHOULD  
                         ## TODO: make this configurable  
                         {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 ({  
                 textarea => 1,  
                 iframe => 1,  
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         my $tag_name = $token->{tag_name};  
         my $el;  
         !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
           
         if ($token->{tag_name} eq 'textarea') {  
           ## TODO: $self->{form_element} if defined  
           $self->{content_model_flag} = 'RCDATA';  
         } else {  
           $self->{content_model_flag} = 'CDATA';  
         }  
         delete $self->{escape}; # MUST  
           
         $insert->($el);  
           
         my $text = '';  
         if ($token->{tag_name} eq 'textarea') {  
           !!!next-token;  
           if ($token->{type} eq 'character') {  
             $token->{data} =~ s/^\x0A//;  
             unless (length $token->{data}) {  
               !!!next-token;  
             }  
           }  
         } else {  
           !!!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 {  
           if ($token->{tag_name} eq 'textarea') {  
             !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
           } else {  
             !!!parse-error (type => 'in CDATA:#'.$token->{type});  
           }  
           ## ISSUE: And ignore?  
         }  
         !!!next-token;  
         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,  
                     }->{$_->[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,  
                 }->{$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}) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
           
         splice @{$self->{open_elements}}, $i if defined $i;  
         $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,  
                 }->{$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,  
                 }->{$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});  
 ## TODO: <http://html5.org/tools/web-apps-tracker?from=883&to=884>  
         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, br => 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,  
                 }->{$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 => 'not closed:'.$node->[1]);  
               ## 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  
2735    
2736    B: {    B: {
2737      if ($phase eq 'main') {      if ($token->{type} == DOCTYPE_TOKEN) {
2738        if ($token->{type} eq 'DOCTYPE') {        !!!parse-error (type => 'DOCTYPE in the middle');
2739          !!!parse-error (type => 'in html:#DOCTYPE');        ## Ignore the token
2740          ## Ignore the token        ## Stay in the phase
2741          ## Stay in the phase        !!!next-token;
2742          !!!next-token;        redo B;
2743          redo B;      } elsif ($token->{type} == END_OF_FILE_TOKEN) {
2744        } elsif ($token->{type} eq 'start tag' and        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
2745                 $token->{tag_name} eq 'html') {          #
2746          ## TODO: unless it is the first start tag token, parse-error        } else {
         my $top_el = $self->{open_elements}->[0]->[0];  
         for my $attr_name (keys %{$token->{attributes}}) {  
           unless ($top_el->has_attribute_ns (undef, $attr_name)) {  
             $top_el->set_attribute_ns  
               (undef, [undef, $attr_name],  
                $token->{attributes}->{$attr_name}->{value});  
           }  
         }  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'end-of-file') {  
2747          ## Generate implied end tags          ## Generate implied end tags
2748          if ({          if ({
2749               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,
2750                 tbody => 1, tfoot=> 1, thead => 1,
2751              }->{$self->{open_elements}->[-1]->[1]}) {              }->{$self->{open_elements}->[-1]->[1]}) {
2752            !!!back-token;            !!!back-token;
2753            $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};            $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};
2754            redo B;            redo B;
2755          }          }
2756                    
# Line 3259  sub _tree_construction_main ($) { Line 2763  sub _tree_construction_main ($) {
2763            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
2764          }          }
2765    
         ## Stop parsing  
         last B;  
   
2766          ## ISSUE: There is an issue in the spec.          ## ISSUE: There is an issue in the spec.
2767          }
2768    
2769          ## Stop parsing
2770          last B;
2771        } elsif ($token->{type} == START_TAG_TOKEN and
2772                 $token->{tag_name} eq 'html') {
2773          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
2774            ## Turn into the main phase
2775            !!!parse-error (type => 'after html:html');
2776            $self->{insertion_mode} = AFTER_BODY_IM;
2777          } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
2778            ## Turn into the main phase
2779            !!!parse-error (type => 'after html:html');
2780            $self->{insertion_mode} = AFTER_FRAMESET_IM;
2781          }
2782    
2783    ## ISSUE: "aa<html>" is not a parse error.
2784    ## ISSUE: "<html>" in fragment is not a parse error.
2785          unless ($token->{first_start_tag}) {
2786            !!!parse-error (type => 'not first start tag');
2787          }
2788          my $top_el = $self->{open_elements}->[0]->[0];
2789          for my $attr_name (keys %{$token->{attributes}}) {
2790            unless ($top_el->has_attribute_ns (undef, $attr_name)) {
2791              $top_el->set_attribute_ns
2792                (undef, [undef, $attr_name],
2793                 $token->{attributes}->{$attr_name}->{value});
2794            }
2795          }
2796          !!!next-token;
2797          redo B;
2798        } elsif ($token->{type} == COMMENT_TOKEN) {
2799          my $comment = $self->{document}->create_comment ($token->{data});
2800          if ($self->{insertion_mode} & AFTER_HTML_IMS) {
2801            $self->{document}->append_child ($comment);
2802          } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
2803            $self->{open_elements}->[0]->[0]->append_child ($comment);
2804        } else {        } else {
2805          if ($self->{insertion_mode} eq 'before head') {          $self->{open_elements}->[-1]->[0]->append_child ($comment);
2806            if ($token->{type} eq 'character') {        }
2807              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        !!!next-token;
2808                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);        redo B;
2809                unless (length $token->{data}) {      } elsif ($self->{insertion_mode} & HEAD_IMS) {
2810                  !!!next-token;        if ($token->{type} == CHARACTER_TOKEN) {
2811                  redo B;          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
2812                }            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
2813              }            unless (length $token->{data}) {
             ## As if <head>  
             !!!create-element ($self->{head_element}, 'head');  
             $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
             push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
             $self->{insertion_mode} = 'in head';  
             ## reprocess  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
2814              !!!next-token;              !!!next-token;
2815              redo B;              redo B;
2816            } elsif ($token->{type} eq 'start tag') {            }
2817              my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};          }
2818              !!!create-element ($self->{head_element}, 'head', $attr);  
2819              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2820              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            ## As if <head>
2821              $self->{insertion_mode} = 'in head';            !!!create-element ($self->{head_element}, 'head');
2822              if ($token->{tag_name} eq 'head') {            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2823                !!!next-token;            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2824              #} elsif ({  
2825              #          base => 1, link => 1, meta => 1,            ## Reprocess in the "in head" insertion mode...
2826              #          script => 1, style => 1, title => 1,            pop @{$self->{open_elements}};
2827              #         }->{$token->{tag_name}}) {  
2828              #  ## reprocess            ## Reprocess in the "after head" insertion mode...
2829              } else {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2830                ## reprocess            ## As if </noscript>
2831              }            pop @{$self->{open_elements}};
2832              !!!parse-error (type => 'in noscript:#character');
2833              
2834              ## Reprocess in the "in head" insertion mode...
2835              ## As if </head>
2836              pop @{$self->{open_elements}};
2837    
2838              ## Reprocess in the "after head" insertion mode...
2839            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2840              pop @{$self->{open_elements}};
2841    
2842              ## Reprocess in the "after head" insertion mode...
2843            }
2844    
2845                ## "after head" insertion mode
2846                ## As if <body>
2847                !!!insert-element ('body');
2848                $self->{insertion_mode} = IN_BODY_IM;
2849                ## reprocess
2850              redo B;              redo B;
2851            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
2852              if ($token->{tag_name} eq 'html') {              if ($token->{tag_name} eq 'head') {
2853                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2854                    !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
2855                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2856                    push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
2857                    $self->{insertion_mode} = IN_HEAD_IM;
2858                    !!!next-token;
2859                    redo B;
2860                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2861                    #
2862                  } else {
2863                    !!!parse-error (type => 'in head:head'); # or in head noscript
2864                    ## Ignore the token
2865                    !!!next-token;
2866                    redo B;
2867                  }
2868                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2869                ## As if <head>                ## As if <head>
2870                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head');
2871                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2872                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2873                $self->{insertion_mode} = 'in head';  
2874                ## reprocess                $self->{insertion_mode} = IN_HEAD_IM;
2875                redo B;                ## Reprocess in the "in head" insertion mode...
             } else {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
2876              }              }
2877            } else {  
2878              die "$0: $token->{type}: Unknown type";              if ($token->{tag_name} eq 'base') {
2879            }                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2880          } elsif ($self->{insertion_mode} eq 'in head') {                  ## As if </noscript>
2881            if ($token->{type} eq 'character') {                  pop @{$self->{open_elements}};
2882              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                  !!!parse-error (type => 'in noscript:base');
2883                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                
2884                unless (length $token->{data}) {                  $self->{insertion_mode} = IN_HEAD_IM;
2885                  !!!next-token;                  ## Reprocess in the "in head" insertion mode...
                 redo B;  
2886                }                }
             }  
               
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'title') {  
               ## NOTE: There is an "as if in head" code clone  
               my $title_el;  
               !!!create-element ($title_el, 'title', $token->{attributes});  
               (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])  
                 ->append_child ($title_el);  
               $self->{content_model_flag} = 'RCDATA';  
               delete $self->{escape}; # MUST  
2887    
2888                my $text = '';                ## NOTE: There is a "as if in head" code clone.
2889                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2890                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2891                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2892                  }
2893                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2894                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2895                  pop @{$self->{open_elements}}
2896                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2897                !!!next-token;                !!!next-token;
2898                while ($token->{type} eq 'character') {                redo B;
2899                  $text .= $token->{data};              } elsif ($token->{tag_name} eq 'link') {
2900                  !!!next-token;                ## NOTE: There is a "as if in head" code clone.
2901                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2902                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2903                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2904                }                }
2905                if (length $text) {                !!!insert-element ($token->{tag_name}, $token->{attributes});
2906                  $title_el->manakai_append_text ($text);                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2907                  pop @{$self->{open_elements}}
2908                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2909                  !!!next-token;
2910                  redo B;
2911                } elsif ($token->{tag_name} eq 'meta') {
2912                  ## NOTE: There is a "as if in head" code clone.
2913                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2914                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2915                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2916                }                }
2917                                !!!insert-element ($token->{tag_name}, $token->{attributes});
2918                $self->{content_model_flag} = 'PCDATA';                my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2919                  
2920                if ($token->{type} eq 'end tag' and                unless ($self->{confident}) {
2921                    $token->{tag_name} eq 'title') {                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
2922                  ## Ignore the token                    $self->{change_encoding}
2923                          ->($self, $token->{attributes}->{charset}->{value});
2924                      
2925                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
2926                          ->set_user_data (manakai_has_reference =>
2927                                               $token->{attributes}->{charset}
2928                                                   ->{has_reference});
2929                    } elsif ($token->{attributes}->{content}) {
2930                      ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
2931                      if ($token->{attributes}->{content}->{value}
2932                          =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
2933                              [\x09-\x0D\x20]*=
2934                              [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
2935                              ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
2936                        $self->{change_encoding}
2937                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
2938                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
2939                            ->set_user_data (manakai_has_reference =>
2940                                                 $token->{attributes}->{content}
2941                                                       ->{has_reference});
2942                      }
2943                    }
2944                } else {                } else {
2945                  !!!parse-error (type => 'in RCDATA:#'.$token->{type});                  if ($token->{attributes}->{charset}) {
2946                  ## ISSUE: And ignore?                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
2947                          ->set_user_data (manakai_has_reference =>
2948                                               $token->{attributes}->{charset}
2949                                                   ->{has_reference});
2950                    }
2951                    if ($token->{attributes}->{content}) {
2952                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
2953                          ->set_user_data (manakai_has_reference =>
2954                                               $token->{attributes}->{content}
2955                                                   ->{has_reference});
2956                    }
2957                }                }
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'style') {  
               $style_start_tag->();  
               redo B;  
             } elsif ($token->{tag_name} eq 'script') {  
               $script_start_tag->();  
               redo B;  
             } elsif ({base => 1, link => 1, meta => 1}->{$token->{tag_name}}) {  
               ## NOTE: There are "as if in head" code clones  
               my $el;  
               !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
               (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])  
                 ->append_child ($el);  
2958    
2959                  pop @{$self->{open_elements}}
2960                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2961                !!!next-token;                !!!next-token;
2962                redo B;                redo B;
2963              } elsif ($token->{tag_name} eq 'head') {              } elsif ($token->{tag_name} eq 'title') {
2964                !!!parse-error (type => 'in head:head');                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2965                ## Ignore the token                  ## As if </noscript>
2966                !!!next-token;                  pop @{$self->{open_elements}};
2967                    !!!parse-error (type => 'in noscript:title');
2968                  
2969                    $self->{insertion_mode} = IN_HEAD_IM;
2970                    ## Reprocess in the "in head" insertion mode...
2971                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2972                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2973                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2974                  }
2975    
2976                  ## NOTE: There is a "as if in head" code clone.
2977                  my $parent = defined $self->{head_element} ? $self->{head_element}
2978                      : $self->{open_elements}->[-1]->[0];
2979                  $parse_rcdata->(RCDATA_CONTENT_MODEL,
2980                                  sub { $parent->append_child ($_[0]) });
2981                  pop @{$self->{open_elements}}
2982                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2983                redo B;                redo B;
2984              } else {              } elsif ($token->{tag_name} eq 'style') {
2985                #                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
2986              }                ## insertion mode IN_HEAD_IM)
2987            } elsif ($token->{type} eq 'end tag') {                ## NOTE: There is a "as if in head" code clone.
2988              if ($token->{tag_name} eq 'head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2989                if ($self->{open_elements}->[-1]->[1] eq 'head') {                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2990                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2991                  }
2992                  $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
2993                  pop @{$self->{open_elements}}
2994                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2995                  redo B;
2996                } elsif ($token->{tag_name} eq 'noscript') {
2997                  if ($self->{insertion_mode} == IN_HEAD_IM) {
2998                    ## NOTE: and scripting is disalbed
2999                    !!!insert-element ($token->{tag_name}, $token->{attributes});
3000                    $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
3001                    !!!next-token;
3002                    redo B;
3003                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3004                    !!!parse-error (type => 'in noscript:noscript');
3005                    ## Ignore the token
3006                    !!!next-token;
3007                    redo B;
3008                  } else {
3009                    #
3010                  }
3011                } elsif ($token->{tag_name} eq 'script') {
3012                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3013                    ## As if </noscript>
3014                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3015                    !!!parse-error (type => 'in noscript:script');
3016                  
3017                    $self->{insertion_mode} = IN_HEAD_IM;
3018                    ## Reprocess in the "in head" insertion mode...
3019                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3020                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3021                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3022                  }
3023    
3024                  ## NOTE: There is a "as if in head" code clone.
3025                  $script_start_tag->($insert_to_current);
3026                  pop @{$self->{open_elements}}
3027                      if $self->{insertion_mode} == AFTER_HEAD_IM;
3028                  redo B;
3029                } elsif ($token->{tag_name} eq 'body' or
3030                         $token->{tag_name} eq 'frameset') {
3031                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3032                    ## As if </noscript>
3033                    pop @{$self->{open_elements}};
3034                    !!!parse-error (type => 'in noscript:'.$token->{tag_name});
3035                    
3036                    ## Reprocess in the "in head" insertion mode...
3037                    ## As if </head>
3038                    pop @{$self->{open_elements}};
3039                    
3040                    ## Reprocess in the "after head" insertion mode...
3041                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3042                    pop @{$self->{open_elements}};
3043                    
3044                    ## Reprocess in the "after head" insertion mode...
3045                  }
3046    
3047                  ## "after head" insertion mode
3048                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3049                  if ($token->{tag_name} eq 'body') {
3050                    $self->{insertion_mode} = IN_BODY_IM;
3051                  } elsif ($token->{tag_name} eq 'frameset') {
3052                    $self->{insertion_mode} = IN_FRAMESET_IM;
3053                } else {                } else {
3054                  !!!parse-error (type => 'unmatched end tag:head');                  die "$0: tag name: $self->{tag_name}";
3055                }                }
               $self->{insertion_mode} = 'after head';  
3056                !!!next-token;                !!!next-token;
3057                redo B;                redo B;
             } elsif ($token->{tag_name} eq 'html') {  
               #  
3058              } else {              } else {
3059                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                #
               ## Ignore the token  
               !!!next-token;  
               redo B;  
3060              }              }
           } else {  
             #  
           }  
3061    
3062            if ($self->{open_elements}->[-1]->[1] eq 'head') {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3063              ## As if </head>                ## As if </noscript>
3064              pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3065            }                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
3066            $self->{insertion_mode} = 'after head';                
3067            ## reprocess                ## Reprocess in the "in head" insertion mode...
3068            redo B;                ## As if </head>
3069                  pop @{$self->{open_elements}};
3070    
3071            ## ISSUE: An issue in the spec.                ## Reprocess in the "after head" insertion mode...
3072          } elsif ($self->{insertion_mode} eq 'after head') {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3073            if ($token->{type} eq 'character') {                ## As if </head>
3074              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                pop @{$self->{open_elements}};
3075                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
3076                unless (length $token->{data}) {                ## Reprocess in the "after head" insertion mode...
                 !!!next-token;  
                 redo B;  
               }  
3077              }              }
               
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'body') {  
               !!!insert-element ('body', $token->{attributes});  
               $self->{insertion_mode} = 'in body';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'frameset') {  
               !!!insert-element ('frameset', $token->{attributes});  
               $self->{insertion_mode} = 'in frameset';  
               !!!next-token;  
               redo B;  
             } elsif ({  
                       base => 1, link => 1, meta => 1,  
                       script => 1, style => 1, title => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'after head:'.$token->{tag_name});  
               $self->{insertion_mode} = 'in head';  
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           ## As if <body>  
           !!!insert-element ('body');  
           $self->{insertion_mode} = 'in body';  
           ## reprocess  
           redo B;  
         } 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});  
3078    
3079              !!!next-token;              ## "after head" insertion mode
3080              redo B;              ## As if <body>
3081            } elsif ($token->{type} eq 'comment') {              !!!insert-element ('body');
3082              ## NOTE: There is a code clone of "comment in body".              $self->{insertion_mode} = IN_BODY_IM;
3083              my $comment = $self->{document}->create_comment ($token->{data});              ## reprocess
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } else {  
             $in_body->($insert_to_current);  
3084              redo B;              redo B;
3085            }            } elsif ($token->{type} == END_TAG_TOKEN) {
3086          } elsif ($self->{insertion_mode} eq 'in table') {              if ($token->{tag_name} eq 'head') {
3087            if ($token->{type} eq 'character') {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3088              ## NOTE: There are "character in table" code clones.                  ## As if <head>
3089              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                  !!!create-element ($self->{head_element}, 'head');
3090                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3091                                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3092                unless (length $token->{data}) {  
3093                    ## Reprocess in the "in head" insertion mode...
3094                    pop @{$self->{open_elements}};
3095                    $self->{insertion_mode} = AFTER_HEAD_IM;
3096                    !!!next-token;
3097                    redo B;
3098                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3099                    ## As if </noscript>
3100                    pop @{$self->{open_elements}};
3101                    !!!parse-error (type => 'in noscript:script');
3102                    
3103                    ## Reprocess in the "in head" insertion mode...
3104                    pop @{$self->{open_elements}};
3105                    $self->{insertion_mode} = AFTER_HEAD_IM;
3106                    !!!next-token;
3107                    redo B;
3108                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3109                    pop @{$self->{open_elements}};
3110                    $self->{insertion_mode} = AFTER_HEAD_IM;
3111                  !!!next-token;                  !!!next-token;
3112                  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});  
3113                } else {                } else {
3114                  $foster_parent_element->insert_before                  #
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
3115                }                }
3116              } else {              } elsif ($token->{tag_name} eq 'noscript') {
3117                $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 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!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]);  
3118                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3119                    $self->{insertion_mode} = IN_HEAD_IM;
3120                    !!!next-token;
3121                    redo B;
3122                  } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3123                    !!!parse-error (type => 'unmatched end tag:noscript');
3124                    ## Ignore the token ## ISSUE: An issue in the spec.
3125                    !!!next-token;
3126                    redo B;
3127                  } else {
3128                    #
3129                }                }
   
               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;  
3130              } elsif ({              } elsif ({
3131                        col => 1,                        body => 1, html => 1,
                       td => 1, th => 1, tr => 1,  
3132                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3133                ## Clear back to table context                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3134                while ($self->{open_elements}->[-1]->[1] ne 'table' and                  ## As if <head>
3135                       $self->{open_elements}->[-1]->[1] ne 'html') {                  !!!create-element ($self->{head_element}, 'head');
3136                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3137                  pop @{$self->{open_elements}};                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3138                }  
3139                    $self->{insertion_mode} = IN_HEAD_IM;
3140                !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');                  ## Reprocess in the "in head" insertion mode...
3141                $self->{insertion_mode} = $token->{tag_name} eq 'col'                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3142                  ? 'in column group' : 'in table body';                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3143                ## reprocess                  ## Ignore the token
               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]);  
   
               ## As if </table>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 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>  
3144                  !!!next-token;                  !!!next-token;
3145                  redo B;                  redo B;
3146                }                }
3147                                
3148                ## generate implied end tags                #
3149                if ({              } elsif ({
3150                     dd => 1, dt => 1, li => 1, p => 1,                        p => 1, br => 1,
3151                     td => 1, th => 1, tr => 1,                       }->{$token->{tag_name}}) {
3152                    }->{$self->{open_elements}->[-1]->[1]}) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3153                  !!!back-token; # <table>                  ## As if <head>
3154                  $token = {type => 'end tag', tag_name => 'table'};                  !!!create-element ($self->{head_element}, 'head');
3155                  !!!back-token;                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3156                  $token = {type => 'end tag',                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
3157    
3158                if ($self->{open_elements}->[-1]->[1] ne 'table') {                  $self->{insertion_mode} = IN_HEAD_IM;
3159                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  ## Reprocess in the "in head" insertion mode...
3160                }                }
3161    
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
3162                #                #
3163              }              } else {
3164            } elsif ($token->{type} eq 'end tag') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3165              if ($token->{tag_name} eq 'table') {                  #
3166                ## 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) {  
3167                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3168                  ## Ignore the token                  ## Ignore the token
3169                  !!!next-token;                  !!!next-token;
3170                  redo B;                  redo B;
3171                }                }
3172                              }
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 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]);  
               }  
3173    
3174                splice @{$self->{open_elements}}, $i;              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3175                  ## As if </noscript>
3176                  pop @{$self->{open_elements}};
3177                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
3178                  
3179                  ## Reprocess in the "in head" insertion mode...
3180                  ## As if </head>
3181                  pop @{$self->{open_elements}};
3182    
3183                $self->_reset_insertion_mode;                ## Reprocess in the "after head" insertion mode...
3184                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3185                  ## As if </head>
3186                  pop @{$self->{open_elements}};
3187    
3188                !!!next-token;                ## Reprocess in the "after head" insertion mode...
3189                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}}) {  
3190                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3191                ## Ignore the token                ## Ignore the token ## ISSUE: An issue in the spec.
3192                !!!next-token;                !!!next-token;
3193                redo B;                redo B;
             } else {  
               #  
3194              }              }
3195    
3196                ## "after head" insertion mode
3197                ## As if <body>
3198                !!!insert-element ('body');
3199                $self->{insertion_mode} = IN_BODY_IM;
3200                ## reprocess
3201                redo B;
3202            } else {            } else {
3203              #              die "$0: $token->{type}: Unknown token type";
3204            }            }
3205    
3206            !!!parse-error (type => 'in table:'.$token->{tag_name});            ## ISSUE: An issue in the spec.
3207            $in_body->($insert_to_foster);      } elsif ($self->{insertion_mode} & BODY_IMS) {
3208            redo B;            if ($token->{type} == CHARACTER_TOKEN) {
3209          } 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".  
3210              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
3211                            
3212              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3213    
3214              !!!next-token;              !!!next-token;
3215              redo B;              redo B;
3216            } elsif ($token->{type} eq 'comment') {            } elsif ($token->{type} == START_TAG_TOKEN) {
             ## NOTE: This is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
3217              if ({              if ({
3218                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
3219                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
3220                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3221                !!!parse-error (type => 'not closed:caption');                if ($self->{insertion_mode} == IN_CELL_IM) {
3222                    ## have an element in table scope
3223                    my $tn;
3224                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3225                      my $node = $self->{open_elements}->[$_];
3226                      if ($node->[1] eq 'td' or $node->[1] eq 'th') {
3227                        $tn = $node->[1];
3228                        last INSCOPE;
3229                      } elsif ({
3230                                table => 1, html => 1,
3231                               }->{$node->[1]}) {
3232                        last INSCOPE;
3233                      }
3234                    } # INSCOPE
3235                      unless (defined $tn) {
3236                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3237                        ## Ignore the token
3238                        !!!next-token;
3239                        redo B;
3240                      }
3241                    
3242                    ## Close the cell
3243                    !!!back-token; # <?>
3244                    $token = {type => END_TAG_TOKEN, tag_name => $tn};
3245                    redo B;
3246                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3247                    !!!parse-error (type => 'not closed:caption');
3248                    
3249                    ## As if </caption>
3250                    ## have a table element in table scope
3251                    my $i;
3252                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3253                      my $node = $self->{open_elements}->[$_];
3254                      if ($node->[1] eq 'caption') {
3255                        $i = $_;
3256                        last INSCOPE;
3257                      } elsif ({
3258                                table => 1, html => 1,
3259                               }->{$node->[1]}) {
3260                        last INSCOPE;
3261                      }
3262                    } # INSCOPE
3263                      unless (defined $i) {
3264                        !!!parse-error (type => 'unmatched end tag:caption');
3265                        ## Ignore the token
3266                        !!!next-token;
3267                        redo B;
3268                      }
3269                    
3270                    ## generate implied end tags
3271                    if ({
3272                         dd => 1, dt => 1, li => 1, p => 1,
3273                         td => 1, th => 1, tr => 1,
3274                         tbody => 1, tfoot=> 1, thead => 1,
3275                        }->{$self->{open_elements}->[-1]->[1]}) {
3276                      !!!back-token; # <?>
3277                      $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3278                      !!!back-token;
3279                      $token = {type => END_TAG_TOKEN,
3280                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3281                      redo B;
3282                    }
3283    
3284                ## As if </caption>                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3285                ## 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;  
3286                  }                  }
3287                } # INSCOPE                  
3288                unless (defined $i) {                  splice @{$self->{open_elements}}, $i;
3289                  !!!parse-error (type => 'unmatched end tag:caption');                  
3290                    $clear_up_to_marker->();
3291                    
3292                    $self->{insertion_mode} = IN_TABLE_IM;
3293                    
3294                    ## reprocess
3295                    redo B;
3296                  } else {
3297                    #
3298                  }
3299                } else {
3300                  #
3301                }
3302              } elsif ($token->{type} == END_TAG_TOKEN) {
3303                if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
3304                  if ($self->{insertion_mode} == IN_CELL_IM) {
3305                    ## have an element in table scope
3306                    my $i;
3307                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3308                      my $node = $self->{open_elements}->[$_];
3309                      if ($node->[1] eq $token->{tag_name}) {
3310                        $i = $_;
3311                        last INSCOPE;
3312                      } elsif ({
3313                                table => 1, html => 1,
3314                               }->{$node->[1]}) {
3315                        last INSCOPE;
3316                      }
3317                    } # INSCOPE
3318                      unless (defined $i) {
3319                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3320                        ## Ignore the token
3321                        !!!next-token;
3322                        redo B;
3323                      }
3324                    
3325                    ## generate implied end tags
3326                    if ({
3327                         dd => 1, dt => 1, li => 1, p => 1,
3328                         td => ($token->{tag_name} eq 'th'),
3329                         th => ($token->{tag_name} eq 'td'),
3330                         tr => 1,
3331                         tbody => 1, tfoot=> 1, thead => 1,
3332                        }->{$self->{open_elements}->[-1]->[1]}) {
3333                      !!!back-token;
3334                      $token = {type => END_TAG_TOKEN,
3335                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3336                      redo B;
3337                    }
3338                    
3339                    if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
3340                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3341                    }
3342                    
3343                    splice @{$self->{open_elements}}, $i;
3344                    
3345                    $clear_up_to_marker->();
3346                    
3347                    $self->{insertion_mode} = IN_ROW_IM;
3348                    
3349                    !!!next-token;
3350                    redo B;
3351                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3352                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3353                  ## Ignore the token                  ## Ignore the token
3354                  !!!next-token;                  !!!next-token;
3355                  redo B;                  redo B;
3356                  } else {
3357                    #
3358                }                }
3359                              } elsif ($token->{tag_name} eq 'caption') {
3360                ## generate implied end tags                if ($self->{insertion_mode} == IN_CAPTION_IM) {
3361                if ({                  ## have a table element in table scope
3362                     dd => 1, dt => 1, li => 1, p => 1,                  my $i;
3363                     td => 1, th => 1, tr => 1,                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3364                    }->{$self->{open_elements}->[-1]->[1]}) {                    my $node = $self->{open_elements}->[$_];
3365                  !!!back-token; # <?>                    if ($node->[1] eq $token->{tag_name}) {
3366                  $token = {type => 'end tag', tag_name => 'caption'};                      $i = $_;
3367                  !!!back-token;                      last INSCOPE;
3368                  $token = {type => 'end tag',                    } elsif ({
3369                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                              table => 1, html => 1,
3370                               }->{$node->[1]}) {
3371                        last INSCOPE;
3372                      }
3373                    } # INSCOPE
3374                      unless (defined $i) {
3375                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3376                        ## Ignore the token
3377                        !!!next-token;
3378                        redo B;
3379                      }
3380                    
3381                    ## generate implied end tags
3382                    if ({
3383                         dd => 1, dt => 1, li => 1, p => 1,
3384                         td => 1, th => 1, tr => 1,
3385                         tbody => 1, tfoot=> 1, thead => 1,
3386                        }->{$self->{open_elements}->[-1]->[1]}) {
3387                      !!!back-token;
3388                      $token = {type => END_TAG_TOKEN,
3389                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3390                      redo B;
3391                    }
3392                    
3393                    if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3394                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3395                    }
3396                    
3397                    splice @{$self->{open_elements}}, $i;
3398                    
3399                    $clear_up_to_marker->();
3400                    
3401                    $self->{insertion_mode} = IN_TABLE_IM;
3402                    
3403                    !!!next-token;
3404                  redo B;                  redo B;
3405                  } elsif ($self->{insertion_mode} == IN_CELL_IM) {
3406                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3407                    ## Ignore the token
3408                    !!!next-token;
3409                    redo B;
3410                  } else {
3411                    #
3412                }                }
3413                } elsif ({
3414                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                        table => 1, tbody => 1, tfoot => 1,
3415                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                        thead => 1, tr => 1,
3416                }                       }->{$token->{tag_name}} and
3417                         $self->{insertion_mode} == IN_CELL_IM) {
3418                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  
3419                my $i;                my $i;
3420                  my $tn;
3421                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3422                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3423                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
3424                    $i = $_;                    $i = $_;
3425                    last INSCOPE;                    last INSCOPE;
3426                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {
3427                      $tn = $node->[1];
3428                      ## NOTE: There is exactly one |td| or |th| element
3429                      ## in scope in the stack of open elements by definition.
3430                  } elsif ({                  } elsif ({
3431                            table => 1, html => 1,                            table => 1, html => 1,
3432                           }->{$node->[1]}) {                           }->{$node->[1]}) {
# Line 3798  sub _tree_construction_main ($) { Line 3439  sub _tree_construction_main ($) {
3439                  !!!next-token;                  !!!next-token;
3440                  redo B;                  redo B;
3441                }                }
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
3442    
3443                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                ## Close the cell
3444                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!back-token; # </?>
3445                }                $token = {type => END_TAG_TOKEN, tag_name => $tn};
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in table';  
   
               !!!next-token;  
3446                redo B;                redo B;
3447              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table' and
3448                         $self->{insertion_mode} == IN_CAPTION_IM) {
3449                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption');
3450    
3451                ## As if </caption>                ## As if </caption>
# Line 3850  sub _tree_construction_main ($) { Line 3473  sub _tree_construction_main ($) {
3473                if ({                if ({
3474                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3475                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3476                       tbody => 1, tfoot=> 1, thead => 1,
3477                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3478                  !!!back-token; # </table>                  !!!back-token; # </table>
3479                  $token = {type => 'end tag', tag_name => 'caption'};                  $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3480                  !!!back-token;                  !!!back-token;
3481                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3482                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3483                  redo B;                  redo B;
3484                }                }
# Line 3867  sub _tree_construction_main ($) { Line 3491  sub _tree_construction_main ($) {
3491    
3492                $clear_up_to_marker->();                $clear_up_to_marker->();
3493    
3494                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
3495    
3496                ## reprocess                ## reprocess
3497                redo B;                redo B;
3498              } elsif ({              } elsif ({
3499                        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,  
3500                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3501                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
3502                ## 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 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!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');  
3503                  ## Ignore the token                  ## Ignore the token
3504                  !!!next-token;                  !!!next-token;
3505                  redo B;                  redo B;
3506                } else {                } else {
3507                  pop @{$self->{open_elements}}; # colgroup                  #
                 $self->{insertion_mode} = 'in table';  
                 !!!next-token;  
                 redo B;              
3508                }                }
3509              } elsif ($token->{tag_name} eq 'col') {              } elsif ({
3510                !!!parse-error (type => 'unmatched end tag:col');                        tbody => 1, tfoot => 1,
3511                          thead => 1, tr => 1,
3512                         }->{$token->{tag_name}} and
3513                         $self->{insertion_mode} == IN_CAPTION_IM) {
3514                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3515                ## Ignore the token                ## Ignore the token
3516                !!!next-token;                !!!next-token;
3517                redo B;                redo B;
3518              } else {              } else {
3519                #                #
3520              }              }
3521            } else {        } else {
3522              #          die "$0: $token->{type}: Unknown token type";
3523            }        }
3524    
3525            ## As if </colgroup>        $insert = $insert_to_current;
3526            if ($self->{open_elements}->[-1]->[1] eq 'html') {        #
3527              !!!parse-error (type => 'unmatched end tag:colgroup');      } elsif ($self->{insertion_mode} & TABLE_IMS) {
3528              ## 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.  
3529              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3530                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3531                                
# Line 3969  sub _tree_construction_main ($) { Line 3542  sub _tree_construction_main ($) {
3542              ## into the current node" while characters might not be              ## into the current node" while characters might not be
3543              ## result in a new Text node.              ## result in a new Text node.
3544              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
3545                
3546              if ({              if ({
3547                   table => 1, tbody => 1, tfoot => 1,                   table => 1, tbody => 1, tfoot => 1,
3548                   thead => 1, tr => 1,                   thead => 1, tr => 1,
# Line 4009  sub _tree_construction_main ($) { Line 3582  sub _tree_construction_main ($) {
3582                            
3583              !!!next-token;              !!!next-token;
3584              redo B;              redo B;
3585            } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == START_TAG_TOKEN) {
             ## Copied from 'in table'  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
3586              if ({              if ({
3587                   tr => 1,                   tr => ($self->{insertion_mode} != IN_ROW_IM),
3588                   th => 1, td => 1,                   th => 1, td => 1,
3589                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3590                unless ($token->{tag_name} eq 'tr') {                if ($self->{insertion_mode} == IN_TABLE_IM) {
3591                  !!!parse-error (type => 'missing start tag:tr');                  ## Clear back to table context
3592                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3593                           $self->{open_elements}->[-1]->[1] ne 'html') {
3594                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3595                      pop @{$self->{open_elements}};
3596                    }
3597                    
3598                    !!!insert-element ('tbody');
3599                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3600                    ## reprocess in the "in table body" insertion mode...
3601                }                }
3602    
3603                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3604                    unless ($token->{tag_name} eq 'tr') {
3605                      !!!parse-error (type => 'missing start tag:tr');
3606                    }
3607                    
3608                    ## Clear back to table body context
3609                    while (not {
3610                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3611                    }->{$self->{open_elements}->[-1]->[1]}) {
3612                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3613                      pop @{$self->{open_elements}};
3614                    }
3615                    
3616                    $self->{insertion_mode} = IN_ROW_IM;
3617                    if ($token->{tag_name} eq 'tr') {
3618                      !!!insert-element ($token->{tag_name}, $token->{attributes});
3619                      !!!next-token;
3620                      redo B;
3621                    } else {
3622                      !!!insert-element ('tr');
3623                      ## reprocess in the "in row" insertion mode
3624                    }
3625                  }
3626    
3627                  ## Clear back to table row context
3628                while (not {                while (not {
3629                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tr => 1, html => 1,
3630                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
3631                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3632                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3633                }                }
3634                                
3635                $self->{insertion_mode} = 'in row';                !!!insert-element ($token->{tag_name}, $token->{attributes});
3636                if ($token->{tag_name} eq 'tr') {                $self->{insertion_mode} = IN_CELL_IM;
3637                  !!!insert-element ($token->{tag_name}, $token->{attributes});  
3638                  !!!next-token;                push @$active_formatting_elements, ['#marker', ''];
3639                } else {                
3640                  !!!insert-element ('tr');                !!!next-token;
                 ## reprocess  
               }  
3641                redo B;                redo B;
3642              } elsif ({              } elsif ({
3643                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
3644                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
3645                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3646                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3647                ## have an element in table scope                if ($self->{insertion_mode} == IN_ROW_IM) {
3648                my $i;                  ## As if </tr>
3649                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
3650                  my $node = $self->{open_elements}->[$_];                  my $i;
3651                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3652                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
3653                      }->{$node->[1]}) {                    if ($node->[1] eq 'tr') {
3654                    $i = $_;                      $i = $_;
3655                    last INSCOPE;                      last INSCOPE;
3656                  } elsif ({                    } elsif ({
3657                            table => 1, html => 1,                              table => 1, html => 1,
3658                           }->{$node->[1]}) {                             }->{$node->[1]}) {
3659                    last INSCOPE;                      last INSCOPE;
3660                      }
3661                    } # INSCOPE
3662                    unless (defined $i) {
3663                      !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});
3664                      ## Ignore the token
3665                      !!!next-token;
3666                      redo B;
3667                    }
3668                    
3669                    ## Clear back to table row context
3670                    while (not {
3671                      tr => 1, html => 1,
3672                    }->{$self->{open_elements}->[-1]->[1]}) {
3673                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3674                      pop @{$self->{open_elements}};
3675                    }
3676                    
3677                    pop @{$self->{open_elements}}; # tr
3678                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3679                    if ($token->{tag_name} eq 'tr') {
3680                      ## reprocess
3681                      redo B;
3682                    } else {
3683                      ## reprocess in the "in table body" insertion mode...
3684                  }                  }
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
3685                }                }
3686    
3687                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3688                while (not {                  ## have an element in table scope
3689                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  my $i;
3690                }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3691                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    my $node = $self->{open_elements}->[$_];
3692                      if ({
3693                           tbody => 1, thead => 1, tfoot => 1,
3694                          }->{$node->[1]}) {
3695                        $i = $_;
3696                        last INSCOPE;
3697                      } elsif ({
3698                                table => 1, html => 1,
3699                               }->{$node->[1]}) {
3700                        last INSCOPE;
3701                      }
3702                    } # INSCOPE
3703                    unless (defined $i) {
3704                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3705                      ## Ignore the token
3706                      !!!next-token;
3707                      redo B;
3708                    }
3709    
3710                    ## Clear back to table body context
3711                    while (not {
3712                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3713                    }->{$self->{open_elements}->[-1]->[1]}) {
3714                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3715                      pop @{$self->{open_elements}};
3716                    }
3717                    
3718                    ## As if <{current node}>
3719                    ## have an element in table scope
3720                    ## true by definition
3721                    
3722                    ## Clear back to table body context
3723                    ## nop by definition
3724                    
3725                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3726                    $self->{insertion_mode} = IN_TABLE_IM;
3727                    ## reprocess in "in table" insertion mode...
3728                }                }
3729    
3730                ## As if <{current node}>                if ($token->{tag_name} eq 'col') {
3731                ## have an element in table scope                  ## Clear back to table context
3732                ## true by definition                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
3733                           $self->{open_elements}->[-1]->[1] ne 'html') {
3734                ## Clear back to table body context                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3735                ## nop by definition                    pop @{$self->{open_elements}};
3736                    }
3737                pop @{$self->{open_elements}};                  
3738                $self->{insertion_mode} = 'in table';                  !!!insert-element ('colgroup');
3739                ## reprocess                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
3740                redo B;                  ## reprocess
3741                    redo B;
3742                  } elsif ({
3743                            caption => 1,
3744                            colgroup => 1,
3745                            tbody => 1, tfoot => 1, thead => 1,
3746                           }->{$token->{tag_name}}) {
3747                    ## Clear back to table context
3748                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3749                           $self->{open_elements}->[-1]->[1] ne 'html') {
3750                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3751                      pop @{$self->{open_elements}};
3752                    }
3753                    
3754                    push @$active_formatting_elements, ['#marker', '']
3755                        if $token->{tag_name} eq 'caption';
3756                    
3757                    !!!insert-element ($token->{tag_name}, $token->{attributes});
3758                    $self->{insertion_mode} = {
3759                                               caption => IN_CAPTION_IM,
3760                                               colgroup => IN_COLUMN_GROUP_IM,
3761                                               tbody => IN_TABLE_BODY_IM,
3762                                               tfoot => IN_TABLE_BODY_IM,
3763                                               thead => IN_TABLE_BODY_IM,
3764                                              }->{$token->{tag_name}};
3765                    !!!next-token;
3766                    redo B;
3767                  } else {
3768                    die "$0: in table: <>: $token->{tag_name}";
3769                  }
3770              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
3771                ## 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');  
3772    
3773                ## As if </table>                ## As if </table>
3774                ## have a table element in table scope                ## have a table element in table scope
# Line 4115  sub _tree_construction_main ($) { Line 3795  sub _tree_construction_main ($) {
3795                if ({                if ({
3796                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3797                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3798                       tbody => 1, tfoot=> 1, thead => 1,
3799                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3800                  !!!back-token; # <table>                  !!!back-token; # <table>
3801                  $token = {type => 'end tag', tag_name => 'table'};                  $token = {type => END_TAG_TOKEN, tag_name => 'table'};
3802                  !!!back-token;                  !!!back-token;
3803                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3804                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3805                  redo B;                  redo B;
3806                }                }
# Line 4130  sub _tree_construction_main ($) { Line 3811  sub _tree_construction_main ($) {
3811    
3812                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
3813    
3814                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
3815    
3816                ## reprocess                ## reprocess
3817                redo B;                redo B;
3818              } else {          } else {
3819                #            !!!parse-error (type => 'in table:'.$token->{tag_name});
3820              }  
3821            } elsif ($token->{type} eq 'end tag') {            $insert = $insert_to_foster;
3822              if ({            #
3823                   tbody => 1, tfoot => 1, thead => 1,          }
3824                  }->{$token->{tag_name}}) {        } elsif ($token->{type} == END_TAG_TOKEN) {
3825                if ($token->{tag_name} eq 'tr' and
3826                    $self->{insertion_mode} == IN_ROW_IM) {
3827                ## have an element in table scope                ## have an element in table scope
3828                my $i;                my $i;
3829                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 4161  sub _tree_construction_main ($) { Line 3844  sub _tree_construction_main ($) {
3844                  redo B;                  redo B;
3845                }                }
3846    
3847                ## Clear back to table body context                ## Clear back to table row context
3848                while (not {                while (not {
3849                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tr => 1, html => 1,
3850                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
3851                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3852                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3853                }                }
3854    
3855                pop @{$self->{open_elements}};                pop @{$self->{open_elements}}; # tr
3856                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
3857                !!!next-token;                !!!next-token;
3858                redo B;                redo B;
3859              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
3860                ## have an element in table scope                if ($self->{insertion_mode} == IN_ROW_IM) {
3861                my $i;                  ## As if </tr>
3862                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
3863                  my $node = $self->{open_elements}->[$_];                  my $i;
3864                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3865                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
3866                      }->{$node->[1]}) {                    if ($node->[1] eq 'tr') {
3867                    $i = $_;                      $i = $_;
3868                    last INSCOPE;                      last INSCOPE;
3869                  } elsif ({                    } elsif ({
3870                            table => 1, html => 1,                              table => 1, html => 1,
3871                           }->{$node->[1]}) {                             }->{$node->[1]}) {
3872                    last INSCOPE;                      last INSCOPE;
3873                      }
3874                    } # INSCOPE
3875                    unless (defined $i) {
3876                      !!!parse-error (type => 'unmatched end tag:'.$token->{type});
3877                      ## Ignore the token
3878                      !!!next-token;
3879                      redo B;
3880                  }                  }
3881                } # INSCOPE                  
3882                unless (defined $i) {                  ## Clear back to table row context
3883                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  while (not {
3884                  ## 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,  
3885                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
3886                # MUST                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3887                my $foster_parent_element;                    pop @{$self->{open_elements}};
3888                my $next_sibling;                  }
3889                my $prev_sibling;                  
3890                OE: for (reverse 0..$#{$self->{open_elements}}) {                  pop @{$self->{open_elements}}; # tr
3891                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3892                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                  ## reprocess in the "in table body" insertion mode...
3893                    if (defined $parent and $parent->node_type == 1) {                }
3894                      $foster_parent_element = $parent;  
3895                      $next_sibling = $self->{open_elements}->[$_]->[0];                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3896                      $prev_sibling = $next_sibling->previous_sibling;                  ## have an element in table scope
3897                    } else {                  my $i;
3898                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3899                      $prev_sibling = $foster_parent_element->last_child;                    my $node = $self->{open_elements}->[$_];
3900                      if ({
3901                           tbody => 1, thead => 1, tfoot => 1,
3902                          }->{$node->[1]}) {
3903                        $i = $_;
3904                        last INSCOPE;
3905                      } elsif ({
3906                                table => 1, html => 1,
3907                               }->{$node->[1]}) {
3908                        last INSCOPE;
3909                    }                    }
3910                    last OE;                  } # INSCOPE
3911                    unless (defined $i) {
3912                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3913                      ## Ignore the token
3914                      !!!next-token;
3915                      redo B;
3916                  }                  }
3917                } # OE                  
3918                $foster_parent_element = $self->{open_elements}->[0]->[0] and                  ## Clear back to table body context
3919                $prev_sibling = $foster_parent_element->last_child                  while (not {
3920                  unless defined $foster_parent_element;                    tbody => 1, tfoot => 1, thead => 1, html => 1,
3921                if (defined $prev_sibling and                  }->{$self->{open_elements}->[-1]->[1]}) {
3922                    $prev_sibling->node_type == 3) {                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3923                  $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 'comment') {  
             ## Copied from 'in table'  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!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;  
3924                  }                  }
3925                } # INSCOPE                  
3926                unless (defined $i) {                  ## As if <{current node}>
3927                  !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                  ## have an element in table scope
3928                  ## Ignore the token                  ## true by definition
3929                  !!!next-token;                  
3930                  redo B;                  ## Clear back to table body context
3931                }                  ## nop by definition
3932                    
               ## 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]);  
3933                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3934                    $self->{insertion_mode} = IN_TABLE_IM;
3935                    ## reprocess in the "in table" insertion mode...
3936                }                }
3937    
               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>  
3938                ## have a table element in table scope                ## have a table element in table scope
3939                my $i;                my $i;
3940                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3941                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3942                  if ($node->[1] eq 'table') {                  if ($node->[1] eq $token->{tag_name}) {
3943                    $i = $_;                    $i = $_;
3944                    last INSCOPE;                    last INSCOPE;
3945                  } elsif ({                  } elsif ({
# Line 4373  sub _tree_construction_main ($) { Line 3949  sub _tree_construction_main ($) {
3949                  }                  }
3950                } # INSCOPE                } # INSCOPE
3951                unless (defined $i) {                unless (defined $i) {
3952                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3953                  ## Ignore tokens </table><table>                  ## Ignore the token
3954                  !!!next-token;                  !!!next-token;
3955                  redo B;                  redo B;
3956                }                }
3957                  
3958                ## generate implied end tags                ## generate implied end tags
3959                if ({                if ({
3960                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3961                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3962                       tbody => 1, tfoot=> 1, thead => 1,
3963                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
3964                  !!!back-token;                  !!!back-token;
3965                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3966                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3967                  redo B;                  redo B;
3968                }                }
3969                  
3970                if ($self->{open_elements}->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
3971                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3972                }                }
3973                    
3974                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
3975                  
3976                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
3977                  
               ## 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';  
3978                !!!next-token;                !!!next-token;
3979                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;  
3980              } elsif ({              } elsif ({
3981                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
3982                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}} and
3983                ## have an element in table scope                       $self->{insertion_mode} & ROW_IMS) {
3984                my $i;                if ($self->{insertion_mode} == IN_ROW_IM) {
3985                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
3986                  my $node = $self->{open_elements}->[$_];                  my $i;
3987                  if ($node->[1] eq $token->{tag_name}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3988                    $i = $_;                    my $node = $self->{open_elements}->[$_];
3989                    last INSCOPE;                    if ($node->[1] eq $token->{tag_name}) {
3990                  } elsif ({                      $i = $_;
3991                            table => 1, html => 1,                      last INSCOPE;
3992                           }->{$node->[1]}) {                    } elsif ({
3993                    last INSCOPE;                              table => 1, html => 1,
3994                               }->{$node->[1]}) {
3995                        last INSCOPE;
3996                      }
3997                    } # INSCOPE
3998                      unless (defined $i) {
3999                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4000                        ## Ignore the token
4001                        !!!next-token;
4002                        redo B;
4003                      }
4004                    
4005                    ## As if </tr>
4006                    ## have an element in table scope
4007                    my $i;
4008                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4009                      my $node = $self->{open_elements}->[$_];
4010                      if ($node->[1] eq 'tr') {
4011                        $i = $_;
4012                        last INSCOPE;
4013                      } elsif ({
4014                                table => 1, html => 1,
4015                               }->{$node->[1]}) {
4016                        last INSCOPE;
4017                      }
4018                    } # INSCOPE
4019                      unless (defined $i) {
4020                        !!!parse-error (type => 'unmatched end tag:tr');
4021                        ## Ignore the token
4022                        !!!next-token;
4023                        redo B;
4024                      }
4025                    
4026                    ## Clear back to table row context
4027                    while (not {
4028                      tr => 1, html => 1,
4029                    }->{$self->{open_elements}->[-1]->[1]}) {
4030                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4031                      pop @{$self->{open_elements}};
4032                  }                  }
4033                } # INSCOPE                  
4034                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
4035                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4036                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
4037                }                }
4038    
               ## As if </tr>  
4039                ## have an element in table scope                ## have an element in table scope
4040                my $i;                my $i;
4041                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4042                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4043                  if ($node->[1] eq 'tr') {                  if ($node->[1] eq $token->{tag_name}) {
4044                    $i = $_;                    $i = $_;
4045                    last INSCOPE;                    last INSCOPE;
4046                  } elsif ({                  } elsif ({
# Line 4511  sub _tree_construction_main ($) { Line 4050  sub _tree_construction_main ($) {
4050                  }                  }
4051                } # INSCOPE                } # INSCOPE
4052                unless (defined $i) {                unless (defined $i) {
4053                  !!!parse-error (type => 'unmatched end tag:tr');                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4054                  ## Ignore the token                  ## Ignore the token
4055                  !!!next-token;                  !!!next-token;
4056                  redo B;                  redo B;
4057                }                }
4058    
4059                ## Clear back to table row context                ## Clear back to table body context
4060                while (not {                while (not {
4061                  tr => 1, html => 1,                  tbody => 1, tfoot => 1, thead => 1, html => 1,
4062                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4063                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4064                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4065                }                }
4066    
4067                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}};
4068                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_IM;
4069                ## reprocess                !!!next-token;
4070                redo B;                redo B;
4071              } elsif ({              } elsif ({
4072                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
4073                        colgroup => 1, html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
4074                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
4075                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
4076                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4077                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4078                ## Ignore the token                ## Ignore the token
4079                !!!next-token;                !!!next-token;
4080                redo B;                redo B;
4081              } else {          } else {
4082                #            !!!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});  
4083    
4084              !!!next-token;            $insert = $insert_to_foster;
4085              redo B;            #
4086            } elsif ($token->{type} eq 'comment') {          }
4087              ## NOTE: This is a code clone of "comment in body".        } else {
4088              my $comment = $self->{document}->create_comment ($token->{data});          die "$0: $token->{type}: Unknown token type";
4089              $self->{open_elements}->[-1]->[0]->append_child ($comment);        }
4090              !!!next-token;      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
4091              redo B;            if ($token->{type} == CHARACTER_TOKEN) {
4092            } elsif ($token->{type} eq 'start tag') {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4093              if ({                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4094                   caption => 1, col => 1, colgroup => 1,                unless (length $token->{data}) {
                  tbody => 1, td => 1, tfoot => 1, th => 1,  
                  thead => 1, tr => 1,  
                 }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $tn;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 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  
4095                  !!!next-token;                  !!!next-token;
4096                  redo B;                  redo B;
4097                }                }
4098                }
4099                ## Close the cell              
4100                !!!back-token; # <?>              #
4101                $token = {type => 'end tag', tag_name => $tn};            } elsif ($token->{type} == START_TAG_TOKEN) {
4102                if ($token->{tag_name} eq 'col') {
4103                  !!!insert-element ($token->{tag_name}, $token->{attributes});
4104                  pop @{$self->{open_elements}};
4105                  !!!next-token;
4106                redo B;                redo B;
4107              } else {              } else {
4108                #                #
4109              }              }
4110            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
4111              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'colgroup') {
4112                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] eq 'html') {
4113                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});  
4114                  ## Ignore the token                  ## Ignore the token
4115                  !!!next-token;                  !!!next-token;
4116                  redo B;                  redo B;
4117                  } else {
4118                    pop @{$self->{open_elements}}; # colgroup
4119                    $self->{insertion_mode} = IN_TABLE_IM;
4120                    !!!next-token;
4121                    redo B;            
4122                }                }
4123                              } elsif ($token->{tag_name} eq 'col') {
4124                ## 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,  
                   }->{$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});  
4125                ## Ignore the token                ## Ignore the token
4126                !!!next-token;                !!!next-token;
4127                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;  
4128              } else {              } else {
4129                #                #
4130              }              }
4131            } else {            } else {
4132              #              #
4133            }            }
4134              
4135            $in_body->($insert_to_current);            ## As if </colgroup>
4136            redo B;            if ($self->{open_elements}->[-1]->[1] eq 'html') {
4137          } elsif ($self->{insertion_mode} eq 'in select') {              !!!parse-error (type => 'unmatched end tag:colgroup');
4138            if ($token->{type} eq 'character') {              ## Ignore the token
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
4139              !!!next-token;              !!!next-token;
4140              redo B;              redo B;
4141            } elsif ($token->{type} eq 'comment') {            } else {
4142              my $comment = $self->{document}->create_comment ($token->{data});              pop @{$self->{open_elements}}; # colgroup
4143              $self->{open_elements}->[-1]->[0]->append_child ($comment);              $self->{insertion_mode} = IN_TABLE_IM;
4144              !!!next-token;              ## reprocess
4145              redo B;              redo B;
4146            } elsif ($token->{type} eq 'start tag') {            }
4147        } elsif ($self->{insertion_mode} == IN_SELECT_IM) {
4148          if ($token->{type} == CHARACTER_TOKEN) {
4149            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4150            !!!next-token;
4151            redo B;
4152          } elsif ($token->{type} == START_TAG_TOKEN) {
4153              if ($token->{tag_name} eq 'option') {              if ($token->{tag_name} eq 'option') {
4154                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
4155                  ## As if </option>                  ## As if </option>
# Line 4756  sub _tree_construction_main ($) { Line 4202  sub _tree_construction_main ($) {
4202    
4203                !!!next-token;                !!!next-token;
4204                redo B;                redo B;
4205              } else {          } else {
4206                #            !!!parse-error (type => 'in select:'.$token->{tag_name});
4207              }            ## Ignore the token
4208            } elsif ($token->{type} eq 'end tag') {            !!!next-token;
4209              redo B;
4210            }
4211          } elsif ($token->{type} == END_TAG_TOKEN) {
4212              if ($token->{tag_name} eq 'optgroup') {              if ($token->{tag_name} eq 'optgroup') {
4213                if ($self->{open_elements}->[-1]->[1] eq 'option' and                if ($self->{open_elements}->[-1]->[1] eq 'option' and
4214                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {
# Line 4861  sub _tree_construction_main ($) { Line 4310  sub _tree_construction_main ($) {
4310    
4311                ## reprocess                ## reprocess
4312                redo B;                redo B;
4313              } else {          } else {
4314                #            !!!parse-error (type => 'in select:/'.$token->{tag_name});
             }  
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in select:'.$token->{tag_name});  
4315            ## Ignore the token            ## Ignore the token
4316            !!!next-token;            !!!next-token;
4317            redo B;            redo B;
4318          } elsif ($self->{insertion_mode} eq 'after body') {          }
4319            if ($token->{type} eq 'character') {        } else {
4320              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          die "$0: $token->{type}: Unknown token type";
4321                ## As if in body        }
4322                $reconstruct_active_formatting_elements->($insert_to_current);      } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
4323          if ($token->{type} == CHARACTER_TOKEN) {
4324            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4325              my $data = $1;
4326              ## As if in body
4327              $reconstruct_active_formatting_elements->($insert_to_current);
4328                                
4329                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4330              
4331              unless (length $token->{data}) {
4332                !!!next-token;
4333                redo B;
4334              }
4335            }
4336            
4337            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4338              !!!parse-error (type => 'after html:#character');
4339    
4340                unless (length $token->{data}) {            ## Reprocess in the "main" phase, "after body" insertion mode...
4341                  !!!next-token;          }
4342                  redo B;          
4343                }          ## "after body" insertion mode
4344              }          !!!parse-error (type => 'after body:#character');
4345                
4346              #          $self->{insertion_mode} = IN_BODY_IM;
4347              !!!parse-error (type => 'after body:#'.$token->{type});          ## reprocess
4348            } elsif ($token->{type} eq 'comment') {          redo B;
4349              my $comment = $self->{document}->create_comment ($token->{data});        } elsif ($token->{type} == START_TAG_TOKEN) {
4350              $self->{open_elements}->[0]->[0]->append_child ($comment);          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4351              !!!parse-error (type => 'after html:'.$token->{tag_name});
4352              
4353              ## Reprocess in the "main" phase, "after body" insertion mode...
4354            }
4355    
4356            ## "after body" insertion mode
4357            !!!parse-error (type => 'after body:'.$token->{tag_name});
4358    
4359            $self->{insertion_mode} = IN_BODY_IM;
4360            ## reprocess
4361            redo B;
4362          } elsif ($token->{type} == END_TAG_TOKEN) {
4363            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4364              !!!parse-error (type => 'after html:/'.$token->{tag_name});
4365              
4366              $self->{insertion_mode} = AFTER_BODY_IM;
4367              ## Reprocess in the "main" phase, "after body" insertion mode...
4368            }
4369    
4370            ## "after body" insertion mode
4371            if ($token->{tag_name} eq 'html') {
4372              if (defined $self->{inner_html_node}) {
4373                !!!parse-error (type => 'unmatched end tag:html');
4374                ## Ignore the token
4375              !!!next-token;              !!!next-token;
4376              redo B;              redo B;
           } elsif ($token->{type} eq 'start tag') {  
             !!!parse-error (type => 'after body:'.$token->{tag_name});  
             #  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'html') {  
               if (defined $self->{inner_html_node}) {  
                 !!!parse-error (type => 'unmatched end tag:html');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               } else {  
                 $phase = 'trailing end';  
                 !!!next-token;  
                 redo B;  
               }  
             } else {  
               !!!parse-error (type => 'after body:/'.$token->{tag_name});  
             }  
4377            } else {            } else {
4378              !!!parse-error (type => 'after body:#'.$token->{type});              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
4379                !!!next-token;
4380                redo B;
4381            }            }
4382            } else {
4383              !!!parse-error (type => 'after body:/'.$token->{tag_name});
4384    
4385            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = IN_BODY_IM;
4386            ## reprocess            ## reprocess
4387            redo B;            redo B;
4388          } elsif ($self->{insertion_mode} eq 'in frameset') {          }
4389            if ($token->{type} eq 'character') {        } else {
4390              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          die "$0: $token->{type}: Unknown token type";
4391                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});        }
4392        } elsif ($self->{insertion_mode} & FRAME_IMS) {
4393                unless (length $token->{data}) {        if ($token->{type} == CHARACTER_TOKEN) {
4394                  !!!next-token;          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4395                  redo B;            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4396                }            
4397              }            unless (length $token->{data}) {
   
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
4398              !!!next-token;              !!!next-token;
4399              redo B;              redo B;
4400            } elsif ($token->{type} eq 'start tag') {            }
4401              if ($token->{tag_name} eq 'frameset') {          }
4402                !!!insert-element ($token->{tag_name}, $token->{attributes});          
4403                !!!next-token;          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
4404                redo B;            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4405              } elsif ($token->{tag_name} eq 'frame') {              !!!parse-error (type => 'in frameset:#character');
4406                !!!insert-element ($token->{tag_name}, $token->{attributes});            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
4407                pop @{$self->{open_elements}};              !!!parse-error (type => 'after frameset:#character');
4408                !!!next-token;            } else { # "after html frameset"
4409                redo B;              !!!parse-error (type => 'after html:#character');
4410              } elsif ($token->{tag_name} eq 'noframes') {  
4411                $in_body->($insert_to_current);              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4412                redo B;              ## Reprocess in the "main" phase, "after frameset"...
4413              } else {              !!!parse-error (type => 'after frameset:#character');
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'frameset') {  
               if ($self->{open_elements}->[-1]->[1] eq 'html' and  
                   @{$self->{open_elements}} == 1) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
               } else {  
                 pop @{$self->{open_elements}};  
                 !!!next-token;  
               }  
                 
               ## if not inner_html and  
               if ($self->{open_elements}->[-1]->[1] ne 'frameset') {  
                 $self->{insertion_mode} = 'after frameset';  
               }  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
4414            }            }
4415                        
4416            if (defined $token->{tag_name}) {            ## Ignore the token.
4417              if (length $token->{data}) {
4418                ## reprocess the rest of characters
4419              } else {
4420                !!!next-token;
4421              }
4422              redo B;
4423            }
4424            
4425            die qq[$0: Character "$token->{data}"];
4426          } elsif ($token->{type} == START_TAG_TOKEN) {
4427            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4428              !!!parse-error (type => 'after html:'.$token->{tag_name});
4429    
4430              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4431              ## Process in the "main" phase, "after frameset" insertion mode...
4432            }
4433    
4434            if ($token->{tag_name} eq 'frameset' and
4435                $self->{insertion_mode} == IN_FRAMESET_IM) {
4436              !!!insert-element ($token->{tag_name}, $token->{attributes});
4437              !!!next-token;
4438              redo B;
4439            } elsif ($token->{tag_name} eq 'frame' and
4440                     $self->{insertion_mode} == IN_FRAMESET_IM) {
4441              !!!insert-element ($token->{tag_name}, $token->{attributes});
4442              pop @{$self->{open_elements}};
4443              !!!next-token;
4444              redo B;
4445            } elsif ($token->{tag_name} eq 'noframes') {
4446              ## NOTE: As if in body.
4447              $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
4448              redo B;
4449            } else {
4450              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4451              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset:'.$token->{tag_name});
4452            } else {            } else {
4453              !!!parse-error (type => 'in frameset:#'.$token->{type});              !!!parse-error (type => 'after frameset:'.$token->{tag_name});
4454            }            }
4455            ## Ignore the token            ## Ignore the token
4456            !!!next-token;            !!!next-token;
4457            redo B;            redo B;
4458          } elsif ($self->{insertion_mode} eq 'after frameset') {          }
4459            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_TAG_TOKEN) {
4460              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4461                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            !!!parse-error (type => 'after html:/'.$token->{tag_name});
4462    
4463                unless (length $token->{data}) {            $self->{insertion_mode} = AFTER_FRAMESET_IM;
4464                  !!!next-token;            ## Process in the "main" phase, "after frameset" insertion mode...
4465                  redo B;          }
               }  
             }  
4466    
4467              #          if ($token->{tag_name} eq 'frameset' and
4468            } elsif ($token->{type} eq 'comment') {              $self->{insertion_mode} == IN_FRAMESET_IM) {
4469              my $comment = $self->{document}->create_comment ($token->{data});            if ($self->{open_elements}->[-1]->[1] eq 'html' and
4470              $self->{open_elements}->[-1]->[0]->append_child ($comment);                @{$self->{open_elements}} == 1) {
4471                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4472                ## Ignore the token
4473              !!!next-token;              !!!next-token;
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'noframes') {  
               $in_body->($insert_to_current);  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'html') {  
               $phase = 'trailing end';  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
4474            } else {            } else {
4475              #              pop @{$self->{open_elements}};
4476                !!!next-token;
4477            }            }
4478              
4479            if (defined $token->{tag_name}) {            if (not defined $self->{inner_html_node} and
4480              !!!parse-error (type => 'after frameset:'.$token->{tag_name});                $self->{open_elements}->[-1]->[1] ne 'frameset') {
4481                $self->{insertion_mode} = AFTER_FRAMESET_IM;
4482              }
4483              redo B;
4484            } elsif ($token->{tag_name} eq 'html' and
4485                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
4486              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
4487              !!!next-token;
4488              redo B;
4489            } else {
4490              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4491                !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
4492            } else {            } else {
4493              !!!parse-error (type => 'after frameset:#'.$token->{type});              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
4494            }            }
4495            ## Ignore the token            ## Ignore the token
4496            !!!next-token;            !!!next-token;
4497            redo B;            redo B;
4498            }
4499          } else {
4500            die "$0: $token->{type}: Unknown token type";
4501          }
4502    
4503          ## ISSUE: An issue in spec here
4504        } else {
4505          die "$0: $self->{insertion_mode}: Unknown insertion mode";
4506        }
4507    
4508        ## "in body" insertion mode
4509        if ($token->{type} == START_TAG_TOKEN) {
4510          if ($token->{tag_name} eq 'script') {
4511            ## NOTE: This is an "as if in head" code clone
4512            $script_start_tag->($insert);
4513            redo B;
4514          } elsif ($token->{tag_name} eq 'style') {
4515            ## NOTE: This is an "as if in head" code clone
4516            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4517            redo B;
4518          } elsif ({
4519                    base => 1, link => 1,
4520                   }->{$token->{tag_name}}) {
4521            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4522            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4523            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4524            !!!next-token;
4525            redo B;
4526          } elsif ($token->{tag_name} eq 'meta') {
4527            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4528            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4529            my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4530    
4531            unless ($self->{confident}) {
4532              if ($token->{attributes}->{charset}) { ## TODO: And if supported
4533                $self->{change_encoding}
4534                    ->($self, $token->{attributes}->{charset}->{value});
4535                
4536                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4537                    ->set_user_data (manakai_has_reference =>
4538                                         $token->{attributes}->{charset}
4539                                             ->{has_reference});
4540              } elsif ($token->{attributes}->{content}) {
4541                ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
4542                if ($token->{attributes}->{content}->{value}
4543                    =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4544                        [\x09-\x0D\x20]*=
4545                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4546                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4547                  $self->{change_encoding}
4548                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
4549                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4550                      ->set_user_data (manakai_has_reference =>
4551                                           $token->{attributes}->{content}
4552                                                 ->{has_reference});
4553                }
4554              }
4555            } else {
4556              if ($token->{attributes}->{charset}) {
4557                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4558                    ->set_user_data (manakai_has_reference =>
4559                                         $token->{attributes}->{charset}
4560                                             ->{has_reference});
4561              }
4562              if ($token->{attributes}->{content}) {
4563                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4564                    ->set_user_data (manakai_has_reference =>
4565                                         $token->{attributes}->{content}
4566                                             ->{has_reference});
4567              }
4568            }
4569    
4570            ## ISSUE: An issue in spec there          !!!next-token;
4571            redo B;
4572          } elsif ($token->{tag_name} eq 'title') {
4573            !!!parse-error (type => 'in body:title');
4574            ## NOTE: This is an "as if in head" code clone
4575            $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {
4576              if (defined $self->{head_element}) {
4577                $self->{head_element}->append_child ($_[0]);
4578              } else {
4579                $insert->($_[0]);
4580              }
4581            });
4582            redo B;
4583          } elsif ($token->{tag_name} eq 'body') {
4584            !!!parse-error (type => 'in body:body');
4585                  
4586            if (@{$self->{open_elements}} == 1 or
4587                $self->{open_elements}->[1]->[1] ne 'body') {
4588              ## Ignore the token
4589          } else {          } else {
4590            die "$0: $self->{insertion_mode}: Unknown insertion mode";            my $body_el = $self->{open_elements}->[1]->[0];
4591              for my $attr_name (keys %{$token->{attributes}}) {
4592                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
4593                  $body_el->set_attribute_ns
4594                    (undef, [undef, $attr_name],
4595                     $token->{attributes}->{$attr_name}->{value});
4596                }
4597              }
4598          }          }
       }  
     } elsif ($phase eq 'trailing end') {  
       ## states in the main stage is preserved yet # MUST  
         
       if ($token->{type} eq 'DOCTYPE') {  
         !!!parse-error (type => 'after html:#DOCTYPE');  
         ## Ignore the token  
4599          !!!next-token;          !!!next-token;
4600          redo B;          redo B;
4601        } elsif ($token->{type} eq 'comment') {        } elsif ({
4602          my $comment = $self->{document}->create_comment ($token->{data});                  address => 1, blockquote => 1, center => 1, dir => 1,
4603          $self->{document}->append_child ($comment);                  div => 1, dl => 1, fieldset => 1, listing => 1,
4604                    menu => 1, ol => 1, p => 1, ul => 1,
4605                    pre => 1,
4606                   }->{$token->{tag_name}}) {
4607            ## has a p element in scope
4608            INSCOPE: for (reverse @{$self->{open_elements}}) {
4609              if ($_->[1] eq 'p') {
4610                !!!back-token;
4611                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4612                redo B;
4613              } elsif ({
4614                        table => 1, caption => 1, td => 1, th => 1,
4615                        button => 1, marquee => 1, object => 1, html => 1,
4616                       }->{$_->[1]}) {
4617                last INSCOPE;
4618              }
4619            } # INSCOPE
4620              
4621            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4622            if ($token->{tag_name} eq 'pre') {
4623              !!!next-token;
4624              if ($token->{type} == CHARACTER_TOKEN) {
4625                $token->{data} =~ s/^\x0A//;
4626                unless (length $token->{data}) {
4627                  !!!next-token;
4628                }
4629              }
4630            } else {
4631              !!!next-token;
4632            }
4633            redo B;
4634          } elsif ($token->{tag_name} eq 'form') {
4635            if (defined $self->{form_element}) {
4636              !!!parse-error (type => 'in form:form');
4637              ## Ignore the token
4638              !!!next-token;
4639              redo B;
4640            } else {
4641              ## has a p element in scope
4642              INSCOPE: for (reverse @{$self->{open_elements}}) {
4643                if ($_->[1] eq 'p') {
4644                  !!!back-token;
4645                  $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4646                  redo B;
4647                } elsif ({
4648                          table => 1, caption => 1, td => 1, th => 1,
4649                          button => 1, marquee => 1, object => 1, html => 1,
4650                         }->{$_->[1]}) {
4651                  last INSCOPE;
4652                }
4653              } # INSCOPE
4654                
4655              !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4656              $self->{form_element} = $self->{open_elements}->[-1]->[0];
4657              !!!next-token;
4658              redo B;
4659            }
4660          } elsif ($token->{tag_name} eq 'li') {
4661            ## has a p element in scope
4662            INSCOPE: for (reverse @{$self->{open_elements}}) {
4663              if ($_->[1] eq 'p') {
4664                !!!back-token;
4665                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4666                redo B;
4667              } elsif ({
4668                        table => 1, caption => 1, td => 1, th => 1,
4669                        button => 1, marquee => 1, object => 1, html => 1,
4670                       }->{$_->[1]}) {
4671                last INSCOPE;
4672              }
4673            } # INSCOPE
4674              
4675            ## Step 1
4676            my $i = -1;
4677            my $node = $self->{open_elements}->[$i];
4678            LI: {
4679              ## Step 2
4680              if ($node->[1] eq 'li') {
4681                if ($i != -1) {
4682                  !!!parse-error (type => 'end tag missing:'.
4683                                  $self->{open_elements}->[-1]->[1]);
4684                }
4685                splice @{$self->{open_elements}}, $i;
4686                last LI;
4687              }
4688              
4689              ## Step 3
4690              if (not $formatting_category->{$node->[1]} and
4691                  #not $phrasing_category->{$node->[1]} and
4692                  ($special_category->{$node->[1]} or
4693                   $scoping_category->{$node->[1]}) and
4694                  $node->[1] ne 'address' and $node->[1] ne 'div') {
4695                last LI;
4696              }
4697              
4698              ## Step 4
4699              $i--;
4700              $node = $self->{open_elements}->[$i];
4701              redo LI;
4702            } # LI
4703              
4704            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4705          !!!next-token;          !!!next-token;
4706          redo B;          redo B;
4707        } elsif ($token->{type} eq 'character') {        } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {
4708          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          ## has a p element in scope
4709            my $data = $1;          INSCOPE: for (reverse @{$self->{open_elements}}) {
4710            ## As if in the main phase.            if ($_->[1] eq 'p') {
4711            ## NOTE: The insertion mode in the main phase              !!!back-token;
4712            ## just before the phase has been changed to the trailing              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4713            ## end phase is either "after body" or "after frameset".              redo B;
4714            $reconstruct_active_formatting_elements->($insert_to_current)            } elsif ({
4715              if $phase eq 'main';                      table => 1, caption => 1, td => 1, th => 1,
4716                        button => 1, marquee => 1, object => 1, html => 1,
4717                       }->{$_->[1]}) {
4718                last INSCOPE;
4719              }
4720            } # INSCOPE
4721              
4722            ## Step 1
4723            my $i = -1;
4724            my $node = $self->{open_elements}->[$i];
4725            LI: {
4726              ## Step 2
4727              if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {
4728                if ($i != -1) {
4729                  !!!parse-error (type => 'end tag missing:'.
4730                                  $self->{open_elements}->[-1]->[1]);
4731                }
4732                splice @{$self->{open_elements}}, $i;
4733                last LI;
4734              }
4735              
4736              ## Step 3
4737              if (not $formatting_category->{$node->[1]} and
4738                  #not $phrasing_category->{$node->[1]} and
4739                  ($special_category->{$node->[1]} or
4740                   $scoping_category->{$node->[1]}) and
4741                  $node->[1] ne 'address' and $node->[1] ne 'div') {
4742                last LI;
4743              }
4744              
4745              ## Step 4
4746              $i--;
4747              $node = $self->{open_elements}->[$i];
4748              redo LI;
4749            } # LI
4750              
4751            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4752            !!!next-token;
4753            redo B;
4754          } elsif ($token->{tag_name} eq 'plaintext') {
4755            ## has a p element in scope
4756            INSCOPE: for (reverse @{$self->{open_elements}}) {
4757              if ($_->[1] eq 'p') {
4758                !!!back-token;
4759                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4760                redo B;
4761              } elsif ({
4762                        table => 1, caption => 1, td => 1, th => 1,
4763                        button => 1, marquee => 1, object => 1, html => 1,
4764                       }->{$_->[1]}) {
4765                last INSCOPE;
4766              }
4767            } # INSCOPE
4768              
4769            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4770              
4771            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
4772              
4773            !!!next-token;
4774            redo B;
4775          } elsif ({
4776                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4777                   }->{$token->{tag_name}}) {
4778            ## has a p element in scope
4779            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4780              my $node = $self->{open_elements}->[$_];
4781              if ($node->[1] eq 'p') {
4782                !!!back-token;
4783                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4784                redo B;
4785              } elsif ({
4786                        table => 1, caption => 1, td => 1, th => 1,
4787                        button => 1, marquee => 1, object => 1, html => 1,
4788                       }->{$node->[1]}) {
4789                last INSCOPE;
4790              }
4791            } # INSCOPE
4792              
4793            ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>
4794            ## has an element in scope
4795            #my $i;
4796            #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4797            #  my $node = $self->{open_elements}->[$_];
4798            #  if ({
4799            #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4800            #      }->{$node->[1]}) {
4801            #    $i = $_;
4802            #    last INSCOPE;
4803            #  } elsif ({
4804            #            table => 1, caption => 1, td => 1, th => 1,
4805            #            button => 1, marquee => 1, object => 1, html => 1,
4806            #           }->{$node->[1]}) {
4807            #    last INSCOPE;
4808            #  }
4809            #} # INSCOPE
4810            #  
4811            #if (defined $i) {
4812            #  !!! parse-error (type => 'in hn:hn');
4813            #  splice @{$self->{open_elements}}, $i;
4814            #}
4815              
4816            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4817              
4818            !!!next-token;
4819            redo B;
4820          } elsif ($token->{tag_name} eq 'a') {
4821            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
4822              my $node = $active_formatting_elements->[$i];
4823              if ($node->[1] eq 'a') {
4824                !!!parse-error (type => 'in a:a');
4825                
4826                !!!back-token;
4827                $token = {type => END_TAG_TOKEN, tag_name => 'a'};
4828                $formatting_end_tag->($token->{tag_name});
4829                
4830                AFE2: for (reverse 0..$#$active_formatting_elements) {
4831                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
4832                    splice @$active_formatting_elements, $_, 1;
4833                    last AFE2;
4834                  }
4835                } # AFE2
4836                OE: for (reverse 0..$#{$self->{open_elements}}) {
4837                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
4838                    splice @{$self->{open_elements}}, $_, 1;
4839                    last OE;
4840                  }
4841                } # OE
4842                last AFE;
4843              } elsif ($node->[0] eq '#marker') {
4844                last AFE;
4845              }
4846            } # AFE
4847              
4848            $reconstruct_active_formatting_elements->($insert_to_current);
4849    
4850            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4851            push @$active_formatting_elements, $self->{open_elements}->[-1];
4852    
4853            !!!next-token;
4854            redo B;
4855          } elsif ({
4856                    b => 1, big => 1, em => 1, font => 1, i => 1,
4857                    s => 1, small => 1, strile => 1,
4858                    strong => 1, tt => 1, u => 1,
4859                   }->{$token->{tag_name}}) {
4860            $reconstruct_active_formatting_elements->($insert_to_current);
4861            
4862            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4863            push @$active_formatting_elements, $self->{open_elements}->[-1];
4864            
4865            !!!next-token;
4866            redo B;
4867          } elsif ($token->{tag_name} eq 'nobr') {
4868            $reconstruct_active_formatting_elements->($insert_to_current);
4869    
4870            ## has a |nobr| element in scope
4871            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4872              my $node = $self->{open_elements}->[$_];
4873              if ($node->[1] eq 'nobr') {
4874                !!!parse-error (type => 'in nobr:nobr');
4875                !!!back-token;
4876                $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};
4877                redo B;
4878              } elsif ({
4879                        table => 1, caption => 1, td => 1, th => 1,
4880                        button => 1, marquee => 1, object => 1, html => 1,
4881                       }->{$node->[1]}) {
4882                last INSCOPE;
4883              }
4884            } # INSCOPE
4885            
4886            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4887            push @$active_formatting_elements, $self->{open_elements}->[-1];
4888            
4889            !!!next-token;
4890            redo B;
4891          } elsif ($token->{tag_name} eq 'button') {
4892            ## has a button element in scope
4893            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4894              my $node = $self->{open_elements}->[$_];
4895              if ($node->[1] eq 'button') {
4896                !!!parse-error (type => 'in button:button');
4897                !!!back-token;
4898                $token = {type => END_TAG_TOKEN, tag_name => 'button'};
4899                redo B;
4900              } elsif ({
4901                        table => 1, caption => 1, td => 1, th => 1,
4902                        button => 1, marquee => 1, object => 1, html => 1,
4903                       }->{$node->[1]}) {
4904                last INSCOPE;
4905              }
4906            } # INSCOPE
4907              
4908            $reconstruct_active_formatting_elements->($insert_to_current);
4909              
4910            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4911            push @$active_formatting_elements, ['#marker', ''];
4912    
4913            !!!next-token;
4914            redo B;
4915          } elsif ($token->{tag_name} eq 'marquee' or
4916                   $token->{tag_name} eq 'object') {
4917            $reconstruct_active_formatting_elements->($insert_to_current);
4918            
4919            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4920            push @$active_formatting_elements, ['#marker', ''];
4921            
4922            !!!next-token;
4923            redo B;
4924          } elsif ($token->{tag_name} eq 'xmp') {
4925            $reconstruct_active_formatting_elements->($insert_to_current);
4926            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4927            redo B;
4928          } elsif ($token->{tag_name} eq 'table') {
4929            ## has a p element in scope
4930            INSCOPE: for (reverse @{$self->{open_elements}}) {
4931              if ($_->[1] eq 'p') {
4932                !!!back-token;
4933                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4934                redo B;
4935              } elsif ({
4936                        table => 1, caption => 1, td => 1, th => 1,
4937                        button => 1, marquee => 1, object => 1, html => 1,
4938                       }->{$_->[1]}) {
4939                last INSCOPE;
4940              }
4941            } # INSCOPE
4942              
4943            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4944              
4945            $self->{insertion_mode} = IN_TABLE_IM;
4946              
4947            !!!next-token;
4948            redo B;
4949          } elsif ({
4950                    area => 1, basefont => 1, bgsound => 1, br => 1,
4951                    embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
4952                    image => 1,
4953                   }->{$token->{tag_name}}) {
4954            if ($token->{tag_name} eq 'image') {
4955              !!!parse-error (type => 'image');
4956              $token->{tag_name} = 'img';
4957            }
4958    
4959            ## NOTE: There is an "as if <br>" code clone.
4960            $reconstruct_active_formatting_elements->($insert_to_current);
4961            
4962            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4963            pop @{$self->{open_elements}};
4964            
4965            !!!next-token;
4966            redo B;
4967          } elsif ($token->{tag_name} eq 'hr') {
4968            ## has a p element in scope
4969            INSCOPE: for (reverse @{$self->{open_elements}}) {
4970              if ($_->[1] eq 'p') {
4971                !!!back-token;
4972                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4973                redo B;
4974              } elsif ({
4975                        table => 1, caption => 1, td => 1, th => 1,
4976                        button => 1, marquee => 1, object => 1, html => 1,
4977                       }->{$_->[1]}) {
4978                last INSCOPE;
4979              }
4980            } # INSCOPE
4981                        
4982            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4983            pop @{$self->{open_elements}};
4984                        
4985            !!!next-token;
4986            redo B;
4987          } elsif ($token->{tag_name} eq 'input') {
4988            $reconstruct_active_formatting_elements->($insert_to_current);
4989            
4990            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4991            ## TODO: associate with $self->{form_element} if defined
4992            pop @{$self->{open_elements}};
4993            
4994            !!!next-token;
4995            redo B;
4996          } elsif ($token->{tag_name} eq 'isindex') {
4997            !!!parse-error (type => 'isindex');
4998            
4999            if (defined $self->{form_element}) {
5000              ## Ignore the token
5001              !!!next-token;
5002              redo B;
5003            } else {
5004              my $at = $token->{attributes};
5005              my $form_attrs;
5006              $form_attrs->{action} = $at->{action} if $at->{action};
5007              my $prompt_attr = $at->{prompt};
5008              $at->{name} = {name => 'name', value => 'isindex'};
5009              delete $at->{action};
5010              delete $at->{prompt};
5011              my @tokens = (
5012                            {type => START_TAG_TOKEN, tag_name => 'form',
5013                             attributes => $form_attrs},
5014                            {type => START_TAG_TOKEN, tag_name => 'hr'},
5015                            {type => START_TAG_TOKEN, tag_name => 'p'},
5016                            {type => START_TAG_TOKEN, tag_name => 'label'},
5017                           );
5018              if ($prompt_attr) {
5019                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};
5020              } else {
5021                push @tokens, {type => CHARACTER_TOKEN,
5022                               data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
5023                ## TODO: make this configurable
5024              }
5025              push @tokens,
5026                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},
5027                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
5028                            {type => END_TAG_TOKEN, tag_name => 'label'},
5029                            {type => END_TAG_TOKEN, tag_name => 'p'},
5030                            {type => START_TAG_TOKEN, tag_name => 'hr'},
5031                            {type => END_TAG_TOKEN, tag_name => 'form'};
5032              $token = shift @tokens;
5033              !!!back-token (@tokens);
5034              redo B;
5035            }
5036          } elsif ($token->{tag_name} eq 'textarea') {
5037            my $tag_name = $token->{tag_name};
5038            my $el;
5039            !!!create-element ($el, $token->{tag_name}, $token->{attributes});
5040            
5041            ## TODO: $self->{form_element} if defined
5042            $self->{content_model} = RCDATA_CONTENT_MODEL;
5043            delete $self->{escape}; # MUST
5044            
5045            $insert->($el);
5046            
5047            my $text = '';
5048            !!!next-token;
5049            if ($token->{type} == CHARACTER_TOKEN) {
5050              $token->{data} =~ s/^\x0A//;
5051            unless (length $token->{data}) {            unless (length $token->{data}) {
5052              !!!next-token;              !!!next-token;
             redo B;  
5053            }            }
5054          }          }
5055            while ($token->{type} == CHARACTER_TOKEN) {
5056              $text .= $token->{data};
5057              !!!next-token;
5058            }
5059            if (length $text) {
5060              $el->manakai_append_text ($text);
5061            }
5062            
5063            $self->{content_model} = PCDATA_CONTENT_MODEL;
5064            
5065            if ($token->{type} == END_TAG_TOKEN and
5066                $token->{tag_name} eq $tag_name) {
5067              ## Ignore the token
5068            } else {
5069              !!!parse-error (type => 'in RCDATA:#'.$token->{type});
5070            }
5071            !!!next-token;
5072            redo B;
5073          } elsif ({
5074                    iframe => 1,
5075                    noembed => 1,
5076                    noframes => 1,
5077                    noscript => 0, ## TODO: 1 if scripting is enabled
5078                   }->{$token->{tag_name}}) {
5079            ## NOTE: There is an "as if in body" code clone.
5080            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
5081            redo B;
5082          } elsif ($token->{tag_name} eq 'select') {
5083            $reconstruct_active_formatting_elements->($insert_to_current);
5084            
5085            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5086            
5087            $self->{insertion_mode} = IN_SELECT_IM;
5088            !!!next-token;
5089            redo B;
5090          } elsif ({
5091                    caption => 1, col => 1, colgroup => 1, frame => 1,
5092                    frameset => 1, head => 1, option => 1, optgroup => 1,
5093                    tbody => 1, td => 1, tfoot => 1, th => 1,
5094                    thead => 1, tr => 1,
5095                   }->{$token->{tag_name}}) {
5096            !!!parse-error (type => 'in body:'.$token->{tag_name});
5097            ## Ignore the token
5098            !!!next-token;
5099            redo B;
5100            
5101            ## ISSUE: An issue on HTML5 new elements in the spec.
5102          } else {
5103            $reconstruct_active_formatting_elements->($insert_to_current);
5104            
5105            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5106            
5107            !!!next-token;
5108            redo B;
5109          }
5110        } elsif ($token->{type} == END_TAG_TOKEN) {
5111          if ($token->{tag_name} eq 'body') {
5112            if (@{$self->{open_elements}} > 1 and
5113                $self->{open_elements}->[1]->[1] eq 'body') {
5114              for (@{$self->{open_elements}}) {
5115                unless ({
5116                           dd => 1, dt => 1, li => 1, p => 1, td => 1,
5117                           th => 1, tr => 1, body => 1, html => 1,
5118                         tbody => 1, tfoot => 1, thead => 1,
5119                        }->{$_->[1]}) {
5120                  !!!parse-error (type => 'not closed:'.$_->[1]);
5121                }
5122              }
5123    
5124          !!!parse-error (type => 'after html:#character');            $self->{insertion_mode} = AFTER_BODY_IM;
5125          $phase = 'main';            !!!next-token;
5126          ## reprocess            redo B;
5127            } else {
5128              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5129              ## Ignore the token
5130              !!!next-token;
5131              redo B;
5132            }
5133          } elsif ($token->{tag_name} eq 'html') {
5134            if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {
5135              ## ISSUE: There is an issue in the spec.
5136              if ($self->{open_elements}->[-1]->[1] ne 'body') {
5137                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
5138              }
5139              $self->{insertion_mode} = AFTER_BODY_IM;
5140              ## reprocess
5141              redo B;
5142            } else {
5143              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5144              ## Ignore the token
5145              !!!next-token;
5146              redo B;
5147            }
5148          } elsif ({
5149                    address => 1, blockquote => 1, center => 1, dir => 1,
5150                    div => 1, dl => 1, fieldset => 1, listing => 1,
5151                    menu => 1, ol => 1, pre => 1, ul => 1,
5152                    p => 1,
5153                    dd => 1, dt => 1, li => 1,
5154                    button => 1, marquee => 1, object => 1,
5155                   }->{$token->{tag_name}}) {
5156            ## has an element in scope
5157            my $i;
5158            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5159              my $node = $self->{open_elements}->[$_];
5160              if ($node->[1] eq $token->{tag_name}) {
5161                ## generate implied end tags
5162                if ({
5163                     dd => ($token->{tag_name} ne 'dd'),
5164                     dt => ($token->{tag_name} ne 'dt'),
5165                     li => ($token->{tag_name} ne 'li'),
5166                     p => ($token->{tag_name} ne 'p'),
5167                     td => 1, th => 1, tr => 1,
5168                     tbody => 1, tfoot=> 1, thead => 1,
5169                    }->{$self->{open_elements}->[-1]->[1]}) {
5170                  !!!back-token;
5171                  $token = {type => END_TAG_TOKEN,
5172                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5173                  redo B;
5174                }
5175                $i = $_;
5176                last INSCOPE unless $token->{tag_name} eq 'p';
5177              } elsif ({
5178                        table => 1, caption => 1, td => 1, th => 1,
5179                        button => 1, marquee => 1, object => 1, html => 1,
5180                       }->{$node->[1]}) {
5181                last INSCOPE;
5182              }
5183            } # INSCOPE
5184            
5185            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5186              if (defined $i) {
5187                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5188              } else {
5189                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5190              }
5191            }
5192            
5193            if (defined $i) {
5194              splice @{$self->{open_elements}}, $i;
5195            } elsif ($token->{tag_name} eq 'p') {
5196              ## As if <p>, then reprocess the current token
5197              my $el;
5198              !!!create-element ($el, 'p');
5199              $insert->($el);
5200            }
5201            $clear_up_to_marker->()
5202              if {
5203                button => 1, marquee => 1, object => 1,
5204              }->{$token->{tag_name}};
5205            !!!next-token;
5206          redo B;          redo B;
5207        } elsif ($token->{type} eq 'start tag' or        } elsif ($token->{tag_name} eq 'form') {
5208                 $token->{type} eq 'end tag') {          ## has an element in scope
5209          !!!parse-error (type => 'after html:'.$token->{tag_name});          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5210          $phase = 'main';            my $node = $self->{open_elements}->[$_];
5211          ## reprocess            if ($node->[1] eq $token->{tag_name}) {
5212                ## generate implied end tags
5213                if ({
5214                     dd => 1, dt => 1, li => 1, p => 1,
5215                     td => 1, th => 1, tr => 1,
5216                     tbody => 1, tfoot=> 1, thead => 1,
5217                    }->{$self->{open_elements}->[-1]->[1]}) {
5218                  !!!back-token;
5219                  $token = {type => END_TAG_TOKEN,
5220                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5221                  redo B;
5222                }
5223                last INSCOPE;
5224              } elsif ({
5225                        table => 1, caption => 1, td => 1, th => 1,
5226                        button => 1, marquee => 1, object => 1, html => 1,
5227                       }->{$node->[1]}) {
5228                last INSCOPE;
5229              }
5230            } # INSCOPE
5231            
5232            if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
5233              pop @{$self->{open_elements}};
5234            } else {
5235              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5236            }
5237    
5238            undef $self->{form_element};
5239            !!!next-token;
5240            redo B;
5241          } elsif ({
5242                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5243                   }->{$token->{tag_name}}) {
5244            ## has an element in scope
5245            my $i;
5246            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5247              my $node = $self->{open_elements}->[$_];
5248              if ({
5249                   h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5250                  }->{$node->[1]}) {
5251                ## generate implied end tags
5252                if ({
5253                     dd => 1, dt => 1, li => 1, p => 1,
5254                     td => 1, th => 1, tr => 1,
5255                     tbody => 1, tfoot=> 1, thead => 1,
5256                    }->{$self->{open_elements}->[-1]->[1]}) {
5257                  !!!back-token;
5258                  $token = {type => END_TAG_TOKEN,
5259                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5260                  redo B;
5261                }
5262                $i = $_;
5263                last INSCOPE;
5264              } elsif ({
5265                        table => 1, caption => 1, td => 1, th => 1,
5266                        button => 1, marquee => 1, object => 1, html => 1,
5267                       }->{$node->[1]}) {
5268                last INSCOPE;
5269              }
5270            } # INSCOPE
5271            
5272            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5273              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5274            }
5275            
5276            splice @{$self->{open_elements}}, $i if defined $i;
5277            !!!next-token;
5278            redo B;
5279          } elsif ({
5280                    a => 1,
5281                    b => 1, big => 1, em => 1, font => 1, i => 1,
5282                    nobr => 1, s => 1, small => 1, strile => 1,
5283                    strong => 1, tt => 1, u => 1,
5284                   }->{$token->{tag_name}}) {
5285            $formatting_end_tag->($token->{tag_name});
5286            redo B;
5287          } elsif ($token->{tag_name} eq 'br') {
5288            !!!parse-error (type => 'unmatched end tag:br');
5289    
5290            ## As if <br>
5291            $reconstruct_active_formatting_elements->($insert_to_current);
5292            
5293            my $el;
5294            !!!create-element ($el, 'br');
5295            $insert->($el);
5296            
5297            ## Ignore the token.
5298            !!!next-token;
5299          redo B;          redo B;
5300        } elsif ($token->{type} eq 'end-of-file') {        } elsif ({
5301          ## Stop parsing                  caption => 1, col => 1, colgroup => 1, frame => 1,
5302          last B;                  frameset => 1, head => 1, option => 1, optgroup => 1,
5303                    tbody => 1, td => 1, tfoot => 1, th => 1,
5304                    thead => 1, tr => 1,
5305                    area => 1, basefont => 1, bgsound => 1,
5306                    embed => 1, hr => 1, iframe => 1, image => 1,
5307                    img => 1, input => 1, isindex => 1, noembed => 1,
5308                    noframes => 1, param => 1, select => 1, spacer => 1,
5309                    table => 1, textarea => 1, wbr => 1,
5310                    noscript => 0, ## TODO: if scripting is enabled
5311                   }->{$token->{tag_name}}) {
5312            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5313            ## Ignore the token
5314            !!!next-token;
5315            redo B;
5316            
5317            ## ISSUE: Issue on HTML5 new elements in spec
5318            
5319        } else {        } else {
5320          die "$0: $token->{type}: Unknown token";          ## Step 1
5321            my $node_i = -1;
5322            my $node = $self->{open_elements}->[$node_i];
5323    
5324            ## Step 2
5325            S2: {
5326              if ($node->[1] eq $token->{tag_name}) {
5327                ## Step 1
5328                ## generate implied end tags
5329                if ({
5330                     dd => 1, dt => 1, li => 1, p => 1,
5331                     td => 1, th => 1, tr => 1,
5332                     tbody => 1, tfoot => 1, thead => 1,
5333                    }->{$self->{open_elements}->[-1]->[1]}) {
5334                  !!!back-token;
5335                  $token = {type => END_TAG_TOKEN,
5336                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5337                  redo B;
5338                }
5339            
5340                ## Step 2
5341                if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
5342                  ## NOTE: <x><y></x>
5343                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5344                }
5345                
5346                ## Step 3
5347                splice @{$self->{open_elements}}, $node_i;
5348    
5349                !!!next-token;
5350                last S2;
5351              } else {
5352                ## Step 3
5353                if (not $formatting_category->{$node->[1]} and
5354                    #not $phrasing_category->{$node->[1]} and
5355                    ($special_category->{$node->[1]} or
5356                     $scoping_category->{$node->[1]})) {
5357                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5358                  ## Ignore the token
5359                  !!!next-token;
5360                  last S2;
5361                }
5362              }
5363              
5364              ## Step 4
5365              $node_i--;
5366              $node = $self->{open_elements}->[$node_i];
5367              
5368              ## Step 5;
5369              redo S2;
5370            } # S2
5371            redo B;
5372        }        }
5373      }      }
5374        redo B;
5375    } # B    } # B
5376    
5377      ## NOTE: The "trailing end" phase in HTML5 is split into
5378      ## two insertion modes: "after html body" and "after html frameset".
5379      ## NOTE: States in the main stage is preserved while
5380      ## the parser stays in the trailing end phase. # MUST
5381    
5382    ## Stop parsing # MUST    ## Stop parsing # MUST
5383        
5384    ## TODO: script stuffs    ## TODO: script stuffs
# Line 5094  sub set_inner_html ($$$) { Line 5390  sub set_inner_html ($$$) {
5390    my $s = \$_[0];    my $s = \$_[0];
5391    my $onerror = $_[1];    my $onerror = $_[1];
5392    
5393      ## ISSUE: Should {confident} be true?
5394    
5395    my $nt = $node->node_type;    my $nt = $node->node_type;
5396    if ($nt == 9) {    if ($nt == 9) {
5397      # MUST      # MUST
# Line 5167  sub set_inner_html ($$$) { Line 5465  sub set_inner_html ($$$) {
5465    
5466      ## Step 2      ## Step 2
5467      my $node_ln = $node->local_name;      my $node_ln = $node->local_name;
5468      $p->{content_model_flag} = {      $p->{content_model} = {
5469        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
5470        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
5471        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
5472        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
5473        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
5474        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
5475        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
5476        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
5477        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
5478        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
5479      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
5480         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
5481            unless defined $p->{content_model};
5482            ## ISSUE: What is "the name of the element"? local name?
5483    
5484      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $node_ln];
5485    
# Line 5244  sub set_inner_html ($$$) { Line 5544  sub set_inner_html ($$$) {
5544    
5545  } # tree construction stage  } # tree construction stage
5546    
5547  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
5548    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 = lc $child->tag_name; ## ISSUE: Definition of "lowercase"  
       $s .= '<' . $tag_name;  
   
       ## ISSUE: Non-html elements  
   
       my @attrs = @{$child->attributes}; # sort order MUST be stable  
       for my $attr (@attrs) { # order is implementation dependent  
         my $attr_name = lc $attr->name; ## ISSUE: Definition of "lowercase"  
         $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};  
   
       if (not $in_cdata and {  
         style => 1, script => 1, xmp => 1, iframe => 1,  
         noembed => 1, noframes => 1, noscript => 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  
5549    
5550  1;  1;
5551  # $Date$  # $Date$

Legend:
Removed from v.1.20  
changed lines
  Added in v.1.70

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24