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

Legend:
Removed from v.1.16  
changed lines
  Added in v.1.65

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24