/[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.13 by wakaba, Sat Jun 23 05:29:48 2007 UTC revision 1.61 by wakaba, Sun Nov 4 04:15:06 2007 UTC
# Line 2  package Whatpm::HTML; Line 2  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    
5  ## This is an early version of an HTML parser.  ## ISSUE:
6    ## var doc = implementation.createDocument (null, null, null);
7    ## doc.write ('');
8    ## alert (doc.compatMode);
9    
10    ## ISSUE: HTML5 revision 967 says that the encoding layer MUST NOT
11    ## strip BOM and the HTML layer MUST ignore it.  Whether we can do it
12    ## is not yet clear.
13    ## "{U+FEFF}..." in UTF-16BE/UTF-16LE is three or four characters?
14    ## "{U+FEFF}..." in GB18030?
15    
16  my $permitted_slash_tag_name = {  my $permitted_slash_tag_name = {
17    base => 1,    base => 1,
# Line 18  my $permitted_slash_tag_name = { Line 27  my $permitted_slash_tag_name = {
27    input => 1,    input => 1,
28  };  };
29    
 my $entity_char = {  
   AElig => "\x{00C6}",  
   Aacute => "\x{00C1}",  
   Acirc => "\x{00C2}",  
   Agrave => "\x{00C0}",  
   Alpha => "\x{0391}",  
   Aring => "\x{00C5}",  
   Atilde => "\x{00C3}",  
   Auml => "\x{00C4}",  
   Beta => "\x{0392}",  
   Ccedil => "\x{00C7}",  
   Chi => "\x{03A7}",  
   Dagger => "\x{2021}",  
   Delta => "\x{0394}",  
   ETH => "\x{00D0}",  
   Eacute => "\x{00C9}",  
   Ecirc => "\x{00CA}",  
   Egrave => "\x{00C8}",  
   Epsilon => "\x{0395}",  
   Eta => "\x{0397}",  
   Euml => "\x{00CB}",  
   Gamma => "\x{0393}",  
   Iacute => "\x{00CD}",  
   Icirc => "\x{00CE}",  
   Igrave => "\x{00CC}",  
   Iota => "\x{0399}",  
   Iuml => "\x{00CF}",  
   Kappa => "\x{039A}",  
   Lambda => "\x{039B}",  
   Mu => "\x{039C}",  
   Ntilde => "\x{00D1}",  
   Nu => "\x{039D}",  
   OElig => "\x{0152}",  
   Oacute => "\x{00D3}",  
   Ocirc => "\x{00D4}",  
   Ograve => "\x{00D2}",  
   Omega => "\x{03A9}",  
   Omicron => "\x{039F}",  
   Oslash => "\x{00D8}",  
   Otilde => "\x{00D5}",  
   Ouml => "\x{00D6}",  
   Phi => "\x{03A6}",  
   Pi => "\x{03A0}",  
   Prime => "\x{2033}",  
   Psi => "\x{03A8}",  
   Rho => "\x{03A1}",  
   Scaron => "\x{0160}",  
   Sigma => "\x{03A3}",  
   THORN => "\x{00DE}",  
   Tau => "\x{03A4}",  
   Theta => "\x{0398}",  
   Uacute => "\x{00DA}",  
   Ucirc => "\x{00DB}",  
   Ugrave => "\x{00D9}",  
   Upsilon => "\x{03A5}",  
   Uuml => "\x{00DC}",  
   Xi => "\x{039E}",  
   Yacute => "\x{00DD}",  
   Yuml => "\x{0178}",  
   Zeta => "\x{0396}",  
   aacute => "\x{00E1}",  
   acirc => "\x{00E2}",  
   acute => "\x{00B4}",  
   aelig => "\x{00E6}",  
   agrave => "\x{00E0}",  
   alefsym => "\x{2135}",  
   alpha => "\x{03B1}",  
   amp => "\x{0026}",  
   AMP => "\x{0026}",  
   and => "\x{2227}",  
   ang => "\x{2220}",  
   apos => "\x{0027}",  
   aring => "\x{00E5}",  
   asymp => "\x{2248}",  
   atilde => "\x{00E3}",  
   auml => "\x{00E4}",  
   bdquo => "\x{201E}",  
   beta => "\x{03B2}",  
   brvbar => "\x{00A6}",  
   bull => "\x{2022}",  
   cap => "\x{2229}",  
   ccedil => "\x{00E7}",  
   cedil => "\x{00B8}",  
   cent => "\x{00A2}",  
   chi => "\x{03C7}",  
   circ => "\x{02C6}",  
   clubs => "\x{2663}",  
   cong => "\x{2245}",  
   copy => "\x{00A9}",  
   COPY => "\x{00A9}",  
   crarr => "\x{21B5}",  
   cup => "\x{222A}",  
   curren => "\x{00A4}",  
   dArr => "\x{21D3}",  
   dagger => "\x{2020}",  
   darr => "\x{2193}",  
   deg => "\x{00B0}",  
   delta => "\x{03B4}",  
   diams => "\x{2666}",  
   divide => "\x{00F7}",  
   eacute => "\x{00E9}",  
   ecirc => "\x{00EA}",  
   egrave => "\x{00E8}",  
   empty => "\x{2205}",  
   emsp => "\x{2003}",  
   ensp => "\x{2002}",  
   epsilon => "\x{03B5}",  
   equiv => "\x{2261}",  
   eta => "\x{03B7}",  
   eth => "\x{00F0}",  
   euml => "\x{00EB}",  
   euro => "\x{20AC}",  
   exist => "\x{2203}",  
   fnof => "\x{0192}",  
   forall => "\x{2200}",  
   frac12 => "\x{00BD}",  
   frac14 => "\x{00BC}",  
   frac34 => "\x{00BE}",  
   frasl => "\x{2044}",  
   gamma => "\x{03B3}",  
   ge => "\x{2265}",  
   gt => "\x{003E}",  
   GT => "\x{003E}",  
   hArr => "\x{21D4}",  
   harr => "\x{2194}",  
   hearts => "\x{2665}",  
   hellip => "\x{2026}",  
   iacute => "\x{00ED}",  
   icirc => "\x{00EE}",  
   iexcl => "\x{00A1}",  
   igrave => "\x{00EC}",  
   image => "\x{2111}",  
   infin => "\x{221E}",  
   int => "\x{222B}",  
   iota => "\x{03B9}",  
   iquest => "\x{00BF}",  
   isin => "\x{2208}",  
   iuml => "\x{00EF}",  
   kappa => "\x{03BA}",  
   lArr => "\x{21D0}",  
   lambda => "\x{03BB}",  
   lang => "\x{2329}",  
   laquo => "\x{00AB}",  
   larr => "\x{2190}",  
   lceil => "\x{2308}",  
   ldquo => "\x{201C}",  
   le => "\x{2264}",  
   lfloor => "\x{230A}",  
   lowast => "\x{2217}",  
   loz => "\x{25CA}",  
   lrm => "\x{200E}",  
   lsaquo => "\x{2039}",  
   lsquo => "\x{2018}",  
   lt => "\x{003C}",  
   LT => "\x{003C}",  
   macr => "\x{00AF}",  
   mdash => "\x{2014}",  
   micro => "\x{00B5}",  
   middot => "\x{00B7}",  
   minus => "\x{2212}",  
   mu => "\x{03BC}",  
   nabla => "\x{2207}",  
   nbsp => "\x{00A0}",  
   ndash => "\x{2013}",  
   ne => "\x{2260}",  
   ni => "\x{220B}",  
   not => "\x{00AC}",  
   notin => "\x{2209}",  
   nsub => "\x{2284}",  
   ntilde => "\x{00F1}",  
   nu => "\x{03BD}",  
   oacute => "\x{00F3}",  
   ocirc => "\x{00F4}",  
   oelig => "\x{0153}",  
   ograve => "\x{00F2}",  
   oline => "\x{203E}",  
   omega => "\x{03C9}",  
   omicron => "\x{03BF}",  
   oplus => "\x{2295}",  
   or => "\x{2228}",  
   ordf => "\x{00AA}",  
   ordm => "\x{00BA}",  
   oslash => "\x{00F8}",  
   otilde => "\x{00F5}",  
   otimes => "\x{2297}",  
   ouml => "\x{00F6}",  
   para => "\x{00B6}",  
   part => "\x{2202}",  
   permil => "\x{2030}",  
   perp => "\x{22A5}",  
   phi => "\x{03C6}",  
   pi => "\x{03C0}",  
   piv => "\x{03D6}",  
   plusmn => "\x{00B1}",  
   pound => "\x{00A3}",  
   prime => "\x{2032}",  
   prod => "\x{220F}",  
   prop => "\x{221D}",  
   psi => "\x{03C8}",  
   quot => "\x{0022}",  
   QUOT => "\x{0022}",  
   rArr => "\x{21D2}",  
   radic => "\x{221A}",  
   rang => "\x{232A}",  
   raquo => "\x{00BB}",  
   rarr => "\x{2192}",  
   rceil => "\x{2309}",  
   rdquo => "\x{201D}",  
   real => "\x{211C}",  
   reg => "\x{00AE}",  
   REG => "\x{00AE}",  
   rfloor => "\x{230B}",  
   rho => "\x{03C1}",  
   rlm => "\x{200F}",  
   rsaquo => "\x{203A}",  
   rsquo => "\x{2019}",  
   sbquo => "\x{201A}",  
   scaron => "\x{0161}",  
   sdot => "\x{22C5}",  
   sect => "\x{00A7}",  
   shy => "\x{00AD}",  
   sigma => "\x{03C3}",  
   sigmaf => "\x{03C2}",  
   sim => "\x{223C}",  
   spades => "\x{2660}",  
   sub => "\x{2282}",  
   sube => "\x{2286}",  
   sum => "\x{2211}",  
   sup => "\x{2283}",  
   sup1 => "\x{00B9}",  
   sup2 => "\x{00B2}",  
   sup3 => "\x{00B3}",  
   supe => "\x{2287}",  
   szlig => "\x{00DF}",  
   tau => "\x{03C4}",  
   there4 => "\x{2234}",  
   theta => "\x{03B8}",  
   thetasym => "\x{03D1}",  
   thinsp => "\x{2009}",  
   thorn => "\x{00FE}",  
   tilde => "\x{02DC}",  
   times => "\x{00D7}",  
   trade => "\x{2122}",  
   uArr => "\x{21D1}",  
   uacute => "\x{00FA}",  
   uarr => "\x{2191}",  
   ucirc => "\x{00FB}",  
   ugrave => "\x{00F9}",  
   uml => "\x{00A8}",  
   upsih => "\x{03D2}",  
   upsilon => "\x{03C5}",  
   uuml => "\x{00FC}",  
   weierp => "\x{2118}",  
   xi => "\x{03BE}",  
   yacute => "\x{00FD}",  
   yen => "\x{00A5}",  
   yuml => "\x{00FF}",  
   zeta => "\x{03B6}",  
   zwj => "\x{200D}",  
   zwnj => "\x{200C}",  
 }; # $entity_char  
   
30  my $c1_entity_char = {  my $c1_entity_char = {
31    0x80 => 0x20AC,    0x80 => 0x20AC,
32    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 361  sub parse_string ($$$;$) { Line 108  sub parse_string ($$$;$) {
108        $line++;        $line++;
109        $column = 0;        $column = 0;
110      } elsif ($self->{next_input_character} == 0x000D) { # CR      } elsif ($self->{next_input_character} == 0x000D) { # CR
111        if ($i >= length $$s) {        $i++ if substr ($$s, $i, 1) eq "\x0A";
         #  
       } else {  
         my $next_char = ord substr $$s, $i++, 1;  
         if ($next_char == 0x000A) { # LF  
           #  
         } else {  
           push @{$self->{char}}, $next_char;  
         }  
       }  
112        $self->{next_input_character} = 0x000A; # LF # MUST        $self->{next_input_character} = 0x000A; # LF # MUST
113        $line++;        $line++;
114        $column = 0;        $column = 0;
# Line 409  sub new ($) { Line 147  sub new ($) {
147    $self->{parse_error} = sub {    $self->{parse_error} = sub {
148      #      #
149    };    };
150      $self->{application_cache_selection} = sub {
151        #
152      };
153    return $self;    return $self;
154  } # new  } # new
155    
156    sub CM_ENTITY () { 0b001 } # & markup in data
157    sub CM_LIMITED_MARKUP () { 0b010 } # < markup in data (limited)
158    sub CM_FULL_MARKUP () { 0b100 } # < markup in data (any)
159    
160    sub PLAINTEXT_CONTENT_MODEL () { 0 }
161    sub CDATA_CONTENT_MODEL () { CM_LIMITED_MARKUP }
162    sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
163    sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
164    
165    sub DATA_STATE () { 0 }
166    sub ENTITY_DATA_STATE () { 1 }
167    sub TAG_OPEN_STATE () { 2 }
168    sub CLOSE_TAG_OPEN_STATE () { 3 }
169    sub TAG_NAME_STATE () { 4 }
170    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
171    sub ATTRIBUTE_NAME_STATE () { 6 }
172    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
173    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
174    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
175    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
176    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
177    sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
178    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
179    sub COMMENT_START_STATE () { 14 }
180    sub COMMENT_START_DASH_STATE () { 15 }
181    sub COMMENT_STATE () { 16 }
182    sub COMMENT_END_STATE () { 17 }
183    sub COMMENT_END_DASH_STATE () { 18 }
184    sub BOGUS_COMMENT_STATE () { 19 }
185    sub DOCTYPE_STATE () { 20 }
186    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
187    sub DOCTYPE_NAME_STATE () { 22 }
188    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
189    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
190    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
191    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
192    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
193    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
194    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
195    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
196    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
197    sub BOGUS_DOCTYPE_STATE () { 32 }
198    
199    sub DOCTYPE_TOKEN () { 1 }
200    sub COMMENT_TOKEN () { 2 }
201    sub START_TAG_TOKEN () { 3 }
202    sub END_TAG_TOKEN () { 4 }
203    sub END_OF_FILE_TOKEN () { 5 }
204    sub CHARACTER_TOKEN () { 6 }
205    
206    sub AFTER_HTML_IMS () { 0b100 }
207    sub HEAD_IMS ()       { 0b1000 }
208    sub BODY_IMS ()       { 0b10000 }
209    sub BODY_TABLE_IMS () { 0b100000 }
210    sub TABLE_IMS ()      { 0b1000000 }
211    sub ROW_IMS ()        { 0b10000000 }
212    sub BODY_AFTER_IMS () { 0b100000000 }
213    sub FRAME_IMS ()      { 0b1000000000 }
214    
215    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
216    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
217    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
218    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
219    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
220    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
221    sub IN_BODY_IM () { BODY_IMS }
222    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
223    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
224    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
225    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
226    sub IN_TABLE_IM () { TABLE_IMS }
227    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
228    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
229    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
230    sub IN_SELECT_IM () { 0b01 }
231    sub IN_COLUMN_GROUP_IM () { 0b10 }
232    
233  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
234    
235  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
236    my $self = shift;    my $self = shift;
237    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
238    $self->{content_model_flag} = 'PCDATA'; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
239    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
240    undef $self->{current_attribute};    undef $self->{current_attribute};
241    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
# Line 426  sub _initialize_tokenizer ($) { Line 244  sub _initialize_tokenizer ($) {
244    # $self->{next_input_character}    # $self->{next_input_character}
245    !!!next-input-character;    !!!next-input-character;
246    $self->{token} = [];    $self->{token} = [];
247      # $self->{escape}
248  } # _initialize_tokenizer  } # _initialize_tokenizer
249    
250  ## A token has:  ## A token has:
251  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
252  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
253  ##   ->{name} (DOCTYPE, start tag (tagname), end tag (tagname))  ##   ->{name} (DOCTYPE_TOKEN)
254      ## ISSUE: the spec need s/tagname/tag name/  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
255  ##   ->{error} == 1 or 0 (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
256  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
257  ##   ->{data} (comment, character)  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)
258    ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
259  ## Macros  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
 ##   Macros MUST be preceded by three EXCLAMATION MARKs.  
 ##   emit ($token)  
 ##     Emits the specified token.  
260    
261  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
262    
# Line 450  sub _initialize_tokenizer ($) { Line 266  sub _initialize_tokenizer ($) {
266  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
267  ## and removed from the list.  ## and removed from the list.
268    
269    ## NOTE: HTML5 "Writing HTML documents" section, applied to
270    ## documents and not to user agents and conformance checkers,
271    ## contains some requirements that are not detected by the
272    ## parsing algorithm:
273    ## - Some requirements on character encoding declarations. ## TODO
274    ## - "Elements MUST NOT contain content that their content model disallows."
275    ##   ... Some are parse error, some are not (will be reported by c.c.).
276    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
277    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
278    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
279    
280    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
281    ## be detected by the HTML5 parsing algorithm:
282    ## - Text,
283    
284  sub _get_next_token ($) {  sub _get_next_token ($) {
285    my $self = shift;    my $self = shift;
286    if (@{$self->{token}}) {    if (@{$self->{token}}) {
# Line 457  sub _get_next_token ($) { Line 288  sub _get_next_token ($) {
288    }    }
289    
290    A: {    A: {
291      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
292        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_input_character} == 0x0026) { # &
293          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA
294              $self->{content_model_flag} eq 'RCDATA') {            $self->{state} = ENTITY_DATA_STATE;
           $self->{state} = 'entity data';  
295            !!!next-input-character;            !!!next-input-character;
296            redo A;            redo A;
297          } else {          } else {
298            #            #
299          }          }
300        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_input_character} == 0x002D) { # -
301          if ($self->{content_model_flag} eq 'RCDATA' or          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
             $self->{content_model_flag} eq 'CDATA') {  
302            unless ($self->{escape}) {            unless ($self->{escape}) {
303              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_input_character}->[0] == 0x002D and # -
304                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_input_character}->[1] == 0x0021 and # !
# Line 481  sub _get_next_token ($) { Line 310  sub _get_next_token ($) {
310                    
311          #          #
312        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_input_character} == 0x003C) { # <
313          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
314              (($self->{content_model_flag} eq 'CDATA' or              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
               $self->{content_model_flag} eq 'RCDATA') and  
315               not $self->{escape})) {               not $self->{escape})) {
316            $self->{state} = 'tag open';            $self->{state} = TAG_OPEN_STATE;
317            !!!next-input-character;            !!!next-input-character;
318            redo A;            redo A;
319          } else {          } else {
# Line 493  sub _get_next_token ($) { Line 321  sub _get_next_token ($) {
321          }          }
322        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
323          if ($self->{escape} and          if ($self->{escape} and
324              ($self->{content_model_flag} eq 'RCDATA' or              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
              $self->{content_model_flag} eq 'CDATA')) {  
325            if ($self->{prev_input_character}->[0] == 0x002D and # -            if ($self->{prev_input_character}->[0] == 0x002D and # -
326                $self->{prev_input_character}->[1] == 0x002D) { # -                $self->{prev_input_character}->[1] == 0x002D) { # -
327              delete $self->{escape};              delete $self->{escape};
# Line 503  sub _get_next_token ($) { Line 330  sub _get_next_token ($) {
330                    
331          #          #
332        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
333          !!!emit ({type => 'end-of-file'});          !!!emit ({type => END_OF_FILE_TOKEN});
334          last A; ## TODO: ok?          last A; ## TODO: ok?
335        }        }
336        # Anything else        # Anything else
337        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
338                     data => chr $self->{next_input_character}};                     data => chr $self->{next_input_character}};
339        ## Stay in the data state        ## Stay in the data state
340        !!!next-input-character;        !!!next-input-character;
# Line 515  sub _get_next_token ($) { Line 342  sub _get_next_token ($) {
342        !!!emit ($token);        !!!emit ($token);
343    
344        redo A;        redo A;
345      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
346        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
347                
348        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);
349    
350        $self->{state} = 'data';        $self->{state} = DATA_STATE;
351        # next-input-character is already done        # next-input-character is already done
352    
353        unless (defined $token) {        unless (defined $token) {
354          !!!emit ({type => 'character', data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&'});
355        } else {        } else {
356          !!!emit ($token);          !!!emit ($token);
357        }        }
358    
359        redo A;        redo A;
360      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
361        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
           $self->{content_model_flag} eq 'CDATA') {  
362          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_input_character} == 0x002F) { # /
363            !!!next-input-character;            !!!next-input-character;
364            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
365            redo A;            redo A;
366          } else {          } else {
367            ## reconsume            ## reconsume
368            $self->{state} = 'data';            $self->{state} = DATA_STATE;
369    
370            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
371    
372            redo A;            redo A;
373          }          }
374        } elsif ($self->{content_model_flag} eq 'PCDATA') {        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
375          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_input_character} == 0x0021) { # !
376            $self->{state} = 'markup declaration open';            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
377            !!!next-input-character;            !!!next-input-character;
378            redo A;            redo A;
379          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_input_character} == 0x002F) { # /
380            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
381            !!!next-input-character;            !!!next-input-character;
382            redo A;            redo A;
383          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_input_character} and
384                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_input_character} <= 0x005A) { # A..Z
385            $self->{current_token}            $self->{current_token}
386              = {type => 'start tag',              = {type => START_TAG_TOKEN,
387                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_input_character} + 0x0020)};
388            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
389            !!!next-input-character;            !!!next-input-character;
390            redo A;            redo A;
391          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_input_character} and
392                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_input_character} <= 0x007A) { # a..z
393            $self->{current_token} = {type => 'start tag',            $self->{current_token} = {type => START_TAG_TOKEN,
394                              tag_name => chr ($self->{next_input_character})};                              tag_name => chr ($self->{next_input_character})};
395            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
396            !!!next-input-character;            !!!next-input-character;
397            redo A;            redo A;
398          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_input_character} == 0x003E) { # >
399            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag');
400            $self->{state} = 'data';            $self->{state} = DATA_STATE;
401            !!!next-input-character;            !!!next-input-character;
402    
403            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});
404    
405            redo A;            redo A;
406          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_input_character} == 0x003F) { # ?
407            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio');
408            $self->{state} = 'bogus comment';            $self->{state} = BOGUS_COMMENT_STATE;
409            ## $self->{next_input_character} is intentionally left as is            ## $self->{next_input_character} is intentionally left as is
410            redo A;            redo A;
411          } else {          } else {
412            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago');
413            $self->{state} = 'data';            $self->{state} = DATA_STATE;
414            ## reconsume            ## reconsume
415    
416            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
417    
418            redo A;            redo A;
419          }          }
420        } else {        } else {
421          die "$0: $self->{content_model_flag}: Unknown content model flag";          die "$0: $self->{content_model} in tag open";
422        }        }
423      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
424        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
425            $self->{content_model_flag} eq 'CDATA') {          if (defined $self->{last_emitted_start_tag_name}) {
426          my @next_char;            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
427          TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            my @next_char;
428              TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
429                push @next_char, $self->{next_input_character};
430                my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
431                my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
432                if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {
433                  !!!next-input-character;
434                  next TAGNAME;
435                } else {
436                  $self->{next_input_character} = shift @next_char; # reconsume
437                  !!!back-next-input-character (@next_char);
438                  $self->{state} = DATA_STATE;
439    
440                  !!!emit ({type => CHARACTER_TOKEN, data => '</'});
441      
442                  redo A;
443                }
444              }
445            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_input_character};
446            my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);        
447            my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;            unless ($self->{next_input_character} == 0x0009 or # HT
448            if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {                    $self->{next_input_character} == 0x000A or # LF
449              !!!next-input-character;                    $self->{next_input_character} == 0x000B or # VT
450              next TAGNAME;                    $self->{next_input_character} == 0x000C or # FF
451            } else {                    $self->{next_input_character} == 0x0020 or # SP
452              !!!parse-error (type => 'unmatched end tag');                    $self->{next_input_character} == 0x003E or # >
453                      $self->{next_input_character} == 0x002F or # /
454                      $self->{next_input_character} == -1) {
455              $self->{next_input_character} = shift @next_char; # reconsume              $self->{next_input_character} = shift @next_char; # reconsume
456              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
457              $self->{state} = 'data';              $self->{state} = DATA_STATE;
458                !!!emit ({type => CHARACTER_TOKEN, data => '</'});
             !!!emit ({type => 'character', data => '</'});  
   
459              redo A;              redo A;
460              } else {
461                $self->{next_input_character} = shift @next_char;
462                !!!back-next-input-character (@next_char);
463                # and consume...
464            }            }
         }  
         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;  
465          } else {          } else {
466            $self->{next_input_character} = shift @next_char;            ## No start tag token has ever been emitted
467            !!!back-next-input-character (@next_char);            # next-input-character is already done
468            # and consume...            $self->{state} = DATA_STATE;
469              !!!emit ({type => CHARACTER_TOKEN, data => '</'});
470              redo A;
471          }          }
472        }        }
473                
474        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_input_character} and
475            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_input_character} <= 0x005A) { # A..Z
476          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
477                            tag_name => chr ($self->{next_input_character} + 0x0020)};                            tag_name => chr ($self->{next_input_character} + 0x0020)};
478          $self->{state} = 'tag name';          $self->{state} = TAG_NAME_STATE;
479          !!!next-input-character;          !!!next-input-character;
480          redo A;          redo A;
481        } elsif (0x0061 <= $self->{next_input_character} and        } elsif (0x0061 <= $self->{next_input_character} and
482                 $self->{next_input_character} <= 0x007A) { # a..z                 $self->{next_input_character} <= 0x007A) { # a..z
483          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
484                            tag_name => chr ($self->{next_input_character})};                            tag_name => chr ($self->{next_input_character})};
485          $self->{state} = 'tag name';          $self->{state} = TAG_NAME_STATE;
486          !!!next-input-character;          !!!next-input-character;
487          redo A;          redo A;
488        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
489          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag');
490          $self->{state} = 'data';          $self->{state} = DATA_STATE;
491          !!!next-input-character;          !!!next-input-character;
492          redo A;          redo A;
493        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
494          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
495          $self->{state} = 'data';          $self->{state} = DATA_STATE;
496          # reconsume          # reconsume
497    
498          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</'});
499    
500          redo A;          redo A;
501        } else {        } else {
502          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
503          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
504          ## $self->{next_input_character} is intentionally left as is          ## $self->{next_input_character} is intentionally left as is
505          redo A;          redo A;
506        }        }
507      } elsif ($self->{state} eq 'tag name') {      } elsif ($self->{state} == TAG_NAME_STATE) {
508        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
509            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
510            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
511            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
512            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
513          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
514          !!!next-input-character;          !!!next-input-character;
515          redo A;          redo A;
516        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
517          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
518              $self->{current_token}->{first_start_tag}
519                  = not defined $self->{last_emitted_start_tag_name};
520            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
521          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
522            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
523            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
524              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
525            }            }
526          } else {          } else {
527            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
528          }          }
529          $self->{state} = 'data';          $self->{state} = DATA_STATE;
530          !!!next-input-character;          !!!next-input-character;
531    
532          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
533    
534          redo A;          redo A;
535        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 709  sub _get_next_token ($) { Line 539  sub _get_next_token ($) {
539          ## Stay in this state          ## Stay in this state
540          !!!next-input-character;          !!!next-input-character;
541          redo A;          redo A;
542        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
543          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
544          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
545              $self->{current_token}->{first_start_tag}
546                  = not defined $self->{last_emitted_start_tag_name};
547            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
548          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
549            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
550            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
551              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
552            }            }
553          } else {          } else {
554            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
555          }          }
556          $self->{state} = 'data';          $self->{state} = DATA_STATE;
557          # reconsume          # reconsume
558    
559          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
560    
561          redo A;          redo A;
562        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
563          !!!next-input-character;          !!!next-input-character;
564          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
565              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
566              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
567            # permitted slash            # permitted slash
568            #            #
569          } else {          } else {
570            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
571          }          }
572          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
573          # next-input-character is already done          # next-input-character is already done
574          redo A;          redo A;
575        } else {        } else {
# Line 749  sub _get_next_token ($) { Line 579  sub _get_next_token ($) {
579          !!!next-input-character;          !!!next-input-character;
580          redo A;          redo A;
581        }        }
582      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
583        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
584            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
585            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 759  sub _get_next_token ($) { Line 589  sub _get_next_token ($) {
589          !!!next-input-character;          !!!next-input-character;
590          redo A;          redo A;
591        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
592          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
593              $self->{current_token}->{first_start_tag}
594                  = not defined $self->{last_emitted_start_tag_name};
595            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
596          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
597            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
598            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
599              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
600            }            }
601          } else {          } else {
602            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
603          }          }
604          $self->{state} = 'data';          $self->{state} = DATA_STATE;
605          !!!next-input-character;          !!!next-input-character;
606    
607          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
608    
609          redo A;          redo A;
610        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
611                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_input_character} <= 0x005A) { # A..Z
612          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),
613                                value => ''};                                value => ''};
614          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
615          !!!next-input-character;          !!!next-input-character;
616          redo A;          redo A;
617        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
618          !!!next-input-character;          !!!next-input-character;
619          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
620              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
621              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
622            # permitted slash            # permitted slash
623            #            #
# Line 796  sub _get_next_token ($) { Line 627  sub _get_next_token ($) {
627          ## Stay in the state          ## Stay in the state
628          # next-input-character is already done          # next-input-character is already done
629          redo A;          redo A;
630        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
631          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
632          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
633              $self->{current_token}->{first_start_tag}
634                  = not defined $self->{last_emitted_start_tag_name};
635            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
636          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
637            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
638            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
639              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
640            }            }
641          } else {          } else {
642            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
643          }          }
644          $self->{state} = 'data';          $self->{state} = DATA_STATE;
645          # reconsume          # reconsume
646    
647          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
648    
649          redo A;          redo A;
650        } else {        } else {
651          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute} = {name => chr ($self->{next_input_character}),
652                                value => ''};                                value => ''};
653          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
654          !!!next-input-character;          !!!next-input-character;
655          redo A;          redo A;
656        }        }
657      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
658        my $before_leave = sub {        my $before_leave = sub {
659          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
660              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
661            !!!parse-error (type => 'dupulicate attribute');            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});
662            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
663          } else {          } else {
664            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
# Line 841  sub _get_next_token ($) { Line 672  sub _get_next_token ($) {
672            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
673            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
674          $before_leave->();          $before_leave->();
675          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
676          !!!next-input-character;          !!!next-input-character;
677          redo A;          redo A;
678        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_input_character} == 0x003D) { # =
679          $before_leave->();          $before_leave->();
680          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
681          !!!next-input-character;          !!!next-input-character;
682          redo A;          redo A;
683        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
684          $before_leave->();          $before_leave->();
685          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
686              $self->{current_token}->{first_start_tag}
687                  = not defined $self->{last_emitted_start_tag_name};
688            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
689          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
690            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
691            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
692              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
693            }            }
694          } else {          } else {
695            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
696          }          }
697          $self->{state} = 'data';          $self->{state} = DATA_STATE;
698          !!!next-input-character;          !!!next-input-character;
699    
700          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
701    
702          redo A;          redo A;
703        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 878  sub _get_next_token ($) { Line 710  sub _get_next_token ($) {
710          $before_leave->();          $before_leave->();
711          !!!next-input-character;          !!!next-input-character;
712          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
713              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
714              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
715            # permitted slash            # permitted slash
716            #            #
717          } else {          } else {
718            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
719          }          }
720          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
721          # next-input-character is already done          # next-input-character is already done
722          redo A;          redo A;
723        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
724          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
725          $before_leave->();          $before_leave->();
726          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
727              $self->{current_token}->{first_start_tag}
728                  = not defined $self->{last_emitted_start_tag_name};
729            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
730          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
731            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
732            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
733              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
734            }            }
735          } else {          } else {
736            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
737          }          }
738          $self->{state} = 'data';          $self->{state} = DATA_STATE;
739          # reconsume          # reconsume
740    
741          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
742    
743          redo A;          redo A;
744        } else {        } else {
# Line 915  sub _get_next_token ($) { Line 747  sub _get_next_token ($) {
747          !!!next-input-character;          !!!next-input-character;
748          redo A;          redo A;
749        }        }
750      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
751        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
752            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
753            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 925  sub _get_next_token ($) { Line 757  sub _get_next_token ($) {
757          !!!next-input-character;          !!!next-input-character;
758          redo A;          redo A;
759        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_input_character} == 0x003D) { # =
760          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
761          !!!next-input-character;          !!!next-input-character;
762          redo A;          redo A;
763        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
764          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
765              $self->{current_token}->{first_start_tag}
766                  = not defined $self->{last_emitted_start_tag_name};
767            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
768          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
769            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
770            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
771              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
772            }            }
773          } else {          } else {
774            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
775          }          }
776          $self->{state} = 'data';          $self->{state} = DATA_STATE;
777          !!!next-input-character;          !!!next-input-character;
778    
779          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
780    
781          redo A;          redo A;
782        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
783                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_input_character} <= 0x005A) { # A..Z
784          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),
785                                value => ''};                                value => ''};
786          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
787          !!!next-input-character;          !!!next-input-character;
788          redo A;          redo A;
789        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
790          !!!next-input-character;          !!!next-input-character;
791          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
792              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
793              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
794            # permitted slash            # permitted slash
795            #            #
796          } else {          } else {
797            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
798              ## TODO: Different error type for <aa / bb> than <aa/>
799          }          }
800          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
801          # next-input-character is already done          # next-input-character is already done
802          redo A;          redo A;
803        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
804          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
805          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
806              $self->{current_token}->{first_start_tag}
807                  = not defined $self->{last_emitted_start_tag_name};
808            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
809          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
810            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
811            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
812              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
813            }            }
814          } else {          } else {
815            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
816          }          }
817          $self->{state} = 'data';          $self->{state} = DATA_STATE;
818          # reconsume          # reconsume
819    
820          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
821    
822          redo A;          redo A;
823        } else {        } else {
824          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute} = {name => chr ($self->{next_input_character}),
825                                value => ''};                                value => ''};
826          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
827          !!!next-input-character;          !!!next-input-character;
828          redo A;                  redo A;        
829        }        }
830      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
831        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
832            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
833            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 1003  sub _get_next_token ($) { Line 837  sub _get_next_token ($) {
837          !!!next-input-character;          !!!next-input-character;
838          redo A;          redo A;
839        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
840          $self->{state} = 'attribute value (double-quoted)';          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
841          !!!next-input-character;          !!!next-input-character;
842          redo A;          redo A;
843        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
844          $self->{state} = 'attribute value (unquoted)';          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
845          ## reconsume          ## reconsume
846          redo A;          redo A;
847        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
848          $self->{state} = 'attribute value (single-quoted)';          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
849          !!!next-input-character;          !!!next-input-character;
850          redo A;          redo A;
851        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
852          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
853              $self->{current_token}->{first_start_tag}
854                  = not defined $self->{last_emitted_start_tag_name};
855            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
856          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
857            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
858            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
859              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
860            }            }
861          } else {          } else {
862            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
863          }          }
864          $self->{state} = 'data';          $self->{state} = DATA_STATE;
865          !!!next-input-character;          !!!next-input-character;
866    
867          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
868    
869          redo A;          redo A;
870        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
871          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
872          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
873              $self->{current_token}->{first_start_tag}
874                  = not defined $self->{last_emitted_start_tag_name};
875            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
876          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
877            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
878            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
879              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
880            }            }
881          } else {          } else {
882            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
883          }          }
884          $self->{state} = 'data';          $self->{state} = DATA_STATE;
885          ## reconsume          ## reconsume
886    
887          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
888    
889          redo A;          redo A;
890        } else {        } else {
891          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});
892          $self->{state} = 'attribute value (unquoted)';          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
893          !!!next-input-character;          !!!next-input-character;
894          redo A;          redo A;
895        }        }
896      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
897        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
898          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
899          !!!next-input-character;          !!!next-input-character;
900          redo A;          redo A;
901        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
902          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          $self->{last_attribute_value_state} = $self->{state};
903          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
904          !!!next-input-character;          !!!next-input-character;
905          redo A;          redo A;
906        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
907          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
908          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
909              $self->{current_token}->{first_start_tag}
910                  = not defined $self->{last_emitted_start_tag_name};
911            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
912          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
913            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
914            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
915              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
916            }            }
917          } else {          } else {
918            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
919          }          }
920          $self->{state} = 'data';          $self->{state} = DATA_STATE;
921          ## reconsume          ## reconsume
922    
923          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
924    
925          redo A;          redo A;
926        } else {        } else {
# Line 1093  sub _get_next_token ($) { Line 929  sub _get_next_token ($) {
929          !!!next-input-character;          !!!next-input-character;
930          redo A;          redo A;
931        }        }
932      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
933        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
934          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
935          !!!next-input-character;          !!!next-input-character;
936          redo A;          redo A;
937        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
938          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          $self->{last_attribute_value_state} = $self->{state};
939          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
940          !!!next-input-character;          !!!next-input-character;
941          redo A;          redo A;
942        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
943          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
944          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
945              $self->{current_token}->{first_start_tag}
946                  = not defined $self->{last_emitted_start_tag_name};
947            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
948          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
949            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
950            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
951              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
952            }            }
953          } else {          } else {
954            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
955          }          }
956          $self->{state} = 'data';          $self->{state} = DATA_STATE;
957          ## reconsume          ## reconsume
958    
959          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
960    
961          redo A;          redo A;
962        } else {        } else {
# Line 1128  sub _get_next_token ($) { Line 965  sub _get_next_token ($) {
965          !!!next-input-character;          !!!next-input-character;
966          redo A;          redo A;
967        }        }
968      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
969        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
970            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
971            $self->{next_input_character} == 0x000B or # HT            $self->{next_input_character} == 0x000B or # HT
972            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
973            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
974          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
975          !!!next-input-character;          !!!next-input-character;
976          redo A;          redo A;
977        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
978          $self->{last_attribute_value_state} = 'attribute value (unquoted)';          $self->{last_attribute_value_state} = $self->{state};
979          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
980          !!!next-input-character;          !!!next-input-character;
981          redo A;          redo A;
982        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
983          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
984              $self->{current_token}->{first_start_tag}
985                  = not defined $self->{last_emitted_start_tag_name};
986            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
987          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
988            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
989            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
990              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
991            }            }
992          } else {          } else {
993            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
994          }          }
995          $self->{state} = 'data';          $self->{state} = DATA_STATE;
996          !!!next-input-character;          !!!next-input-character;
997    
998          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
999    
1000          redo A;          redo A;
1001        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
1002          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1003          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1004              $self->{current_token}->{first_start_tag}
1005                  = not defined $self->{last_emitted_start_tag_name};
1006            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1007          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1008            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1009            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1010              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1011            }            }
1012          } else {          } else {
1013            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1014          }          }
1015          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1016          ## reconsume          ## reconsume
1017    
1018          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1019    
1020          redo A;          redo A;
1021        } else {        } else {
# Line 1186  sub _get_next_token ($) { Line 1024  sub _get_next_token ($) {
1024          !!!next-input-character;          !!!next-input-character;
1025          redo A;          redo A;
1026        }        }
1027      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1028        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);
1029    
1030        unless (defined $token) {        unless (defined $token) {
1031          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
# Line 1199  sub _get_next_token ($) { Line 1037  sub _get_next_token ($) {
1037        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1038        # next-input-character is already done        # next-input-character is already done
1039        redo A;        redo A;
1040      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1041        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1042                
1043        my $token = {type => 'comment', data => ''};        my $token = {type => COMMENT_TOKEN, data => ''};
1044    
1045        BC: {        BC: {
1046          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_input_character} == 0x003E) { # >
1047            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1048            !!!next-input-character;            !!!next-input-character;
1049    
1050            !!!emit ($token);            !!!emit ($token);
1051    
1052            redo A;            redo A;
1053          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_input_character} == -1) {
1054            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1055            ## reconsume            ## reconsume
1056    
1057            !!!emit ($token);            !!!emit ($token);
# Line 1225  sub _get_next_token ($) { Line 1063  sub _get_next_token ($) {
1063            redo BC;            redo BC;
1064          }          }
1065        } # BC        } # BC
1066      } elsif ($self->{state} eq 'markup declaration open') {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1067        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1068    
1069        my @next_char;        my @next_char;
# Line 1235  sub _get_next_token ($) { Line 1073  sub _get_next_token ($) {
1073          !!!next-input-character;          !!!next-input-character;
1074          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_input_character};
1075          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_input_character} == 0x002D) { # -
1076            $self->{current_token} = {type => 'comment', data => ''};            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};
1077            $self->{state} = 'comment';            $self->{state} = COMMENT_START_STATE;
1078            !!!next-input-character;            !!!next-input-character;
1079            redo A;            redo A;
1080          }          }
# Line 1267  sub _get_next_token ($) { Line 1105  sub _get_next_token ($) {
1105                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_input_character} == 0x0045 or # E
1106                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_input_character} == 0x0065) { # e
1107                      ## ISSUE: What a stupid code this is!                      ## ISSUE: What a stupid code this is!
1108                      $self->{state} = 'DOCTYPE';                      $self->{state} = DOCTYPE_STATE;
1109                      !!!next-input-character;                      !!!next-input-character;
1110                      redo A;                      redo A;
1111                    }                    }
# Line 1278  sub _get_next_token ($) { Line 1116  sub _get_next_token ($) {
1116          }          }
1117        }        }
1118    
1119        !!!parse-error (type => 'bogus comment open');        !!!parse-error (type => 'bogus comment');
1120        $self->{next_input_character} = shift @next_char;        $self->{next_input_character} = shift @next_char;
1121        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1122        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
1123        redo A;        redo A;
1124                
1125        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1126        ## 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?
1127      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_START_STATE) {
1128          if ($self->{next_input_character} == 0x002D) { # -
1129            $self->{state} = COMMENT_START_DASH_STATE;
1130            !!!next-input-character;
1131            redo A;
1132          } elsif ($self->{next_input_character} == 0x003E) { # >
1133            !!!parse-error (type => 'bogus comment');
1134            $self->{state} = DATA_STATE;
1135            !!!next-input-character;
1136    
1137            !!!emit ($self->{current_token}); # comment
1138    
1139            redo A;
1140          } elsif ($self->{next_input_character} == -1) {
1141            !!!parse-error (type => 'unclosed comment');
1142            $self->{state} = DATA_STATE;
1143            ## reconsume
1144    
1145            !!!emit ($self->{current_token}); # comment
1146    
1147            redo A;
1148          } else {
1149            $self->{current_token}->{data} # comment
1150                .= chr ($self->{next_input_character});
1151            $self->{state} = COMMENT_STATE;
1152            !!!next-input-character;
1153            redo A;
1154          }
1155        } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
1156        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1157          $self->{state} = 'comment dash';          $self->{state} = COMMENT_END_STATE;
1158            !!!next-input-character;
1159            redo A;
1160          } elsif ($self->{next_input_character} == 0x003E) { # >
1161            !!!parse-error (type => 'bogus comment');
1162            $self->{state} = DATA_STATE;
1163          !!!next-input-character;          !!!next-input-character;
1164    
1165            !!!emit ($self->{current_token}); # comment
1166    
1167          redo A;          redo A;
1168        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1169          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1170          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1171            ## reconsume
1172    
1173            !!!emit ($self->{current_token}); # comment
1174    
1175            redo A;
1176          } else {
1177            $self->{current_token}->{data} # comment
1178                .= '-' . chr ($self->{next_input_character});
1179            $self->{state} = COMMENT_STATE;
1180            !!!next-input-character;
1181            redo A;
1182          }
1183        } elsif ($self->{state} == COMMENT_STATE) {
1184          if ($self->{next_input_character} == 0x002D) { # -
1185            $self->{state} = COMMENT_END_DASH_STATE;
1186            !!!next-input-character;
1187            redo A;
1188          } elsif ($self->{next_input_character} == -1) {
1189            !!!parse-error (type => 'unclosed comment');
1190            $self->{state} = DATA_STATE;
1191          ## reconsume          ## reconsume
1192    
1193          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1194    
1195          redo A;          redo A;
1196        } else {        } else {
# Line 1306  sub _get_next_token ($) { Line 1199  sub _get_next_token ($) {
1199          !!!next-input-character;          !!!next-input-character;
1200          redo A;          redo A;
1201        }        }
1202      } elsif ($self->{state} eq 'comment dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
1203        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1204          $self->{state} = 'comment end';          $self->{state} = COMMENT_END_STATE;
1205          !!!next-input-character;          !!!next-input-character;
1206          redo A;          redo A;
1207        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1208          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1209          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1210          ## reconsume          ## reconsume
1211    
1212          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1213    
1214          redo A;          redo A;
1215        } else {        } else {
1216          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment
1217          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1218          !!!next-input-character;          !!!next-input-character;
1219          redo A;          redo A;
1220        }        }
1221      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
1222        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_input_character} == 0x003E) { # >
1223          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1224          !!!next-input-character;          !!!next-input-character;
1225    
1226          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1227    
1228          redo A;          redo A;
1229        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_input_character} == 0x002D) { # -
# Line 1343  sub _get_next_token ($) { Line 1234  sub _get_next_token ($) {
1234          redo A;          redo A;
1235        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1236          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1237          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1238          ## reconsume          ## reconsume
1239    
1240          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1241    
1242          redo A;          redo A;
1243        } else {        } else {
1244          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment');
1245          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment
1246          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1247          !!!next-input-character;          !!!next-input-character;
1248          redo A;          redo A;
1249        }        }
1250      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
1251        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1252            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1253            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1254            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1255            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1256          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1257          !!!next-input-character;          !!!next-input-character;
1258          redo A;          redo A;
1259        } else {        } else {
1260          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
1261          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1262          ## reconsume          ## reconsume
1263          redo A;          redo A;
1264        }        }
1265      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
1266        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1267            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1268            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 1381  sub _get_next_token ($) { Line 1271  sub _get_next_token ($) {
1271          ## Stay in the state          ## Stay in the state
1272          !!!next-input-character;          !!!next-input-character;
1273          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;  
1274        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1275          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1276          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1277          !!!next-input-character;          !!!next-input-character;
1278    
1279          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1280    
1281          redo A;          redo A;
1282        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1283          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1284          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1285          ## reconsume          ## reconsume
1286    
1287          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1288    
1289          redo A;          redo A;
1290        } else {        } else {
1291          $self->{current_token} = {type => 'DOCTYPE',          $self->{current_token}
1292                            name => chr ($self->{next_input_character}),              = {type => DOCTYPE_TOKEN,
1293                            error => 1};                 name => chr ($self->{next_input_character}),
1294                   correct => 1};
1295  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
1296          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
1297          !!!next-input-character;          !!!next-input-character;
1298          redo A;          redo A;
1299        }        }
1300      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
1301    ## ISSUE: Redundant "First," in the spec.
1302        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1303            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1304            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1305            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1306            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1307          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
         $self->{state} = 'after DOCTYPE name';  
1308          !!!next-input-character;          !!!next-input-character;
1309          redo A;          redo A;
1310        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1311          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE          $self->{state} = DATA_STATE;
         $self->{state} = 'data';  
1312          !!!next-input-character;          !!!next-input-character;
1313    
1314          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1315    
1316          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;  
1317        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1318          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1319          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE          $self->{state} = DATA_STATE;
         $self->{state} = 'data';  
1320          ## reconsume          ## reconsume
1321    
1322          !!!emit ($self->{current_token});          delete $self->{current_token}->{correct};
1323          undef $self->{current_token};          !!!emit ($self->{current_token}); # DOCTYPE
1324    
1325          redo A;          redo A;
1326        } else {        } else {
1327          $self->{current_token}->{name}          $self->{current_token}->{name}
1328            .= chr ($self->{next_input_character}); # DOCTYPE            .= chr ($self->{next_input_character}); # DOCTYPE
         #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');  
1329          ## Stay in the state          ## Stay in the state
1330          !!!next-input-character;          !!!next-input-character;
1331          redo A;          redo A;
1332        }        }
1333      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
1334        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1335            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1336            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 1469  sub _get_next_token ($) { Line 1340  sub _get_next_token ($) {
1340          !!!next-input-character;          !!!next-input-character;
1341          redo A;          redo A;
1342        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1343          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1344            !!!next-input-character;
1345    
1346            !!!emit ($self->{current_token}); # DOCTYPE
1347    
1348            redo A;
1349          } elsif ($self->{next_input_character} == -1) {
1350            !!!parse-error (type => 'unclosed DOCTYPE');
1351            $self->{state} = DATA_STATE;
1352            ## reconsume
1353    
1354            delete $self->{current_token}->{correct};
1355            !!!emit ($self->{current_token}); # DOCTYPE
1356    
1357            redo A;
1358          } elsif ($self->{next_input_character} == 0x0050 or # P
1359                   $self->{next_input_character} == 0x0070) { # p
1360            !!!next-input-character;
1361            if ($self->{next_input_character} == 0x0055 or # U
1362                $self->{next_input_character} == 0x0075) { # u
1363              !!!next-input-character;
1364              if ($self->{next_input_character} == 0x0042 or # B
1365                  $self->{next_input_character} == 0x0062) { # b
1366                !!!next-input-character;
1367                if ($self->{next_input_character} == 0x004C or # L
1368                    $self->{next_input_character} == 0x006C) { # l
1369                  !!!next-input-character;
1370                  if ($self->{next_input_character} == 0x0049 or # I
1371                      $self->{next_input_character} == 0x0069) { # i
1372                    !!!next-input-character;
1373                    if ($self->{next_input_character} == 0x0043 or # C
1374                        $self->{next_input_character} == 0x0063) { # c
1375                      $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1376                      !!!next-input-character;
1377                      redo A;
1378                    }
1379                  }
1380                }
1381              }
1382            }
1383    
1384            #
1385          } elsif ($self->{next_input_character} == 0x0053 or # S
1386                   $self->{next_input_character} == 0x0073) { # s
1387            !!!next-input-character;
1388            if ($self->{next_input_character} == 0x0059 or # Y
1389                $self->{next_input_character} == 0x0079) { # y
1390              !!!next-input-character;
1391              if ($self->{next_input_character} == 0x0053 or # S
1392                  $self->{next_input_character} == 0x0073) { # s
1393                !!!next-input-character;
1394                if ($self->{next_input_character} == 0x0054 or # T
1395                    $self->{next_input_character} == 0x0074) { # t
1396                  !!!next-input-character;
1397                  if ($self->{next_input_character} == 0x0045 or # E
1398                      $self->{next_input_character} == 0x0065) { # e
1399                    !!!next-input-character;
1400                    if ($self->{next_input_character} == 0x004D or # M
1401                        $self->{next_input_character} == 0x006D) { # m
1402                      $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1403                      !!!next-input-character;
1404                      redo A;
1405                    }
1406                  }
1407                }
1408              }
1409            }
1410    
1411            #
1412          } else {
1413            !!!next-input-character;
1414            #
1415          }
1416    
1417          !!!parse-error (type => 'string after DOCTYPE name');
1418          $self->{state} = BOGUS_DOCTYPE_STATE;
1419          # next-input-character is already done
1420          redo A;
1421        } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1422          if ({
1423                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1424                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1425              }->{$self->{next_input_character}}) {
1426            ## Stay in the state
1427            !!!next-input-character;
1428            redo A;
1429          } elsif ($self->{next_input_character} eq 0x0022) { # "
1430            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1431            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
1432            !!!next-input-character;
1433            redo A;
1434          } elsif ($self->{next_input_character} eq 0x0027) { # '
1435            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1436            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
1437            !!!next-input-character;
1438            redo A;
1439          } elsif ($self->{next_input_character} eq 0x003E) { # >
1440            !!!parse-error (type => 'no PUBLIC literal');
1441    
1442            $self->{state} = DATA_STATE;
1443            !!!next-input-character;
1444    
1445            delete $self->{current_token}->{correct};
1446            !!!emit ($self->{current_token}); # DOCTYPE
1447    
1448            redo A;
1449          } elsif ($self->{next_input_character} == -1) {
1450            !!!parse-error (type => 'unclosed DOCTYPE');
1451    
1452            $self->{state} = DATA_STATE;
1453            ## reconsume
1454    
1455            delete $self->{current_token}->{correct};
1456            !!!emit ($self->{current_token}); # DOCTYPE
1457    
1458            redo A;
1459          } else {
1460            !!!parse-error (type => 'string after PUBLIC');
1461            $self->{state} = BOGUS_DOCTYPE_STATE;
1462            !!!next-input-character;
1463            redo A;
1464          }
1465        } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1466          if ($self->{next_input_character} == 0x0022) { # "
1467            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1468            !!!next-input-character;
1469            redo A;
1470          } elsif ($self->{next_input_character} == -1) {
1471            !!!parse-error (type => 'unclosed PUBLIC literal');
1472    
1473            $self->{state} = DATA_STATE;
1474            ## reconsume
1475    
1476            delete $self->{current_token}->{correct};
1477            !!!emit ($self->{current_token}); # DOCTYPE
1478    
1479            redo A;
1480          } else {
1481            $self->{current_token}->{public_identifier} # DOCTYPE
1482                .= chr $self->{next_input_character};
1483            ## Stay in the state
1484            !!!next-input-character;
1485            redo A;
1486          }
1487        } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
1488          if ($self->{next_input_character} == 0x0027) { # '
1489            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1490            !!!next-input-character;
1491            redo A;
1492          } elsif ($self->{next_input_character} == -1) {
1493            !!!parse-error (type => 'unclosed PUBLIC literal');
1494    
1495            $self->{state} = DATA_STATE;
1496            ## reconsume
1497    
1498            delete $self->{current_token}->{correct};
1499            !!!emit ($self->{current_token}); # DOCTYPE
1500    
1501            redo A;
1502          } else {
1503            $self->{current_token}->{public_identifier} # DOCTYPE
1504                .= chr $self->{next_input_character};
1505            ## Stay in the state
1506            !!!next-input-character;
1507            redo A;
1508          }
1509        } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1510          if ({
1511                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1512                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1513              }->{$self->{next_input_character}}) {
1514            ## Stay in the state
1515            !!!next-input-character;
1516            redo A;
1517          } elsif ($self->{next_input_character} == 0x0022) { # "
1518            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1519            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1520            !!!next-input-character;
1521            redo A;
1522          } elsif ($self->{next_input_character} == 0x0027) { # '
1523            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1524            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1525            !!!next-input-character;
1526            redo A;
1527          } elsif ($self->{next_input_character} == 0x003E) { # >
1528            $self->{state} = DATA_STATE;
1529            !!!next-input-character;
1530    
1531            !!!emit ($self->{current_token}); # DOCTYPE
1532    
1533            redo A;
1534          } elsif ($self->{next_input_character} == -1) {
1535            !!!parse-error (type => 'unclosed DOCTYPE');
1536    
1537            $self->{state} = DATA_STATE;
1538            ## reconsume
1539    
1540            delete $self->{current_token}->{correct};
1541            !!!emit ($self->{current_token}); # DOCTYPE
1542    
1543            redo A;
1544          } else {
1545            !!!parse-error (type => 'string after PUBLIC literal');
1546            $self->{state} = BOGUS_DOCTYPE_STATE;
1547            !!!next-input-character;
1548            redo A;
1549          }
1550        } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1551          if ({
1552                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1553                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1554              }->{$self->{next_input_character}}) {
1555            ## Stay in the state
1556            !!!next-input-character;
1557            redo A;
1558          } elsif ($self->{next_input_character} == 0x0022) { # "
1559            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1560            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1561            !!!next-input-character;
1562            redo A;
1563          } elsif ($self->{next_input_character} == 0x0027) { # '
1564            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1565            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1566            !!!next-input-character;
1567            redo A;
1568          } elsif ($self->{next_input_character} == 0x003E) { # >
1569            !!!parse-error (type => 'no SYSTEM literal');
1570            $self->{state} = DATA_STATE;
1571            !!!next-input-character;
1572    
1573            delete $self->{current_token}->{correct};
1574            !!!emit ($self->{current_token}); # DOCTYPE
1575    
1576            redo A;
1577          } elsif ($self->{next_input_character} == -1) {
1578            !!!parse-error (type => 'unclosed DOCTYPE');
1579    
1580            $self->{state} = DATA_STATE;
1581            ## reconsume
1582    
1583            delete $self->{current_token}->{correct};
1584            !!!emit ($self->{current_token}); # DOCTYPE
1585    
1586            redo A;
1587          } else {
1588            !!!parse-error (type => 'string after SYSTEM');
1589            $self->{state} = BOGUS_DOCTYPE_STATE;
1590            !!!next-input-character;
1591            redo A;
1592          }
1593        } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1594          if ($self->{next_input_character} == 0x0022) { # "
1595            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1596            !!!next-input-character;
1597            redo A;
1598          } elsif ($self->{next_input_character} == -1) {
1599            !!!parse-error (type => 'unclosed SYSTEM literal');
1600    
1601            $self->{state} = DATA_STATE;
1602            ## reconsume
1603    
1604            delete $self->{current_token}->{correct};
1605            !!!emit ($self->{current_token}); # DOCTYPE
1606    
1607            redo A;
1608          } else {
1609            $self->{current_token}->{system_identifier} # DOCTYPE
1610                .= chr $self->{next_input_character};
1611            ## Stay in the state
1612            !!!next-input-character;
1613            redo A;
1614          }
1615        } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
1616          if ($self->{next_input_character} == 0x0027) { # '
1617            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1618            !!!next-input-character;
1619            redo A;
1620          } elsif ($self->{next_input_character} == -1) {
1621            !!!parse-error (type => 'unclosed SYSTEM literal');
1622    
1623            $self->{state} = DATA_STATE;
1624            ## reconsume
1625    
1626            delete $self->{current_token}->{correct};
1627            !!!emit ($self->{current_token}); # DOCTYPE
1628    
1629            redo A;
1630          } else {
1631            $self->{current_token}->{system_identifier} # DOCTYPE
1632                .= chr $self->{next_input_character};
1633            ## Stay in the state
1634            !!!next-input-character;
1635            redo A;
1636          }
1637        } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1638          if ({
1639                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1640                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1641              }->{$self->{next_input_character}}) {
1642            ## Stay in the state
1643            !!!next-input-character;
1644            redo A;
1645          } elsif ($self->{next_input_character} == 0x003E) { # >
1646            $self->{state} = DATA_STATE;
1647          !!!next-input-character;          !!!next-input-character;
1648    
1649          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1650    
1651          redo A;          redo A;
1652        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1653          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1654          $self->{state} = 'data';  
1655            $self->{state} = DATA_STATE;
1656          ## reconsume          ## reconsume
1657    
1658            delete $self->{current_token}->{correct};
1659          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1660    
1661          redo A;          redo A;
1662        } else {        } else {
1663          !!!parse-error (type => 'string after DOCTYPE name');          !!!parse-error (type => 'string after SYSTEM literal');
1664          $self->{current_token}->{error} = 1; # DOCTYPE          $self->{state} = BOGUS_DOCTYPE_STATE;
         $self->{state} = 'bogus DOCTYPE';  
1665          !!!next-input-character;          !!!next-input-character;
1666          redo A;          redo A;
1667        }        }
1668      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
1669        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_input_character} == 0x003E) { # >
1670          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1671          !!!next-input-character;          !!!next-input-character;
1672    
1673            delete $self->{current_token}->{correct};
1674          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1675    
1676          redo A;          redo A;
1677        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1678          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1679          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1680          ## reconsume          ## reconsume
1681    
1682            delete $self->{current_token}->{correct};
1683          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1684    
1685          redo A;          redo A;
1686        } else {        } else {
# Line 1523  sub _get_next_token ($) { Line 1696  sub _get_next_token ($) {
1696    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
1697  } # _get_next_token  } # _get_next_token
1698    
1699  sub _tokenize_attempt_to_consume_an_entity ($) {  sub _tokenize_attempt_to_consume_an_entity ($$) {
1700    my $self = shift;    my ($self, $in_attr) = @_;
1701      
1702    if ($self->{next_input_character} == 0x0023) { # #    if ({
1703           0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
1704           0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
1705          }->{$self->{next_input_character}}) {
1706        ## Don't consume
1707        ## No error
1708        return undef;
1709      } elsif ($self->{next_input_character} == 0x0023) { # #
1710      !!!next-input-character;      !!!next-input-character;
1711      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_input_character} == 0x0078 or # x
1712          $self->{next_input_character} == 0x0058) { # X          $self->{next_input_character} == 0x0058) { # X
1713        my $num;        my $code;
1714        X: {        X: {
1715          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_input_character};
1716          !!!next-input-character;          !!!next-input-character;
1717          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_input_character} and
1718              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_input_character} <= 0x0039) { # 0..9
1719            $num ||= 0;            $code ||= 0;
1720            $num *= 0x10;            $code *= 0x10;
1721            $num += $self->{next_input_character} - 0x0030;            $code += $self->{next_input_character} - 0x0030;
1722            redo X;            redo X;
1723          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_input_character} and
1724                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_input_character} <= 0x0066) { # a..f
1725            ## ISSUE: the spec says U+0078, which is apparently incorrect            $code ||= 0;
1726            $num ||= 0;            $code *= 0x10;
1727            $num *= 0x10;            $code += $self->{next_input_character} - 0x0060 + 9;
           $num += $self->{next_input_character} - 0x0060 + 9;  
1728            redo X;            redo X;
1729          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_input_character} and
1730                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_input_character} <= 0x0046) { # A..F
1731            ## ISSUE: the spec says U+0058, which is apparently incorrect            $code ||= 0;
1732            $num ||= 0;            $code *= 0x10;
1733            $num *= 0x10;            $code += $self->{next_input_character} - 0x0040 + 9;
           $num += $self->{next_input_character} - 0x0040 + 9;  
1734            redo X;            redo X;
1735          } elsif (not defined $num) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
1736            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro');
1737              !!!back-next-input-character ($x_char, $self->{next_input_character});
1738            $self->{next_input_character} = 0x0023; # #            $self->{next_input_character} = 0x0023; # #
           !!!back-next-input-character ($x_char);  
1739            return undef;            return undef;
1740          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_input_character} == 0x003B) { # ;
1741            !!!next-input-character;            !!!next-input-character;
# Line 1565  sub _tokenize_attempt_to_consume_an_enti Line 1743  sub _tokenize_attempt_to_consume_an_enti
1743            !!!parse-error (type => 'no refc');            !!!parse-error (type => 'no refc');
1744          }          }
1745    
1746          ## TODO: check the definition for |a valid Unicode character|.          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
1747          ## <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8189>            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
1748          if ($num > 1114111 or $num == 0) {            $code = 0xFFFD;
1749            $num = 0xFFFD; # REPLACEMENT CHARACTER          } elsif ($code > 0x10FFFF) {
1750            ## ISSUE: Why this is not an error?            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
1751          } elsif (0x80 <= $num and $num <= 0x9F) {            $code = 0xFFFD;
1752            !!!parse-error (type => sprintf 'c1 entity:U+%04X', $num);          } elsif ($code == 0x000D) {
1753            $num = $c1_entity_char->{$num};            !!!parse-error (type => 'CR character reference');
1754              $code = 0x000A;
1755            } elsif (0x80 <= $code and $code <= 0x9F) {
1756              !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
1757              $code = $c1_entity_char->{$code};
1758          }          }
1759    
1760          return {type => 'character', data => chr $num};          return {type => CHARACTER_TOKEN, data => chr $code};
1761        } # X        } # X
1762      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_input_character} and
1763               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_input_character} <= 0x0039) { # 0..9
# Line 1596  sub _tokenize_attempt_to_consume_an_enti Line 1778  sub _tokenize_attempt_to_consume_an_enti
1778          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc');
1779        }        }
1780    
1781        ## TODO: check the definition for |a valid Unicode character|.        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
1782        if ($code > 1114111 or $code == 0) {          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
1783          $code = 0xFFFD; # REPLACEMENT CHARACTER          $code = 0xFFFD;
1784          ## ISSUE: Why this is not an error?        } elsif ($code > 0x10FFFF) {
1785            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
1786            $code = 0xFFFD;
1787          } elsif ($code == 0x000D) {
1788            !!!parse-error (type => 'CR character reference');
1789            $code = 0x000A;
1790        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
1791          !!!parse-error (type => sprintf 'c1 entity:U+%04X', $code);          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
1792          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
1793        }        }
1794                
1795        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code};
1796      } else {      } else {
1797        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero');
1798        !!!back-next-input-character ($self->{next_input_character});        !!!back-next-input-character ($self->{next_input_character});
# Line 1620  sub _tokenize_attempt_to_consume_an_enti Line 1807  sub _tokenize_attempt_to_consume_an_enti
1807      !!!next-input-character;      !!!next-input-character;
1808    
1809      my $value = $entity_name;      my $value = $entity_name;
1810      my $match;      my $match = 0;
1811        require Whatpm::_NamedEntityList;
1812        our $EntityChar;
1813    
1814      while (length $entity_name < 10 and      while (length $entity_name < 10 and
1815             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
1816             ((0x0041 <= $self->{next_input_character} and             ((0x0041 <= $self->{next_input_character} and # a
1817               $self->{next_input_character} <= 0x005A) or               $self->{next_input_character} <= 0x005A) or # x
1818              (0x0061 <= $self->{next_input_character} and              (0x0061 <= $self->{next_input_character} and # a
1819               $self->{next_input_character} <= 0x007A) or               $self->{next_input_character} <= 0x007A) or # z
1820              (0x0030 <= $self->{next_input_character} and              (0x0030 <= $self->{next_input_character} and # 0
1821               $self->{next_input_character} <= 0x0039))) {               $self->{next_input_character} <= 0x0039) or # 9
1822                $self->{next_input_character} == 0x003B)) { # ;
1823        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_input_character};
1824        if (defined $entity_char->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
1825          $value = $entity_char->{$entity_name};          if ($self->{next_input_character} == 0x003B) { # ;
1826          $match = 1;            $value = $EntityChar->{$entity_name};
1827              $match = 1;
1828              !!!next-input-character;
1829              last;
1830            } else {
1831              $value = $EntityChar->{$entity_name};
1832              $match = -1;
1833              !!!next-input-character;
1834            }
1835        } else {        } else {
1836          $value .= chr $self->{next_input_character};          $value .= chr $self->{next_input_character};
1837            $match *= 2;
1838            !!!next-input-character;
1839        }        }
       !!!next-input-character;  
1840      }      }
1841            
1842      if ($match) {      if ($match > 0) {
1843        if ($self->{next_input_character} == 0x003B) { # ;        return {type => CHARACTER_TOKEN, data => $value};
1844          !!!next-input-character;      } elsif ($match < 0) {
1845          !!!parse-error (type => 'no refc');
1846          if ($in_attr and $match < -1) {
1847            return {type => CHARACTER_TOKEN, data => '&'.$entity_name};
1848        } else {        } else {
1849          !!!parse-error (type => 'refc');          return {type => CHARACTER_TOKEN, data => $value};
1850        }        }
   
       return {type => 'character', data => $value};  
1851      } else {      } else {
1852        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero');
1853        ## NOTE: No characters are consumed in the spec.        ## NOTE: No characters are consumed in the spec.
1854        !!!back-token ({type => 'character', data => $value});        return {type => CHARACTER_TOKEN, data => '&'.$value};
       return undef;  
1855      }      }
1856    } else {    } else {
1857      ## no characters are consumed      ## no characters are consumed
# Line 1667  sub _initialize_tree_constructor ($) { Line 1866  sub _initialize_tree_constructor ($) {
1866    $self->{document}->strict_error_checking (0);    $self->{document}->strict_error_checking (0);
1867    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
1868    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
1869    ## TODO: Mark the Document as an HTML document # MUST    $self->{document}->manakai_is_html (1); # MUST
1870  } # _initialize_tree_constructor  } # _initialize_tree_constructor
1871    
1872  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 1694  sub _construct_tree ($) { Line 1893  sub _construct_tree ($) {
1893        
1894    !!!next-token;    !!!next-token;
1895    
1896    $self->{insertion_mode} = 'before head';    $self->{insertion_mode} = BEFORE_HEAD_IM;
1897    undef $self->{form_element};    undef $self->{form_element};
1898    undef $self->{head_element};    undef $self->{head_element};
1899    $self->{open_elements} = [];    $self->{open_elements} = [];
# Line 1707  sub _construct_tree ($) { Line 1906  sub _construct_tree ($) {
1906    
1907  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
1908    my $self = shift;    my $self = shift;
1909    B: {    INITIAL: {
1910        if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
1911          if ($token->{error}) {        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
1912            ## ISSUE: Spec currently left this case undefined.        ## error, switch to a conformance checking mode for another
1913            !!!parse-error (type => 'bogus DOCTYPE');        ## language.
1914          }        my $doctype_name = $token->{name};
1915          my $doctype = $self->{document}->create_document_type_definition        $doctype_name = '' unless defined $doctype_name;
1916            ($token->{name});        $doctype_name =~ tr/a-z/A-Z/;
1917          $self->{document}->append_child ($doctype);        if (not defined $token->{name} or # <!DOCTYPE>
1918          #$phase = 'root element';            defined $token->{public_identifier} or
1919          !!!next-token;            defined $token->{system_identifier}) {
1920          #redo B;          !!!parse-error (type => 'not HTML5');
1921          return;        } elsif ($doctype_name ne 'HTML') {
1922        } elsif ({          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
1923                  comment => 1,          !!!parse-error (type => 'not HTML5');
1924                  'start tag' => 1,        }
1925                  'end tag' => 1,        
1926                  'end-of-file' => 1,        my $doctype = $self->{document}->create_document_type_definition
1927                 }->{$token->{type}}) {          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
1928          ## ISSUE: Spec currently left this case undefined.        $doctype->public_id ($token->{public_identifier})
1929          !!!parse-error (type => 'missing DOCTYPE');            if defined $token->{public_identifier};
1930          #$phase = 'root element';        $doctype->system_id ($token->{system_identifier})
1931          ## reprocess            if defined $token->{system_identifier};
1932          #redo B;        ## NOTE: Other DocumentType attributes are null or empty lists.
1933          return;        ## ISSUE: internalSubset = null??
1934        } elsif ($token->{type} eq 'character') {        $self->{document}->append_child ($doctype);
1935          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        
1936            $self->{document}->manakai_append_text ($1);        if (not $token->{correct} or $doctype_name ne 'HTML') {
1937            ## ISSUE: DOM3 Core does not allow Document > Text          $self->{document}->manakai_compat_mode ('quirks');
1938            unless (length $token->{data}) {        } elsif (defined $token->{public_identifier}) {
1939              ## Stay in the phase          my $pubid = $token->{public_identifier};
1940              !!!next-token;          $pubid =~ tr/a-z/A-z/;
1941              redo B;          if ({
1942              "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,
1943              "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,
1944              "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,
1945              "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,
1946              "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,
1947              "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,
1948              "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,
1949              "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,
1950              "-//IETF//DTD HTML 2.0//EN" => 1,
1951              "-//IETF//DTD HTML 2.1E//EN" => 1,
1952              "-//IETF//DTD HTML 3.0//EN" => 1,
1953              "-//IETF//DTD HTML 3.0//EN//" => 1,
1954              "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,
1955              "-//IETF//DTD HTML 3.2//EN" => 1,
1956              "-//IETF//DTD HTML 3//EN" => 1,
1957              "-//IETF//DTD HTML LEVEL 0//EN" => 1,
1958              "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,
1959              "-//IETF//DTD HTML LEVEL 1//EN" => 1,
1960              "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,
1961              "-//IETF//DTD HTML LEVEL 2//EN" => 1,
1962              "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,
1963              "-//IETF//DTD HTML LEVEL 3//EN" => 1,
1964              "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,
1965              "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,
1966              "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,
1967              "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,
1968              "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,
1969              "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,
1970              "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,
1971              "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,
1972              "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,
1973              "-//IETF//DTD HTML STRICT//EN" => 1,
1974              "-//IETF//DTD HTML STRICT//EN//2.0" => 1,
1975              "-//IETF//DTD HTML STRICT//EN//3.0" => 1,
1976              "-//IETF//DTD HTML//EN" => 1,
1977              "-//IETF//DTD HTML//EN//2.0" => 1,
1978              "-//IETF//DTD HTML//EN//3.0" => 1,
1979              "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,
1980              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,
1981              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,
1982              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,
1983              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,
1984              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,
1985              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,
1986              "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,
1987              "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,
1988              "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,
1989              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,
1990              "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,
1991              "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,
1992              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,
1993              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,
1994              "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,
1995              "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,
1996              "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,
1997              "-//W3C//DTD HTML 3.2//EN" => 1,
1998              "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,
1999              "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,
2000              "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,
2001              "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,
2002              "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,
2003              "-//W3C//DTD W3 HTML//EN" => 1,
2004              "-//W3O//DTD W3 HTML 3.0//EN" => 1,
2005              "-//W3O//DTD W3 HTML 3.0//EN//" => 1,
2006              "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,
2007              "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,
2008              "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,
2009              "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,
2010              "HTML" => 1,
2011            }->{$pubid}) {
2012              $self->{document}->manakai_compat_mode ('quirks');
2013            } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or
2014                     $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {
2015              if (defined $token->{system_identifier}) {
2016                $self->{document}->manakai_compat_mode ('quirks');
2017              } else {
2018                $self->{document}->manakai_compat_mode ('limited quirks');
2019            }            }
2020            } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or
2021                     $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {
2022              $self->{document}->manakai_compat_mode ('limited quirks');
2023            }
2024          }
2025          if (defined $token->{system_identifier}) {
2026            my $sysid = $token->{system_identifier};
2027            $sysid =~ tr/A-Z/a-z/;
2028            if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
2029              $self->{document}->manakai_compat_mode ('quirks');
2030          }          }
         ## 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";  
2031        }        }
2032      } # B        
2033          ## Go to the root element phase.
2034          !!!next-token;
2035          return;
2036        } elsif ({
2037                  START_TAG_TOKEN, 1,
2038                  END_TAG_TOKEN, 1,
2039                  END_OF_FILE_TOKEN, 1,
2040                 }->{$token->{type}}) {
2041          !!!parse-error (type => 'no DOCTYPE');
2042          $self->{document}->manakai_compat_mode ('quirks');
2043          ## Go to the root element phase
2044          ## reprocess
2045          return;
2046        } elsif ($token->{type} == CHARACTER_TOKEN) {
2047          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2048            ## Ignore the token
2049    
2050            unless (length $token->{data}) {
2051              ## Stay in the phase
2052              !!!next-token;
2053              redo INITIAL;
2054            }
2055          }
2056    
2057          !!!parse-error (type => 'no DOCTYPE');
2058          $self->{document}->manakai_compat_mode ('quirks');
2059          ## Go to the root element phase
2060          ## reprocess
2061          return;
2062        } elsif ($token->{type} == COMMENT_TOKEN) {
2063          my $comment = $self->{document}->create_comment ($token->{data});
2064          $self->{document}->append_child ($comment);
2065          
2066          ## Stay in the phase.
2067          !!!next-token;
2068          redo INITIAL;
2069        } else {
2070          die "$0: $token->{type}: Unknown token type";
2071        }
2072      } # INITIAL
2073  } # _tree_construction_initial  } # _tree_construction_initial
2074    
2075  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
2076    my $self = shift;    my $self = shift;
2077        
2078    B: {    B: {
2079        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
2080          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE');
2081          ## Ignore the token          ## Ignore the token
2082          ## Stay in the phase          ## Stay in the phase
2083          !!!next-token;          !!!next-token;
2084          redo B;          redo B;
2085        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
2086          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
2087          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2088          ## Stay in the phase          ## Stay in the phase
2089          !!!next-token;          !!!next-token;
2090          redo B;          redo B;
2091        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
2092          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2093            $self->{document}->manakai_append_text ($1);            ## Ignore the token.
2094            ## ISSUE: DOM3 Core does not allow Document > Text  
2095            unless (length $token->{data}) {            unless (length $token->{data}) {
2096              ## Stay in the phase              ## Stay in the phase
2097              !!!next-token;              !!!next-token;
2098              redo B;              redo B;
2099            }            }
2100          }          }
2101    
2102            $self->{application_cache_selection}->(undef);
2103    
2104            #
2105          } elsif ($token->{type} == START_TAG_TOKEN) {
2106            if ($token->{tag_name} eq 'html' and
2107                $token->{attributes}->{manifest}) { ## ISSUE: Spec spells as "application"
2108              $self->{application_cache_selection}
2109                   ->($token->{attributes}->{manifest}->{value});
2110              ## ISSUE: No relative reference resolution?
2111            } else {
2112              $self->{application_cache_selection}->(undef);
2113            }
2114    
2115            ## ISSUE: There is an issue in the spec
2116          #          #
2117        } elsif ({        } elsif ({
2118                  'start tag' => 1,                  END_TAG_TOKEN, 1,
2119                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
2120                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2121            $self->{application_cache_selection}->(undef);
2122    
2123          ## ISSUE: There is an issue in the spec          ## ISSUE: There is an issue in the spec
2124          #          #
2125        } else {        } else {
2126          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
2127        }        }
2128    
2129        my $root_element; !!!create-element ($root_element, 'html');        my $root_element; !!!create-element ($root_element, 'html');
2130        $self->{document}->append_child ($root_element);        $self->{document}->append_child ($root_element);
2131        push @{$self->{open_elements}}, [$root_element, 'html'];        push @{$self->{open_elements}}, [$root_element, 'html'];
       #$phase = 'main';  
2132        ## reprocess        ## reprocess
2133        #redo B;        #redo B;
2134        return;        return; ## Go to the main phase.
2135    } # B    } # B
2136  } # _tree_construction_root_element  } # _tree_construction_root_element
2137    
# Line 1813  sub _reset_insertion_mode ($) { Line 2147  sub _reset_insertion_mode ($) {
2147            
2148      ## Step 3      ## Step 3
2149      S3: {      S3: {
2150        $last = 1 if $self->{open_elements}->[0]->[0] eq $node->[0];        ## ISSUE: Oops! "If node is the first node in the stack of open
2151        if (defined $self->{inner_html_node}) {        ## elements, then set last to true. If the context element of the
2152          if ($self->{inner_html_node}->[1] eq 'td' or        ## HTML fragment parsing algorithm is neither a td element nor a
2153              $self->{inner_html_node}->[1] eq 'th') {        ## th element, then set node to the context element. (fragment case)":
2154            #        ## The second "if" is in the scope of the first "if"!?
2155          } else {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
2156            $node = $self->{inner_html_node};          $last = 1;
2157            if (defined $self->{inner_html_node}) {
2158              if ($self->{inner_html_node}->[1] eq 'td' or
2159                  $self->{inner_html_node}->[1] eq 'th') {
2160                #
2161              } else {
2162                $node = $self->{inner_html_node};
2163              }
2164          }          }
2165        }        }
2166            
2167        ## Step 4..13        ## Step 4..13
2168        my $new_mode = {        my $new_mode = {
2169                        select => 'in select',                        select => IN_SELECT_IM,
2170                        td => 'in cell',                        td => IN_CELL_IM,
2171                        th => 'in cell',                        th => IN_CELL_IM,
2172                        tr => 'in row',                        tr => IN_ROW_IM,
2173                        tbody => 'in table body',                        tbody => IN_TABLE_BODY_IM,
2174                        thead => 'in table head',                        thead => IN_TABLE_BODY_IM,
2175                        tfoot => 'in table foot',                        tfoot => IN_TABLE_BODY_IM,
2176                        caption => 'in caption',                        caption => IN_CAPTION_IM,
2177                        colgroup => 'in column group',                        colgroup => IN_COLUMN_GROUP_IM,
2178                        table => 'in table',                        table => IN_TABLE_IM,
2179                        head => 'in body', # not in head!                        head => IN_BODY_IM, # not in head!
2180                        body => 'in body',                        body => IN_BODY_IM,
2181                        frameset => 'in frameset',                        frameset => IN_FRAMESET_IM,
2182                       }->{$node->[1]};                       }->{$node->[1]};
2183        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
2184                
2185        ## Step 14        ## Step 14
2186        if ($node->[1] eq 'html') {        if ($node->[1] eq 'html') {
2187          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
2188            $self->{insertion_mode} = 'before head';            $self->{insertion_mode} = BEFORE_HEAD_IM;
2189          } else {          } else {
2190            $self->{insertion_mode} = 'after head';            $self->{insertion_mode} = AFTER_HEAD_IM;
2191          }          }
2192          return;          return;
2193        }        }
2194                
2195        ## Step 15        ## Step 15
2196        $self->{insertion_mode} = 'in body' and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
2197                
2198        ## Step 16        ## Step 16
2199        $i--;        $i--;
# Line 1866  sub _reset_insertion_mode ($) { Line 2207  sub _reset_insertion_mode ($) {
2207  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
2208    my $self = shift;    my $self = shift;
2209    
   my $phase = 'main';  
   
2210    my $active_formatting_elements = [];    my $active_formatting_elements = [];
2211    
2212    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 1950  sub _tree_construction_main ($) { Line 2289  sub _tree_construction_main ($) {
2289      }      }
2290    }; # $clear_up_to_marker    }; # $clear_up_to_marker
2291    
2292    my $style_start_tag = sub {    my $parse_rcdata = sub ($$) {
2293      my $style_el; !!!create-element ($style_el, 'style', $token->{attributes});      my ($content_model_flag, $insert) = @_;
2294      ## $self->{insertion_mode} eq 'in head' and ... (always true)  
2295      (($self->{insertion_mode} eq 'in head' and defined $self->{head_element})      ## Step 1
2296       ? $self->{head_element} : $self->{open_elements}->[-1]->[0])      my $start_tag_name = $token->{tag_name};
2297        ->append_child ($style_el);      my $el;
2298      $self->{content_model_flag} = 'CDATA';      !!!create-element ($el, $start_tag_name, $token->{attributes});
2299    
2300        ## Step 2
2301        $insert->($el); # /context node/->append_child ($el)
2302    
2303        ## Step 3
2304        $self->{content_model} = $content_model_flag; # CDATA or RCDATA
2305      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
2306                  
2307        ## Step 4
2308      my $text = '';      my $text = '';
2309      !!!next-token;      !!!next-token;
2310      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
2311        $text .= $token->{data};        $text .= $token->{data};
2312        !!!next-token;        !!!next-token;
2313      } # stop if non-character token or tokenizer stops tokenising      }
2314    
2315        ## Step 5
2316      if (length $text) {      if (length $text) {
2317        $style_el->manakai_append_text ($text);        my $text = $self->{document}->create_text_node ($text);
2318          $el->append_child ($text);
2319      }      }
2320        
2321      $self->{content_model_flag} = 'PCDATA';      ## Step 6
2322                      $self->{content_model} = PCDATA_CONTENT_MODEL;
2323      if ($token->{type} eq 'end tag' and $token->{tag_name} eq 'style') {  
2324        ## Step 7
2325        if ($token->{type} == END_TAG_TOKEN and $token->{tag_name} eq $start_tag_name) {
2326        ## Ignore the token        ## Ignore the token
2327      } else {      } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {
2328        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type});
2329        ## ISSUE: And ignore?      } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
2330          !!!parse-error (type => 'in RCDATA:#'.$token->{type});
2331        } else {
2332          die "$0: $content_model_flag in parse_rcdata";
2333      }      }
2334      !!!next-token;      !!!next-token;
2335    }; # $style_start_tag    }; # $parse_rcdata
2336    
2337    my $script_start_tag = sub {    my $script_start_tag = sub ($) {
2338        my $insert = $_[0];
2339      my $script_el;      my $script_el;
2340      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes});
2341      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
2342    
2343      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
2344      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
2345            
2346      my $text = '';      my $text = '';
2347      !!!next-token;      !!!next-token;
2348      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
2349        $text .= $token->{data};        $text .= $token->{data};
2350        !!!next-token;        !!!next-token;
2351      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
# Line 1998  sub _tree_construction_main ($) { Line 2353  sub _tree_construction_main ($) {
2353        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
2354      }      }
2355                                
2356      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
2357    
2358      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
2359          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
2360        ## Ignore the token        ## Ignore the token
2361      } else {      } else {
# Line 2014  sub _tree_construction_main ($) { Line 2369  sub _tree_construction_main ($) {
2369      } else {      } else {
2370        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
2371        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
2372          
2373        (($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);  
2374                
2375        ## TODO: insertion point = $old_insertion_point (might be "undefined")        ## TODO: insertion point = $old_insertion_point (might be "undefined")
2376                
# Line 2210  sub _tree_construction_main ($) { Line 2564  sub _tree_construction_main ($) {
2564    }; # $formatting_end_tag    }; # $formatting_end_tag
2565    
2566    my $insert_to_current = sub {    my $insert_to_current = sub {
2567      $self->{open_elements}->[-1]->[0]->append_child (shift);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
2568    }; # $insert_to_current    }; # $insert_to_current
2569    
2570    my $insert_to_foster = sub {    my $insert_to_foster = sub {
# Line 2244  sub _tree_construction_main ($) { Line 2598  sub _tree_construction_main ($) {
2598                         }                         }
2599    }; # $insert_to_foster    }; # $insert_to_foster
2600    
2601    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  
2602    
2603    B: {    B: {
2604      if ($phase eq 'main') {      if ($token->{type} == DOCTYPE_TOKEN) {
2605        if ($token->{type} eq 'DOCTYPE') {        !!!parse-error (type => 'DOCTYPE in the middle');
2606          !!!parse-error (type => 'in html:#DOCTYPE');        ## Ignore the token
2607          ## Ignore the token        ## Stay in the phase
2608          ## Stay in the phase        !!!next-token;
2609          !!!next-token;        redo B;
2610          redo B;      } elsif ($token->{type} == END_OF_FILE_TOKEN) {
2611        } elsif ($token->{type} eq 'start tag' and        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
2612                 $token->{tag_name} eq 'html') {          #
2613          ## 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') {  
2614          ## Generate implied end tags          ## Generate implied end tags
2615          if ({          if ({
2616               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,
2617                 tbody => 1, tfoot=> 1, thead => 1,
2618              }->{$self->{open_elements}->[-1]->[1]}) {              }->{$self->{open_elements}->[-1]->[1]}) {
2619            !!!back-token;            !!!back-token;
2620            $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};            $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};
2621            redo B;            redo B;
2622          }          }
2623                    
# Line 3078  sub _tree_construction_main ($) { Line 2630  sub _tree_construction_main ($) {
2630            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
2631          }          }
2632    
         ## Stop parsing  
         last B;  
   
2633          ## ISSUE: There is an issue in the spec.          ## ISSUE: There is an issue in the spec.
2634          }
2635    
2636          ## Stop parsing
2637          last B;
2638        } elsif ($token->{type} == START_TAG_TOKEN and
2639                 $token->{tag_name} eq 'html') {
2640          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
2641            ## Turn into the main phase
2642            !!!parse-error (type => 'after html:html');
2643            $self->{insertion_mode} = AFTER_BODY_IM;
2644          } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
2645            ## Turn into the main phase
2646            !!!parse-error (type => 'after html:html');
2647            $self->{insertion_mode} = AFTER_FRAMESET_IM;
2648          }
2649    
2650    ## ISSUE: "aa<html>" is not a parse error.
2651    ## ISSUE: "<html>" in fragment is not a parse error.
2652          unless ($token->{first_start_tag}) {
2653            !!!parse-error (type => 'not first start tag');
2654          }
2655          my $top_el = $self->{open_elements}->[0]->[0];
2656          for my $attr_name (keys %{$token->{attributes}}) {
2657            unless ($top_el->has_attribute_ns (undef, $attr_name)) {
2658              $top_el->set_attribute_ns
2659                (undef, [undef, $attr_name],
2660                 $token->{attributes}->{$attr_name}->{value});
2661            }
2662          }
2663          !!!next-token;
2664          redo B;
2665        } elsif ($token->{type} == COMMENT_TOKEN) {
2666          my $comment = $self->{document}->create_comment ($token->{data});
2667          if ($self->{insertion_mode} & AFTER_HTML_IMS) {
2668            $self->{document}->append_child ($comment);
2669          } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
2670            $self->{open_elements}->[0]->[0]->append_child ($comment);
2671        } else {        } else {
2672          if ($self->{insertion_mode} eq 'before head') {          $self->{open_elements}->[-1]->[0]->append_child ($comment);
2673            if ($token->{type} eq 'character') {        }
2674              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        !!!next-token;
2675                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);        redo B;
2676                unless (length $token->{data}) {      } elsif ($self->{insertion_mode} & HEAD_IMS) {
2677                  !!!next-token;        if ($token->{type} == CHARACTER_TOKEN) {
2678                  redo B;          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
2679                }            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
2680              }            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);  
2681              !!!next-token;              !!!next-token;
2682              redo B;              redo B;
2683            } elsif ($token->{type} eq 'start tag') {            }
2684              my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};          }
2685              !!!create-element ($self->{head_element}, 'head', $attr);  
2686              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2687              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            ## As if <head>
2688              $self->{insertion_mode} = 'in head';            !!!create-element ($self->{head_element}, 'head');
2689              if ($token->{tag_name} eq 'head') {            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2690                !!!next-token;            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2691              #} elsif ({  
2692              #          base => 1, link => 1, meta => 1,            ## Reprocess in the "in head" insertion mode...
2693              #          script => 1, style => 1, title => 1,            pop @{$self->{open_elements}};
2694              #         }->{$token->{tag_name}}) {  
2695              #  ## reprocess            ## Reprocess in the "after head" insertion mode...
2696              } else {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2697                ## reprocess            ## As if </noscript>
2698              }            pop @{$self->{open_elements}};
2699              !!!parse-error (type => 'in noscript:#character');
2700              
2701              ## Reprocess in the "in head" insertion mode...
2702              ## As if </head>
2703              pop @{$self->{open_elements}};
2704    
2705              ## Reprocess in the "after head" insertion mode...
2706            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2707              pop @{$self->{open_elements}};
2708    
2709              ## Reprocess in the "after head" insertion mode...
2710            }
2711    
2712                ## "after head" insertion mode
2713                ## As if <body>
2714                !!!insert-element ('body');
2715                $self->{insertion_mode} = IN_BODY_IM;
2716                ## reprocess
2717              redo B;              redo B;
2718            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
2719              if ($token->{tag_name} eq 'html') {              if ($token->{tag_name} eq 'head') {
2720                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2721                    !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
2722                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2723                    push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
2724                    $self->{insertion_mode} = IN_HEAD_IM;
2725                    !!!next-token;
2726                    redo B;
2727                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2728                    #
2729                  } else {
2730                    !!!parse-error (type => 'in head:head'); # or in head noscript
2731                    ## Ignore the token
2732                    !!!next-token;
2733                    redo B;
2734                  }
2735                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2736                ## As if <head>                ## As if <head>
2737                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head');
2738                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2739                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2740                $self->{insertion_mode} = 'in head';  
2741                ## reprocess                $self->{insertion_mode} = IN_HEAD_IM;
2742                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;  
               }  
2743              }              }
               
             #  
           } 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  
2744    
2745                my $text = '';              if ($token->{tag_name} eq 'base') {
2746                !!!next-token;                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2747                while ($token->{type} eq 'character') {                  ## As if </noscript>
2748                  $text .= $token->{data};                  pop @{$self->{open_elements}};
2749                  !!!next-token;                  !!!parse-error (type => 'in noscript:base');
               }  
               if (length $text) {  
                 $title_el->manakai_append_text ($text);  
               }  
                 
               $self->{content_model_flag} = 'PCDATA';  
2750                                
2751                if ($token->{type} eq 'end tag' and                  $self->{insertion_mode} = IN_HEAD_IM;
2752                    $token->{tag_name} eq 'title') {                  ## Reprocess in the "in head" insertion mode...
2753                  ## Ignore the token                }
2754                } else {  
2755                  !!!parse-error (type => 'in RCDATA:#'.$token->{type});                ## NOTE: There is a "as if in head" code clone.
2756                  ## ISSUE: And ignore?                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2757                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2758                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2759                }                }
2760                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2761                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2762                  pop @{$self->{open_elements}}
2763                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2764                !!!next-token;                !!!next-token;
2765                redo B;                redo B;
2766              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'link') {
2767                $style_start_tag->();                ## NOTE: There is a "as if in head" code clone.
2768                redo B;                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2769              } elsif ($token->{tag_name} eq 'script') {                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2770                $script_start_tag->();                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2771                  }
2772                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2773                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2774                  pop @{$self->{open_elements}}
2775                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2776                  !!!next-token;
2777                redo B;                redo B;
2778              } elsif ({base => 1, link => 1, meta => 1}->{$token->{tag_name}}) {              } elsif ($token->{tag_name} eq 'meta') {
2779                ## NOTE: There are "as if in head" code clones                ## NOTE: There is a "as if in head" code clone.
2780                my $el;                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2781                !!!create-element ($el, $token->{tag_name}, $token->{attributes});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2782                (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2783                  ->append_child ($el);                }
2784                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2785                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2786    
2787                  unless ($self->{confident}) {
2788                    my $charset;
2789                    if ($token->{attributes}->{charset}) { ## TODO: And if supported
2790                      $charset = $token->{attributes}->{charset}->{value};
2791                    }
2792                    if ($token->{attributes}->{'http-equiv'}) {
2793                      ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
2794                      if ($token->{attributes}->{'http-equiv'}->{value}
2795                          =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
2796                              [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
2797                              ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
2798                        $charset = defined $1 ? $1 : defined $2 ? $2 : $3;
2799                      } ## TODO: And if supported
2800                    }
2801                    ## TODO: Change the encoding
2802                  }
2803    
2804                  ## TODO: Extracting |charset| from |meta|.
2805                  pop @{$self->{open_elements}}
2806                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2807                !!!next-token;                !!!next-token;
2808                redo B;                redo B;
2809              } elsif ($token->{tag_name} eq 'head') {              } elsif ($token->{tag_name} eq 'title') {
2810                !!!parse-error (type => 'in head:head');                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2811                ## Ignore the token                  ## As if </noscript>
2812                !!!next-token;                  pop @{$self->{open_elements}};
2813                    !!!parse-error (type => 'in noscript:title');
2814                  
2815                    $self->{insertion_mode} = IN_HEAD_IM;
2816                    ## Reprocess in the "in head" insertion mode...
2817                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2818                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2819                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2820                  }
2821    
2822                  ## NOTE: There is a "as if in head" code clone.
2823                  my $parent = defined $self->{head_element} ? $self->{head_element}
2824                      : $self->{open_elements}->[-1]->[0];
2825                  $parse_rcdata->(RCDATA_CONTENT_MODEL,
2826                                  sub { $parent->append_child ($_[0]) });
2827                  pop @{$self->{open_elements}}
2828                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2829                redo B;                redo B;
2830              } else {              } elsif ($token->{tag_name} eq 'style') {
2831                #                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
2832              }                ## insertion mode IN_HEAD_IM)
2833            } elsif ($token->{type} eq 'end tag') {                ## NOTE: There is a "as if in head" code clone.
2834              if ($token->{tag_name} eq 'head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2835                if ($self->{open_elements}->[-1]->[1] eq 'head') {                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2836                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2837                  }
2838                  $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
2839                  pop @{$self->{open_elements}}
2840                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2841                  redo B;
2842                } elsif ($token->{tag_name} eq 'noscript') {
2843                  if ($self->{insertion_mode} == IN_HEAD_IM) {
2844                    ## NOTE: and scripting is disalbed
2845                    !!!insert-element ($token->{tag_name}, $token->{attributes});
2846                    $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
2847                    !!!next-token;
2848                    redo B;
2849                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2850                    !!!parse-error (type => 'in noscript:noscript');
2851                    ## Ignore the token
2852                    !!!next-token;
2853                    redo B;
2854                  } else {
2855                    #
2856                  }
2857                } elsif ($token->{tag_name} eq 'script') {
2858                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2859                    ## As if </noscript>
2860                    pop @{$self->{open_elements}};
2861                    !!!parse-error (type => 'in noscript:script');
2862                  
2863                    $self->{insertion_mode} = IN_HEAD_IM;
2864                    ## Reprocess in the "in head" insertion mode...
2865                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2866                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2867                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2868                  }
2869    
2870                  ## NOTE: There is a "as if in head" code clone.
2871                  $script_start_tag->($insert_to_current);
2872                  pop @{$self->{open_elements}}
2873                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2874                  redo B;
2875                } elsif ($token->{tag_name} eq 'body' or
2876                         $token->{tag_name} eq 'frameset') {
2877                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2878                    ## As if </noscript>
2879                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2880                    !!!parse-error (type => 'in noscript:'.$token->{tag_name});
2881                    
2882                    ## Reprocess in the "in head" insertion mode...
2883                    ## As if </head>
2884                    pop @{$self->{open_elements}};
2885                    
2886                    ## Reprocess in the "after head" insertion mode...
2887                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2888                    pop @{$self->{open_elements}};
2889                    
2890                    ## Reprocess in the "after head" insertion mode...
2891                  }
2892    
2893                  ## "after head" insertion mode
2894                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2895                  if ($token->{tag_name} eq 'body') {
2896                    $self->{insertion_mode} = IN_BODY_IM;
2897                  } elsif ($token->{tag_name} eq 'frameset') {
2898                    $self->{insertion_mode} = IN_FRAMESET_IM;
2899                } else {                } else {
2900                  !!!parse-error (type => 'unmatched end tag:head');                  die "$0: tag name: $self->{tag_name}";
2901                }                }
               $self->{insertion_mode} = 'after head';  
2902                !!!next-token;                !!!next-token;
2903                redo B;                redo B;
             } elsif ($token->{tag_name} eq 'html') {  
               #  
2904              } else {              } else {
2905                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                #
               ## Ignore the token  
               !!!next-token;  
               redo B;  
2906              }              }
           } else {  
             #  
           }  
2907    
2908            if ($self->{open_elements}->[-1]->[1] eq 'head') {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2909              ## As if </head>                ## As if </noscript>
2910              pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
2911            }                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
2912            $self->{insertion_mode} = 'after head';                
2913            ## reprocess                ## Reprocess in the "in head" insertion mode...
2914            redo B;                ## As if </head>
2915                  pop @{$self->{open_elements}};
2916    
2917            ## ISSUE: An issue in the spec.                ## Reprocess in the "after head" insertion mode...
2918          } elsif ($self->{insertion_mode} eq 'after head') {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2919            if ($token->{type} eq 'character') {                ## As if </head>
2920              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                pop @{$self->{open_elements}};
2921                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
2922                unless (length $token->{data}) {                ## Reprocess in the "after head" insertion mode...
                 !!!next-token;  
                 redo B;  
               }  
2923              }              }
               
             #  
           } 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});  
2924    
2925              !!!next-token;              ## "after head" insertion mode
2926              redo B;              ## As if <body>
2927            } elsif ($token->{type} eq 'comment') {              !!!insert-element ('body');
2928              ## NOTE: There is a code clone of "comment in body".              $self->{insertion_mode} = IN_BODY_IM;
2929              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);  
2930              redo B;              redo B;
2931            }            } elsif ($token->{type} == END_TAG_TOKEN) {
2932          } elsif ($self->{insertion_mode} eq 'in table') {              if ($token->{tag_name} eq 'head') {
2933            if ($token->{type} eq 'character') {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2934              ## NOTE: There are "character in table" code clones.                  ## As if <head>
2935              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                  !!!create-element ($self->{head_element}, 'head');
2936                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2937                                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2938                unless (length $token->{data}) {  
2939                    ## Reprocess in the "in head" insertion mode...
2940                    pop @{$self->{open_elements}};
2941                    $self->{insertion_mode} = AFTER_HEAD_IM;
2942                    !!!next-token;
2943                    redo B;
2944                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2945                    ## As if </noscript>
2946                    pop @{$self->{open_elements}};
2947                    !!!parse-error (type => 'in noscript:script');
2948                    
2949                    ## Reprocess in the "in head" insertion mode...
2950                    pop @{$self->{open_elements}};
2951                    $self->{insertion_mode} = AFTER_HEAD_IM;
2952                    !!!next-token;
2953                    redo B;
2954                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2955                    pop @{$self->{open_elements}};
2956                    $self->{insertion_mode} = AFTER_HEAD_IM;
2957                  !!!next-token;                  !!!next-token;
2958                  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});  
2959                } else {                } else {
2960                  $foster_parent_element->insert_before                  #
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
2961                }                }
2962              } else {              } elsif ($token->{tag_name} eq 'noscript') {
2963                $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]);  
2964                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2965                    $self->{insertion_mode} = IN_HEAD_IM;
2966                    !!!next-token;
2967                    redo B;
2968                  } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2969                    !!!parse-error (type => 'unmatched end tag:noscript');
2970                    ## Ignore the token ## ISSUE: An issue in the spec.
2971                    !!!next-token;
2972                    redo B;
2973                  } else {
2974                    #
2975                }                }
   
               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;  
2976              } elsif ({              } elsif ({
2977                        col => 1,                        body => 1, html => 1,
                       td => 1, th => 1, tr => 1,  
2978                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
2979                ## Clear back to table context                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2980                while ($self->{open_elements}->[-1]->[1] ne 'table' and                  ## As if <head>
2981                       $self->{open_elements}->[-1]->[1] ne 'html') {                  !!!create-element ($self->{head_element}, 'head');
2982                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2983                  pop @{$self->{open_elements}};                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2984                }  
2985                    $self->{insertion_mode} = IN_HEAD_IM;
2986                !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');                  ## Reprocess in the "in head" insertion mode...
2987                $self->{insertion_mode} = $token->{tag_name} eq 'col'                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2988                  ? 'in column group' : 'in table body';                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2989                ## 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>  
2990                  !!!next-token;                  !!!next-token;
2991                  redo B;                  redo B;
2992                }                }
2993                                
2994                ## generate implied end tags                #
2995                if ({              } elsif ({
2996                     dd => 1, dt => 1, li => 1, p => 1,                        p => 1, br => 1,
2997                     td => 1, th => 1, tr => 1,                       }->{$token->{tag_name}}) {
2998                    }->{$self->{open_elements}->[-1]->[1]}) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2999                  !!!back-token; # <table>                  ## As if <head>
3000                  $token = {type => 'end tag', tag_name => 'table'};                  !!!create-element ($self->{head_element}, 'head');
3001                  !!!back-token;                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3002                  $token = {type => 'end tag',                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
3003    
3004                if ($self->{open_elements}->[-1]->[1] ne 'table') {                  $self->{insertion_mode} = IN_HEAD_IM;
3005                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  ## Reprocess in the "in head" insertion mode...
3006                }                }
3007    
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
3008                #                #
3009              }              } else {
3010            } elsif ($token->{type} eq 'end tag') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3011              if ($token->{tag_name} eq 'table') {                  #
3012                ## 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) {  
3013                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3014                  ## Ignore the token                  ## Ignore the token
3015                  !!!next-token;                  !!!next-token;
3016                  redo B;                  redo B;
3017                }                }
3018                              }
               ## 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]);  
               }  
3019    
3020                splice @{$self->{open_elements}}, $i;              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3021                  ## As if </noscript>
3022                  pop @{$self->{open_elements}};
3023                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
3024                  
3025                  ## Reprocess in the "in head" insertion mode...
3026                  ## As if </head>
3027                  pop @{$self->{open_elements}};
3028    
3029                $self->_reset_insertion_mode;                ## Reprocess in the "after head" insertion mode...
3030                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3031                  ## As if </head>
3032                  pop @{$self->{open_elements}};
3033    
3034                !!!next-token;                ## Reprocess in the "after head" insertion mode...
3035                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}}) {  
3036                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3037                ## Ignore the token                ## Ignore the token ## ISSUE: An issue in the spec.
3038                !!!next-token;                !!!next-token;
3039                redo B;                redo B;
             } else {  
               #  
3040              }              }
3041    
3042                ## "after head" insertion mode
3043                ## As if <body>
3044                !!!insert-element ('body');
3045                $self->{insertion_mode} = IN_BODY_IM;
3046                ## reprocess
3047                redo B;
3048            } else {            } else {
3049              #              die "$0: $token->{type}: Unknown token type";
3050            }            }
3051    
3052            !!!parse-error (type => 'in table:'.$token->{tag_name});            ## ISSUE: An issue in the spec.
3053            $in_body->($insert_to_foster);      } elsif ($self->{insertion_mode} & BODY_IMS) {
3054            redo B;            if ($token->{type} == CHARACTER_TOKEN) {
3055          } 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".  
3056              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
3057                            
3058              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3059    
3060              !!!next-token;              !!!next-token;
3061              redo B;              redo B;
3062            } 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') {  
3063              if ({              if ({
3064                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
3065                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
3066                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3067                !!!parse-error (type => 'not closed:caption');                if ($self->{insertion_mode} == IN_CELL_IM) {
3068                    ## have an element in table scope
3069                    my $tn;
3070                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3071                      my $node = $self->{open_elements}->[$_];
3072                      if ($node->[1] eq 'td' or $node->[1] eq 'th') {
3073                        $tn = $node->[1];
3074                        last INSCOPE;
3075                      } elsif ({
3076                                table => 1, html => 1,
3077                               }->{$node->[1]}) {
3078                        last INSCOPE;
3079                      }
3080                    } # INSCOPE
3081                      unless (defined $tn) {
3082                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3083                        ## Ignore the token
3084                        !!!next-token;
3085                        redo B;
3086                      }
3087                    
3088                    ## Close the cell
3089                    !!!back-token; # <?>
3090                    $token = {type => END_TAG_TOKEN, tag_name => $tn};
3091                    redo B;
3092                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3093                    !!!parse-error (type => 'not closed:caption');
3094                    
3095                    ## As if </caption>
3096                    ## have a table element in table scope
3097                    my $i;
3098                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3099                      my $node = $self->{open_elements}->[$_];
3100                      if ($node->[1] eq 'caption') {
3101                        $i = $_;
3102                        last INSCOPE;
3103                      } elsif ({
3104                                table => 1, html => 1,
3105                               }->{$node->[1]}) {
3106                        last INSCOPE;
3107                      }
3108                    } # INSCOPE
3109                      unless (defined $i) {
3110                        !!!parse-error (type => 'unmatched end tag:caption');
3111                        ## Ignore the token
3112                        !!!next-token;
3113                        redo B;
3114                      }
3115                    
3116                    ## generate implied end tags
3117                    if ({
3118                         dd => 1, dt => 1, li => 1, p => 1,
3119                         td => 1, th => 1, tr => 1,
3120                         tbody => 1, tfoot=> 1, thead => 1,
3121                        }->{$self->{open_elements}->[-1]->[1]}) {
3122                      !!!back-token; # <?>
3123                      $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3124                      !!!back-token;
3125                      $token = {type => END_TAG_TOKEN,
3126                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3127                      redo B;
3128                    }
3129    
3130                ## As if </caption>                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3131                ## 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;  
3132                  }                  }
3133                } # INSCOPE                  
3134                unless (defined $i) {                  splice @{$self->{open_elements}}, $i;
3135                  !!!parse-error (type => 'unmatched end tag:caption');                  
3136                    $clear_up_to_marker->();
3137                    
3138                    $self->{insertion_mode} = IN_TABLE_IM;
3139                    
3140                    ## reprocess
3141                    redo B;
3142                  } else {
3143                    #
3144                  }
3145                } else {
3146                  #
3147                }
3148              } elsif ($token->{type} == END_TAG_TOKEN) {
3149                if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
3150                  if ($self->{insertion_mode} == IN_CELL_IM) {
3151                    ## have an element in table scope
3152                    my $i;
3153                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3154                      my $node = $self->{open_elements}->[$_];
3155                      if ($node->[1] eq $token->{tag_name}) {
3156                        $i = $_;
3157                        last INSCOPE;
3158                      } elsif ({
3159                                table => 1, html => 1,
3160                               }->{$node->[1]}) {
3161                        last INSCOPE;
3162                      }
3163                    } # INSCOPE
3164                      unless (defined $i) {
3165                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3166                        ## Ignore the token
3167                        !!!next-token;
3168                        redo B;
3169                      }
3170                    
3171                    ## generate implied end tags
3172                    if ({
3173                         dd => 1, dt => 1, li => 1, p => 1,
3174                         td => ($token->{tag_name} eq 'th'),
3175                         th => ($token->{tag_name} eq 'td'),
3176                         tr => 1,
3177                         tbody => 1, tfoot=> 1, thead => 1,
3178                        }->{$self->{open_elements}->[-1]->[1]}) {
3179                      !!!back-token;
3180                      $token = {type => END_TAG_TOKEN,
3181                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3182                      redo B;
3183                    }
3184                    
3185                    if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
3186                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3187                    }
3188                    
3189                    splice @{$self->{open_elements}}, $i;
3190                    
3191                    $clear_up_to_marker->();
3192                    
3193                    $self->{insertion_mode} = IN_ROW_IM;
3194                    
3195                    !!!next-token;
3196                    redo B;
3197                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3198                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3199                  ## Ignore the token                  ## Ignore the token
3200                  !!!next-token;                  !!!next-token;
3201                  redo B;                  redo B;
3202                  } else {
3203                    #
3204                }                }
3205                              } elsif ($token->{tag_name} eq 'caption') {
3206                ## generate implied end tags                if ($self->{insertion_mode} == IN_CAPTION_IM) {
3207                if ({                  ## have a table element in table scope
3208                     dd => 1, dt => 1, li => 1, p => 1,                  my $i;
3209                     td => 1, th => 1, tr => 1,                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3210                    }->{$self->{open_elements}->[-1]->[1]}) {                    my $node = $self->{open_elements}->[$_];
3211                  !!!back-token; # <?>                    if ($node->[1] eq $token->{tag_name}) {
3212                  $token = {type => 'end tag', tag_name => 'caption'};                      $i = $_;
3213                  !!!back-token;                      last INSCOPE;
3214                  $token = {type => 'end tag',                    } elsif ({
3215                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                              table => 1, html => 1,
3216                               }->{$node->[1]}) {
3217                        last INSCOPE;
3218                      }
3219                    } # INSCOPE
3220                      unless (defined $i) {
3221                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3222                        ## Ignore the token
3223                        !!!next-token;
3224                        redo B;
3225                      }
3226                    
3227                    ## generate implied end tags
3228                    if ({
3229                         dd => 1, dt => 1, li => 1, p => 1,
3230                         td => 1, th => 1, tr => 1,
3231                         tbody => 1, tfoot=> 1, thead => 1,
3232                        }->{$self->{open_elements}->[-1]->[1]}) {
3233                      !!!back-token;
3234                      $token = {type => END_TAG_TOKEN,
3235                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3236                      redo B;
3237                    }
3238                    
3239                    if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3240                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3241                    }
3242                    
3243                    splice @{$self->{open_elements}}, $i;
3244                    
3245                    $clear_up_to_marker->();
3246                    
3247                    $self->{insertion_mode} = IN_TABLE_IM;
3248                    
3249                    !!!next-token;
3250                  redo B;                  redo B;
3251                  } elsif ($self->{insertion_mode} == IN_CELL_IM) {
3252                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3253                    ## Ignore the token
3254                    !!!next-token;
3255                    redo B;
3256                  } else {
3257                    #
3258                }                }
3259                } elsif ({
3260                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                        table => 1, tbody => 1, tfoot => 1,
3261                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                        thead => 1, tr => 1,
3262                }                       }->{$token->{tag_name}} and
3263                         $self->{insertion_mode} == IN_CELL_IM) {
3264                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  
3265                my $i;                my $i;
3266                  my $tn;
3267                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3268                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3269                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
3270                    $i = $_;                    $i = $_;
3271                    last INSCOPE;                    last INSCOPE;
3272                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {
3273                      $tn = $node->[1];
3274                      ## NOTE: There is exactly one |td| or |th| element
3275                      ## in scope in the stack of open elements by definition.
3276                  } elsif ({                  } elsif ({
3277                            table => 1, html => 1,                            table => 1, html => 1,
3278                           }->{$node->[1]}) {                           }->{$node->[1]}) {
# Line 3617  sub _tree_construction_main ($) { Line 3285  sub _tree_construction_main ($) {
3285                  !!!next-token;                  !!!next-token;
3286                  redo B;                  redo B;
3287                }                }
                 
               ## 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 'caption') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
3288    
3289                $self->{insertion_mode} = 'in table';                ## Close the cell
3290                  !!!back-token; # </?>
3291                !!!next-token;                $token = {type => END_TAG_TOKEN, tag_name => $tn};
3292                redo B;                redo B;
3293              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table' and
3294                         $self->{insertion_mode} == IN_CAPTION_IM) {
3295                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption');
3296    
3297                ## As if </caption>                ## As if </caption>
# Line 3669  sub _tree_construction_main ($) { Line 3319  sub _tree_construction_main ($) {
3319                if ({                if ({
3320                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3321                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3322                       tbody => 1, tfoot=> 1, thead => 1,
3323                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3324                  !!!back-token; # </table>                  !!!back-token; # </table>
3325                  $token = {type => 'end tag', tag_name => 'caption'};                  $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3326                  !!!back-token;                  !!!back-token;
3327                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3328                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3329                  redo B;                  redo B;
3330                }                }
# Line 3686  sub _tree_construction_main ($) { Line 3337  sub _tree_construction_main ($) {
3337    
3338                $clear_up_to_marker->();                $clear_up_to_marker->();
3339    
3340                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
3341    
3342                ## reprocess                ## reprocess
3343                redo B;                redo B;
3344              } elsif ({              } elsif ({
3345                        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,  
3346                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3347                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
3348                ## 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');  
3349                  ## Ignore the token                  ## Ignore the token
3350                  !!!next-token;                  !!!next-token;
3351                  redo B;                  redo B;
3352                } else {                } else {
3353                  pop @{$self->{open_elements}}; # colgroup                  #
                 $self->{insertion_mode} = 'in table';  
                 !!!next-token;  
                 redo B;              
3354                }                }
3355              } elsif ($token->{tag_name} eq 'col') {              } elsif ({
3356                !!!parse-error (type => 'unmatched end tag:col');                        tbody => 1, tfoot => 1,
3357                          thead => 1, tr => 1,
3358                         }->{$token->{tag_name}} and
3359                         $self->{insertion_mode} == IN_CAPTION_IM) {
3360                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3361                ## Ignore the token                ## Ignore the token
3362                !!!next-token;                !!!next-token;
3363                redo B;                redo B;
3364              } else {              } else {
3365                #                #
3366              }              }
3367            } else {        } else {
3368              #          die "$0: $token->{type}: Unknown token type";
3369            }        }
3370    
3371            ## As if </colgroup>        $insert = $insert_to_current;
3372            if ($self->{open_elements}->[-1]->[1] eq 'html') {        #
3373              !!!parse-error (type => 'unmatched end tag:colgroup');      } elsif ($self->{insertion_mode} & TABLE_IMS) {
3374              ## 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.  
3375              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3376                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3377                                
# Line 3788  sub _tree_construction_main ($) { Line 3388  sub _tree_construction_main ($) {
3388              ## into the current node" while characters might not be              ## into the current node" while characters might not be
3389              ## result in a new Text node.              ## result in a new Text node.
3390              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
3391                
3392              if ({              if ({
3393                   table => 1, tbody => 1, tfoot => 1,                   table => 1, tbody => 1, tfoot => 1,
3394                   thead => 1, tr => 1,                   thead => 1, tr => 1,
# Line 3828  sub _tree_construction_main ($) { Line 3428  sub _tree_construction_main ($) {
3428                            
3429              !!!next-token;              !!!next-token;
3430              redo B;              redo B;
3431            } 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') {  
3432              if ({              if ({
3433                   tr => 1,                   tr => ($self->{insertion_mode} != IN_ROW_IM),
3434                   th => 1, td => 1,                   th => 1, td => 1,
3435                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3436                unless ($token->{tag_name} eq 'tr') {                if ($self->{insertion_mode} == IN_TABLE_IM) {
3437                  !!!parse-error (type => 'missing start tag:tr');                  ## Clear back to table context
3438                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3439                           $self->{open_elements}->[-1]->[1] ne 'html') {
3440                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3441                      pop @{$self->{open_elements}};
3442                    }
3443                    
3444                    !!!insert-element ('tbody');
3445                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3446                    ## reprocess in the "in table body" insertion mode...
3447                }                }
3448    
3449                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3450                    unless ($token->{tag_name} eq 'tr') {
3451                      !!!parse-error (type => 'missing start tag:tr');
3452                    }
3453                    
3454                    ## Clear back to table body context
3455                    while (not {
3456                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3457                    }->{$self->{open_elements}->[-1]->[1]}) {
3458                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3459                      pop @{$self->{open_elements}};
3460                    }
3461                    
3462                    $self->{insertion_mode} = IN_ROW_IM;
3463                    if ($token->{tag_name} eq 'tr') {
3464                      !!!insert-element ($token->{tag_name}, $token->{attributes});
3465                      !!!next-token;
3466                      redo B;
3467                    } else {
3468                      !!!insert-element ('tr');
3469                      ## reprocess in the "in row" insertion mode
3470                    }
3471                  }
3472    
3473                  ## Clear back to table row context
3474                while (not {                while (not {
3475                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tr => 1, html => 1,
3476                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
3477                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3478                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3479                }                }
3480                                
3481                $self->{insertion_mode} = 'in row';                !!!insert-element ($token->{tag_name}, $token->{attributes});
3482                if ($token->{tag_name} eq 'tr') {                $self->{insertion_mode} = IN_CELL_IM;
3483                  !!!insert-element ($token->{tag_name}, $token->{attributes});  
3484                  !!!next-token;                push @$active_formatting_elements, ['#marker', ''];
3485                } else {                
3486                  !!!insert-element ('tr');                !!!next-token;
                 ## reprocess  
               }  
3487                redo B;                redo B;
3488              } elsif ({              } elsif ({
3489                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
3490                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
3491                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3492                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3493                ## have an element in table scope                if ($self->{insertion_mode} == IN_ROW_IM) {
3494                my $i;                  ## As if </tr>
3495                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
3496                  my $node = $self->{open_elements}->[$_];                  my $i;
3497                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3498                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
3499                      }->{$node->[1]}) {                    if ($node->[1] eq 'tr') {
3500                    $i = $_;                      $i = $_;
3501                    last INSCOPE;                      last INSCOPE;
3502                  } elsif ({                    } elsif ({
3503                            table => 1, html => 1,                              table => 1, html => 1,
3504                           }->{$node->[1]}) {                             }->{$node->[1]}) {
3505                    last INSCOPE;                      last INSCOPE;
3506                      }
3507                    } # INSCOPE
3508                    unless (defined $i) {
3509                      !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});
3510                      ## Ignore the token
3511                      !!!next-token;
3512                      redo B;
3513                    }
3514                    
3515                    ## Clear back to table row context
3516                    while (not {
3517                      tr => 1, html => 1,
3518                    }->{$self->{open_elements}->[-1]->[1]}) {
3519                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3520                      pop @{$self->{open_elements}};
3521                    }
3522                    
3523                    pop @{$self->{open_elements}}; # tr
3524                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3525                    if ($token->{tag_name} eq 'tr') {
3526                      ## reprocess
3527                      redo B;
3528                    } else {
3529                      ## reprocess in the "in table body" insertion mode...
3530                  }                  }
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
3531                }                }
3532    
3533                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3534                while (not {                  ## have an element in table scope
3535                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  my $i;
3536                }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3537                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    my $node = $self->{open_elements}->[$_];
3538                      if ({
3539                           tbody => 1, thead => 1, tfoot => 1,
3540                          }->{$node->[1]}) {
3541                        $i = $_;
3542                        last INSCOPE;
3543                      } elsif ({
3544                                table => 1, html => 1,
3545                               }->{$node->[1]}) {
3546                        last INSCOPE;
3547                      }
3548                    } # INSCOPE
3549                    unless (defined $i) {
3550                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3551                      ## Ignore the token
3552                      !!!next-token;
3553                      redo B;
3554                    }
3555    
3556                    ## Clear back to table body context
3557                    while (not {
3558                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3559                    }->{$self->{open_elements}->[-1]->[1]}) {
3560                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3561                      pop @{$self->{open_elements}};
3562                    }
3563                    
3564                    ## As if <{current node}>
3565                    ## have an element in table scope
3566                    ## true by definition
3567                    
3568                    ## Clear back to table body context
3569                    ## nop by definition
3570                    
3571                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3572                    $self->{insertion_mode} = IN_TABLE_IM;
3573                    ## reprocess in "in table" insertion mode...
3574                }                }
3575    
3576                ## As if <{current node}>                if ($token->{tag_name} eq 'col') {
3577                ## have an element in table scope                  ## Clear back to table context
3578                ## true by definition                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
3579                           $self->{open_elements}->[-1]->[1] ne 'html') {
3580                ## Clear back to table body context                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3581                ## nop by definition                    pop @{$self->{open_elements}};
3582                    }
3583                pop @{$self->{open_elements}};                  
3584                $self->{insertion_mode} = 'in table';                  !!!insert-element ('colgroup');
3585                ## reprocess                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
3586                redo B;                  ## reprocess
3587                    redo B;
3588                  } elsif ({
3589                            caption => 1,
3590                            colgroup => 1,
3591                            tbody => 1, tfoot => 1, thead => 1,
3592                           }->{$token->{tag_name}}) {
3593                    ## Clear back to table context
3594                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3595                           $self->{open_elements}->[-1]->[1] ne 'html') {
3596                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3597                      pop @{$self->{open_elements}};
3598                    }
3599                    
3600                    push @$active_formatting_elements, ['#marker', '']
3601                        if $token->{tag_name} eq 'caption';
3602                    
3603                    !!!insert-element ($token->{tag_name}, $token->{attributes});
3604                    $self->{insertion_mode} = {
3605                                               caption => IN_CAPTION_IM,
3606                                               colgroup => IN_COLUMN_GROUP_IM,
3607                                               tbody => IN_TABLE_BODY_IM,
3608                                               tfoot => IN_TABLE_BODY_IM,
3609                                               thead => IN_TABLE_BODY_IM,
3610                                              }->{$token->{tag_name}};
3611                    !!!next-token;
3612                    redo B;
3613                  } else {
3614                    die "$0: in table: <>: $token->{tag_name}";
3615                  }
3616              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
3617                ## 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');  
3618    
3619                ## As if </table>                ## As if </table>
3620                ## have a table element in table scope                ## have a table element in table scope
# Line 3934  sub _tree_construction_main ($) { Line 3641  sub _tree_construction_main ($) {
3641                if ({                if ({
3642                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3643                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3644                       tbody => 1, tfoot=> 1, thead => 1,
3645                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3646                  !!!back-token; # <table>                  !!!back-token; # <table>
3647                  $token = {type => 'end tag', tag_name => 'table'};                  $token = {type => END_TAG_TOKEN, tag_name => 'table'};
3648                  !!!back-token;                  !!!back-token;
3649                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3650                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3651                  redo B;                  redo B;
3652                }                }
# Line 3949  sub _tree_construction_main ($) { Line 3657  sub _tree_construction_main ($) {
3657    
3658                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
3659    
3660                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
3661    
3662                ## reprocess                ## reprocess
3663                redo B;                redo B;
3664              } else {          } else {
3665                #            !!!parse-error (type => 'in table:'.$token->{tag_name});
3666              }  
3667            } elsif ($token->{type} eq 'end tag') {            $insert = $insert_to_foster;
3668              if ({            #
3669                   tbody => 1, tfoot => 1, thead => 1,          }
3670                  }->{$token->{tag_name}}) {        } elsif ($token->{type} == END_TAG_TOKEN) {
3671                if ($token->{tag_name} eq 'tr' and
3672                    $self->{insertion_mode} == IN_ROW_IM) {
3673                ## have an element in table scope                ## have an element in table scope
3674                my $i;                my $i;
3675                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3980  sub _tree_construction_main ($) { Line 3690  sub _tree_construction_main ($) {
3690                  redo B;                  redo B;
3691                }                }
3692    
3693                ## Clear back to table body context                ## Clear back to table row context
3694                while (not {                while (not {
3695                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tr => 1, html => 1,
3696                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
3697                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3698                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3699                }                }
3700    
3701                pop @{$self->{open_elements}};                pop @{$self->{open_elements}}; # tr
3702                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
3703                !!!next-token;                !!!next-token;
3704                redo B;                redo B;
3705              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
3706                ## have an element in table scope                if ($self->{insertion_mode} == IN_ROW_IM) {
3707                my $i;                  ## As if </tr>
3708                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
3709                  my $node = $self->{open_elements}->[$_];                  my $i;
3710                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3711                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
3712                      }->{$node->[1]}) {                    if ($node->[1] eq 'tr') {
3713                    $i = $_;                      $i = $_;
3714                    last INSCOPE;                      last INSCOPE;
3715                  } elsif ({                    } elsif ({
3716                            table => 1, html => 1,                              table => 1, html => 1,
3717                           }->{$node->[1]}) {                             }->{$node->[1]}) {
3718                    last INSCOPE;                      last INSCOPE;
3719                      }
3720                    } # INSCOPE
3721                    unless (defined $i) {
3722                      !!!parse-error (type => 'unmatched end tag:'.$token->{type});
3723                      ## Ignore the token
3724                      !!!next-token;
3725                      redo B;
3726                  }                  }
3727                } # INSCOPE                  
3728                unless (defined $i) {                  ## Clear back to table row context
3729                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  while (not {
3730                  ## 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,  
3731                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
3732                # MUST                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3733                my $foster_parent_element;                    pop @{$self->{open_elements}};
3734                my $next_sibling;                  }
3735                my $prev_sibling;                  
3736                OE: for (reverse 0..$#{$self->{open_elements}}) {                  pop @{$self->{open_elements}}; # tr
3737                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3738                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                  ## reprocess in the "in table body" insertion mode...
3739                    if (defined $parent and $parent->node_type == 1) {                }
3740                      $foster_parent_element = $parent;  
3741                      $next_sibling = $self->{open_elements}->[$_]->[0];                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3742                      $prev_sibling = $next_sibling->previous_sibling;                  ## have an element in table scope
3743                    } else {                  my $i;
3744                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3745                      $prev_sibling = $foster_parent_element->last_child;                    my $node = $self->{open_elements}->[$_];
3746                      if ({
3747                           tbody => 1, thead => 1, tfoot => 1,
3748                          }->{$node->[1]}) {
3749                        $i = $_;
3750                        last INSCOPE;
3751                      } elsif ({
3752                                table => 1, html => 1,
3753                               }->{$node->[1]}) {
3754                        last INSCOPE;
3755                    }                    }
3756                    last OE;                  } # INSCOPE
3757                    unless (defined $i) {
3758                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3759                      ## Ignore the token
3760                      !!!next-token;
3761                      redo B;
3762                  }                  }
3763                } # OE                  
3764                $foster_parent_element = $self->{open_elements}->[0]->[0] and                  ## Clear back to table body context
3765                $prev_sibling = $foster_parent_element->last_child                  while (not {
3766                  unless defined $foster_parent_element;                    tbody => 1, tfoot => 1, thead => 1, html => 1,
3767                if (defined $prev_sibling and                  }->{$self->{open_elements}->[-1]->[1]}) {
3768                    $prev_sibling->node_type == 3) {                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3769                  $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;  
3770                  }                  }
3771                } # INSCOPE                  
3772                unless (defined $i) {                  ## As if <{current node}>
3773                  !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                  ## have an element in table scope
3774                  ## Ignore the token                  ## true by definition
3775                  !!!next-token;                  
3776                  redo B;                  ## Clear back to table body context
3777                }                  ## nop by definition
3778                    
               ## 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]);  
3779                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3780                    $self->{insertion_mode} = IN_TABLE_IM;
3781                    ## reprocess in the "in table" insertion mode...
3782                }                }
3783    
               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>  
3784                ## have a table element in table scope                ## have a table element in table scope
3785                my $i;                my $i;
3786                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3787                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3788                  if ($node->[1] eq 'table') {                  if ($node->[1] eq $token->{tag_name}) {
3789                    $i = $_;                    $i = $_;
3790                    last INSCOPE;                    last INSCOPE;
3791                  } elsif ({                  } elsif ({
# Line 4192  sub _tree_construction_main ($) { Line 3795  sub _tree_construction_main ($) {
3795                  }                  }
3796                } # INSCOPE                } # INSCOPE
3797                unless (defined $i) {                unless (defined $i) {
3798                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3799                  ## Ignore tokens </table><table>                  ## Ignore the token
3800                  !!!next-token;                  !!!next-token;
3801                  redo B;                  redo B;
3802                }                }
3803                  
3804                ## generate implied end tags                ## generate implied end tags
3805                if ({                if ({
3806                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3807                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3808                       tbody => 1, tfoot=> 1, thead => 1,
3809                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
3810                  !!!back-token;                  !!!back-token;
3811                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3812                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3813                  redo B;                  redo B;
3814                }                }
3815                  
3816                if ($self->{open_elements}->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
3817                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3818                }                }
3819                    
3820                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
3821                  
3822                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
3823                  
               ## 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';  
3824                !!!next-token;                !!!next-token;
3825                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;  
3826              } elsif ({              } elsif ({
3827                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
3828                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}} and
3829                ## have an element in table scope                       $self->{insertion_mode} & ROW_IMS) {
3830                my $i;                if ($self->{insertion_mode} == IN_ROW_IM) {
3831                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
3832                  my $node = $self->{open_elements}->[$_];                  my $i;
3833                  if ($node->[1] eq $token->{tag_name}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3834                    $i = $_;                    my $node = $self->{open_elements}->[$_];
3835                    last INSCOPE;                    if ($node->[1] eq $token->{tag_name}) {
3836                  } elsif ({                      $i = $_;
3837                            table => 1, html => 1,                      last INSCOPE;
3838                           }->{$node->[1]}) {                    } elsif ({
3839                    last INSCOPE;                              table => 1, html => 1,
3840                               }->{$node->[1]}) {
3841                        last INSCOPE;
3842                      }
3843                    } # INSCOPE
3844                      unless (defined $i) {
3845                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3846                        ## Ignore the token
3847                        !!!next-token;
3848                        redo B;
3849                      }
3850                    
3851                    ## As if </tr>
3852                    ## have an element in table scope
3853                    my $i;
3854                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3855                      my $node = $self->{open_elements}->[$_];
3856                      if ($node->[1] eq 'tr') {
3857                        $i = $_;
3858                        last INSCOPE;
3859                      } elsif ({
3860                                table => 1, html => 1,
3861                               }->{$node->[1]}) {
3862                        last INSCOPE;
3863                      }
3864                    } # INSCOPE
3865                      unless (defined $i) {
3866                        !!!parse-error (type => 'unmatched end tag:tr');
3867                        ## Ignore the token
3868                        !!!next-token;
3869                        redo B;
3870                      }
3871                    
3872                    ## Clear back to table row context
3873                    while (not {
3874                      tr => 1, html => 1,
3875                    }->{$self->{open_elements}->[-1]->[1]}) {
3876                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3877                      pop @{$self->{open_elements}};
3878                  }                  }
3879                } # INSCOPE                  
3880                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
3881                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3882                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
3883                }                }
3884    
               ## As if </tr>  
3885                ## have an element in table scope                ## have an element in table scope
3886                my $i;                my $i;
3887                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3888                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3889                  if ($node->[1] eq 'tr') {                  if ($node->[1] eq $token->{tag_name}) {
3890                    $i = $_;                    $i = $_;
3891                    last INSCOPE;                    last INSCOPE;
3892                  } elsif ({                  } elsif ({
# Line 4330  sub _tree_construction_main ($) { Line 3896  sub _tree_construction_main ($) {
3896                  }                  }
3897                } # INSCOPE                } # INSCOPE
3898                unless (defined $i) {                unless (defined $i) {
3899                  !!!parse-error (type => 'unmatched end tag:tr');                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3900                  ## Ignore the token                  ## Ignore the token
3901                  !!!next-token;                  !!!next-token;
3902                  redo B;                  redo B;
3903                }                }
3904    
3905                ## Clear back to table row context                ## Clear back to table body context
3906                while (not {                while (not {
3907                  tr => 1, html => 1,                  tbody => 1, tfoot => 1, thead => 1, html => 1,
3908                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
3909                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3910                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3911                }                }
3912    
3913                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}};
3914                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_IM;
3915                ## reprocess                !!!next-token;
3916                redo B;                redo B;
3917              } elsif ({              } elsif ({
3918                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
3919                        colgroup => 1, html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
3920                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3921                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
3922                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3923                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3924                ## Ignore the token                ## Ignore the token
3925                !!!next-token;                !!!next-token;
3926                redo B;                redo B;
3927              } else {          } else {
3928                #            !!!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});  
3929    
3930              !!!next-token;            $insert = $insert_to_foster;
3931              redo B;            #
3932            } elsif ($token->{type} eq 'comment') {          }
3933              ## NOTE: This is a code clone of "comment in body".        } else {
3934              my $comment = $self->{document}->create_comment ($token->{data});          die "$0: $token->{type}: Unknown token type";
3935              $self->{open_elements}->[-1]->[0]->append_child ($comment);        }
3936              !!!next-token;      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
3937              redo B;            if ($token->{type} == CHARACTER_TOKEN) {
3938            } elsif ($token->{type} eq 'start tag') {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3939              if ({                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3940                   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  
3941                  !!!next-token;                  !!!next-token;
3942                  redo B;                  redo B;
3943                }                }
3944                }
3945                ## Close the cell              
3946                !!!back-token; # <?>              #
3947                $token = {type => 'end tag', tag_name => $tn};            } elsif ($token->{type} == START_TAG_TOKEN) {
3948                if ($token->{tag_name} eq 'col') {
3949                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3950                  pop @{$self->{open_elements}};
3951                  !!!next-token;
3952                redo B;                redo B;
3953              } else {              } else {
3954                #                #
3955              }              }
3956            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
3957              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'colgroup') {
3958                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] eq 'html') {
3959                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});  
3960                  ## Ignore the token                  ## Ignore the token
3961                  !!!next-token;                  !!!next-token;
3962                  redo B;                  redo B;
3963                  } else {
3964                    pop @{$self->{open_elements}}; # colgroup
3965                    $self->{insertion_mode} = IN_TABLE_IM;
3966                    !!!next-token;
3967                    redo B;            
3968                }                }
3969                              } elsif ($token->{tag_name} eq 'col') {
3970                ## 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});  
3971                ## Ignore the token                ## Ignore the token
3972                !!!next-token;                !!!next-token;
3973                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;  
3974              } else {              } else {
3975                #                #
3976              }              }
3977            } else {            } else {
3978              #              #
3979            }            }
3980              
3981            $in_body->($insert_to_current);            ## As if </colgroup>
3982            redo B;            if ($self->{open_elements}->[-1]->[1] eq 'html') {
3983          } elsif ($self->{insertion_mode} eq 'in select') {              !!!parse-error (type => 'unmatched end tag:colgroup');
3984            if ($token->{type} eq 'character') {              ## Ignore the token
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
3985              !!!next-token;              !!!next-token;
3986              redo B;              redo B;
3987            } elsif ($token->{type} eq 'comment') {            } else {
3988              my $comment = $self->{document}->create_comment ($token->{data});              pop @{$self->{open_elements}}; # colgroup
3989              $self->{open_elements}->[-1]->[0]->append_child ($comment);              $self->{insertion_mode} = IN_TABLE_IM;
3990              !!!next-token;              ## reprocess
3991              redo B;              redo B;
3992            } elsif ($token->{type} eq 'start tag') {            }
3993        } elsif ($self->{insertion_mode} == IN_SELECT_IM) {
3994          if ($token->{type} == CHARACTER_TOKEN) {
3995            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3996            !!!next-token;
3997            redo B;
3998          } elsif ($token->{type} == START_TAG_TOKEN) {
3999              if ($token->{tag_name} eq 'option') {              if ($token->{tag_name} eq 'option') {
4000                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
4001                  ## As if </option>                  ## As if </option>
# Line 4575  sub _tree_construction_main ($) { Line 4048  sub _tree_construction_main ($) {
4048    
4049                !!!next-token;                !!!next-token;
4050                redo B;                redo B;
4051              } else {          } else {
4052                #            !!!parse-error (type => 'in select:'.$token->{tag_name});
4053              }            ## Ignore the token
4054            } elsif ($token->{type} eq 'end tag') {            !!!next-token;
4055              redo B;
4056            }
4057          } elsif ($token->{type} == END_TAG_TOKEN) {
4058              if ($token->{tag_name} eq 'optgroup') {              if ($token->{tag_name} eq 'optgroup') {
4059                if ($self->{open_elements}->[-1]->[1] eq 'option' and                if ($self->{open_elements}->[-1]->[1] eq 'option' and
4060                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {
# Line 4680  sub _tree_construction_main ($) { Line 4156  sub _tree_construction_main ($) {
4156    
4157                ## reprocess                ## reprocess
4158                redo B;                redo B;
4159              } else {          } else {
4160                #            !!!parse-error (type => 'in select:/'.$token->{tag_name});
             }  
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in select:'.$token->{tag_name});  
4161            ## Ignore the token            ## Ignore the token
4162            !!!next-token;            !!!next-token;
4163            redo B;            redo B;
4164          } elsif ($self->{insertion_mode} eq 'after body') {          }
4165            if ($token->{type} eq 'character') {        } else {
4166              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          die "$0: $token->{type}: Unknown token type";
4167                ## As if in body        }
4168                $reconstruct_active_formatting_elements->($insert_to_current);      } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
4169          if ($token->{type} == CHARACTER_TOKEN) {
4170            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4171              my $data = $1;
4172              ## As if in body
4173              $reconstruct_active_formatting_elements->($insert_to_current);
4174                                
4175                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4176              
4177              unless (length $token->{data}) {
4178                !!!next-token;
4179                redo B;
4180              }
4181            }
4182            
4183            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4184              !!!parse-error (type => 'after html:#character');
4185    
4186                unless (length $token->{data}) {            ## Reprocess in the "main" phase, "after body" insertion mode...
4187                  !!!next-token;          }
4188                  redo B;          
4189                }          ## "after body" insertion mode
4190              }          !!!parse-error (type => 'after body:#character');
4191                
4192              #          $self->{insertion_mode} = IN_BODY_IM;
4193              !!!parse-error (type => 'after body:#'.$token->{type});          ## reprocess
4194            } elsif ($token->{type} eq 'comment') {          redo B;
4195              my $comment = $self->{document}->create_comment ($token->{data});        } elsif ($token->{type} == START_TAG_TOKEN) {
4196              $self->{open_elements}->[0]->[0]->append_child ($comment);          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4197              !!!parse-error (type => 'after html:'.$token->{tag_name});
4198              
4199              ## Reprocess in the "main" phase, "after body" insertion mode...
4200            }
4201    
4202            ## "after body" insertion mode
4203            !!!parse-error (type => 'after body:'.$token->{tag_name});
4204    
4205            $self->{insertion_mode} = IN_BODY_IM;
4206            ## reprocess
4207            redo B;
4208          } elsif ($token->{type} == END_TAG_TOKEN) {
4209            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4210              !!!parse-error (type => 'after html:/'.$token->{tag_name});
4211              
4212              $self->{insertion_mode} = AFTER_BODY_IM;
4213              ## Reprocess in the "main" phase, "after body" insertion mode...
4214            }
4215    
4216            ## "after body" insertion mode
4217            if ($token->{tag_name} eq 'html') {
4218              if (defined $self->{inner_html_node}) {
4219                !!!parse-error (type => 'unmatched end tag:html');
4220                ## Ignore the token
4221              !!!next-token;              !!!next-token;
4222              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});  
             }  
4223            } else {            } else {
4224              !!!parse-error (type => 'after body:#'.$token->{type});              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
4225                !!!next-token;
4226                redo B;
4227            }            }
4228            } else {
4229              !!!parse-error (type => 'after body:/'.$token->{tag_name});
4230    
4231            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = IN_BODY_IM;
4232            ## reprocess            ## reprocess
4233            redo B;            redo B;
4234          } elsif ($self->{insertion_mode} eq 'in frameset') {          }
4235            if ($token->{type} eq 'character') {        } else {
4236              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          die "$0: $token->{type}: Unknown token type";
4237                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});        }
4238        } elsif ($self->{insertion_mode} & FRAME_IMS) {
4239                unless (length $token->{data}) {        if ($token->{type} == CHARACTER_TOKEN) {
4240                  !!!next-token;          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4241                  redo B;            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4242                }            
4243              }            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);  
4244              !!!next-token;              !!!next-token;
4245              redo B;              redo B;
4246            } elsif ($token->{type} eq 'start tag') {            }
4247              if ($token->{tag_name} eq 'frameset') {          }
4248                !!!insert-element ($token->{tag_name}, $token->{attributes});          
4249                !!!next-token;          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
4250                redo B;            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4251              } elsif ($token->{tag_name} eq 'frame') {              !!!parse-error (type => 'in frameset:#character');
4252                !!!insert-element ($token->{tag_name}, $token->{attributes});            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
4253                pop @{$self->{open_elements}};              !!!parse-error (type => 'after frameset:#character');
4254                !!!next-token;            } else { # "after html frameset"
4255                redo B;              !!!parse-error (type => 'after html:#character');
4256              } elsif ($token->{tag_name} eq 'noframes') {  
4257                $in_body->($insert_to_current);              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4258                redo B;              ## Reprocess in the "main" phase, "after frameset"...
4259              } 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 {  
             #  
4260            }            }
4261                        
4262            if (defined $token->{tag_name}) {            ## Ignore the token.
4263              if (length $token->{data}) {
4264                ## reprocess the rest of characters
4265              } else {
4266                !!!next-token;
4267              }
4268              redo B;
4269            }
4270            
4271            die qq[$0: Character "$token->{data}"];
4272          } elsif ($token->{type} == START_TAG_TOKEN) {
4273            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4274              !!!parse-error (type => 'after html:'.$token->{tag_name});
4275    
4276              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4277              ## Process in the "main" phase, "after frameset" insertion mode...
4278            }
4279    
4280            if ($token->{tag_name} eq 'frameset' and
4281                $self->{insertion_mode} == IN_FRAMESET_IM) {
4282              !!!insert-element ($token->{tag_name}, $token->{attributes});
4283              !!!next-token;
4284              redo B;
4285            } elsif ($token->{tag_name} eq 'frame' and
4286                     $self->{insertion_mode} == IN_FRAMESET_IM) {
4287              !!!insert-element ($token->{tag_name}, $token->{attributes});
4288              pop @{$self->{open_elements}};
4289              !!!next-token;
4290              redo B;
4291            } elsif ($token->{tag_name} eq 'noframes') {
4292              ## NOTE: As if in body.
4293              $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
4294              redo B;
4295            } else {
4296              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4297              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset:'.$token->{tag_name});
4298            } else {            } else {
4299              !!!parse-error (type => 'in frameset:#'.$token->{type});              !!!parse-error (type => 'after frameset:'.$token->{tag_name});
4300            }            }
4301            ## Ignore the token            ## Ignore the token
4302            !!!next-token;            !!!next-token;
4303            redo B;            redo B;
4304          } elsif ($self->{insertion_mode} eq 'after frameset') {          }
4305            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_TAG_TOKEN) {
4306              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4307                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            !!!parse-error (type => 'after html:/'.$token->{tag_name});
4308    
4309                unless (length $token->{data}) {            $self->{insertion_mode} = AFTER_FRAMESET_IM;
4310                  !!!next-token;            ## Process in the "main" phase, "after frameset" insertion mode...
4311                  redo B;          }
               }  
             }  
4312    
4313              #          if ($token->{tag_name} eq 'frameset' and
4314            } elsif ($token->{type} eq 'comment') {              $self->{insertion_mode} == IN_FRAMESET_IM) {
4315              my $comment = $self->{document}->create_comment ($token->{data});            if ($self->{open_elements}->[-1]->[1] eq 'html' and
4316              $self->{open_elements}->[-1]->[0]->append_child ($comment);                @{$self->{open_elements}} == 1) {
4317                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4318                ## Ignore the token
4319              !!!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 {  
               #  
             }  
4320            } else {            } else {
4321              #              pop @{$self->{open_elements}};
4322                !!!next-token;
4323            }            }
4324              
4325            if (defined $token->{tag_name}) {            if (not defined $self->{inner_html_node} and
4326              !!!parse-error (type => 'after frameset:'.$token->{tag_name});                $self->{open_elements}->[-1]->[1] ne 'frameset') {
4327                $self->{insertion_mode} = AFTER_FRAMESET_IM;
4328              }
4329              redo B;
4330            } elsif ($token->{tag_name} eq 'html' and
4331                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
4332              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
4333              !!!next-token;
4334              redo B;
4335            } else {
4336              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4337                !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
4338            } else {            } else {
4339              !!!parse-error (type => 'after frameset:#'.$token->{type});              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
4340            }            }
4341            ## Ignore the token            ## Ignore the token
4342            !!!next-token;            !!!next-token;
4343            redo B;            redo B;
4344            }
4345          } else {
4346            die "$0: $token->{type}: Unknown token type";
4347          }
4348    
4349          ## ISSUE: An issue in spec here
4350        } else {
4351          die "$0: $self->{insertion_mode}: Unknown insertion mode";
4352        }
4353    
4354        ## "in body" insertion mode
4355        if ($token->{type} == START_TAG_TOKEN) {
4356          if ($token->{tag_name} eq 'script') {
4357            ## NOTE: This is an "as if in head" code clone
4358            $script_start_tag->($insert);
4359            redo B;
4360          } elsif ($token->{tag_name} eq 'style') {
4361            ## NOTE: This is an "as if in head" code clone
4362            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4363            redo B;
4364          } elsif ({
4365                    base => 1, link => 1,
4366                   }->{$token->{tag_name}}) {
4367            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4368            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4369            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4370            !!!next-token;
4371            redo B;
4372          } elsif ($token->{tag_name} eq 'meta') {
4373            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4374            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4375            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4376    
4377            ## ISSUE: An issue in spec there          unless ($self->{confident}) {
4378              my $charset;
4379              if ($token->{attributes}->{charset}) { ## TODO: And if supported
4380                $charset = $token->{attributes}->{charset}->{value};
4381              }
4382              if ($token->{attributes}->{'http-equiv'}) {
4383                ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
4384                if ($token->{attributes}->{'http-equiv'}->{value}
4385                    =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
4386                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4387                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4388                  $charset = defined $1 ? $1 : defined $2 ? $2 : $3;
4389                } ## TODO: And if supported
4390              }
4391              ## TODO: Change the encoding
4392            }
4393    
4394            !!!next-token;
4395            redo B;
4396          } elsif ($token->{tag_name} eq 'title') {
4397            !!!parse-error (type => 'in body:title');
4398            ## NOTE: This is an "as if in head" code clone
4399            $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {
4400              if (defined $self->{head_element}) {
4401                $self->{head_element}->append_child ($_[0]);
4402              } else {
4403                $insert->($_[0]);
4404              }
4405            });
4406            redo B;
4407          } elsif ($token->{tag_name} eq 'body') {
4408            !!!parse-error (type => 'in body:body');
4409                  
4410            if (@{$self->{open_elements}} == 1 or
4411                $self->{open_elements}->[1]->[1] ne 'body') {
4412              ## Ignore the token
4413          } else {          } else {
4414            die "$0: $self->{insertion_mode}: Unknown insertion mode";            my $body_el = $self->{open_elements}->[1]->[0];
4415              for my $attr_name (keys %{$token->{attributes}}) {
4416                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
4417                  $body_el->set_attribute_ns
4418                    (undef, [undef, $attr_name],
4419                     $token->{attributes}->{$attr_name}->{value});
4420                }
4421              }
4422          }          }
       }  
     } 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  
4423          !!!next-token;          !!!next-token;
4424          redo B;          redo B;
4425        } elsif ($token->{type} eq 'comment') {        } elsif ({
4426          my $comment = $self->{document}->create_comment ($token->{data});                  address => 1, blockquote => 1, center => 1, dir => 1,
4427          $self->{document}->append_child ($comment);                  div => 1, dl => 1, fieldset => 1, listing => 1,
4428                    menu => 1, ol => 1, p => 1, ul => 1,
4429                    pre => 1,
4430                   }->{$token->{tag_name}}) {
4431            ## has a p element in scope
4432            INSCOPE: for (reverse @{$self->{open_elements}}) {
4433              if ($_->[1] eq 'p') {
4434                !!!back-token;
4435                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4436                redo B;
4437              } elsif ({
4438                        table => 1, caption => 1, td => 1, th => 1,
4439                        button => 1, marquee => 1, object => 1, html => 1,
4440                       }->{$_->[1]}) {
4441                last INSCOPE;
4442              }
4443            } # INSCOPE
4444              
4445            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4446            if ($token->{tag_name} eq 'pre') {
4447              !!!next-token;
4448              if ($token->{type} == CHARACTER_TOKEN) {
4449                $token->{data} =~ s/^\x0A//;
4450                unless (length $token->{data}) {
4451                  !!!next-token;
4452                }
4453              }
4454            } else {
4455              !!!next-token;
4456            }
4457            redo B;
4458          } elsif ($token->{tag_name} eq 'form') {
4459            if (defined $self->{form_element}) {
4460              !!!parse-error (type => 'in form:form');
4461              ## Ignore the token
4462              !!!next-token;
4463              redo B;
4464            } else {
4465              ## has a p element in scope
4466              INSCOPE: for (reverse @{$self->{open_elements}}) {
4467                if ($_->[1] eq 'p') {
4468                  !!!back-token;
4469                  $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4470                  redo B;
4471                } elsif ({
4472                          table => 1, caption => 1, td => 1, th => 1,
4473                          button => 1, marquee => 1, object => 1, html => 1,
4474                         }->{$_->[1]}) {
4475                  last INSCOPE;
4476                }
4477              } # INSCOPE
4478                
4479              !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4480              $self->{form_element} = $self->{open_elements}->[-1]->[0];
4481              !!!next-token;
4482              redo B;
4483            }
4484          } elsif ($token->{tag_name} eq 'li') {
4485            ## has a p element in scope
4486            INSCOPE: for (reverse @{$self->{open_elements}}) {
4487              if ($_->[1] eq 'p') {
4488                !!!back-token;
4489                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4490                redo B;
4491              } elsif ({
4492                        table => 1, caption => 1, td => 1, th => 1,
4493                        button => 1, marquee => 1, object => 1, html => 1,
4494                       }->{$_->[1]}) {
4495                last INSCOPE;
4496              }
4497            } # INSCOPE
4498              
4499            ## Step 1
4500            my $i = -1;
4501            my $node = $self->{open_elements}->[$i];
4502            LI: {
4503              ## Step 2
4504              if ($node->[1] eq 'li') {
4505                if ($i != -1) {
4506                  !!!parse-error (type => 'end tag missing:'.
4507                                  $self->{open_elements}->[-1]->[1]);
4508                }
4509                splice @{$self->{open_elements}}, $i;
4510                last LI;
4511              }
4512              
4513              ## Step 3
4514              if (not $formatting_category->{$node->[1]} and
4515                  #not $phrasing_category->{$node->[1]} and
4516                  ($special_category->{$node->[1]} or
4517                   $scoping_category->{$node->[1]}) and
4518                  $node->[1] ne 'address' and $node->[1] ne 'div') {
4519                last LI;
4520              }
4521              
4522              ## Step 4
4523              $i--;
4524              $node = $self->{open_elements}->[$i];
4525              redo LI;
4526            } # LI
4527              
4528            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4529          !!!next-token;          !!!next-token;
4530          redo B;          redo B;
4531        } elsif ($token->{type} eq 'character') {        } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {
4532          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          ## has a p element in scope
4533            my $data = $1;          INSCOPE: for (reverse @{$self->{open_elements}}) {
4534            ## As if in the main phase.            if ($_->[1] eq 'p') {
4535            ## NOTE: The insertion mode in the main phase              !!!back-token;
4536            ## just before the phase has been changed to the trailing              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4537            ## end phase is either "after body" or "after frameset".              redo B;
4538            $reconstruct_active_formatting_elements->($insert_to_current)            } elsif ({
4539              if $phase eq 'main';                      table => 1, caption => 1, td => 1, th => 1,
4540                        button => 1, marquee => 1, object => 1, html => 1,
4541                       }->{$_->[1]}) {
4542                last INSCOPE;
4543              }
4544            } # INSCOPE
4545              
4546            ## Step 1
4547            my $i = -1;
4548            my $node = $self->{open_elements}->[$i];
4549            LI: {
4550              ## Step 2
4551              if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {
4552                if ($i != -1) {
4553                  !!!parse-error (type => 'end tag missing:'.
4554                                  $self->{open_elements}->[-1]->[1]);
4555                }
4556                splice @{$self->{open_elements}}, $i;
4557                last LI;
4558              }
4559              
4560              ## Step 3
4561              if (not $formatting_category->{$node->[1]} and
4562                  #not $phrasing_category->{$node->[1]} and
4563                  ($special_category->{$node->[1]} or
4564                   $scoping_category->{$node->[1]}) and
4565                  $node->[1] ne 'address' and $node->[1] ne 'div') {
4566                last LI;
4567              }
4568              
4569              ## Step 4
4570              $i--;
4571              $node = $self->{open_elements}->[$i];
4572              redo LI;
4573            } # LI
4574              
4575            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4576            !!!next-token;
4577            redo B;
4578          } elsif ($token->{tag_name} eq 'plaintext') {
4579            ## has a p element in scope
4580            INSCOPE: for (reverse @{$self->{open_elements}}) {
4581              if ($_->[1] eq 'p') {
4582                !!!back-token;
4583                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4584                redo B;
4585              } elsif ({
4586                        table => 1, caption => 1, td => 1, th => 1,
4587                        button => 1, marquee => 1, object => 1, html => 1,
4588                       }->{$_->[1]}) {
4589                last INSCOPE;
4590              }
4591            } # INSCOPE
4592              
4593            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4594              
4595            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
4596              
4597            !!!next-token;
4598            redo B;
4599          } elsif ({
4600                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4601                   }->{$token->{tag_name}}) {
4602            ## has a p element in scope
4603            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4604              my $node = $self->{open_elements}->[$_];
4605              if ($node->[1] eq 'p') {
4606                !!!back-token;
4607                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4608                redo B;
4609              } elsif ({
4610                        table => 1, caption => 1, td => 1, th => 1,
4611                        button => 1, marquee => 1, object => 1, html => 1,
4612                       }->{$node->[1]}) {
4613                last INSCOPE;
4614              }
4615            } # INSCOPE
4616              
4617            ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>
4618            ## has an element in scope
4619            #my $i;
4620            #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4621            #  my $node = $self->{open_elements}->[$_];
4622            #  if ({
4623            #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4624            #      }->{$node->[1]}) {
4625            #    $i = $_;
4626            #    last INSCOPE;
4627            #  } elsif ({
4628            #            table => 1, caption => 1, td => 1, th => 1,
4629            #            button => 1, marquee => 1, object => 1, html => 1,
4630            #           }->{$node->[1]}) {
4631            #    last INSCOPE;
4632            #  }
4633            #} # INSCOPE
4634            #  
4635            #if (defined $i) {
4636            #  !!! parse-error (type => 'in hn:hn');
4637            #  splice @{$self->{open_elements}}, $i;
4638            #}
4639              
4640            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4641              
4642            !!!next-token;
4643            redo B;
4644          } elsif ($token->{tag_name} eq 'a') {
4645            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
4646              my $node = $active_formatting_elements->[$i];
4647              if ($node->[1] eq 'a') {
4648                !!!parse-error (type => 'in a:a');
4649                
4650                !!!back-token;
4651                $token = {type => END_TAG_TOKEN, tag_name => 'a'};
4652                $formatting_end_tag->($token->{tag_name});
4653                
4654                AFE2: for (reverse 0..$#$active_formatting_elements) {
4655                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
4656                    splice @$active_formatting_elements, $_, 1;
4657                    last AFE2;
4658                  }
4659                } # AFE2
4660                OE: for (reverse 0..$#{$self->{open_elements}}) {
4661                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
4662                    splice @{$self->{open_elements}}, $_, 1;
4663                    last OE;
4664                  }
4665                } # OE
4666                last AFE;
4667              } elsif ($node->[0] eq '#marker') {
4668                last AFE;
4669              }
4670            } # AFE
4671              
4672            $reconstruct_active_formatting_elements->($insert_to_current);
4673    
4674            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4675            push @$active_formatting_elements, $self->{open_elements}->[-1];
4676    
4677            !!!next-token;
4678            redo B;
4679          } elsif ({
4680                    b => 1, big => 1, em => 1, font => 1, i => 1,
4681                    s => 1, small => 1, strile => 1,
4682                    strong => 1, tt => 1, u => 1,
4683                   }->{$token->{tag_name}}) {
4684            $reconstruct_active_formatting_elements->($insert_to_current);
4685            
4686            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4687            push @$active_formatting_elements, $self->{open_elements}->[-1];
4688            
4689            !!!next-token;
4690            redo B;
4691          } elsif ($token->{tag_name} eq 'nobr') {
4692            $reconstruct_active_formatting_elements->($insert_to_current);
4693    
4694            ## has a |nobr| element in scope
4695            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4696              my $node = $self->{open_elements}->[$_];
4697              if ($node->[1] eq 'nobr') {
4698                !!!parse-error (type => 'in nobr:nobr');
4699                !!!back-token;
4700                $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};
4701                redo B;
4702              } elsif ({
4703                        table => 1, caption => 1, td => 1, th => 1,
4704                        button => 1, marquee => 1, object => 1, html => 1,
4705                       }->{$node->[1]}) {
4706                last INSCOPE;
4707              }
4708            } # INSCOPE
4709            
4710            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4711            push @$active_formatting_elements, $self->{open_elements}->[-1];
4712            
4713            !!!next-token;
4714            redo B;
4715          } elsif ($token->{tag_name} eq 'button') {
4716            ## has a button element in scope
4717            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4718              my $node = $self->{open_elements}->[$_];
4719              if ($node->[1] eq 'button') {
4720                !!!parse-error (type => 'in button:button');
4721                !!!back-token;
4722                $token = {type => END_TAG_TOKEN, tag_name => 'button'};
4723                redo B;
4724              } elsif ({
4725                        table => 1, caption => 1, td => 1, th => 1,
4726                        button => 1, marquee => 1, object => 1, html => 1,
4727                       }->{$node->[1]}) {
4728                last INSCOPE;
4729              }
4730            } # INSCOPE
4731              
4732            $reconstruct_active_formatting_elements->($insert_to_current);
4733              
4734            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4735            push @$active_formatting_elements, ['#marker', ''];
4736    
4737            !!!next-token;
4738            redo B;
4739          } elsif ($token->{tag_name} eq 'marquee' or
4740                   $token->{tag_name} eq 'object') {
4741            $reconstruct_active_formatting_elements->($insert_to_current);
4742            
4743            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4744            push @$active_formatting_elements, ['#marker', ''];
4745            
4746            !!!next-token;
4747            redo B;
4748          } elsif ($token->{tag_name} eq 'xmp') {
4749            $reconstruct_active_formatting_elements->($insert_to_current);
4750            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4751            redo B;
4752          } elsif ($token->{tag_name} eq 'table') {
4753            ## has a p element in scope
4754            INSCOPE: for (reverse @{$self->{open_elements}}) {
4755              if ($_->[1] eq 'p') {
4756                !!!back-token;
4757                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4758                redo B;
4759              } elsif ({
4760                        table => 1, caption => 1, td => 1, th => 1,
4761                        button => 1, marquee => 1, object => 1, html => 1,
4762                       }->{$_->[1]}) {
4763                last INSCOPE;
4764              }
4765            } # INSCOPE
4766              
4767            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4768                        
4769            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);          $self->{insertion_mode} = IN_TABLE_IM;
4770              
4771            !!!next-token;
4772            redo B;
4773          } elsif ({
4774                    area => 1, basefont => 1, bgsound => 1, br => 1,
4775                    embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
4776                    image => 1,
4777                   }->{$token->{tag_name}}) {
4778            if ($token->{tag_name} eq 'image') {
4779              !!!parse-error (type => 'image');
4780              $token->{tag_name} = 'img';
4781            }
4782    
4783            ## NOTE: There is an "as if <br>" code clone.
4784            $reconstruct_active_formatting_elements->($insert_to_current);
4785            
4786            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4787            pop @{$self->{open_elements}};
4788            
4789            !!!next-token;
4790            redo B;
4791          } elsif ($token->{tag_name} eq 'hr') {
4792            ## has a p element in scope
4793            INSCOPE: for (reverse @{$self->{open_elements}}) {
4794              if ($_->[1] eq 'p') {
4795                !!!back-token;
4796                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4797                redo B;
4798              } elsif ({
4799                        table => 1, caption => 1, td => 1, th => 1,
4800                        button => 1, marquee => 1, object => 1, html => 1,
4801                       }->{$_->[1]}) {
4802                last INSCOPE;
4803              }
4804            } # INSCOPE
4805              
4806            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4807            pop @{$self->{open_elements}};
4808                        
4809            !!!next-token;
4810            redo B;
4811          } elsif ($token->{tag_name} eq 'input') {
4812            $reconstruct_active_formatting_elements->($insert_to_current);
4813            
4814            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4815            ## TODO: associate with $self->{form_element} if defined
4816            pop @{$self->{open_elements}};
4817            
4818            !!!next-token;
4819            redo B;
4820          } elsif ($token->{tag_name} eq 'isindex') {
4821            !!!parse-error (type => 'isindex');
4822            
4823            if (defined $self->{form_element}) {
4824              ## Ignore the token
4825              !!!next-token;
4826              redo B;
4827            } else {
4828              my $at = $token->{attributes};
4829              my $form_attrs;
4830              $form_attrs->{action} = $at->{action} if $at->{action};
4831              my $prompt_attr = $at->{prompt};
4832              $at->{name} = {name => 'name', value => 'isindex'};
4833              delete $at->{action};
4834              delete $at->{prompt};
4835              my @tokens = (
4836                            {type => START_TAG_TOKEN, tag_name => 'form',
4837                             attributes => $form_attrs},
4838                            {type => START_TAG_TOKEN, tag_name => 'hr'},
4839                            {type => START_TAG_TOKEN, tag_name => 'p'},
4840                            {type => START_TAG_TOKEN, tag_name => 'label'},
4841                           );
4842              if ($prompt_attr) {
4843                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};
4844              } else {
4845                push @tokens, {type => CHARACTER_TOKEN,
4846                               data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
4847                ## TODO: make this configurable
4848              }
4849              push @tokens,
4850                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},
4851                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
4852                            {type => END_TAG_TOKEN, tag_name => 'label'},
4853                            {type => END_TAG_TOKEN, tag_name => 'p'},
4854                            {type => START_TAG_TOKEN, tag_name => 'hr'},
4855                            {type => END_TAG_TOKEN, tag_name => 'form'};
4856              $token = shift @tokens;
4857              !!!back-token (@tokens);
4858              redo B;
4859            }
4860          } elsif ($token->{tag_name} eq 'textarea') {
4861            my $tag_name = $token->{tag_name};
4862            my $el;
4863            !!!create-element ($el, $token->{tag_name}, $token->{attributes});
4864            
4865            ## TODO: $self->{form_element} if defined
4866            $self->{content_model} = RCDATA_CONTENT_MODEL;
4867            delete $self->{escape}; # MUST
4868            
4869            $insert->($el);
4870            
4871            my $text = '';
4872            !!!next-token;
4873            if ($token->{type} == CHARACTER_TOKEN) {
4874              $token->{data} =~ s/^\x0A//;
4875            unless (length $token->{data}) {            unless (length $token->{data}) {
4876              !!!next-token;              !!!next-token;
             redo B;  
4877            }            }
4878          }          }
4879            while ($token->{type} == CHARACTER_TOKEN) {
4880              $text .= $token->{data};
4881              !!!next-token;
4882            }
4883            if (length $text) {
4884              $el->manakai_append_text ($text);
4885            }
4886            
4887            $self->{content_model} = PCDATA_CONTENT_MODEL;
4888            
4889            if ($token->{type} == END_TAG_TOKEN and
4890                $token->{tag_name} eq $tag_name) {
4891              ## Ignore the token
4892            } else {
4893              !!!parse-error (type => 'in RCDATA:#'.$token->{type});
4894            }
4895            !!!next-token;
4896            redo B;
4897          } elsif ({
4898                    iframe => 1,
4899                    noembed => 1,
4900                    noframes => 1,
4901                    noscript => 0, ## TODO: 1 if scripting is enabled
4902                   }->{$token->{tag_name}}) {
4903            ## NOTE: There is an "as if in body" code clone.
4904            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4905            redo B;
4906          } elsif ($token->{tag_name} eq 'select') {
4907            $reconstruct_active_formatting_elements->($insert_to_current);
4908            
4909            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4910            
4911            $self->{insertion_mode} = IN_SELECT_IM;
4912            !!!next-token;
4913            redo B;
4914          } elsif ({
4915                    caption => 1, col => 1, colgroup => 1, frame => 1,
4916                    frameset => 1, head => 1, option => 1, optgroup => 1,
4917                    tbody => 1, td => 1, tfoot => 1, th => 1,
4918                    thead => 1, tr => 1,
4919                   }->{$token->{tag_name}}) {
4920            !!!parse-error (type => 'in body:'.$token->{tag_name});
4921            ## Ignore the token
4922            !!!next-token;
4923            redo B;
4924            
4925            ## ISSUE: An issue on HTML5 new elements in the spec.
4926          } else {
4927            $reconstruct_active_formatting_elements->($insert_to_current);
4928            
4929            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4930            
4931            !!!next-token;
4932            redo B;
4933          }
4934        } elsif ($token->{type} == END_TAG_TOKEN) {
4935          if ($token->{tag_name} eq 'body') {
4936            if (@{$self->{open_elements}} > 1 and
4937                $self->{open_elements}->[1]->[1] eq 'body') {
4938              for (@{$self->{open_elements}}) {
4939                unless ({
4940                           dd => 1, dt => 1, li => 1, p => 1, td => 1,
4941                           th => 1, tr => 1, body => 1, html => 1,
4942                         tbody => 1, tfoot => 1, thead => 1,
4943                        }->{$_->[1]}) {
4944                  !!!parse-error (type => 'not closed:'.$_->[1]);
4945                }
4946              }
4947    
4948          !!!parse-error (type => 'after html:#character');            $self->{insertion_mode} = AFTER_BODY_IM;
4949          $phase = 'main';            !!!next-token;
4950          ## reprocess            redo B;
4951            } else {
4952              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4953              ## Ignore the token
4954              !!!next-token;
4955              redo B;
4956            }
4957          } elsif ($token->{tag_name} eq 'html') {
4958            if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {
4959              ## ISSUE: There is an issue in the spec.
4960              if ($self->{open_elements}->[-1]->[1] ne 'body') {
4961                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
4962              }
4963              $self->{insertion_mode} = AFTER_BODY_IM;
4964              ## reprocess
4965              redo B;
4966            } else {
4967              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4968              ## Ignore the token
4969              !!!next-token;
4970              redo B;
4971            }
4972          } elsif ({
4973                    address => 1, blockquote => 1, center => 1, dir => 1,
4974                    div => 1, dl => 1, fieldset => 1, listing => 1,
4975                    menu => 1, ol => 1, pre => 1, ul => 1,
4976                    p => 1,
4977                    dd => 1, dt => 1, li => 1,
4978                    button => 1, marquee => 1, object => 1,
4979                   }->{$token->{tag_name}}) {
4980            ## has an element in scope
4981            my $i;
4982            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4983              my $node = $self->{open_elements}->[$_];
4984              if ($node->[1] eq $token->{tag_name}) {
4985                ## generate implied end tags
4986                if ({
4987                     dd => ($token->{tag_name} ne 'dd'),
4988                     dt => ($token->{tag_name} ne 'dt'),
4989                     li => ($token->{tag_name} ne 'li'),
4990                     p => ($token->{tag_name} ne 'p'),
4991                     td => 1, th => 1, tr => 1,
4992                     tbody => 1, tfoot=> 1, thead => 1,
4993                    }->{$self->{open_elements}->[-1]->[1]}) {
4994                  !!!back-token;
4995                  $token = {type => END_TAG_TOKEN,
4996                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4997                  redo B;
4998                }
4999                $i = $_;
5000                last INSCOPE unless $token->{tag_name} eq 'p';
5001              } elsif ({
5002                        table => 1, caption => 1, td => 1, th => 1,
5003                        button => 1, marquee => 1, object => 1, html => 1,
5004                       }->{$node->[1]}) {
5005                last INSCOPE;
5006              }
5007            } # INSCOPE
5008            
5009            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5010              if (defined $i) {
5011                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5012              } else {
5013                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5014              }
5015            }
5016            
5017            if (defined $i) {
5018              splice @{$self->{open_elements}}, $i;
5019            } elsif ($token->{tag_name} eq 'p') {
5020              ## As if <p>, then reprocess the current token
5021              my $el;
5022              !!!create-element ($el, 'p');
5023              $insert->($el);
5024            }
5025            $clear_up_to_marker->()
5026              if {
5027                button => 1, marquee => 1, object => 1,
5028              }->{$token->{tag_name}};
5029            !!!next-token;
5030          redo B;          redo B;
5031        } elsif ($token->{type} eq 'start tag' or        } elsif ($token->{tag_name} eq 'form') {
5032                 $token->{type} eq 'end tag') {          ## has an element in scope
5033          !!!parse-error (type => 'after html:'.$token->{tag_name});          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5034          $phase = 'main';            my $node = $self->{open_elements}->[$_];
5035          ## reprocess            if ($node->[1] eq $token->{tag_name}) {
5036                ## generate implied end tags
5037                if ({
5038                     dd => 1, dt => 1, li => 1, p => 1,
5039                     td => 1, th => 1, tr => 1,
5040                     tbody => 1, tfoot=> 1, thead => 1,
5041                    }->{$self->{open_elements}->[-1]->[1]}) {
5042                  !!!back-token;
5043                  $token = {type => END_TAG_TOKEN,
5044                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5045                  redo B;
5046                }
5047                last INSCOPE;
5048              } elsif ({
5049                        table => 1, caption => 1, td => 1, th => 1,
5050                        button => 1, marquee => 1, object => 1, html => 1,
5051                       }->{$node->[1]}) {
5052                last INSCOPE;
5053              }
5054            } # INSCOPE
5055            
5056            if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
5057              pop @{$self->{open_elements}};
5058            } else {
5059              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5060            }
5061    
5062            undef $self->{form_element};
5063            !!!next-token;
5064          redo B;          redo B;
5065        } elsif ($token->{type} eq 'end-of-file') {        } elsif ({
5066          ## Stop parsing                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5067          last B;                 }->{$token->{tag_name}}) {
5068            ## has an element in scope
5069            my $i;
5070            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5071              my $node = $self->{open_elements}->[$_];
5072              if ({
5073                   h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5074                  }->{$node->[1]}) {
5075                ## generate implied end tags
5076                if ({
5077                     dd => 1, dt => 1, li => 1, p => 1,
5078                     td => 1, th => 1, tr => 1,
5079                     tbody => 1, tfoot=> 1, thead => 1,
5080                    }->{$self->{open_elements}->[-1]->[1]}) {
5081                  !!!back-token;
5082                  $token = {type => END_TAG_TOKEN,
5083                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5084                  redo B;
5085                }
5086                $i = $_;
5087                last INSCOPE;
5088              } elsif ({
5089                        table => 1, caption => 1, td => 1, th => 1,
5090                        button => 1, marquee => 1, object => 1, html => 1,
5091                       }->{$node->[1]}) {
5092                last INSCOPE;
5093              }
5094            } # INSCOPE
5095            
5096            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5097              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5098            }
5099            
5100            splice @{$self->{open_elements}}, $i if defined $i;
5101            !!!next-token;
5102            redo B;
5103          } elsif ({
5104                    a => 1,
5105                    b => 1, big => 1, em => 1, font => 1, i => 1,
5106                    nobr => 1, s => 1, small => 1, strile => 1,
5107                    strong => 1, tt => 1, u => 1,
5108                   }->{$token->{tag_name}}) {
5109            $formatting_end_tag->($token->{tag_name});
5110            redo B;
5111          } elsif ($token->{tag_name} eq 'br') {
5112            !!!parse-error (type => 'unmatched end tag:br');
5113    
5114            ## As if <br>
5115            $reconstruct_active_formatting_elements->($insert_to_current);
5116            
5117            my $el;
5118            !!!create-element ($el, 'br');
5119            $insert->($el);
5120            
5121            ## Ignore the token.
5122            !!!next-token;
5123            redo B;
5124          } elsif ({
5125                    caption => 1, col => 1, colgroup => 1, frame => 1,
5126                    frameset => 1, head => 1, option => 1, optgroup => 1,
5127                    tbody => 1, td => 1, tfoot => 1, th => 1,
5128                    thead => 1, tr => 1,
5129                    area => 1, basefont => 1, bgsound => 1,
5130                    embed => 1, hr => 1, iframe => 1, image => 1,
5131                    img => 1, input => 1, isindex => 1, noembed => 1,
5132                    noframes => 1, param => 1, select => 1, spacer => 1,
5133                    table => 1, textarea => 1, wbr => 1,
5134                    noscript => 0, ## TODO: if scripting is enabled
5135                   }->{$token->{tag_name}}) {
5136            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5137            ## Ignore the token
5138            !!!next-token;
5139            redo B;
5140            
5141            ## ISSUE: Issue on HTML5 new elements in spec
5142            
5143        } else {        } else {
5144          die "$0: $token->{type}: Unknown token";          ## Step 1
5145            my $node_i = -1;
5146            my $node = $self->{open_elements}->[$node_i];
5147    
5148            ## Step 2
5149            S2: {
5150              if ($node->[1] eq $token->{tag_name}) {
5151                ## Step 1
5152                ## generate implied end tags
5153                if ({
5154                     dd => 1, dt => 1, li => 1, p => 1,
5155                     td => 1, th => 1, tr => 1,
5156                     tbody => 1, tfoot => 1, thead => 1,
5157                    }->{$self->{open_elements}->[-1]->[1]}) {
5158                  !!!back-token;
5159                  $token = {type => END_TAG_TOKEN,
5160                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5161                  redo B;
5162                }
5163            
5164                ## Step 2
5165                if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
5166                  ## NOTE: <x><y></x>
5167                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5168                }
5169                
5170                ## Step 3
5171                splice @{$self->{open_elements}}, $node_i;
5172    
5173                !!!next-token;
5174                last S2;
5175              } else {
5176                ## Step 3
5177                if (not $formatting_category->{$node->[1]} and
5178                    #not $phrasing_category->{$node->[1]} and
5179                    ($special_category->{$node->[1]} or
5180                     $scoping_category->{$node->[1]})) {
5181                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5182                  ## Ignore the token
5183                  !!!next-token;
5184                  last S2;
5185                }
5186              }
5187              
5188              ## Step 4
5189              $node_i--;
5190              $node = $self->{open_elements}->[$node_i];
5191              
5192              ## Step 5;
5193              redo S2;
5194            } # S2
5195            redo B;
5196        }        }
5197      }      }
5198        redo B;
5199    } # B    } # B
5200    
5201      ## NOTE: The "trailing end" phase in HTML5 is split into
5202      ## two insertion modes: "after html body" and "after html frameset".
5203      ## NOTE: States in the main stage is preserved while
5204      ## the parser stays in the trailing end phase. # MUST
5205    
5206    ## Stop parsing # MUST    ## Stop parsing # MUST
5207        
5208    ## TODO: script stuffs    ## TODO: script stuffs
# Line 4935  sub set_inner_html ($$$) { Line 5236  sub set_inner_html ($$$) {
5236      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
5237    
5238      ## Step 1 # MUST      ## Step 1 # MUST
5239      my $doc = $node->owner_document->implementation->create_document;      my $this_doc = $node->owner_document;
5240      ## TODO: Mark as HTML document      my $doc = $this_doc->implementation->create_document;
5241        $doc->manakai_is_html (1);
5242      my $p = $class->new;      my $p = $class->new;
5243      $p->{document} = $doc;      $p->{document} = $doc;
5244    
# Line 4946  sub set_inner_html ($$$) { Line 5248  sub set_inner_html ($$$) {
5248      my $column = 0;      my $column = 0;
5249      $p->{set_next_input_character} = sub {      $p->{set_next_input_character} = sub {
5250        my $self = shift;        my $self = shift;
5251    
5252          pop @{$self->{prev_input_character}};
5253          unshift @{$self->{prev_input_character}}, $self->{next_input_character};
5254    
5255        $self->{next_input_character} = -1 and return if $i >= length $$s;        $self->{next_input_character} = -1 and return if $i >= length $$s;
5256        $self->{next_input_character} = ord substr $$s, $i++, 1;        $self->{next_input_character} = ord substr $$s, $i++, 1;
5257        $column++;        $column++;
# Line 4954  sub set_inner_html ($$$) { Line 5260  sub set_inner_html ($$$) {
5260          $line++;          $line++;
5261          $column = 0;          $column = 0;
5262        } elsif ($self->{next_input_character} == 0x000D) { # CR        } elsif ($self->{next_input_character} == 0x000D) { # CR
5263          if ($i >= length $$s) {          $i++ if substr ($$s, $i, 1) eq "\x0A";
           #  
         } else {  
           my $next_char = ord substr $$s, $i++, 1;  
           if ($next_char == 0x000A) { # LF  
             #  
           } else {  
             push @{$self->{char}}, $next_char;  
           }  
         }  
5264          $self->{next_input_character} = 0x000A; # LF # MUST          $self->{next_input_character} = 0x000A; # LF # MUST
5265          $line++;          $line++;
5266          $column = 0;          $column = 0;
5267        } elsif ($self->{next_input_character} > 0x10FFFF) {        } elsif ($self->{next_input_character} > 0x10FFFF) {
5268          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
5269        } elsif ($self->{next_input_character} == 0x0000) { # NULL        } elsif ($self->{next_input_character} == 0x0000) { # NULL
5270            !!!parse-error (type => 'NULL');
5271          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
5272        }        }
5273      };      };
5274        $p->{prev_input_character} = [-1, -1, -1];
5275        $p->{next_input_character} = -1;
5276            
5277      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
5278        my (%opt) = @_;        my (%opt) = @_;
# Line 4987  sub set_inner_html ($$$) { Line 5287  sub set_inner_html ($$$) {
5287    
5288      ## Step 2      ## Step 2
5289      my $node_ln = $node->local_name;      my $node_ln = $node->local_name;
5290      $p->{content_model_flag} = {      $p->{content_model} = {
5291        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
5292        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
5293        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
5294        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
5295        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
5296        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
5297        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
5298        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
5299        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
5300        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
5301      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
5302         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
5303            unless defined $p->{content_model};
5304            ## ISSUE: What is "the name of the element"? local name?
5305    
5306      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $node_ln];
5307    
# Line 5051  sub set_inner_html ($$$) { Line 5353  sub set_inner_html ($$$) {
5353      ## Step 12 # MUST      ## Step 12 # MUST
5354      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
5355      for (@cn) {      for (@cn) {
5356          $this_doc->adopt_node ($_);
5357        $node->append_child ($_);        $node->append_child ($_);
5358      }      }
5359      ## ISSUE: adopt_node? mutation events?      ## ISSUE: mutation events?
5360    
5361      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
5362    } else {    } else {
# Line 5098  sub get_inner_html ($$$) { Line 5401  sub get_inner_html ($$$) {
5401            
5402      my $nt = $child->node_type;      my $nt = $child->node_type;
5403      if ($nt == 1) { # Element      if ($nt == 1) { # Element
5404        my $tag_name = lc $child->tag_name; ## ISSUE: Definition of "lowercase"        my $tag_name = $child->tag_name; ## TODO: manakai_tag_name
5405        $s .= '<' . $tag_name;        $s .= '<' . $tag_name;
5406          ## NOTE: Non-HTML case:
5407        ## ISSUE: Non-html elements        ## <http://permalink.gmane.org/gmane.org.w3c.whatwg.discuss/11191>
5408    
5409        my @attrs = @{$child->attributes}; # sort order MUST be stable        my @attrs = @{$child->attributes}; # sort order MUST be stable
5410        for my $attr (@attrs) { # order is implementation dependent        for my $attr (@attrs) { # order is implementation dependent
5411          my $attr_name = lc $attr->name; ## ISSUE: Definition of "lowercase"          my $attr_name = $attr->name; ## TODO: manakai_name
5412          $s .= ' ' . $attr_name . '="';          $s .= ' ' . $attr_name . '="';
5413          my $attr_value = $attr->value;          my $attr_value = $attr->value;
5414          ## escape          ## escape
# Line 5124  sub get_inner_html ($$$) { Line 5427  sub get_inner_html ($$$) {
5427          spacer => 1, wbr => 1,          spacer => 1, wbr => 1,
5428        }->{$tag_name};        }->{$tag_name};
5429    
5430          $s .= "\x0A" if $tag_name eq 'pre' or $tag_name eq 'textarea';
5431    
5432        if (not $in_cdata and {        if (not $in_cdata and {
5433          style => 1, script => 1, xmp => 1, iframe => 1,          style => 1, script => 1, xmp => 1, iframe => 1,
5434          noembed => 1, noframes => 1, noscript => 1,          noembed => 1, noframes => 1, noscript => 1,
5435            plaintext => 1,
5436        }->{$tag_name}) {        }->{$tag_name}) {
5437          unshift @node, 'cdata-out';          unshift @node, 'cdata-out';
5438          $in_cdata = 1;          $in_cdata = 1;

Legend:
Removed from v.1.13  
changed lines
  Added in v.1.61

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24