/[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.3 by wakaba, Wed May 2 13:44:34 2007 UTC revision 1.55 by wakaba, Sat Aug 11 06:53:38 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    
30  my $entity_char = {  my $c1_entity_char = {
31    AElig => "\x{00C6}",    0x80 => 0x20AC,
32    Aacute => "\x{00C1}",    0x81 => 0xFFFD,
33    Acirc => "\x{00C2}",    0x82 => 0x201A,
34    Agrave => "\x{00C0}",    0x83 => 0x0192,
35    Alpha => "\x{0391}",    0x84 => 0x201E,
36    Aring => "\x{00C5}",    0x85 => 0x2026,
37    Atilde => "\x{00C3}",    0x86 => 0x2020,
38    Auml => "\x{00C4}",    0x87 => 0x2021,
39    Beta => "\x{0392}",    0x88 => 0x02C6,
40    Ccedil => "\x{00C7}",    0x89 => 0x2030,
41    Chi => "\x{03A7}",    0x8A => 0x0160,
42    Dagger => "\x{2021}",    0x8B => 0x2039,
43    Delta => "\x{0394}",    0x8C => 0x0152,
44    ETH => "\x{00D0}",    0x8D => 0xFFFD,
45    Eacute => "\x{00C9}",    0x8E => 0x017D,
46    Ecirc => "\x{00CA}",    0x8F => 0xFFFD,
47    Egrave => "\x{00C8}",    0x90 => 0xFFFD,
48    Epsilon => "\x{0395}",    0x91 => 0x2018,
49    Eta => "\x{0397}",    0x92 => 0x2019,
50    Euml => "\x{00CB}",    0x93 => 0x201C,
51    Gamma => "\x{0393}",    0x94 => 0x201D,
52    Iacute => "\x{00CD}",    0x95 => 0x2022,
53    Icirc => "\x{00CE}",    0x96 => 0x2013,
54    Igrave => "\x{00CC}",    0x97 => 0x2014,
55    Iota => "\x{0399}",    0x98 => 0x02DC,
56    Iuml => "\x{00CF}",    0x99 => 0x2122,
57    Kappa => "\x{039A}",    0x9A => 0x0161,
58    Lambda => "\x{039B}",    0x9B => 0x203A,
59    Mu => "\x{039C}",    0x9C => 0x0153,
60    Ntilde => "\x{00D1}",    0x9D => 0xFFFD,
61    Nu => "\x{039D}",    0x9E => 0x017E,
62    OElig => "\x{0152}",    0x9F => 0x0178,
63    Oacute => "\x{00D3}",  }; # $c1_entity_char
   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}",  
 };  
64    
65  my $special_category = {  my $special_category = {
66    address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,    address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,
# Line 314  sub parse_string ($$$;$) { Line 96  sub parse_string ($$$;$) {
96    my $column = 0;    my $column = 0;
97    $self->{set_next_input_character} = sub {    $self->{set_next_input_character} = sub {
98      my $self = shift;      my $self = shift;
99    
100        pop @{$self->{prev_input_character}};
101        unshift @{$self->{prev_input_character}}, $self->{next_input_character};
102    
103      $self->{next_input_character} = -1 and return if $i >= length $$s;      $self->{next_input_character} = -1 and return if $i >= length $$s;
104      $self->{next_input_character} = ord substr $$s, $i++, 1;      $self->{next_input_character} = ord substr $$s, $i++, 1;
105      $column++;      $column++;
106            
107      if ($self->{next_input_character} == 0x000D) { # CR      if ($self->{next_input_character} == 0x000A) { # LF
108        if ($i >= length $$s) {        $line++;
109          #        $column = 0;
110        } else {      } elsif ($self->{next_input_character} == 0x000D) { # CR
111          my $next_char = ord substr $$s, $i++, 1;        $i++ if substr ($$s, $i, 1) eq "\x0A";
         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 = -1;        $column = 0;
115      } elsif ($self->{next_input_character} > 0x10FFFF) {      } elsif ($self->{next_input_character} > 0x10FFFF) {
116        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
117      } elsif ($self->{next_input_character} == 0x0000) { # NULL      } elsif ($self->{next_input_character} == 0x0000) { # NULL
118          !!!parse-error (type => 'NULL');
119        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
120      }      }
121    };    };
122      $self->{prev_input_character} = [-1, -1, -1];
123      $self->{next_input_character} = -1;
124    
125    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
126      my (%opt) = @_;      my (%opt) = @_;
# Line 367  sub new ($) { Line 150  sub new ($) {
150    return $self;    return $self;
151  } # new  } # new
152    
153    sub CM_ENTITY () { 0b001 } # & markup in data
154    sub CM_LIMITED_MARKUP () { 0b010 } # < markup in data (limited)
155    sub CM_FULL_MARKUP () { 0b100 } # < markup in data (any)
156    
157    sub PLAINTEXT_CONTENT_MODEL () { 0 }
158    sub CDATA_CONTENT_MODEL () { CM_LIMITED_MARKUP }
159    sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
160    sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
161    
162    sub DOCTYPE_TOKEN () { 1 }
163    sub COMMENT_TOKEN () { 2 }
164    sub START_TAG_TOKEN () { 3 }
165    sub END_TAG_TOKEN () { 4 }
166    sub END_OF_FILE_TOKEN () { 5 }
167    sub CHARACTER_TOKEN () { 6 }
168    
169    sub AFTER_HTML_IMS () { 0b100 }
170    sub HEAD_IMS ()       { 0b1000 }
171    sub BODY_IMS ()       { 0b10000 }
172    sub BODY_TABLE_IMS () { 0b100000 | BODY_IMS }
173    sub TABLE_IMS ()      { 0b1000000 }
174    sub ROW_IMS ()        { 0b10000000 | TABLE_IMS }
175    sub BODY_AFTER_IMS () { 0b100000000 }
176    sub FRAME_IMS ()      { 0b1000000000 }
177    
178    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
179    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
180    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
181    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
182    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
183    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
184    sub IN_BODY_IM () { BODY_IMS }
185    sub IN_CELL_IM () { BODY_TABLE_IMS | 0b01 }
186    sub IN_CAPTION_IM () { BODY_TABLE_IMS | 0b10 }
187    sub IN_ROW_IM () { ROW_IMS | 0b01 }
188    sub IN_TABLE_BODY_IM () { ROW_IMS | 0b10 }
189    sub IN_TABLE_IM () { TABLE_IMS }
190    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
191    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
192    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
193    sub IN_SELECT_IM () { 0b01 }
194    sub IN_COLUMN_GROUP_IM () { 0b10 }
195    
196  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
197    
198  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
199    my $self = shift;    my $self = shift;
200    $self->{state} = 'data'; # MUST    $self->{state} = 'data'; # MUST
201    $self->{content_model_flag} = 'PCDATA'; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
202    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
203    undef $self->{current_attribute};    undef $self->{current_attribute};
204    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
# Line 381  sub _initialize_tokenizer ($) { Line 207  sub _initialize_tokenizer ($) {
207    # $self->{next_input_character}    # $self->{next_input_character}
208    !!!next-input-character;    !!!next-input-character;
209    $self->{token} = [];    $self->{token} = [];
210      # $self->{escape}
211  } # _initialize_tokenizer  } # _initialize_tokenizer
212    
213  ## A token has:  ## A token has:
214  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
215  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
216  ##   ->{name} (DOCTYPE, start tag (tagname), end tag (tagname))  ##   ->{name} (DOCTYPE_TOKEN)
217      ## ISSUE: the spec need s/tagname/tag name/  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
218  ##   ->{error} == 1 or 0 (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
219  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
220  ##   ->{data} (comment, character)  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)
221    ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
222  ## Macros  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
 ##   Macros MUST be preceded by three EXCLAMATION MARKs.  
 ##   emit ($token)  
 ##     Emits the specified token.  
223    
224  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
225    
# Line 414  sub _get_next_token ($) { Line 238  sub _get_next_token ($) {
238    A: {    A: {
239      if ($self->{state} eq 'data') {      if ($self->{state} eq 'data') {
240        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_input_character} == 0x0026) { # &
241          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA
             $self->{content_model_flag} eq 'RCDATA') {  
242            $self->{state} = 'entity data';            $self->{state} = 'entity data';
243            !!!next-input-character;            !!!next-input-character;
244            redo A;            redo A;
245          } else {          } else {
246            #            #
247          }          }
248          } elsif ($self->{next_input_character} == 0x002D) { # -
249            if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
250              unless ($self->{escape}) {
251                if ($self->{prev_input_character}->[0] == 0x002D and # -
252                    $self->{prev_input_character}->[1] == 0x0021 and # !
253                    $self->{prev_input_character}->[2] == 0x003C) { # <
254                  $self->{escape} = 1;
255                }
256              }
257            }
258            
259            #
260        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_input_character} == 0x003C) { # <
261          if ($self->{content_model_flag} ne 'PLAINTEXT') {          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
262                (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
263                 not $self->{escape})) {
264            $self->{state} = 'tag open';            $self->{state} = 'tag open';
265            !!!next-input-character;            !!!next-input-character;
266            redo A;            redo A;
267          } else {          } else {
268            #            #
269          }          }
270          } elsif ($self->{next_input_character} == 0x003E) { # >
271            if ($self->{escape} and
272                ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
273              if ($self->{prev_input_character}->[0] == 0x002D and # -
274                  $self->{prev_input_character}->[1] == 0x002D) { # -
275                delete $self->{escape};
276              }
277            }
278            
279            #
280        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
281          !!!emit ({type => 'end-of-file'});          !!!emit ({type => END_OF_FILE_TOKEN});
282          last A; ## TODO: ok?          last A; ## TODO: ok?
283        }        }
284        # Anything else        # Anything else
285        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
286                     data => chr $self->{next_input_character}};                     data => chr $self->{next_input_character}};
287        ## Stay in the data state        ## Stay in the data state
288        !!!next-input-character;        !!!next-input-character;
# Line 446  sub _get_next_token ($) { Line 293  sub _get_next_token ($) {
293      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} eq 'entity data') {
294        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
295                
296        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);
297    
298        $self->{state} = 'data';        $self->{state} = 'data';
299        # next-input-character is already done        # next-input-character is already done
300    
301        unless (defined $token) {        unless (defined $token) {
302          !!!emit ({type => 'character', data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&'});
303        } else {        } else {
304          !!!emit ($token);          !!!emit ($token);
305        }        }
306    
307        redo A;        redo A;
308      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} eq 'tag open') {
309        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
           $self->{content_model_flag} eq 'CDATA') {  
310          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_input_character} == 0x002F) { # /
311            !!!next-input-character;            !!!next-input-character;
312            $self->{state} = 'close tag open';            $self->{state} = 'close tag open';
# Line 469  sub _get_next_token ($) { Line 315  sub _get_next_token ($) {
315            ## reconsume            ## reconsume
316            $self->{state} = 'data';            $self->{state} = 'data';
317    
318            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
319    
320            redo A;            redo A;
321          }          }
322        } elsif ($self->{content_model_flag} eq 'PCDATA') {        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
323          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_input_character} == 0x0021) { # !
324            $self->{state} = 'markup declaration open';            $self->{state} = 'markup declaration open';
325            !!!next-input-character;            !!!next-input-character;
# Line 485  sub _get_next_token ($) { Line 331  sub _get_next_token ($) {
331          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_input_character} and
332                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_input_character} <= 0x005A) { # A..Z
333            $self->{current_token}            $self->{current_token}
334              = {type => 'start tag',              = {type => START_TAG_TOKEN,
335                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_input_character} + 0x0020)};
336            $self->{state} = 'tag name';            $self->{state} = 'tag name';
337            !!!next-input-character;            !!!next-input-character;
338            redo A;            redo A;
339          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_input_character} and
340                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_input_character} <= 0x007A) { # a..z
341            $self->{current_token} = {type => 'start tag',            $self->{current_token} = {type => START_TAG_TOKEN,
342                              tag_name => chr ($self->{next_input_character})};                              tag_name => chr ($self->{next_input_character})};
343            $self->{state} = 'tag name';            $self->{state} = 'tag name';
344            !!!next-input-character;            !!!next-input-character;
# Line 502  sub _get_next_token ($) { Line 348  sub _get_next_token ($) {
348            $self->{state} = 'data';            $self->{state} = 'data';
349            !!!next-input-character;            !!!next-input-character;
350    
351            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});
352    
353            redo A;            redo A;
354          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_input_character} == 0x003F) { # ?
# Line 515  sub _get_next_token ($) { Line 361  sub _get_next_token ($) {
361            $self->{state} = 'data';            $self->{state} = 'data';
362            ## reconsume            ## reconsume
363    
364            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
365    
366            redo A;            redo A;
367          }          }
368        } else {        } else {
369          die "$0: $self->{content_model_flag}: Unknown content model flag";          die "$0: $self->{content_model} in tag open";
370        }        }
371      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} eq 'close tag open') {
372        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
373            $self->{content_model_flag} eq 'CDATA') {          if (defined $self->{last_emitted_start_tag_name}) {
374          my @next_char;            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
375          TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            my @next_char;
376              TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
377                push @next_char, $self->{next_input_character};
378                my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
379                my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
380                if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {
381                  !!!next-input-character;
382                  next TAGNAME;
383                } else {
384                  $self->{next_input_character} = shift @next_char; # reconsume
385                  !!!back-next-input-character (@next_char);
386                  $self->{state} = 'data';
387    
388                  !!!emit ({type => CHARACTER_TOKEN, data => '</'});
389      
390                  redo A;
391                }
392              }
393            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_input_character};
394            my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);        
395            my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;            unless ($self->{next_input_character} == 0x0009 or # HT
396            if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {                    $self->{next_input_character} == 0x000A or # LF
397              !!!next-input-character;                    $self->{next_input_character} == 0x000B or # VT
398              next TAGNAME;                    $self->{next_input_character} == 0x000C or # FF
399            } else {                    $self->{next_input_character} == 0x0020 or # SP
400              !!!parse-error (type => 'unmatched end tag');                    $self->{next_input_character} == 0x003E or # >
401                      $self->{next_input_character} == 0x002F or # /
402                      $self->{next_input_character} == -1) {
403              $self->{next_input_character} = shift @next_char; # reconsume              $self->{next_input_character} = shift @next_char; # reconsume
404              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
405              $self->{state} = 'data';              $self->{state} = 'data';
406                !!!emit ({type => CHARACTER_TOKEN, data => '</'});
             !!!emit ({type => 'character', data => '</'});  
   
407              redo A;              redo A;
408              } else {
409                $self->{next_input_character} = shift @next_char;
410                !!!back-next-input-character (@next_char);
411                # and consume...
412            }            }
413          }          } else {
414          push @next_char, $self->{next_input_character};            ## No start tag token has ever been emitted
415                  # next-input-character is already done
         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);  
416            $self->{state} = 'data';            $self->{state} = 'data';
417              !!!emit ({type => CHARACTER_TOKEN, data => '</'});
           !!!emit ({type => 'character', data => '</'});  
   
418            redo A;            redo A;
         } else {  
           $self->{next_input_character} = shift @next_char;  
           !!!back-next-input-character (@next_char);  
           # and consume...  
419          }          }
420        }        }
421                
422        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_input_character} and
423            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_input_character} <= 0x005A) { # A..Z
424          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
425                            tag_name => chr ($self->{next_input_character} + 0x0020)};                            tag_name => chr ($self->{next_input_character} + 0x0020)};
426          $self->{state} = 'tag name';          $self->{state} = 'tag name';
427          !!!next-input-character;          !!!next-input-character;
428          redo A;          redo A;
429        } elsif (0x0061 <= $self->{next_input_character} and        } elsif (0x0061 <= $self->{next_input_character} and
430                 $self->{next_input_character} <= 0x007A) { # a..z                 $self->{next_input_character} <= 0x007A) { # a..z
431          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
432                            tag_name => chr ($self->{next_input_character})};                            tag_name => chr ($self->{next_input_character})};
433          $self->{state} = 'tag name';          $self->{state} = 'tag name';
434          !!!next-input-character;          !!!next-input-character;
# Line 594  sub _get_next_token ($) { Line 443  sub _get_next_token ($) {
443          $self->{state} = 'data';          $self->{state} = 'data';
444          # reconsume          # reconsume
445    
446          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</'});
447    
448          redo A;          redo A;
449        } else {        } else {
# Line 613  sub _get_next_token ($) { Line 462  sub _get_next_token ($) {
462          !!!next-input-character;          !!!next-input-character;
463          redo A;          redo A;
464        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
465          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
466              $self->{current_token}->{first_start_tag}
467                  = not defined $self->{last_emitted_start_tag_name};
468            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
469          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
470            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
471            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
472              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
473            }            }
# Line 627  sub _get_next_token ($) { Line 478  sub _get_next_token ($) {
478          !!!next-input-character;          !!!next-input-character;
479    
480          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
481    
482          redo A;          redo A;
483        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 637  sub _get_next_token ($) { Line 487  sub _get_next_token ($) {
487          ## Stay in this state          ## Stay in this state
488          !!!next-input-character;          !!!next-input-character;
489          redo A;          redo A;
490        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
491          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
492          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
493              $self->{current_token}->{first_start_tag}
494                  = not defined $self->{last_emitted_start_tag_name};
495            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
496          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
497            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
498            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
499              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
500            }            }
# Line 654  sub _get_next_token ($) { Line 505  sub _get_next_token ($) {
505          # reconsume          # reconsume
506    
507          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
508    
509          redo A;          redo A;
510        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
511          !!!next-input-character;          !!!next-input-character;
512          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
513              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
514              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
515            # permitted slash            # permitted slash
516            #            #
# Line 687  sub _get_next_token ($) { Line 537  sub _get_next_token ($) {
537          !!!next-input-character;          !!!next-input-character;
538          redo A;          redo A;
539        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
540          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
541              $self->{current_token}->{first_start_tag}
542                  = not defined $self->{last_emitted_start_tag_name};
543            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
544          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
545            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
546            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
547              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
548            }            }
# Line 701  sub _get_next_token ($) { Line 553  sub _get_next_token ($) {
553          !!!next-input-character;          !!!next-input-character;
554    
555          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
556    
557          redo A;          redo A;
558        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 714  sub _get_next_token ($) { Line 565  sub _get_next_token ($) {
565        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
566          !!!next-input-character;          !!!next-input-character;
567          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
568              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
569              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
570            # permitted slash            # permitted slash
571            #            #
# Line 724  sub _get_next_token ($) { Line 575  sub _get_next_token ($) {
575          ## Stay in the state          ## Stay in the state
576          # next-input-character is already done          # next-input-character is already done
577          redo A;          redo A;
578        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
579          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
580          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
581              $self->{current_token}->{first_start_tag}
582                  = not defined $self->{last_emitted_start_tag_name};
583            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
584          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
585            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
586            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
587              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
588            }            }
# Line 741  sub _get_next_token ($) { Line 593  sub _get_next_token ($) {
593          # reconsume          # reconsume
594    
595          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
596    
597          redo A;          redo A;
598        } else {        } else {
# Line 755  sub _get_next_token ($) { Line 606  sub _get_next_token ($) {
606        my $before_leave = sub {        my $before_leave = sub {
607          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
608              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
609            !!!parse-error (type => 'dupulicate attribute');            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});
610            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
611          } else {          } else {
612            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
# Line 779  sub _get_next_token ($) { Line 630  sub _get_next_token ($) {
630          redo A;          redo A;
631        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
632          $before_leave->();          $before_leave->();
633          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
634              $self->{current_token}->{first_start_tag}
635                  = not defined $self->{last_emitted_start_tag_name};
636            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
637          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
638            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
639            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
640              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
641            }            }
# Line 793  sub _get_next_token ($) { Line 646  sub _get_next_token ($) {
646          !!!next-input-character;          !!!next-input-character;
647    
648          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
649    
650          redo A;          redo A;
651        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 806  sub _get_next_token ($) { Line 658  sub _get_next_token ($) {
658          $before_leave->();          $before_leave->();
659          !!!next-input-character;          !!!next-input-character;
660          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
661              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
662              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
663            # permitted slash            # permitted slash
664            #            #
# Line 816  sub _get_next_token ($) { Line 668  sub _get_next_token ($) {
668          $self->{state} = 'before attribute name';          $self->{state} = 'before attribute name';
669          # next-input-character is already done          # next-input-character is already done
670          redo A;          redo A;
671        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
672          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
673          $before_leave->();          $before_leave->();
674          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
675              $self->{current_token}->{first_start_tag}
676                  = not defined $self->{last_emitted_start_tag_name};
677            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
678          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
679            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
680            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
681              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
682            }            }
# Line 834  sub _get_next_token ($) { Line 687  sub _get_next_token ($) {
687          # reconsume          # reconsume
688    
689          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
690    
691          redo A;          redo A;
692        } else {        } else {
# Line 857  sub _get_next_token ($) { Line 709  sub _get_next_token ($) {
709          !!!next-input-character;          !!!next-input-character;
710          redo A;          redo A;
711        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
712          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
713              $self->{current_token}->{first_start_tag}
714                  = not defined $self->{last_emitted_start_tag_name};
715            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
716          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
717            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
718            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
719              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
720            }            }
# Line 871  sub _get_next_token ($) { Line 725  sub _get_next_token ($) {
725          !!!next-input-character;          !!!next-input-character;
726    
727          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
728    
729          redo A;          redo A;
730        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 884  sub _get_next_token ($) { Line 737  sub _get_next_token ($) {
737        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
738          !!!next-input-character;          !!!next-input-character;
739          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
740              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
741              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
742            # permitted slash            # permitted slash
743            #            #
744          } else {          } else {
745            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
746              ## TODO: Different error type for <aa / bb> than <aa/>
747          }          }
748          $self->{state} = 'before attribute name';          $self->{state} = 'before attribute name';
749          # next-input-character is already done          # next-input-character is already done
750          redo A;          redo A;
751        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
752          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
753          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
754              $self->{current_token}->{first_start_tag}
755                  = not defined $self->{last_emitted_start_tag_name};
756            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
757          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
758            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
759            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
760              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
761            }            }
# Line 911  sub _get_next_token ($) { Line 766  sub _get_next_token ($) {
766          # reconsume          # reconsume
767    
768          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
769    
770          redo A;          redo A;
771        } else {        } else {
# Line 943  sub _get_next_token ($) { Line 797  sub _get_next_token ($) {
797          !!!next-input-character;          !!!next-input-character;
798          redo A;          redo A;
799        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
800          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
801              $self->{current_token}->{first_start_tag}
802                  = not defined $self->{last_emitted_start_tag_name};
803            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
804          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
805            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
806            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
807              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
808            }            }
# Line 957  sub _get_next_token ($) { Line 813  sub _get_next_token ($) {
813          !!!next-input-character;          !!!next-input-character;
814    
815          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
816    
817          redo A;          redo A;
818        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
819          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
820          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
821              $self->{current_token}->{first_start_tag}
822                  = not defined $self->{last_emitted_start_tag_name};
823            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
824          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
825            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
826            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
827              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
828            }            }
# Line 977  sub _get_next_token ($) { Line 833  sub _get_next_token ($) {
833          ## reconsume          ## reconsume
834    
835          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
836    
837          redo A;          redo A;
838        } else {        } else {
# Line 998  sub _get_next_token ($) { Line 853  sub _get_next_token ($) {
853          redo A;          redo A;
854        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
855          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
856          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
857              $self->{current_token}->{first_start_tag}
858                  = not defined $self->{last_emitted_start_tag_name};
859            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
860          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
861            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
862            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
863              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
864            }            }
# Line 1012  sub _get_next_token ($) { Line 869  sub _get_next_token ($) {
869          ## reconsume          ## reconsume
870    
871          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
872    
873          redo A;          redo A;
874        } else {        } else {
# Line 1033  sub _get_next_token ($) { Line 889  sub _get_next_token ($) {
889          redo A;          redo A;
890        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
891          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
892          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
893              $self->{current_token}->{first_start_tag}
894                  = not defined $self->{last_emitted_start_tag_name};
895            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
896          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
897            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
898            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
899              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
900            }            }
# Line 1047  sub _get_next_token ($) { Line 905  sub _get_next_token ($) {
905          ## reconsume          ## reconsume
906    
907          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
908    
909          redo A;          redo A;
910        } else {        } else {
# Line 1071  sub _get_next_token ($) { Line 928  sub _get_next_token ($) {
928          !!!next-input-character;          !!!next-input-character;
929          redo A;          redo A;
930        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
931          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
932              $self->{current_token}->{first_start_tag}
933                  = not defined $self->{last_emitted_start_tag_name};
934            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
935          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
936            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
937            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
938              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
939            }            }
# Line 1085  sub _get_next_token ($) { Line 944  sub _get_next_token ($) {
944          !!!next-input-character;          !!!next-input-character;
945    
946          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
947    
948          redo A;          redo A;
949        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
950          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
951          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
952              $self->{current_token}->{first_start_tag}
953                  = not defined $self->{last_emitted_start_tag_name};
954            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
955          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
956            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
957            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
958              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
959            }            }
# Line 1105  sub _get_next_token ($) { Line 964  sub _get_next_token ($) {
964          ## reconsume          ## reconsume
965    
966          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
967    
968          redo A;          redo A;
969        } else {        } else {
# Line 1115  sub _get_next_token ($) { Line 973  sub _get_next_token ($) {
973          redo A;          redo A;
974        }        }
975      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} eq 'entity in attribute value') {
976        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);
977    
978        unless (defined $token) {        unless (defined $token) {
979          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
# Line 1130  sub _get_next_token ($) { Line 988  sub _get_next_token ($) {
988      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} eq 'bogus comment') {
989        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
990                
991        my $token = {type => 'comment', data => ''};        my $token = {type => COMMENT_TOKEN, data => ''};
992    
993        BC: {        BC: {
994          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_input_character} == 0x003E) { # >
# Line 1163  sub _get_next_token ($) { Line 1021  sub _get_next_token ($) {
1021          !!!next-input-character;          !!!next-input-character;
1022          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_input_character};
1023          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_input_character} == 0x002D) { # -
1024            $self->{current_token} = {type => 'comment', data => ''};            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};
1025            $self->{state} = 'comment';            $self->{state} = 'comment start';
1026            !!!next-input-character;            !!!next-input-character;
1027            redo A;            redo A;
1028          }          }
# Line 1206  sub _get_next_token ($) { Line 1064  sub _get_next_token ($) {
1064          }          }
1065        }        }
1066    
1067        !!!parse-error (type => 'bogus comment open');        !!!parse-error (type => 'bogus comment');
1068        $self->{next_input_character} = shift @next_char;        $self->{next_input_character} = shift @next_char;
1069        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1070        $self->{state} = 'bogus comment';        $self->{state} = 'bogus comment';
# Line 1214  sub _get_next_token ($) { Line 1072  sub _get_next_token ($) {
1072                
1073        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1074        ## 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?
1075        } elsif ($self->{state} eq 'comment start') {
1076          if ($self->{next_input_character} == 0x002D) { # -
1077            $self->{state} = 'comment start dash';
1078            !!!next-input-character;
1079            redo A;
1080          } elsif ($self->{next_input_character} == 0x003E) { # >
1081            !!!parse-error (type => 'bogus comment');
1082            $self->{state} = 'data';
1083            !!!next-input-character;
1084    
1085            !!!emit ($self->{current_token}); # comment
1086    
1087            redo A;
1088          } elsif ($self->{next_input_character} == -1) {
1089            !!!parse-error (type => 'unclosed comment');
1090            $self->{state} = 'data';
1091            ## reconsume
1092    
1093            !!!emit ($self->{current_token}); # comment
1094    
1095            redo A;
1096          } else {
1097            $self->{current_token}->{data} # comment
1098                .= chr ($self->{next_input_character});
1099            $self->{state} = 'comment';
1100            !!!next-input-character;
1101            redo A;
1102          }
1103        } elsif ($self->{state} eq 'comment start dash') {
1104          if ($self->{next_input_character} == 0x002D) { # -
1105            $self->{state} = 'comment end';
1106            !!!next-input-character;
1107            redo A;
1108          } elsif ($self->{next_input_character} == 0x003E) { # >
1109            !!!parse-error (type => 'bogus comment');
1110            $self->{state} = 'data';
1111            !!!next-input-character;
1112    
1113            !!!emit ($self->{current_token}); # comment
1114    
1115            redo A;
1116          } elsif ($self->{next_input_character} == -1) {
1117            !!!parse-error (type => 'unclosed comment');
1118            $self->{state} = 'data';
1119            ## reconsume
1120    
1121            !!!emit ($self->{current_token}); # comment
1122    
1123            redo A;
1124          } else {
1125            $self->{current_token}->{data} # comment
1126                .= '-' . chr ($self->{next_input_character});
1127            $self->{state} = 'comment';
1128            !!!next-input-character;
1129            redo A;
1130          }
1131      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} eq 'comment') {
1132        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1133          $self->{state} = 'comment dash';          $self->{state} = 'comment end dash';
1134          !!!next-input-character;          !!!next-input-character;
1135          redo A;          redo A;
1136        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
# Line 1225  sub _get_next_token ($) { Line 1139  sub _get_next_token ($) {
1139          ## reconsume          ## reconsume
1140    
1141          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1142    
1143          redo A;          redo A;
1144        } else {        } else {
# Line 1234  sub _get_next_token ($) { Line 1147  sub _get_next_token ($) {
1147          !!!next-input-character;          !!!next-input-character;
1148          redo A;          redo A;
1149        }        }
1150      } elsif ($self->{state} eq 'comment dash') {      } elsif ($self->{state} eq 'comment end dash') {
1151        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1152          $self->{state} = 'comment end';          $self->{state} = 'comment end';
1153          !!!next-input-character;          !!!next-input-character;
# Line 1245  sub _get_next_token ($) { Line 1158  sub _get_next_token ($) {
1158          ## reconsume          ## reconsume
1159    
1160          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1161    
1162          redo A;          redo A;
1163        } else {        } else {
# Line 1260  sub _get_next_token ($) { Line 1172  sub _get_next_token ($) {
1172          !!!next-input-character;          !!!next-input-character;
1173    
1174          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1175    
1176          redo A;          redo A;
1177        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_input_character} == 0x002D) { # -
# Line 1275  sub _get_next_token ($) { Line 1186  sub _get_next_token ($) {
1186          ## reconsume          ## reconsume
1187    
1188          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1189    
1190          redo A;          redo A;
1191        } else {        } else {
# Line 1309  sub _get_next_token ($) { Line 1219  sub _get_next_token ($) {
1219          ## Stay in the state          ## Stay in the state
1220          !!!next-input-character;          !!!next-input-character;
1221          redo A;          redo A;
       } elsif (0x0061 <= $self->{next_input_character} and  
                $self->{next_input_character} <= 0x007A) { # a..z  
         $self->{current_token} = {type => 'DOCTYPE',  
                           name => chr ($self->{next_input_character} - 0x0020),  
                           error => 1};  
         $self->{state} = 'DOCTYPE name';  
         !!!next-input-character;  
         redo A;  
1222        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1223          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1224          $self->{state} = 'data';          $self->{state} = 'data';
1225          !!!next-input-character;          !!!next-input-character;
1226    
1227          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1228    
1229          redo A;          redo A;
1230        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
# Line 1330  sub _get_next_token ($) { Line 1232  sub _get_next_token ($) {
1232          $self->{state} = 'data';          $self->{state} = 'data';
1233          ## reconsume          ## reconsume
1234    
1235          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1236    
1237          redo A;          redo A;
1238        } else {        } else {
1239          $self->{current_token} = {type => 'DOCTYPE',          $self->{current_token}
1240                            name => chr ($self->{next_input_character}),              = {type => DOCTYPE_TOKEN,
1241                            error => 1};                 name => chr ($self->{next_input_character}),
1242                   correct => 1};
1243    ## ISSUE: "Set the token's name name to the" in the spec
1244          $self->{state} = 'DOCTYPE name';          $self->{state} = 'DOCTYPE name';
1245          !!!next-input-character;          !!!next-input-character;
1246          redo A;          redo A;
1247        }        }
1248      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} eq 'DOCTYPE name') {
1249    ## ISSUE: Redundant "First," in the spec.
1250        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1251            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1252            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1253            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1254            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
         $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE  
1255          $self->{state} = 'after DOCTYPE name';          $self->{state} = 'after DOCTYPE name';
1256          !!!next-input-character;          !!!next-input-character;
1257          redo A;          redo A;
1258        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
         $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE  
1259          $self->{state} = 'data';          $self->{state} = 'data';
1260          !!!next-input-character;          !!!next-input-character;
1261    
1262          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1263    
1264          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;  
1265        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1266          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
         $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE  
1267          $self->{state} = 'data';          $self->{state} = 'data';
1268          ## reconsume          ## reconsume
1269    
1270          !!!emit ($self->{current_token});          delete $self->{current_token}->{correct};
1271          undef $self->{current_token};          !!!emit ($self->{current_token}); # DOCTYPE
1272    
1273          redo A;          redo A;
1274        } else {        } else {
1275          $self->{current_token}->{name}          $self->{current_token}->{name}
1276            .= chr ($self->{next_input_character}); # DOCTYPE            .= chr ($self->{next_input_character}); # DOCTYPE
         #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');  
1277          ## Stay in the state          ## Stay in the state
1278          !!!next-input-character;          !!!next-input-character;
1279          redo A;          redo A;
# Line 1399  sub _get_next_token ($) { Line 1292  sub _get_next_token ($) {
1292          !!!next-input-character;          !!!next-input-character;
1293    
1294          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1295    
1296          redo A;          redo A;
1297        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
# Line 1407  sub _get_next_token ($) { Line 1299  sub _get_next_token ($) {
1299          $self->{state} = 'data';          $self->{state} = 'data';
1300          ## reconsume          ## reconsume
1301    
1302            delete $self->{current_token}->{correct};
1303          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1304    
1305          redo A;          redo A;
1306          } elsif ($self->{next_input_character} == 0x0050 or # P
1307                   $self->{next_input_character} == 0x0070) { # p
1308            !!!next-input-character;
1309            if ($self->{next_input_character} == 0x0055 or # U
1310                $self->{next_input_character} == 0x0075) { # u
1311              !!!next-input-character;
1312              if ($self->{next_input_character} == 0x0042 or # B
1313                  $self->{next_input_character} == 0x0062) { # b
1314                !!!next-input-character;
1315                if ($self->{next_input_character} == 0x004C or # L
1316                    $self->{next_input_character} == 0x006C) { # l
1317                  !!!next-input-character;
1318                  if ($self->{next_input_character} == 0x0049 or # I
1319                      $self->{next_input_character} == 0x0069) { # i
1320                    !!!next-input-character;
1321                    if ($self->{next_input_character} == 0x0043 or # C
1322                        $self->{next_input_character} == 0x0063) { # c
1323                      $self->{state} = 'before DOCTYPE public identifier';
1324                      !!!next-input-character;
1325                      redo A;
1326                    }
1327                  }
1328                }
1329              }
1330            }
1331    
1332            #
1333          } elsif ($self->{next_input_character} == 0x0053 or # S
1334                   $self->{next_input_character} == 0x0073) { # s
1335            !!!next-input-character;
1336            if ($self->{next_input_character} == 0x0059 or # Y
1337                $self->{next_input_character} == 0x0079) { # y
1338              !!!next-input-character;
1339              if ($self->{next_input_character} == 0x0053 or # S
1340                  $self->{next_input_character} == 0x0073) { # s
1341                !!!next-input-character;
1342                if ($self->{next_input_character} == 0x0054 or # T
1343                    $self->{next_input_character} == 0x0074) { # t
1344                  !!!next-input-character;
1345                  if ($self->{next_input_character} == 0x0045 or # E
1346                      $self->{next_input_character} == 0x0065) { # e
1347                    !!!next-input-character;
1348                    if ($self->{next_input_character} == 0x004D or # M
1349                        $self->{next_input_character} == 0x006D) { # m
1350                      $self->{state} = 'before DOCTYPE system identifier';
1351                      !!!next-input-character;
1352                      redo A;
1353                    }
1354                  }
1355                }
1356              }
1357            }
1358    
1359            #
1360        } else {        } else {
1361          !!!parse-error (type => 'string after DOCTYPE name');          !!!next-input-character;
1362          $self->{current_token}->{error} = 1; # DOCTYPE          #
1363          }
1364    
1365          !!!parse-error (type => 'string after DOCTYPE name');
1366          $self->{state} = 'bogus DOCTYPE';
1367          # next-input-character is already done
1368          redo A;
1369        } elsif ($self->{state} eq 'before DOCTYPE public identifier') {
1370          if ({
1371                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1372                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1373              }->{$self->{next_input_character}}) {
1374            ## Stay in the state
1375            !!!next-input-character;
1376            redo A;
1377          } elsif ($self->{next_input_character} eq 0x0022) { # "
1378            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1379            $self->{state} = 'DOCTYPE public identifier (double-quoted)';
1380            !!!next-input-character;
1381            redo A;
1382          } elsif ($self->{next_input_character} eq 0x0027) { # '
1383            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1384            $self->{state} = 'DOCTYPE public identifier (single-quoted)';
1385            !!!next-input-character;
1386            redo A;
1387          } elsif ($self->{next_input_character} eq 0x003E) { # >
1388            !!!parse-error (type => 'no PUBLIC literal');
1389    
1390            $self->{state} = 'data';
1391            !!!next-input-character;
1392    
1393            delete $self->{current_token}->{correct};
1394            !!!emit ($self->{current_token}); # DOCTYPE
1395    
1396            redo A;
1397          } elsif ($self->{next_input_character} == -1) {
1398            !!!parse-error (type => 'unclosed DOCTYPE');
1399    
1400            $self->{state} = 'data';
1401            ## reconsume
1402    
1403            delete $self->{current_token}->{correct};
1404            !!!emit ($self->{current_token}); # DOCTYPE
1405    
1406            redo A;
1407          } else {
1408            !!!parse-error (type => 'string after PUBLIC');
1409            $self->{state} = 'bogus DOCTYPE';
1410            !!!next-input-character;
1411            redo A;
1412          }
1413        } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {
1414          if ($self->{next_input_character} == 0x0022) { # "
1415            $self->{state} = 'after DOCTYPE public identifier';
1416            !!!next-input-character;
1417            redo A;
1418          } elsif ($self->{next_input_character} == -1) {
1419            !!!parse-error (type => 'unclosed PUBLIC literal');
1420    
1421            $self->{state} = 'data';
1422            ## reconsume
1423    
1424            delete $self->{current_token}->{correct};
1425            !!!emit ($self->{current_token}); # DOCTYPE
1426    
1427            redo A;
1428          } else {
1429            $self->{current_token}->{public_identifier} # DOCTYPE
1430                .= chr $self->{next_input_character};
1431            ## Stay in the state
1432            !!!next-input-character;
1433            redo A;
1434          }
1435        } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {
1436          if ($self->{next_input_character} == 0x0027) { # '
1437            $self->{state} = 'after DOCTYPE public identifier';
1438            !!!next-input-character;
1439            redo A;
1440          } elsif ($self->{next_input_character} == -1) {
1441            !!!parse-error (type => 'unclosed PUBLIC literal');
1442    
1443            $self->{state} = 'data';
1444            ## reconsume
1445    
1446            delete $self->{current_token}->{correct};
1447            !!!emit ($self->{current_token}); # DOCTYPE
1448    
1449            redo A;
1450          } else {
1451            $self->{current_token}->{public_identifier} # DOCTYPE
1452                .= chr $self->{next_input_character};
1453            ## Stay in the state
1454            !!!next-input-character;
1455            redo A;
1456          }
1457        } elsif ($self->{state} eq 'after DOCTYPE public identifier') {
1458          if ({
1459                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1460                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1461              }->{$self->{next_input_character}}) {
1462            ## Stay in the state
1463            !!!next-input-character;
1464            redo A;
1465          } elsif ($self->{next_input_character} == 0x0022) { # "
1466            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1467            $self->{state} = 'DOCTYPE system identifier (double-quoted)';
1468            !!!next-input-character;
1469            redo A;
1470          } elsif ($self->{next_input_character} == 0x0027) { # '
1471            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1472            $self->{state} = 'DOCTYPE system identifier (single-quoted)';
1473            !!!next-input-character;
1474            redo A;
1475          } elsif ($self->{next_input_character} == 0x003E) { # >
1476            $self->{state} = 'data';
1477            !!!next-input-character;
1478    
1479            !!!emit ($self->{current_token}); # DOCTYPE
1480    
1481            redo A;
1482          } elsif ($self->{next_input_character} == -1) {
1483            !!!parse-error (type => 'unclosed DOCTYPE');
1484    
1485            $self->{state} = 'data';
1486            ## reconsume
1487    
1488            delete $self->{current_token}->{correct};
1489            !!!emit ($self->{current_token}); # DOCTYPE
1490    
1491            redo A;
1492          } else {
1493            !!!parse-error (type => 'string after PUBLIC literal');
1494            $self->{state} = 'bogus DOCTYPE';
1495            !!!next-input-character;
1496            redo A;
1497          }
1498        } elsif ($self->{state} eq 'before DOCTYPE system identifier') {
1499          if ({
1500                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1501                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1502              }->{$self->{next_input_character}}) {
1503            ## Stay in the state
1504            !!!next-input-character;
1505            redo A;
1506          } elsif ($self->{next_input_character} == 0x0022) { # "
1507            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1508            $self->{state} = 'DOCTYPE system identifier (double-quoted)';
1509            !!!next-input-character;
1510            redo A;
1511          } elsif ($self->{next_input_character} == 0x0027) { # '
1512            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1513            $self->{state} = 'DOCTYPE system identifier (single-quoted)';
1514            !!!next-input-character;
1515            redo A;
1516          } elsif ($self->{next_input_character} == 0x003E) { # >
1517            !!!parse-error (type => 'no SYSTEM literal');
1518            $self->{state} = 'data';
1519            !!!next-input-character;
1520    
1521            delete $self->{current_token}->{correct};
1522            !!!emit ($self->{current_token}); # DOCTYPE
1523    
1524            redo A;
1525          } elsif ($self->{next_input_character} == -1) {
1526            !!!parse-error (type => 'unclosed DOCTYPE');
1527    
1528            $self->{state} = 'data';
1529            ## reconsume
1530    
1531            delete $self->{current_token}->{correct};
1532            !!!emit ($self->{current_token}); # DOCTYPE
1533    
1534            redo A;
1535          } else {
1536            !!!parse-error (type => 'string after SYSTEM');
1537            $self->{state} = 'bogus DOCTYPE';
1538            !!!next-input-character;
1539            redo A;
1540          }
1541        } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {
1542          if ($self->{next_input_character} == 0x0022) { # "
1543            $self->{state} = 'after DOCTYPE system identifier';
1544            !!!next-input-character;
1545            redo A;
1546          } elsif ($self->{next_input_character} == -1) {
1547            !!!parse-error (type => 'unclosed SYSTEM literal');
1548    
1549            $self->{state} = 'data';
1550            ## reconsume
1551    
1552            delete $self->{current_token}->{correct};
1553            !!!emit ($self->{current_token}); # DOCTYPE
1554    
1555            redo A;
1556          } else {
1557            $self->{current_token}->{system_identifier} # DOCTYPE
1558                .= chr $self->{next_input_character};
1559            ## Stay in the state
1560            !!!next-input-character;
1561            redo A;
1562          }
1563        } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {
1564          if ($self->{next_input_character} == 0x0027) { # '
1565            $self->{state} = 'after DOCTYPE system identifier';
1566            !!!next-input-character;
1567            redo A;
1568          } elsif ($self->{next_input_character} == -1) {
1569            !!!parse-error (type => 'unclosed SYSTEM literal');
1570    
1571            $self->{state} = 'data';
1572            ## reconsume
1573    
1574            delete $self->{current_token}->{correct};
1575            !!!emit ($self->{current_token}); # DOCTYPE
1576    
1577            redo A;
1578          } else {
1579            $self->{current_token}->{system_identifier} # DOCTYPE
1580                .= chr $self->{next_input_character};
1581            ## Stay in the state
1582            !!!next-input-character;
1583            redo A;
1584          }
1585        } elsif ($self->{state} eq 'after DOCTYPE system identifier') {
1586          if ({
1587                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1588                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1589              }->{$self->{next_input_character}}) {
1590            ## Stay in the state
1591            !!!next-input-character;
1592            redo A;
1593          } elsif ($self->{next_input_character} == 0x003E) { # >
1594            $self->{state} = 'data';
1595            !!!next-input-character;
1596    
1597            !!!emit ($self->{current_token}); # DOCTYPE
1598    
1599            redo A;
1600          } elsif ($self->{next_input_character} == -1) {
1601            !!!parse-error (type => 'unclosed DOCTYPE');
1602    
1603            $self->{state} = 'data';
1604            ## reconsume
1605    
1606            delete $self->{current_token}->{correct};
1607            !!!emit ($self->{current_token}); # DOCTYPE
1608    
1609            redo A;
1610          } else {
1611            !!!parse-error (type => 'string after SYSTEM literal');
1612          $self->{state} = 'bogus DOCTYPE';          $self->{state} = 'bogus DOCTYPE';
1613          !!!next-input-character;          !!!next-input-character;
1614          redo A;          redo A;
# Line 1423  sub _get_next_token ($) { Line 1618  sub _get_next_token ($) {
1618          $self->{state} = 'data';          $self->{state} = 'data';
1619          !!!next-input-character;          !!!next-input-character;
1620    
1621            delete $self->{current_token}->{correct};
1622          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1623    
1624          redo A;          redo A;
1625        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
# Line 1432  sub _get_next_token ($) { Line 1627  sub _get_next_token ($) {
1627          $self->{state} = 'data';          $self->{state} = 'data';
1628          ## reconsume          ## reconsume
1629    
1630            delete $self->{current_token}->{correct};
1631          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1632    
1633          redo A;          redo A;
1634        } else {        } else {
# Line 1449  sub _get_next_token ($) { Line 1644  sub _get_next_token ($) {
1644    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
1645  } # _get_next_token  } # _get_next_token
1646    
1647  sub _tokenize_attempt_to_consume_an_entity ($) {  sub _tokenize_attempt_to_consume_an_entity ($$) {
1648    my $self = shift;    my ($self, $in_attr) = @_;
1649      
1650    if ($self->{next_input_character} == 0x0023) { # #    if ({
1651           0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
1652           0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
1653          }->{$self->{next_input_character}}) {
1654        ## Don't consume
1655        ## No error
1656        return undef;
1657      } elsif ($self->{next_input_character} == 0x0023) { # #
1658      !!!next-input-character;      !!!next-input-character;
     my $num;  
1659      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_input_character} == 0x0078 or # x
1660          $self->{next_input_character} == 0x0058) { # X          $self->{next_input_character} == 0x0058) { # X
1661          my $code;
1662        X: {        X: {
1663          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_input_character};
1664          !!!next-input-character;          !!!next-input-character;
1665          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_input_character} and
1666              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_input_character} <= 0x0039) { # 0..9
1667            $num ||= 0;            $code ||= 0;
1668            $num *= 0x10;            $code *= 0x10;
1669            $num += $self->{next_input_character} - 0x0030;            $code += $self->{next_input_character} - 0x0030;
1670            redo X;            redo X;
1671          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_input_character} and
1672                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_input_character} <= 0x0066) { # a..f
1673            ## ISSUE: the spec says U+0078, which is apparently incorrect            $code ||= 0;
1674            $num ||= 0;            $code *= 0x10;
1675            $num *= 0x10;            $code += $self->{next_input_character} - 0x0060 + 9;
           $num += $self->{next_input_character} - 0x0060 + 9;  
1676            redo X;            redo X;
1677          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_input_character} and
1678                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_input_character} <= 0x0046) { # A..F
1679            ## ISSUE: the spec says U+0058, which is apparently incorrect            $code ||= 0;
1680            $num ||= 0;            $code *= 0x10;
1681            $num *= 0x10;            $code += $self->{next_input_character} - 0x0040 + 9;
           $num += $self->{next_input_character} - 0x0040 + 9;  
1682            redo X;            redo X;
1683          } elsif (not defined $num) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
1684            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro');
1685              !!!back-next-input-character ($x_char, $self->{next_input_character});
1686            $self->{next_input_character} = 0x0023; # #            $self->{next_input_character} = 0x0023; # #
           !!!back-next-input-character ($x_char);  
1687            return undef;            return undef;
1688          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_input_character} == 0x003B) { # ;
1689            !!!next-input-character;            !!!next-input-character;
# Line 1491  sub _tokenize_attempt_to_consume_an_enti Line 1691  sub _tokenize_attempt_to_consume_an_enti
1691            !!!parse-error (type => 'no refc');            !!!parse-error (type => 'no refc');
1692          }          }
1693    
1694          ## TODO: check the definition for |a valid Unicode character|.          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
1695          if ($num > 1114111 or $num == 0) {            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
1696            $num = 0xFFFD; # REPLACEMENT CHARACTER            $code = 0xFFFD;
1697            ## ISSUE: Why this is not an error?          } elsif ($code > 0x10FFFF) {
1698              !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
1699              $code = 0xFFFD;
1700            } elsif ($code == 0x000D) {
1701              !!!parse-error (type => 'CR character reference');
1702              $code = 0x000A;
1703            } elsif (0x80 <= $code and $code <= 0x9F) {
1704              !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
1705              $code = $c1_entity_char->{$code};
1706          }          }
1707    
1708          return {type => 'character', data => chr $num};          return {type => CHARACTER_TOKEN, data => chr $code};
1709        } # X        } # X
1710      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_input_character} and
1711               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_input_character} <= 0x0039) { # 0..9
# Line 1518  sub _tokenize_attempt_to_consume_an_enti Line 1726  sub _tokenize_attempt_to_consume_an_enti
1726          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc');
1727        }        }
1728    
1729        ## TODO: check the definition for |a valid Unicode character|.        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
1730        if ($code > 1114111 or $code == 0) {          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
1731          $code = 0xFFFD; # REPLACEMENT CHARACTER          $code = 0xFFFD;
1732          ## ISSUE: Why this is not an error?        } elsif ($code > 0x10FFFF) {
1733            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
1734            $code = 0xFFFD;
1735          } elsif ($code == 0x000D) {
1736            !!!parse-error (type => 'CR character reference');
1737            $code = 0x000A;
1738          } elsif (0x80 <= $code and $code <= 0x9F) {
1739            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
1740            $code = $c1_entity_char->{$code};
1741        }        }
1742                
1743        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code};
1744      } else {      } else {
1745        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero');
1746        !!!back-next-input-character ($self->{next_input_character});        !!!back-next-input-character ($self->{next_input_character});
# Line 1539  sub _tokenize_attempt_to_consume_an_enti Line 1755  sub _tokenize_attempt_to_consume_an_enti
1755      !!!next-input-character;      !!!next-input-character;
1756    
1757      my $value = $entity_name;      my $value = $entity_name;
1758      my $match;      my $match = 0;
1759        require Whatpm::_NamedEntityList;
1760        our $EntityChar;
1761    
1762      while (length $entity_name < 10 and      while (length $entity_name < 10 and
1763             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
1764             ((0x0041 <= $self->{next_input_character} and             ((0x0041 <= $self->{next_input_character} and # a
1765               $self->{next_input_character} <= 0x005A) or               $self->{next_input_character} <= 0x005A) or # x
1766              (0x0061 <= $self->{next_input_character} and              (0x0061 <= $self->{next_input_character} and # a
1767               $self->{next_input_character} <= 0x007A) or               $self->{next_input_character} <= 0x007A) or # z
1768              (0x0030 <= $self->{next_input_character} and              (0x0030 <= $self->{next_input_character} and # 0
1769               $self->{next_input_character} <= 0x0039))) {               $self->{next_input_character} <= 0x0039) or # 9
1770                $self->{next_input_character} == 0x003B)) { # ;
1771        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_input_character};
1772        if (defined $entity_char->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
1773          $value = $entity_char->{$entity_name};          if ($self->{next_input_character} == 0x003B) { # ;
1774          $match = 1;            $value = $EntityChar->{$entity_name};
1775              $match = 1;
1776              !!!next-input-character;
1777              last;
1778            } else {
1779              $value = $EntityChar->{$entity_name};
1780              $match = -1;
1781              !!!next-input-character;
1782            }
1783        } else {        } else {
1784          $value .= chr $self->{next_input_character};          $value .= chr $self->{next_input_character};
1785            $match *= 2;
1786            !!!next-input-character;
1787        }        }
       !!!next-input-character;  
1788      }      }
1789            
1790      if ($match) {      if ($match > 0) {
1791        if ($self->{next_input_character} == 0x003B) { # ;        return {type => CHARACTER_TOKEN, data => $value};
1792          !!!next-input-character;      } elsif ($match < 0) {
1793          !!!parse-error (type => 'no refc');
1794          if ($in_attr and $match < -1) {
1795            return {type => CHARACTER_TOKEN, data => '&'.$entity_name};
1796        } else {        } else {
1797          !!!parse-error (type => 'refc');          return {type => CHARACTER_TOKEN, data => $value};
1798        }        }
   
       return {type => 'character', data => $value};  
1799      } else {      } else {
1800        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero');
1801        ## NOTE: No characters are consumed in the spec.        ## NOTE: No characters are consumed in the spec.
1802        !!!back-token ({type => 'character', data => $value});        return {type => CHARACTER_TOKEN, data => '&'.$value};
       return undef;  
1803      }      }
1804    } else {    } else {
1805      ## no characters are consumed      ## no characters are consumed
# Line 1586  sub _initialize_tree_constructor ($) { Line 1814  sub _initialize_tree_constructor ($) {
1814    $self->{document}->strict_error_checking (0);    $self->{document}->strict_error_checking (0);
1815    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
1816    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
1817    ## TODO: Mark the Document as an HTML document # MUST    $self->{document}->manakai_is_html (1); # MUST
1818  } # _initialize_tree_constructor  } # _initialize_tree_constructor
1819    
1820  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 1613  sub _construct_tree ($) { Line 1841  sub _construct_tree ($) {
1841        
1842    !!!next-token;    !!!next-token;
1843    
1844    $self->{insertion_mode} = 'before head';    $self->{insertion_mode} = BEFORE_HEAD_IM;
1845    undef $self->{form_element};    undef $self->{form_element};
1846    undef $self->{head_element};    undef $self->{head_element};
1847    $self->{open_elements} = [];    $self->{open_elements} = [];
# Line 1626  sub _construct_tree ($) { Line 1854  sub _construct_tree ($) {
1854    
1855  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
1856    my $self = shift;    my $self = shift;
1857    B: {    INITIAL: {
1858        if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
1859          if ($token->{error}) {        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
1860            ## ISSUE: Spec currently left this case undefined.        ## error, switch to a conformance checking mode for another
1861            !!!parse-error (type => 'bogus DOCTYPE');        ## language.
1862          }        my $doctype_name = $token->{name};
1863          my $doctype = $self->{document}->create_document_type_definition        $doctype_name = '' unless defined $doctype_name;
1864            ($token->{name});        $doctype_name =~ tr/a-z/A-Z/;
1865          $self->{document}->append_child ($doctype);        if (not defined $token->{name} or # <!DOCTYPE>
1866          #$phase = 'root element';            defined $token->{public_identifier} or
1867          !!!next-token;            defined $token->{system_identifier}) {
1868          #redo B;          !!!parse-error (type => 'not HTML5');
1869          return;        } elsif ($doctype_name ne 'HTML') {
1870        } elsif ({          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
1871                  comment => 1,          !!!parse-error (type => 'not HTML5');
1872                  'start tag' => 1,        }
1873                  'end tag' => 1,        
1874                  'end-of-file' => 1,        my $doctype = $self->{document}->create_document_type_definition
1875                 }->{$token->{type}}) {          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
1876          ## ISSUE: Spec currently left this case undefined.        $doctype->public_id ($token->{public_identifier})
1877          !!!parse-error (type => 'missing DOCTYPE');            if defined $token->{public_identifier};
1878          #$phase = 'root element';        $doctype->system_id ($token->{system_identifier})
1879          ## reprocess            if defined $token->{system_identifier};
1880          #redo B;        ## NOTE: Other DocumentType attributes are null or empty lists.
1881          return;        ## ISSUE: internalSubset = null??
1882        } elsif ($token->{type} eq 'character') {        $self->{document}->append_child ($doctype);
1883          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        
1884            $self->{document}->manakai_append_text ($1);        if (not $token->{correct} or $doctype_name ne 'HTML') {
1885            ## ISSUE: DOM3 Core does not allow Document > Text          $self->{document}->manakai_compat_mode ('quirks');
1886            unless (length $token->{data}) {        } elsif (defined $token->{public_identifier}) {
1887              ## Stay in the phase          my $pubid = $token->{public_identifier};
1888              !!!next-token;          $pubid =~ tr/a-z/A-z/;
1889              redo B;          if ({
1890              "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,
1891              "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,
1892              "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,
1893              "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,
1894              "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,
1895              "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,
1896              "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,
1897              "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,
1898              "-//IETF//DTD HTML 2.0//EN" => 1,
1899              "-//IETF//DTD HTML 2.1E//EN" => 1,
1900              "-//IETF//DTD HTML 3.0//EN" => 1,
1901              "-//IETF//DTD HTML 3.0//EN//" => 1,
1902              "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,
1903              "-//IETF//DTD HTML 3.2//EN" => 1,
1904              "-//IETF//DTD HTML 3//EN" => 1,
1905              "-//IETF//DTD HTML LEVEL 0//EN" => 1,
1906              "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,
1907              "-//IETF//DTD HTML LEVEL 1//EN" => 1,
1908              "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,
1909              "-//IETF//DTD HTML LEVEL 2//EN" => 1,
1910              "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,
1911              "-//IETF//DTD HTML LEVEL 3//EN" => 1,
1912              "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,
1913              "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,
1914              "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,
1915              "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,
1916              "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,
1917              "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,
1918              "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,
1919              "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,
1920              "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,
1921              "-//IETF//DTD HTML STRICT//EN" => 1,
1922              "-//IETF//DTD HTML STRICT//EN//2.0" => 1,
1923              "-//IETF//DTD HTML STRICT//EN//3.0" => 1,
1924              "-//IETF//DTD HTML//EN" => 1,
1925              "-//IETF//DTD HTML//EN//2.0" => 1,
1926              "-//IETF//DTD HTML//EN//3.0" => 1,
1927              "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,
1928              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,
1929              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,
1930              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,
1931              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,
1932              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,
1933              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,
1934              "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,
1935              "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,
1936              "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,
1937              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,
1938              "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,
1939              "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,
1940              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,
1941              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,
1942              "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,
1943              "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,
1944              "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,
1945              "-//W3C//DTD HTML 3.2//EN" => 1,
1946              "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,
1947              "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,
1948              "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,
1949              "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,
1950              "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,
1951              "-//W3C//DTD W3 HTML//EN" => 1,
1952              "-//W3O//DTD W3 HTML 3.0//EN" => 1,
1953              "-//W3O//DTD W3 HTML 3.0//EN//" => 1,
1954              "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,
1955              "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,
1956              "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,
1957              "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,
1958              "HTML" => 1,
1959            }->{$pubid}) {
1960              $self->{document}->manakai_compat_mode ('quirks');
1961            } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or
1962                     $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {
1963              if (defined $token->{system_identifier}) {
1964                $self->{document}->manakai_compat_mode ('quirks');
1965              } else {
1966                $self->{document}->manakai_compat_mode ('limited quirks');
1967            }            }
1968            } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or
1969                     $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {
1970              $self->{document}->manakai_compat_mode ('limited quirks');
1971            }
1972          }
1973          if (defined $token->{system_identifier}) {
1974            my $sysid = $token->{system_identifier};
1975            $sysid =~ tr/A-Z/a-z/;
1976            if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
1977              $self->{document}->manakai_compat_mode ('quirks');
1978            }
1979          }
1980          
1981          ## Go to the root element phase.
1982          !!!next-token;
1983          return;
1984        } elsif ({
1985                  START_TAG_TOKEN, 1,
1986                  END_TAG_TOKEN, 1,
1987                  END_OF_FILE_TOKEN, 1,
1988                 }->{$token->{type}}) {
1989          !!!parse-error (type => 'no DOCTYPE');
1990          $self->{document}->manakai_compat_mode ('quirks');
1991          ## Go to the root element phase
1992          ## reprocess
1993          return;
1994        } elsif ($token->{type} == CHARACTER_TOKEN) {
1995          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
1996            ## Ignore the token
1997    
1998            unless (length $token->{data}) {
1999              ## Stay in the phase
2000              !!!next-token;
2001              redo INITIAL;
2002          }          }
         ## 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";  
2003        }        }
2004      } # B  
2005          !!!parse-error (type => 'no DOCTYPE');
2006          $self->{document}->manakai_compat_mode ('quirks');
2007          ## Go to the root element phase
2008          ## reprocess
2009          return;
2010        } elsif ($token->{type} == COMMENT_TOKEN) {
2011          my $comment = $self->{document}->create_comment ($token->{data});
2012          $self->{document}->append_child ($comment);
2013          
2014          ## Stay in the phase.
2015          !!!next-token;
2016          redo INITIAL;
2017        } else {
2018          die "$0: $token->{type}: Unknown token type";
2019        }
2020      } # INITIAL
2021  } # _tree_construction_initial  } # _tree_construction_initial
2022    
2023  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
2024    my $self = shift;    my $self = shift;
2025        
2026    B: {    B: {
2027        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
2028          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE');
2029          ## Ignore the token          ## Ignore the token
2030          ## Stay in the phase          ## Stay in the phase
2031          !!!next-token;          !!!next-token;
2032          redo B;          redo B;
2033        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
2034          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
2035          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2036          ## Stay in the phase          ## Stay in the phase
2037          !!!next-token;          !!!next-token;
2038          redo B;          redo B;
2039        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
2040          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2041            $self->{document}->manakai_append_text ($1);            ## Ignore the token.
2042            ## ISSUE: DOM3 Core does not allow Document > Text  
2043            unless (length $token->{data}) {            unless (length $token->{data}) {
2044              ## Stay in the phase              ## Stay in the phase
2045              !!!next-token;              !!!next-token;
# Line 1701  sub _tree_construction_root_element ($) Line 2048  sub _tree_construction_root_element ($)
2048          }          }
2049          #          #
2050        } elsif ({        } elsif ({
2051                  'start tag' => 1,                  START_TAG_TOKEN, 1,
2052                  'end tag' => 1,                  END_TAG_TOKEN, 1,
2053                  'end-of-file' => 1,                  END_OF_FILE_TOKEN, 1,
2054                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2055          ## ISSUE: There is an issue in the spec          ## ISSUE: There is an issue in the spec
2056          #          #
2057        } else {        } else {
2058          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
2059        }        }
2060        my $root_element; !!!create-element ($root_element, 'html');        my $root_element; !!!create-element ($root_element, 'html');
2061        $self->{document}->append_child ($root_element);        $self->{document}->append_child ($root_element);
2062        push @{$self->{open_elements}}, [$root_element, 'html'];        push @{$self->{open_elements}}, [$root_element, 'html'];
       #$phase = 'main';  
2063        ## reprocess        ## reprocess
2064        #redo B;        #redo B;
2065        return;        return; ## Go to the main phase.
2066    } # B    } # B
2067  } # _tree_construction_root_element  } # _tree_construction_root_element
2068    
# Line 1732  sub _reset_insertion_mode ($) { Line 2078  sub _reset_insertion_mode ($) {
2078            
2079      ## Step 3      ## Step 3
2080      S3: {      S3: {
2081        $last = 1 if $self->{open_elements}->[0]->[0] eq $node->[0];        ## ISSUE: Oops! "If node is the first node in the stack of open
2082        if (defined $self->{inner_html_node}) {        ## elements, then set last to true. If the context element of the
2083          if ($self->{inner_html_node}->[1] eq 'td' or        ## HTML fragment parsing algorithm is neither a td element nor a
2084              $self->{inner_html_node}->[1] eq 'th') {        ## th element, then set node to the context element. (fragment case)":
2085            #        ## The second "if" is in the scope of the first "if"!?
2086          } else {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
2087            $node = $self->{inner_html_node};          $last = 1;
2088            if (defined $self->{inner_html_node}) {
2089              if ($self->{inner_html_node}->[1] eq 'td' or
2090                  $self->{inner_html_node}->[1] eq 'th') {
2091                #
2092              } else {
2093                $node = $self->{inner_html_node};
2094              }
2095          }          }
2096        }        }
2097            
2098        ## Step 4..13        ## Step 4..13
2099        my $new_mode = {        my $new_mode = {
2100                        select => 'in select',                        select => IN_SELECT_IM,
2101                        td => 'in cell',                        td => IN_CELL_IM,
2102                        th => 'in cell',                        th => IN_CELL_IM,
2103                        tr => 'in row',                        tr => IN_ROW_IM,
2104                        tbody => 'in table body',                        tbody => IN_TABLE_BODY_IM,
2105                        thead => 'in table head',                        thead => IN_TABLE_BODY_IM,
2106                        tfoot => 'in table foot',                        tfoot => IN_TABLE_BODY_IM,
2107                        caption => 'in caption',                        caption => IN_CAPTION_IM,
2108                        colgroup => 'in column group',                        colgroup => IN_COLUMN_GROUP_IM,
2109                        table => 'in table',                        table => IN_TABLE_IM,
2110                        head => 'in body', # not in head!                        head => IN_BODY_IM, # not in head!
2111                        body => 'in body',                        body => IN_BODY_IM,
2112                        frameset => 'in frameset',                        frameset => IN_FRAMESET_IM,
2113                       }->{$node->[1]};                       }->{$node->[1]};
2114        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
2115                
2116        ## Step 14        ## Step 14
2117        if ($node->[1] eq 'html') {        if ($node->[1] eq 'html') {
2118          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
2119            $self->{insertion_mode} = 'before head';            $self->{insertion_mode} = BEFORE_HEAD_IM;
2120          } else {          } else {
2121            $self->{insertion_mode} = 'after head';            $self->{insertion_mode} = AFTER_HEAD_IM;
2122          }          }
2123          return;          return;
2124        }        }
2125                
2126        ## Step 15        ## Step 15
2127        $self->{insertion_mode} = 'in body' and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
2128                
2129        ## Step 16        ## Step 16
2130        $i--;        $i--;
# Line 1785  sub _reset_insertion_mode ($) { Line 2138  sub _reset_insertion_mode ($) {
2138  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
2139    my $self = shift;    my $self = shift;
2140    
   my $phase = 'main';  
   
2141    my $active_formatting_elements = [];    my $active_formatting_elements = [];
2142    
2143    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 1869  sub _tree_construction_main ($) { Line 2220  sub _tree_construction_main ($) {
2220      }      }
2221    }; # $clear_up_to_marker    }; # $clear_up_to_marker
2222    
2223    my $style_start_tag = sub {    my $parse_rcdata = sub ($$) {
2224      my $style_el; !!!create-element ($style_el, 'style');      my ($content_model_flag, $insert) = @_;
2225      ## $self->{insertion_mode} eq 'in head' and ... (always true)  
2226      (($self->{insertion_mode} eq 'in head' and defined $self->{head_element})      ## Step 1
2227       ? $self->{head_element} : $self->{open_elements}->[-1]->[0])      my $start_tag_name = $token->{tag_name};
2228        ->append_child ($style_el);      my $el;
2229      $self->{content_model_flag} = 'CDATA';      !!!create-element ($el, $start_tag_name, $token->{attributes});
2230                  
2231        ## Step 2
2232        $insert->($el); # /context node/->append_child ($el)
2233    
2234        ## Step 3
2235        $self->{content_model} = $content_model_flag; # CDATA or RCDATA
2236        delete $self->{escape}; # MUST
2237    
2238        ## Step 4
2239      my $text = '';      my $text = '';
2240      !!!next-token;      !!!next-token;
2241      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
2242        $text .= $token->{data};        $text .= $token->{data};
2243        !!!next-token;        !!!next-token;
2244      } # stop if non-character token or tokenizer stops tokenising      }
2245    
2246        ## Step 5
2247      if (length $text) {      if (length $text) {
2248        $style_el->manakai_append_text ($text);        my $text = $self->{document}->create_text_node ($text);
2249          $el->append_child ($text);
2250      }      }
2251        
2252      $self->{content_model_flag} = 'PCDATA';      ## Step 6
2253                      $self->{content_model} = PCDATA_CONTENT_MODEL;
2254      if ($token->{type} eq 'end tag' and $token->{tag_name} eq 'style') {  
2255        ## Step 7
2256        if ($token->{type} == END_TAG_TOKEN and $token->{tag_name} eq $start_tag_name) {
2257        ## Ignore the token        ## Ignore the token
2258      } else {      } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {
2259        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type});
2260        ## ISSUE: And ignore?      } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
2261          !!!parse-error (type => 'in RCDATA:#'.$token->{type});
2262        } else {
2263          die "$0: $content_model_flag in parse_rcdata";
2264      }      }
2265      !!!next-token;      !!!next-token;
2266    }; # $style_start_tag    }; # $parse_rcdata
2267    
2268    my $script_start_tag = sub {    my $script_start_tag = sub ($) {
2269        my $insert = $_[0];
2270      my $script_el;      my $script_el;
2271      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes});
2272      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
2273    
2274      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
2275        delete $self->{escape}; # MUST
2276            
2277      my $text = '';      my $text = '';
2278      !!!next-token;      !!!next-token;
2279      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
2280        $text .= $token->{data};        $text .= $token->{data};
2281        !!!next-token;        !!!next-token;
2282      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
# Line 1915  sub _tree_construction_main ($) { Line 2284  sub _tree_construction_main ($) {
2284        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
2285      }      }
2286                                
2287      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
2288    
2289      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
2290          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
2291        ## Ignore the token        ## Ignore the token
2292      } else {      } else {
# Line 1931  sub _tree_construction_main ($) { Line 2300  sub _tree_construction_main ($) {
2300      } else {      } else {
2301        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
2302        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
2303          
2304        (($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);  
2305                
2306        ## TODO: insertion point = $old_insertion_point (might be "undefined")        ## TODO: insertion point = $old_insertion_point (might be "undefined")
2307                
# Line 1975  sub _tree_construction_main ($) { Line 2343  sub _tree_construction_main ($) {
2343              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
2344              last INSCOPE;              last INSCOPE;
2345            } else { # in open elements but not in scope            } else { # in open elements but not in scope
2346              !!!parse-error;              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2347              ## Ignore the token              ## Ignore the token
2348              !!!next-token;              !!!next-token;
2349              return;              return;
# Line 1988  sub _tree_construction_main ($) { Line 2356  sub _tree_construction_main ($) {
2356          }          }
2357        } # INSCOPE        } # INSCOPE
2358        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
2359          !!!parse-error;          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2360          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
2361          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
2362          return;          return;
2363        }        }
2364        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
2365          !!!parse-error;          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
2366        }        }
2367                
2368        ## Step 2        ## Step 2
# Line 2127  sub _tree_construction_main ($) { Line 2495  sub _tree_construction_main ($) {
2495    }; # $formatting_end_tag    }; # $formatting_end_tag
2496    
2497    my $insert_to_current = sub {    my $insert_to_current = sub {
2498      $self->{open_elements}->[-1]->[0]->append_child (shift);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
2499    }; # $insert_to_current    }; # $insert_to_current
2500    
2501    my $insert_to_foster = sub {    my $insert_to_foster = sub {
# Line 2161  sub _tree_construction_main ($) { Line 2529  sub _tree_construction_main ($) {
2529                         }                         }
2530    }; # $insert_to_foster    }; # $insert_to_foster
2531    
2532    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';  
           
         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  
         } 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') {  
             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') {  
             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';  
           
         !!!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,  
                 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';  
         }  
           
         $insert->($el);  
           
         my $text = '';  
         !!!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 CDATA:#'.$token->{type});  
           } else {  
             !!!parse-error (type => 'in RCDATA:#'.$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,  
                 form => 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;  
         undef $self->{form_element} if $token->{tag_name} eq 'form';  
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
         !!!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});  
         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  
2533    
2534    B: {    B: {
2535      if ($phase eq 'main') {      if ($token->{type} == DOCTYPE_TOKEN) {
2536        if ($token->{type} eq 'DOCTYPE') {        !!!parse-error (type => 'DOCTYPE in the middle');
2537          !!!parse-error (type => 'in html:#DOCTYPE');        ## Ignore the token
2538          ## Ignore the token        ## Stay in the phase
2539          ## Stay in the phase        !!!next-token;
2540          !!!next-token;        redo B;
2541          redo B;      } elsif ($token->{type} == END_OF_FILE_TOKEN) {
2542        } elsif ($token->{type} eq 'start tag' and        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM or
2543                 $token->{tag_name} eq 'html') {            $self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
2544          ## TODO: unless it is the first start tag token, parse-error          #
2545          my $top_el = $self->{open_elements}->[0]->[0];        } else {
         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') {  
2546          ## Generate implied end tags          ## Generate implied end tags
2547          if ({          if ({
2548               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,
2549                 tbody => 1, tfoot=> 1, thead => 1,
2550              }->{$self->{open_elements}->[-1]->[1]}) {              }->{$self->{open_elements}->[-1]->[1]}) {
2551            !!!back-token;            !!!back-token;
2552            $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};            $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};
2553            redo B;            redo B;
2554          }          }
2555                    
# Line 2937  sub _tree_construction_main ($) { Line 2562  sub _tree_construction_main ($) {
2562            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
2563          }          }
2564    
         ## Stop parsing  
         last B;  
   
2565          ## ISSUE: There is an issue in the spec.          ## ISSUE: There is an issue in the spec.
2566          }
2567    
2568          ## Stop parsing
2569          last B;
2570        } elsif ($token->{type} == START_TAG_TOKEN and
2571                 $token->{tag_name} eq 'html') {
2572          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
2573            ## Turn into the main phase
2574            !!!parse-error (type => 'after html:html');
2575            $self->{insertion_mode} = AFTER_BODY_IM;
2576          } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
2577            ## Turn into the main phase
2578            !!!parse-error (type => 'after html:html');
2579            $self->{insertion_mode} = AFTER_FRAMESET_IM;
2580          }
2581    
2582    ## ISSUE: "aa<html>" is not a parse error.
2583    ## ISSUE: "<html>" in fragment is not a parse error.
2584          unless ($token->{first_start_tag}) {
2585            !!!parse-error (type => 'not first start tag');
2586          }
2587          my $top_el = $self->{open_elements}->[0]->[0];
2588          for my $attr_name (keys %{$token->{attributes}}) {
2589            unless ($top_el->has_attribute_ns (undef, $attr_name)) {
2590              $top_el->set_attribute_ns
2591                (undef, [undef, $attr_name],
2592                 $token->{attributes}->{$attr_name}->{value});
2593            }
2594          }
2595          !!!next-token;
2596          redo B;
2597        } elsif ($token->{type} == COMMENT_TOKEN) {
2598          my $comment = $self->{document}->create_comment ($token->{data});
2599          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM or
2600              $self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
2601            $self->{document}->append_child ($comment);
2602          } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
2603            $self->{open_elements}->[0]->[0]->append_child ($comment);
2604        } else {        } else {
2605          if ($self->{insertion_mode} eq 'before head') {          $self->{open_elements}->[-1]->[0]->append_child ($comment);
2606            if ($token->{type} eq 'character') {        }
2607              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        !!!next-token;
2608                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);        redo B;
2609                unless (length $token->{data}) {      } elsif ($self->{insertion_mode} == IN_HEAD_IM or
2610                  !!!next-token;               $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM or
2611                  redo B;               $self->{insertion_mode} == AFTER_HEAD_IM or
2612                }               $self->{insertion_mode} == BEFORE_HEAD_IM) {
2613              }        if ($token->{type} == CHARACTER_TOKEN) {
2614              ## As if <head>          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
2615              !!!create-element ($self->{head_element}, 'head');            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
2616              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            unless (length $token->{data}) {
             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);  
2617              !!!next-token;              !!!next-token;
2618              redo B;              redo B;
2619            } elsif ($token->{type} eq 'start tag') {            }
2620              my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};          }
2621              !!!create-element ($self->{head_element}, 'head', $attr);  
2622              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2623              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            ## As if <head>
2624              $self->{insertion_mode} = 'in head';            !!!create-element ($self->{head_element}, 'head');
2625              if ($token->{tag_name} eq 'head') {            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2626                !!!next-token;            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2627              #} elsif ({  
2628              #          base => 1, link => 1, meta => 1,            ## Reprocess in the "in head" insertion mode...
2629              #          script => 1, style => 1, title => 1,            pop @{$self->{open_elements}};
2630              #         }->{$token->{tag_name}}) {  
2631              #  ## reprocess            ## Reprocess in the "after head" insertion mode...
2632              } else {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2633                ## reprocess            ## As if </noscript>
2634              }            pop @{$self->{open_elements}};
2635              !!!parse-error (type => 'in noscript:#character');
2636              
2637              ## Reprocess in the "in head" insertion mode...
2638              ## As if </head>
2639              pop @{$self->{open_elements}};
2640    
2641              ## Reprocess in the "after head" insertion mode...
2642            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2643              pop @{$self->{open_elements}};
2644    
2645              ## Reprocess in the "after head" insertion mode...
2646            }
2647    
2648                ## "after head" insertion mode
2649                ## As if <body>
2650                !!!insert-element ('body');
2651                $self->{insertion_mode} = IN_BODY_IM;
2652                ## reprocess
2653              redo B;              redo B;
2654            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
2655              if ($token->{tag_name} eq 'html') {              if ($token->{tag_name} eq 'head') {
2656                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2657                    !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
2658                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2659                    push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
2660                    $self->{insertion_mode} = IN_HEAD_IM;
2661                    !!!next-token;
2662                    redo B;
2663                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2664                    #
2665                  } else {
2666                    !!!parse-error (type => 'in head:head'); # or in head noscript
2667                    ## Ignore the token
2668                    !!!next-token;
2669                    redo B;
2670                  }
2671                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2672                ## As if <head>                ## As if <head>
2673                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head');
2674                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2675                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2676                $self->{insertion_mode} = 'in head';  
2677                ## reprocess                $self->{insertion_mode} = IN_HEAD_IM;
2678                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;  
               }  
2679              }              }
               
             #  
           } 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';  
2680    
2681                my $text = '';              if ($token->{tag_name} eq 'base') {
2682                !!!next-token;                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2683                while ($token->{type} eq 'character') {                  ## As if </noscript>
2684                  $text .= $token->{data};                  pop @{$self->{open_elements}};
2685                  !!!next-token;                  !!!parse-error (type => 'in noscript:base');
               }  
               if (length $text) {  
                 $title_el->manakai_append_text ($text);  
               }  
                 
               $self->{content_model_flag} = 'PCDATA';  
2686                                
2687                if ($token->{type} eq 'end tag' and                  $self->{insertion_mode} = IN_HEAD_IM;
2688                    $token->{tag_name} eq 'title') {                  ## Reprocess in the "in head" insertion mode...
2689                  ## Ignore the token                }
2690                } else {  
2691                  !!!parse-error (type => 'in RCDATA:#'.$token->{type});                ## NOTE: There is a "as if in head" code clone.
2692                  ## ISSUE: And ignore?                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2693                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2694                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2695                }                }
2696                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2697                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2698                  pop @{$self->{open_elements}}
2699                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2700                !!!next-token;                !!!next-token;
2701                redo B;                redo B;
2702              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'link') {
2703                $style_start_tag->();                ## NOTE: There is a "as if in head" code clone.
2704                redo B;                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2705              } elsif ($token->{tag_name} eq 'script') {                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2706                $script_start_tag->();                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2707                  }
2708                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2709                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2710                  pop @{$self->{open_elements}}
2711                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2712                  !!!next-token;
2713                redo B;                redo B;
2714              } elsif ({base => 1, link => 1, meta => 1}->{$token->{tag_name}}) {              } elsif ($token->{tag_name} eq 'meta') {
2715                ## NOTE: There are "as if in head" code clones                ## NOTE: There is a "as if in head" code clone.
2716                my $el;                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2717                !!!create-element ($el, $token->{tag_name}, $token->{attributes});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2718                (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2719                  ->append_child ($el);                }
2720                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2721                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2722    
2723                  unless ($self->{confident}) {
2724                    my $charset;
2725                    if ($token->{attributes}->{charset}) { ## TODO: And if supported
2726                      $charset = $token->{attributes}->{charset}->{value};
2727                    }
2728                    if ($token->{attributes}->{'http-equiv'}) {
2729                      ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
2730                      if ($token->{attributes}->{'http-equiv'}->{value}
2731                          =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
2732                              [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
2733                              ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
2734                        $charset = defined $1 ? $1 : defined $2 ? $2 : $3;
2735                      } ## TODO: And if supported
2736                    }
2737                    ## TODO: Change the encoding
2738                  }
2739    
2740                  ## TODO: Extracting |charset| from |meta|.
2741                  pop @{$self->{open_elements}}
2742                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2743                !!!next-token;                !!!next-token;
2744                redo B;                redo B;
2745              } elsif ($token->{tag_name} eq 'head') {              } elsif ($token->{tag_name} eq 'title') {
2746                !!!parse-error (type => 'in head:head');                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2747                ## Ignore the token                  ## As if </noscript>
2748                !!!next-token;                  pop @{$self->{open_elements}};
2749                    !!!parse-error (type => 'in noscript:title');
2750                  
2751                    $self->{insertion_mode} = IN_HEAD_IM;
2752                    ## Reprocess in the "in head" insertion mode...
2753                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2754                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2755                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2756                  }
2757    
2758                  ## NOTE: There is a "as if in head" code clone.
2759                  my $parent = defined $self->{head_element} ? $self->{head_element}
2760                      : $self->{open_elements}->[-1]->[0];
2761                  $parse_rcdata->(RCDATA_CONTENT_MODEL,
2762                                  sub { $parent->append_child ($_[0]) });
2763                  pop @{$self->{open_elements}}
2764                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2765                redo B;                redo B;
2766              } else {              } elsif ($token->{tag_name} eq 'style') {
2767                #                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
2768              }                ## insertion mode IN_HEAD_IM)
2769            } elsif ($token->{type} eq 'end tag') {                ## NOTE: There is a "as if in head" code clone.
2770              if ($token->{tag_name} eq 'head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2771                if ($self->{open_elements}->[-1]->[1] eq 'head') {                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2772                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2773                  }
2774                  $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
2775                  pop @{$self->{open_elements}}
2776                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2777                  redo B;
2778                } elsif ($token->{tag_name} eq 'noscript') {
2779                  if ($self->{insertion_mode} == IN_HEAD_IM) {
2780                    ## NOTE: and scripting is disalbed
2781                    !!!insert-element ($token->{tag_name}, $token->{attributes});
2782                    $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
2783                    !!!next-token;
2784                    redo B;
2785                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2786                    !!!parse-error (type => 'in noscript:noscript');
2787                    ## Ignore the token
2788                    !!!next-token;
2789                    redo B;
2790                  } else {
2791                    #
2792                  }
2793                } elsif ($token->{tag_name} eq 'script') {
2794                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2795                    ## As if </noscript>
2796                    pop @{$self->{open_elements}};
2797                    !!!parse-error (type => 'in noscript:script');
2798                  
2799                    $self->{insertion_mode} = IN_HEAD_IM;
2800                    ## Reprocess in the "in head" insertion mode...
2801                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2802                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2803                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2804                  }
2805    
2806                  ## NOTE: There is a "as if in head" code clone.
2807                  $script_start_tag->($insert_to_current);
2808                  pop @{$self->{open_elements}}
2809                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2810                  redo B;
2811                } elsif ($token->{tag_name} eq 'body' or
2812                         $token->{tag_name} eq 'frameset') {
2813                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2814                    ## As if </noscript>
2815                    pop @{$self->{open_elements}};
2816                    !!!parse-error (type => 'in noscript:'.$token->{tag_name});
2817                    
2818                    ## Reprocess in the "in head" insertion mode...
2819                    ## As if </head>
2820                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2821                    
2822                    ## Reprocess in the "after head" insertion mode...
2823                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2824                    pop @{$self->{open_elements}};
2825                    
2826                    ## Reprocess in the "after head" insertion mode...
2827                  }
2828    
2829                  ## "after head" insertion mode
2830                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2831                  if ($token->{tag_name} eq 'body') {
2832                    $self->{insertion_mode} = IN_BODY_IM;
2833                  } elsif ($token->{tag_name} eq 'frameset') {
2834                    $self->{insertion_mode} = IN_FRAMESET_IM;
2835                } else {                } else {
2836                  !!!parse-error (type => 'unmatched end tag:head');                  die "$0: tag name: $self->{tag_name}";
2837                }                }
               $self->{insertion_mode} = 'after head';  
2838                !!!next-token;                !!!next-token;
2839                redo B;                redo B;
             } elsif ($token->{tag_name} eq 'html') {  
               #  
2840              } else {              } else {
2841                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                #
               ## Ignore the token  
               !!!next-token;  
               redo B;  
2842              }              }
           } else {  
             #  
           }  
2843    
2844            if ($self->{open_elements}->[-1]->[1] eq 'head') {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2845              ## As if </head>                ## As if </noscript>
2846              pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
2847            }                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
2848            $self->{insertion_mode} = 'after head';                
2849            ## reprocess                ## Reprocess in the "in head" insertion mode...
2850            redo B;                ## As if </head>
2851                  pop @{$self->{open_elements}};
2852    
2853            ## ISSUE: An issue in the spec.                ## Reprocess in the "after head" insertion mode...
2854          } elsif ($self->{insertion_mode} eq 'after head') {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2855            if ($token->{type} eq 'character') {                ## As if </head>
2856              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                pop @{$self->{open_elements}};
2857                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
2858                unless (length $token->{data}) {                ## Reprocess in the "after head" insertion mode...
                 !!!next-token;  
                 redo B;  
               }  
2859              }              }
               
             #  
           } 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});  
2860    
2861              !!!next-token;              ## "after head" insertion mode
2862              redo B;              ## As if <body>
2863            } elsif ($token->{type} eq 'comment') {              !!!insert-element ('body');
2864              ## NOTE: There is a code clone of "comment in body".              $self->{insertion_mode} = IN_BODY_IM;
2865              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);  
2866              redo B;              redo B;
2867            }            } elsif ($token->{type} == END_TAG_TOKEN) {
2868          } elsif ($self->{insertion_mode} eq 'in table') {              if ($token->{tag_name} eq 'head') {
2869            if ($token->{type} eq 'character') {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2870              ## NOTE: There are "character in table" code clones.                  ## As if <head>
2871              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                  !!!create-element ($self->{head_element}, 'head');
2872                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2873                                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2874                unless (length $token->{data}) {  
2875                    ## Reprocess in the "in head" insertion mode...
2876                    pop @{$self->{open_elements}};
2877                    $self->{insertion_mode} = AFTER_HEAD_IM;
2878                    !!!next-token;
2879                    redo B;
2880                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2881                    ## As if </noscript>
2882                    pop @{$self->{open_elements}};
2883                    !!!parse-error (type => 'in noscript:script');
2884                    
2885                    ## Reprocess in the "in head" insertion mode...
2886                    pop @{$self->{open_elements}};
2887                    $self->{insertion_mode} = AFTER_HEAD_IM;
2888                    !!!next-token;
2889                    redo B;
2890                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2891                    pop @{$self->{open_elements}};
2892                    $self->{insertion_mode} = AFTER_HEAD_IM;
2893                  !!!next-token;                  !!!next-token;
2894                  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});  
2895                } else {                } else {
2896                  $foster_parent_element->insert_before                  #
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
2897                }                }
2898              } else {              } elsif ($token->{tag_name} eq 'noscript') {
2899                $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]);  
2900                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2901                    $self->{insertion_mode} = IN_HEAD_IM;
2902                    !!!next-token;
2903                    redo B;
2904                  } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2905                    !!!parse-error (type => 'unmatched end tag:noscript');
2906                    ## Ignore the token ## ISSUE: An issue in the spec.
2907                    !!!next-token;
2908                    redo B;
2909                  } else {
2910                    #
2911                }                }
   
               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;  
2912              } elsif ({              } elsif ({
2913                        col => 1,                        body => 1, html => 1,
                       td => 1, th => 1, tr => 1,  
2914                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
2915                ## Clear back to table context                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2916                while ($self->{open_elements}->[-1]->[1] ne 'table' and                  ## As if <head>
2917                       $self->{open_elements}->[-1]->[1] ne 'html') {                  !!!create-element ($self->{head_element}, 'head');
2918                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2919                  pop @{$self->{open_elements}};                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2920                }  
2921                    $self->{insertion_mode} = IN_HEAD_IM;
2922                !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');                  ## Reprocess in the "in head" insertion mode...
2923                $self->{insertion_mode} = $token->{tag_name} eq 'col'                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2924                  ? 'in column group' : 'in table body';                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2925                ## 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>  
2926                  !!!next-token;                  !!!next-token;
2927                  redo B;                  redo B;
2928                }                }
2929                                
2930                ## generate implied end tags                #
2931                if ({              } elsif ({
2932                     dd => 1, dt => 1, li => 1, p => 1,                        p => 1, br => 1,
2933                     td => 1, th => 1, tr => 1,                       }->{$token->{tag_name}}) {
2934                    }->{$self->{open_elements}->[-1]->[1]}) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2935                  !!!back-token; # <table>                  ## As if <head>
2936                  $token = {type => 'end tag', tag_name => 'table'};                  !!!create-element ($self->{head_element}, 'head');
2937                  !!!back-token;                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2938                  $token = {type => 'end tag',                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
2939    
2940                if ($self->{open_elements}->[-1]->[1] ne 'table') {                  $self->{insertion_mode} = IN_HEAD_IM;
2941                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  ## Reprocess in the "in head" insertion mode...
2942                }                }
2943    
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
2944                #                #
2945              }              } else {
2946            } elsif ($token->{type} eq 'end tag') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2947              if ($token->{tag_name} eq 'table') {                  #
2948                ## 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) {  
2949                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2950                  ## Ignore the token                  ## Ignore the token
2951                  !!!next-token;                  !!!next-token;
2952                  redo B;                  redo B;
2953                }                }
2954                              }
               ## 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]);  
               }  
2955    
2956                splice @{$self->{open_elements}}, $i;              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2957                  ## As if </noscript>
2958                  pop @{$self->{open_elements}};
2959                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
2960                  
2961                  ## Reprocess in the "in head" insertion mode...
2962                  ## As if </head>
2963                  pop @{$self->{open_elements}};
2964    
2965                $self->_reset_insertion_mode;                ## Reprocess in the "after head" insertion mode...
2966                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2967                  ## As if </head>
2968                  pop @{$self->{open_elements}};
2969    
2970                !!!next-token;                ## Reprocess in the "after head" insertion mode...
2971                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}}) {  
2972                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2973                ## Ignore the token                ## Ignore the token ## ISSUE: An issue in the spec.
2974                !!!next-token;                !!!next-token;
2975                redo B;                redo B;
             } else {  
               #  
2976              }              }
2977    
2978                ## "after head" insertion mode
2979                ## As if <body>
2980                !!!insert-element ('body');
2981                $self->{insertion_mode} = IN_BODY_IM;
2982                ## reprocess
2983                redo B;
2984            } else {            } else {
2985              #              die "$0: $token->{type}: Unknown token type";
2986            }            }
2987    
2988            !!!parse-error (type => 'in table:'.$token->{tag_name});            ## ISSUE: An issue in the spec.
2989            $in_body->($insert_to_foster);      } elsif ($self->{insertion_mode} == IN_BODY_IM or
2990            redo B;               $self->{insertion_mode} == IN_CELL_IM or
2991          } elsif ($self->{insertion_mode} eq 'in caption') {               $self->{insertion_mode} == IN_CAPTION_IM) {
2992            if ($token->{type} eq 'character') {            if ($token->{type} == CHARACTER_TOKEN) {
2993              ## NOTE: This is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
2994              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
2995                            
2996              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
2997    
2998              !!!next-token;              !!!next-token;
2999              redo B;              redo B;
3000            } 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') {  
3001              if ({              if ({
3002                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
3003                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
3004                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3005                !!!parse-error (type => 'not closed:caption');                if ($self->{insertion_mode} == IN_CELL_IM) {
3006                    ## have an element in table scope
3007                    my $tn;
3008                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3009                      my $node = $self->{open_elements}->[$_];
3010                      if ($node->[1] eq 'td' or $node->[1] eq 'th') {
3011                        $tn = $node->[1];
3012                        last INSCOPE;
3013                      } elsif ({
3014                                table => 1, html => 1,
3015                               }->{$node->[1]}) {
3016                        last INSCOPE;
3017                      }
3018                    } # INSCOPE
3019                      unless (defined $tn) {
3020                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3021                        ## Ignore the token
3022                        !!!next-token;
3023                        redo B;
3024                      }
3025                    
3026                    ## Close the cell
3027                    !!!back-token; # <?>
3028                    $token = {type => END_TAG_TOKEN, tag_name => $tn};
3029                    redo B;
3030                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3031                    !!!parse-error (type => 'not closed:caption');
3032                    
3033                    ## As if </caption>
3034                    ## have a table element in table scope
3035                    my $i;
3036                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3037                      my $node = $self->{open_elements}->[$_];
3038                      if ($node->[1] eq 'caption') {
3039                        $i = $_;
3040                        last INSCOPE;
3041                      } elsif ({
3042                                table => 1, html => 1,
3043                               }->{$node->[1]}) {
3044                        last INSCOPE;
3045                      }
3046                    } # INSCOPE
3047                      unless (defined $i) {
3048                        !!!parse-error (type => 'unmatched end tag:caption');
3049                        ## Ignore the token
3050                        !!!next-token;
3051                        redo B;
3052                      }
3053                    
3054                    ## generate implied end tags
3055                    if ({
3056                         dd => 1, dt => 1, li => 1, p => 1,
3057                         td => 1, th => 1, tr => 1,
3058                         tbody => 1, tfoot=> 1, thead => 1,
3059                        }->{$self->{open_elements}->[-1]->[1]}) {
3060                      !!!back-token; # <?>
3061                      $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3062                      !!!back-token;
3063                      $token = {type => END_TAG_TOKEN,
3064                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3065                      redo B;
3066                    }
3067    
3068                ## As if </caption>                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3069                ## 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;  
3070                  }                  }
3071                } # INSCOPE                  
3072                unless (defined $i) {                  splice @{$self->{open_elements}}, $i;
3073                  !!!parse-error (type => 'unmatched end tag:caption');                  
3074                    $clear_up_to_marker->();
3075                    
3076                    $self->{insertion_mode} = IN_TABLE_IM;
3077                    
3078                    ## reprocess
3079                    redo B;
3080                  } else {
3081                    #
3082                  }
3083                } else {
3084                  #
3085                }
3086              } elsif ($token->{type} == END_TAG_TOKEN) {
3087                if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
3088                  if ($self->{insertion_mode} == IN_CELL_IM) {
3089                    ## have an element in table scope
3090                    my $i;
3091                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3092                      my $node = $self->{open_elements}->[$_];
3093                      if ($node->[1] eq $token->{tag_name}) {
3094                        $i = $_;
3095                        last INSCOPE;
3096                      } elsif ({
3097                                table => 1, html => 1,
3098                               }->{$node->[1]}) {
3099                        last INSCOPE;
3100                      }
3101                    } # INSCOPE
3102                      unless (defined $i) {
3103                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3104                        ## Ignore the token
3105                        !!!next-token;
3106                        redo B;
3107                      }
3108                    
3109                    ## generate implied end tags
3110                    if ({
3111                         dd => 1, dt => 1, li => 1, p => 1,
3112                         td => ($token->{tag_name} eq 'th'),
3113                         th => ($token->{tag_name} eq 'td'),
3114                         tr => 1,
3115                         tbody => 1, tfoot=> 1, thead => 1,
3116                        }->{$self->{open_elements}->[-1]->[1]}) {
3117                      !!!back-token;
3118                      $token = {type => END_TAG_TOKEN,
3119                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3120                      redo B;
3121                    }
3122                    
3123                    if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
3124                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3125                    }
3126                    
3127                    splice @{$self->{open_elements}}, $i;
3128                    
3129                    $clear_up_to_marker->();
3130                    
3131                    $self->{insertion_mode} = IN_ROW_IM;
3132                    
3133                    !!!next-token;
3134                    redo B;
3135                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3136                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3137                  ## Ignore the token                  ## Ignore the token
3138                  !!!next-token;                  !!!next-token;
3139                  redo B;                  redo B;
3140                  } else {
3141                    #
3142                }                }
3143                              } elsif ($token->{tag_name} eq 'caption') {
3144                ## generate implied end tags                if ($self->{insertion_mode} == IN_CAPTION_IM) {
3145                if ({                  ## have a table element in table scope
3146                     dd => 1, dt => 1, li => 1, p => 1,                  my $i;
3147                     td => 1, th => 1, tr => 1,                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3148                    }->{$self->{open_elements}->[-1]->[1]}) {                    my $node = $self->{open_elements}->[$_];
3149                  !!!back-token; # <?>                    if ($node->[1] eq $token->{tag_name}) {
3150                  $token = {type => 'end tag', tag_name => 'caption'};                      $i = $_;
3151                  !!!back-token;                      last INSCOPE;
3152                  $token = {type => 'end tag',                    } elsif ({
3153                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                              table => 1, html => 1,
3154                               }->{$node->[1]}) {
3155                        last INSCOPE;
3156                      }
3157                    } # INSCOPE
3158                      unless (defined $i) {
3159                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3160                        ## Ignore the token
3161                        !!!next-token;
3162                        redo B;
3163                      }
3164                    
3165                    ## generate implied end tags
3166                    if ({
3167                         dd => 1, dt => 1, li => 1, p => 1,
3168                         td => 1, th => 1, tr => 1,
3169                         tbody => 1, tfoot=> 1, thead => 1,
3170                        }->{$self->{open_elements}->[-1]->[1]}) {
3171                      !!!back-token;
3172                      $token = {type => END_TAG_TOKEN,
3173                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3174                      redo B;
3175                    }
3176                    
3177                    if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3178                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3179                    }
3180                    
3181                    splice @{$self->{open_elements}}, $i;
3182                    
3183                    $clear_up_to_marker->();
3184                    
3185                    $self->{insertion_mode} = IN_TABLE_IM;
3186                    
3187                    !!!next-token;
3188                  redo B;                  redo B;
3189                  } elsif ($self->{insertion_mode} == IN_CELL_IM) {
3190                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3191                    ## Ignore the token
3192                    !!!next-token;
3193                    redo B;
3194                  } else {
3195                    #
3196                }                }
3197                } elsif ({
3198                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                        table => 1, tbody => 1, tfoot => 1,
3199                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                        thead => 1, tr => 1,
3200                }                       }->{$token->{tag_name}} and
3201                         $self->{insertion_mode} == IN_CELL_IM) {
3202                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  
3203                my $i;                my $i;
3204                  my $tn;
3205                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3206                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3207                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
3208                    $i = $_;                    $i = $_;
3209                    last INSCOPE;                    last INSCOPE;
3210                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {
3211                      $tn = $node->[1];
3212                      ## NOTE: There is exactly one |td| or |th| element
3213                      ## in scope in the stack of open elements by definition.
3214                  } elsif ({                  } elsif ({
3215                            table => 1, html => 1,                            table => 1, html => 1,
3216                           }->{$node->[1]}) {                           }->{$node->[1]}) {
# Line 3475  sub _tree_construction_main ($) { Line 3223  sub _tree_construction_main ($) {
3223                  !!!next-token;                  !!!next-token;
3224                  redo B;                  redo B;
3225                }                }
                 
               ## 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]);  
               }  
3226    
3227                splice @{$self->{open_elements}}, $i;                ## Close the cell
3228                  !!!back-token; # </?>
3229                $clear_up_to_marker->();                $token = {type => END_TAG_TOKEN, tag_name => $tn};
   
               $self->{insertion_mode} = 'in table';  
   
               !!!next-token;  
3230                redo B;                redo B;
3231              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table' and
3232                         $self->{insertion_mode} == IN_CAPTION_IM) {
3233                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption');
3234    
3235                ## As if </caption>                ## As if </caption>
# Line 3527  sub _tree_construction_main ($) { Line 3257  sub _tree_construction_main ($) {
3257                if ({                if ({
3258                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3259                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3260                       tbody => 1, tfoot=> 1, thead => 1,
3261                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3262                  !!!back-token; # </table>                  !!!back-token; # </table>
3263                  $token = {type => 'end tag', tag_name => 'caption'};                  $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3264                  !!!back-token;                  !!!back-token;
3265                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3266                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3267                  redo B;                  redo B;
3268                }                }
# Line 3544  sub _tree_construction_main ($) { Line 3275  sub _tree_construction_main ($) {
3275    
3276                $clear_up_to_marker->();                $clear_up_to_marker->();
3277    
3278                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
3279    
3280                ## reprocess                ## reprocess
3281                redo B;                redo B;
3282              } elsif ({              } elsif ({
3283                        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,  
3284                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3285                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} == IN_CELL_IM or
3286                ## Ignore the token                    $self->{insertion_mode} == IN_CAPTION_IM) {
3287                redo B;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
             } else {  
               #  
             }  
           } else {  
             #  
           }  
                 
           $in_body->($insert_to_current);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in column group') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
           } elsif ($token->{type} eq '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');  
3288                  ## Ignore the token                  ## Ignore the token
3289                  !!!next-token;                  !!!next-token;
3290                  redo B;                  redo B;
3291                } else {                } else {
3292                  pop @{$self->{open_elements}}; # colgroup                  #
                 $self->{insertion_mode} = 'in table';  
                 !!!next-token;  
                 redo B;              
3293                }                }
3294              } elsif ($token->{tag_name} eq 'col') {              } elsif ({
3295                !!!parse-error (type => 'unmatched end tag:col');                        tbody => 1, tfoot => 1,
3296                          thead => 1, tr => 1,
3297                         }->{$token->{tag_name}} and
3298                         $self->{insertion_mode} == IN_CAPTION_IM) {
3299                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3300                ## Ignore the token                ## Ignore the token
3301                !!!next-token;                !!!next-token;
3302                redo B;                redo B;
3303              } else {              } else {
3304                #                #
3305              }              }
3306            } else {        } else {
3307              #          die "$0: $token->{type}: Unknown token type";
3308            }        }
3309    
3310            ## As if </colgroup>        $insert = $insert_to_current;
3311            if ($self->{open_elements}->[-1]->[1] eq 'html') {        #
3312              !!!parse-error (type => 'unmatched end tag:colgroup');      } elsif ($self->{insertion_mode} == IN_ROW_IM or
3313              ## Ignore the token               $self->{insertion_mode} == IN_TABLE_BODY_IM or
3314              !!!next-token;               $self->{insertion_mode} == IN_TABLE_IM) {
3315              redo B;            if ($token->{type} == CHARACTER_TOKEN) {
3316            } else {              ## NOTE: There are "character in table" code clones.
             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.  
3317              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3318                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3319                                
# Line 3646  sub _tree_construction_main ($) { Line 3330  sub _tree_construction_main ($) {
3330              ## into the current node" while characters might not be              ## into the current node" while characters might not be
3331              ## result in a new Text node.              ## result in a new Text node.
3332              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
3333                
3334              if ({              if ({
3335                   table => 1, tbody => 1, tfoot => 1,                   table => 1, tbody => 1, tfoot => 1,
3336                   thead => 1, tr => 1,                   thead => 1, tr => 1,
# Line 3686  sub _tree_construction_main ($) { Line 3370  sub _tree_construction_main ($) {
3370                            
3371              !!!next-token;              !!!next-token;
3372              redo B;              redo B;
3373            } 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') {  
3374              if ({              if ({
3375                   tr => 1,                   tr => ($self->{insertion_mode} != IN_ROW_IM),
3376                   th => 1, td => 1,                   th => 1, td => 1,
3377                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3378                unless ($token->{tag_name} eq 'tr') {                if ($self->{insertion_mode} == IN_TABLE_IM) {
3379                  !!!parse-error (type => 'missing start tag:tr');                  ## Clear back to table context
3380                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3381                           $self->{open_elements}->[-1]->[1] ne 'html') {
3382                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3383                      pop @{$self->{open_elements}};
3384                    }
3385                    
3386                    !!!insert-element ('tbody');
3387                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3388                    ## reprocess in the "in table body" insertion mode...
3389                }                }
3390    
3391                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3392                    unless ($token->{tag_name} eq 'tr') {
3393                      !!!parse-error (type => 'missing start tag:tr');
3394                    }
3395                    
3396                    ## Clear back to table body context
3397                    while (not {
3398                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3399                    }->{$self->{open_elements}->[-1]->[1]}) {
3400                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3401                      pop @{$self->{open_elements}};
3402                    }
3403                    
3404                    $self->{insertion_mode} = IN_ROW_IM;
3405                    if ($token->{tag_name} eq 'tr') {
3406                      !!!insert-element ($token->{tag_name}, $token->{attributes});
3407                      !!!next-token;
3408                      redo B;
3409                    } else {
3410                      !!!insert-element ('tr');
3411                      ## reprocess in the "in row" insertion mode
3412                    }
3413                  }
3414    
3415                  ## Clear back to table row context
3416                while (not {                while (not {
3417                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tr => 1, html => 1,
3418                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
3419                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3420                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3421                }                }
3422                                
3423                $self->{insertion_mode} = 'in row';                !!!insert-element ($token->{tag_name}, $token->{attributes});
3424                if ($token->{tag_name} eq 'tr') {                $self->{insertion_mode} = IN_CELL_IM;
3425                  !!!insert-element ($token->{tag_name}, $token->{attributes});  
3426                  !!!next-token;                push @$active_formatting_elements, ['#marker', ''];
3427                } else {                
3428                  !!!insert-element ('tr');                !!!next-token;
                 ## reprocess  
               }  
3429                redo B;                redo B;
3430              } elsif ({              } elsif ({
3431                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
3432                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
3433                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3434                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3435                ## have an element in table scope                if ($self->{insertion_mode} == IN_ROW_IM) {
3436                my $i;                  ## As if </tr>
3437                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
3438                  my $node = $self->{open_elements}->[$_];                  my $i;
3439                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3440                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
3441                      }->{$node->[1]}) {                    if ($node->[1] eq 'tr') {
3442                    $i = $_;                      $i = $_;
3443                    last INSCOPE;                      last INSCOPE;
3444                  } elsif ({                    } elsif ({
3445                            table => 1, html => 1,                              table => 1, html => 1,
3446                           }->{$node->[1]}) {                             }->{$node->[1]}) {
3447                    last INSCOPE;                      last INSCOPE;
3448                      }
3449                    } # INSCOPE
3450                    unless (defined $i) {
3451                      !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});
3452                      ## Ignore the token
3453                      !!!next-token;
3454                      redo B;
3455                    }
3456                    
3457                    ## Clear back to table row context
3458                    while (not {
3459                      tr => 1, html => 1,
3460                    }->{$self->{open_elements}->[-1]->[1]}) {
3461                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3462                      pop @{$self->{open_elements}};
3463                    }
3464                    
3465                    pop @{$self->{open_elements}}; # tr
3466                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3467                    if ($token->{tag_name} eq 'tr') {
3468                      ## reprocess
3469                      redo B;
3470                    } else {
3471                      ## reprocess in the "in table body" insertion mode...
3472                  }                  }
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
3473                }                }
3474    
3475                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3476                while (not {                  ## have an element in table scope
3477                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  my $i;
3478                }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3479                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    my $node = $self->{open_elements}->[$_];
3480                      if ({
3481                           tbody => 1, thead => 1, tfoot => 1,
3482                          }->{$node->[1]}) {
3483                        $i = $_;
3484                        last INSCOPE;
3485                      } elsif ({
3486                                table => 1, html => 1,
3487                               }->{$node->[1]}) {
3488                        last INSCOPE;
3489                      }
3490                    } # INSCOPE
3491                    unless (defined $i) {
3492                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3493                      ## Ignore the token
3494                      !!!next-token;
3495                      redo B;
3496                    }
3497    
3498                    ## Clear back to table body context
3499                    while (not {
3500                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3501                    }->{$self->{open_elements}->[-1]->[1]}) {
3502                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3503                      pop @{$self->{open_elements}};
3504                    }
3505                    
3506                    ## As if <{current node}>
3507                    ## have an element in table scope
3508                    ## true by definition
3509                    
3510                    ## Clear back to table body context
3511                    ## nop by definition
3512                    
3513                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3514                    $self->{insertion_mode} = IN_TABLE_IM;
3515                    ## reprocess in "in table" insertion mode...
3516                }                }
3517    
3518                ## As if <{current node}>                if ($token->{tag_name} eq 'col') {
3519                ## have an element in table scope                  ## Clear back to table context
3520                ## true by definition                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
3521                           $self->{open_elements}->[-1]->[1] ne 'html') {
3522                ## Clear back to table body context                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3523                ## nop by definition                    pop @{$self->{open_elements}};
3524                    }
3525                pop @{$self->{open_elements}};                  
3526                $self->{insertion_mode} = 'in table';                  !!!insert-element ('colgroup');
3527                ## reprocess                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
3528                redo B;                  ## reprocess
3529                    redo B;
3530                  } elsif ({
3531                            caption => 1,
3532                            colgroup => 1,
3533                            tbody => 1, tfoot => 1, thead => 1,
3534                           }->{$token->{tag_name}}) {
3535                    ## Clear back to table context
3536                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3537                           $self->{open_elements}->[-1]->[1] ne 'html') {
3538                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3539                      pop @{$self->{open_elements}};
3540                    }
3541                    
3542                    push @$active_formatting_elements, ['#marker', '']
3543                        if $token->{tag_name} eq 'caption';
3544                    
3545                    !!!insert-element ($token->{tag_name}, $token->{attributes});
3546                    $self->{insertion_mode} = {
3547                                               caption => IN_CAPTION_IM,
3548                                               colgroup => IN_COLUMN_GROUP_IM,
3549                                               tbody => IN_TABLE_BODY_IM,
3550                                               tfoot => IN_TABLE_BODY_IM,
3551                                               thead => IN_TABLE_BODY_IM,
3552                                              }->{$token->{tag_name}};
3553                    !!!next-token;
3554                    redo B;
3555                  } else {
3556                    die "$0: in table: <>: $token->{tag_name}";
3557                  }
3558              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
3559                ## NOTE: This is a code clone of "table in table"                ## NOTE: There are code clones for this "table in table"
3560                !!!parse-error (type => 'not closed:table');                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3561    
3562                ## As if </table>                ## As if </table>
3563                ## have a table element in table scope                ## have a table element in table scope
# Line 3792  sub _tree_construction_main ($) { Line 3584  sub _tree_construction_main ($) {
3584                if ({                if ({
3585                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3586                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3587                       tbody => 1, tfoot=> 1, thead => 1,
3588                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3589                  !!!back-token; # <table>                  !!!back-token; # <table>
3590                  $token = {type => 'end tag', tag_name => 'table'};                  $token = {type => END_TAG_TOKEN, tag_name => 'table'};
3591                  !!!back-token;                  !!!back-token;
3592                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3593                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3594                  redo B;                  redo B;
3595                }                }
# Line 3807  sub _tree_construction_main ($) { Line 3600  sub _tree_construction_main ($) {
3600    
3601                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
3602    
3603                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
3604    
3605                ## reprocess                ## reprocess
3606                redo B;                redo B;
3607              } else {              } else {
3608                #                #
3609              }              }
3610            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
3611              if ({              if ($token->{tag_name} eq 'tr' and
3612                   tbody => 1, tfoot => 1, thead => 1,                  $self->{insertion_mode} == IN_ROW_IM) {
                 }->{$token->{tag_name}}) {  
3613                ## have an element in table scope                ## have an element in table scope
3614                my $i;                my $i;
3615                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3838  sub _tree_construction_main ($) { Line 3630  sub _tree_construction_main ($) {
3630                  redo B;                  redo B;
3631                }                }
3632    
3633                ## Clear back to table body context                ## Clear back to table row context
3634                while (not {                while (not {
3635                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tr => 1, html => 1,
3636                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
3637                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3638                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3639                }                }
3640    
3641                pop @{$self->{open_elements}};                pop @{$self->{open_elements}}; # tr
3642                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
3643                !!!next-token;                !!!next-token;
3644                redo B;                redo B;
3645              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
3646                ## have an element in table scope                if ($self->{insertion_mode} == IN_ROW_IM) {
3647                my $i;                  ## As if </tr>
3648                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
3649                  my $node = $self->{open_elements}->[$_];                  my $i;
3650                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3651                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
3652                      }->{$node->[1]}) {                    if ($node->[1] eq 'tr') {
3653                    $i = $_;                      $i = $_;
3654                    last INSCOPE;                      last INSCOPE;
3655                  } elsif ({                    } elsif ({
3656                            table => 1, html => 1,                              table => 1, html => 1,
3657                           }->{$node->[1]}) {                             }->{$node->[1]}) {
3658                    last INSCOPE;                      last INSCOPE;
3659                      }
3660                    } # INSCOPE
3661                    unless (defined $i) {
3662                      !!!parse-error (type => 'unmatched end tag:'.$token->{type});
3663                      ## Ignore the token
3664                      !!!next-token;
3665                      redo B;
3666                  }                  }
3667                } # INSCOPE                  
3668                unless (defined $i) {                  ## Clear back to table row context
3669                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  while (not {
3670                  ## 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,  
3671                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
3672                # MUST                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3673                my $foster_parent_element;                    pop @{$self->{open_elements}};
3674                my $next_sibling;                  }
3675                my $prev_sibling;                  
3676                OE: for (reverse 0..$#{$self->{open_elements}}) {                  pop @{$self->{open_elements}}; # tr
3677                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3678                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                  ## reprocess in the "in table body" insertion mode...
3679                    if (defined $parent and $parent->node_type == 1) {                }
3680                      $foster_parent_element = $parent;  
3681                      $next_sibling = $self->{open_elements}->[$_]->[0];                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3682                      $prev_sibling = $next_sibling->previous_sibling;                  ## have an element in table scope
3683                    } else {                  my $i;
3684                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3685                      $prev_sibling = $foster_parent_element->last_child;                    my $node = $self->{open_elements}->[$_];
3686                      if ({
3687                           tbody => 1, thead => 1, tfoot => 1,
3688                          }->{$node->[1]}) {
3689                        $i = $_;
3690                        last INSCOPE;
3691                      } elsif ({
3692                                table => 1, html => 1,
3693                               }->{$node->[1]}) {
3694                        last INSCOPE;
3695                    }                    }
3696                    last OE;                  } # INSCOPE
3697                    unless (defined $i) {
3698                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3699                      ## Ignore the token
3700                      !!!next-token;
3701                      redo B;
3702                  }                  }
3703                } # OE                  
3704                $foster_parent_element = $self->{open_elements}->[0]->[0] and                  ## Clear back to table body context
3705                $prev_sibling = $foster_parent_element->last_child                  while (not {
3706                  unless defined $foster_parent_element;                    tbody => 1, tfoot => 1, thead => 1, html => 1,
3707                if (defined $prev_sibling and                  }->{$self->{open_elements}->[-1]->[1]}) {
3708                    $prev_sibling->node_type == 3) {                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3709                  $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;  
3710                  }                  }
3711                } # INSCOPE                  
3712                unless (defined $i) {                  ## As if <{current node}>
3713                  !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                  ## have an element in table scope
3714                  ## Ignore the token                  ## true by definition
3715                  !!!next-token;                  
3716                  redo B;                  ## Clear back to table body context
3717                }                  ## nop by definition
3718                    
               ## 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]);  
3719                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3720                    $self->{insertion_mode} = IN_TABLE_IM;
3721                    ## reprocess in the "in table" insertion mode...
3722                }                }
3723    
               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>  
3724                ## have a table element in table scope                ## have a table element in table scope
3725                my $i;                my $i;
3726                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3727                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3728                  if ($node->[1] eq 'table') {                  if ($node->[1] eq $token->{tag_name}) {
3729                    $i = $_;                    $i = $_;
3730                    last INSCOPE;                    last INSCOPE;
3731                  } elsif ({                  } elsif ({
# Line 4050  sub _tree_construction_main ($) { Line 3735  sub _tree_construction_main ($) {
3735                  }                  }
3736                } # INSCOPE                } # INSCOPE
3737                unless (defined $i) {                unless (defined $i) {
3738                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3739                  ## Ignore tokens </table><table>                  ## Ignore the token
3740                  !!!next-token;                  !!!next-token;
3741                  redo B;                  redo B;
3742                }                }
3743                  
3744                ## generate implied end tags                ## generate implied end tags
3745                if ({                if ({
3746                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3747                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3748                       tbody => 1, tfoot=> 1, thead => 1,
3749                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
3750                  !!!back-token;                  !!!back-token;
3751                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3752                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3753                  redo B;                  redo B;
3754                }                }
3755                  
3756                if ($self->{open_elements}->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
3757                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3758                }                }
3759                    
3760                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
3761                  
3762                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
3763                  
               ## 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';  
3764                !!!next-token;                !!!next-token;
3765                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;  
3766              } elsif ({              } elsif ({
3767                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
3768                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}} and
3769                ## have an element in table scope                       ($self->{insertion_mode} == IN_ROW_IM or
3770                my $i;                        $self->{insertion_mode} == IN_TABLE_BODY_IM)) {
3771                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                if ($self->{insertion_mode} == IN_ROW_IM) {
3772                  my $node = $self->{open_elements}->[$_];                  ## have an element in table scope
3773                  if ($node->[1] eq $token->{tag_name}) {                  my $i;
3774                    $i = $_;                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3775                    last INSCOPE;                    my $node = $self->{open_elements}->[$_];
3776                  } elsif ({                    if ($node->[1] eq $token->{tag_name}) {
3777                            table => 1, html => 1,                      $i = $_;
3778                           }->{$node->[1]}) {                      last INSCOPE;
3779                    last INSCOPE;                    } elsif ({
3780                                table => 1, html => 1,
3781                               }->{$node->[1]}) {
3782                        last INSCOPE;
3783                      }
3784                    } # INSCOPE
3785                      unless (defined $i) {
3786                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3787                        ## Ignore the token
3788                        !!!next-token;
3789                        redo B;
3790                      }
3791                    
3792                    ## As if </tr>
3793                    ## have an element in table scope
3794                    my $i;
3795                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3796                      my $node = $self->{open_elements}->[$_];
3797                      if ($node->[1] eq 'tr') {
3798                        $i = $_;
3799                        last INSCOPE;
3800                      } elsif ({
3801                                table => 1, html => 1,
3802                               }->{$node->[1]}) {
3803                        last INSCOPE;
3804                      }
3805                    } # INSCOPE
3806                      unless (defined $i) {
3807                        !!!parse-error (type => 'unmatched end tag:tr');
3808                        ## Ignore the token
3809                        !!!next-token;
3810                        redo B;
3811                      }
3812                    
3813                    ## Clear back to table row context
3814                    while (not {
3815                      tr => 1, html => 1,
3816                    }->{$self->{open_elements}->[-1]->[1]}) {
3817                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3818                      pop @{$self->{open_elements}};
3819                  }                  }
3820                } # INSCOPE                  
3821                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
3822                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3823                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
3824                }                }
3825    
               ## As if </tr>  
3826                ## have an element in table scope                ## have an element in table scope
3827                my $i;                my $i;
3828                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3829                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3830                  if ($node->[1] eq 'tr') {                  if ($node->[1] eq $token->{tag_name}) {
3831                    $i = $_;                    $i = $_;
3832                    last INSCOPE;                    last INSCOPE;
3833                  } elsif ({                  } elsif ({
# Line 4188  sub _tree_construction_main ($) { Line 3837  sub _tree_construction_main ($) {
3837                  }                  }
3838                } # INSCOPE                } # INSCOPE
3839                unless (defined $i) {                unless (defined $i) {
3840                  !!!parse-error (type => 'unmatched end tag:tr');                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3841                  ## Ignore the token                  ## Ignore the token
3842                  !!!next-token;                  !!!next-token;
3843                  redo B;                  redo B;
3844                }                }
3845    
3846                ## Clear back to table row context                ## Clear back to table body context
3847                while (not {                while (not {
3848                  tr => 1, html => 1,                  tbody => 1, tfoot => 1, thead => 1, html => 1,
3849                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
3850                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3851                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3852                }                }
3853    
3854                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}};
3855                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_IM;
3856                ## reprocess                !!!next-token;
3857                redo B;                redo B;
3858              } elsif ({              } elsif ({
3859                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
3860                        colgroup => 1, html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
3861                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3862                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
3863                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3864                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3865                ## Ignore the token                ## Ignore the token
# Line 4218  sub _tree_construction_main ($) { Line 3869  sub _tree_construction_main ($) {
3869                #                #
3870              }              }
3871            } else {            } else {
3872              #              die "$0: $token->{type}: Unknown token type";
3873            }            }
3874    
3875            ## As if in table        !!!parse-error (type => 'in table:'.$token->{tag_name});
           !!!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});  
3876    
3877              !!!next-token;        $insert = $insert_to_foster;
3878              redo B;        #
3879            } elsif ($token->{type} eq 'comment') {      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
3880              ## NOTE: This is a code clone of "comment in body".            if ($token->{type} == CHARACTER_TOKEN) {
3881              my $comment = $self->{document}->create_comment ($token->{data});              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3882              $self->{open_elements}->[-1]->[0]->append_child ($comment);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3883              !!!next-token;                unless (length $token->{data}) {
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  caption => 1, col => 1, colgroup => 1,  
                  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  
3884                  !!!next-token;                  !!!next-token;
3885                  redo B;                  redo B;
3886                }                }
3887                }
3888                ## Close the cell              
3889                !!!back-token; # <?>              #
3890                $token = {type => 'end tag', tag_name => $tn};            } elsif ($token->{type} == START_TAG_TOKEN) {
3891                if ($token->{tag_name} eq 'col') {
3892                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3893                  pop @{$self->{open_elements}};
3894                  !!!next-token;
3895                redo B;                redo B;
3896              } else {              } else {
3897                #                #
3898              }              }
3899            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
3900              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'colgroup') {
3901                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] eq 'html') {
3902                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});  
3903                  ## Ignore the token                  ## Ignore the token
3904                  !!!next-token;                  !!!next-token;
3905                  redo B;                  redo B;
3906                  } else {
3907                    pop @{$self->{open_elements}}; # colgroup
3908                    $self->{insertion_mode} = IN_TABLE_IM;
3909                    !!!next-token;
3910                    redo B;            
3911                }                }
3912                              } elsif ($token->{tag_name} eq 'col') {
3913                ## 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});  
3914                ## Ignore the token                ## Ignore the token
3915                !!!next-token;                !!!next-token;
3916                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;  
3917              } else {              } else {
3918                #                #
3919              }              }
3920            } else {            } else {
3921              #              #
3922            }            }
3923              
3924            $in_body->($insert_to_current);            ## As if </colgroup>
3925            redo B;            if ($self->{open_elements}->[-1]->[1] eq 'html') {
3926          } elsif ($self->{insertion_mode} eq 'in select') {              !!!parse-error (type => 'unmatched end tag:colgroup');
3927            if ($token->{type} eq 'character') {              ## Ignore the token
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
3928              !!!next-token;              !!!next-token;
3929              redo B;              redo B;
3930            } elsif ($token->{type} eq 'comment') {            } else {
3931              my $comment = $self->{document}->create_comment ($token->{data});              pop @{$self->{open_elements}}; # colgroup
3932              $self->{open_elements}->[-1]->[0]->append_child ($comment);              $self->{insertion_mode} = IN_TABLE_IM;
3933                ## reprocess
3934                redo B;
3935              }
3936        } elsif ($self->{insertion_mode} == IN_SELECT_IM) {
3937              if ($token->{type} == CHARACTER_TOKEN) {
3938                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3939              !!!next-token;              !!!next-token;
3940              redo B;              redo B;
3941            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
3942              if ($token->{tag_name} eq 'option') {              if ($token->{tag_name} eq 'option') {
3943                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
3944                  ## As if </option>                  ## As if </option>
# Line 4436  sub _tree_construction_main ($) { Line 3994  sub _tree_construction_main ($) {
3994              } else {              } else {
3995                #                #
3996              }              }
3997            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
3998              if ($token->{tag_name} eq 'optgroup') {              if ($token->{tag_name} eq 'optgroup') {
3999                if ($self->{open_elements}->[-1]->[1] eq 'option' and                if ($self->{open_elements}->[-1]->[1] eq 'option' and
4000                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {
# Line 4549  sub _tree_construction_main ($) { Line 4107  sub _tree_construction_main ($) {
4107            ## Ignore the token            ## Ignore the token
4108            !!!next-token;            !!!next-token;
4109            redo B;            redo B;
4110          } elsif ($self->{insertion_mode} eq 'after body') {      } elsif ($self->{insertion_mode} == AFTER_BODY_IM or
4111            if ($token->{type} eq 'character') {               $self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4112              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        if ($token->{type} == CHARACTER_TOKEN) {
4113                ## As if in body          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4114                $reconstruct_active_formatting_elements->($insert_to_current);            my $data = $1;
4115              ## As if in body
4116              $reconstruct_active_formatting_elements->($insert_to_current);
4117                                
4118                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4119              
4120              unless (length $token->{data}) {
4121                !!!next-token;
4122                redo B;
4123              }
4124            }
4125            
4126            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4127              !!!parse-error (type => 'after html:#character');
4128    
4129                unless (length $token->{data}) {            ## Reprocess in the "main" phase, "after body" insertion mode...
4130                  !!!next-token;          }
4131                  redo B;          
4132                }          ## "after body" insertion mode
4133              }          !!!parse-error (type => 'after body:#character');
4134                
4135              #          $self->{insertion_mode} = IN_BODY_IM;
4136              !!!parse-error (type => 'after body:#'.$token->{type});          ## reprocess
4137            } elsif ($token->{type} eq 'comment') {          redo B;
4138              my $comment = $self->{document}->create_comment ($token->{data});        } elsif ($token->{type} == START_TAG_TOKEN) {
4139              $self->{open_elements}->[0]->[0]->append_child ($comment);          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4140              !!!parse-error (type => 'after html:'.$token->{tag_name});
4141              
4142              ## Reprocess in the "main" phase, "after body" insertion mode...
4143            }
4144    
4145            ## "after body" insertion mode
4146            !!!parse-error (type => 'after body:'.$token->{tag_name});
4147    
4148            $self->{insertion_mode} = IN_BODY_IM;
4149            ## reprocess
4150            redo B;
4151          } elsif ($token->{type} == END_TAG_TOKEN) {
4152            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4153              !!!parse-error (type => 'after html:/'.$token->{tag_name});
4154              
4155              $self->{insertion_mode} = AFTER_BODY_IM;
4156              ## Reprocess in the "main" phase, "after body" insertion mode...
4157            }
4158    
4159            ## "after body" insertion mode
4160            if ($token->{tag_name} eq 'html') {
4161              if (defined $self->{inner_html_node}) {
4162                !!!parse-error (type => 'unmatched end tag:html');
4163                ## Ignore the token
4164              !!!next-token;              !!!next-token;
4165              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});  
             }  
4166            } else {            } else {
4167              !!!parse-error (type => 'after body:#'.$token->{type});              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
4168                !!!next-token;
4169                redo B;
4170            }            }
4171            } else {
4172              !!!parse-error (type => 'after body:/'.$token->{tag_name});
4173    
4174            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = IN_BODY_IM;
4175            ## reprocess            ## reprocess
4176            redo B;            redo B;
4177          } elsif ($self->{insertion_mode} eq 'in frameset') {          }
4178            if ($token->{type} eq 'character') {        } else {
4179              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          die "$0: $token->{type}: Unknown token type";
4180                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});        }
4181        } elsif ($self->{insertion_mode} == IN_FRAMESET_IM or
4182                unless (length $token->{data}) {               $self->{insertion_mode} == AFTER_FRAMESET_IM or
4183                  !!!next-token;               $self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4184                  redo B;        if ($token->{type} == CHARACTER_TOKEN) {
4185                }          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4186              }            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4187              
4188              #            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);  
4189              !!!next-token;              !!!next-token;
4190              redo B;              redo B;
4191            } elsif ($token->{type} eq 'start tag') {            }
4192              if ($token->{tag_name} eq 'frameset') {          }
4193                !!!insert-element ($token->{tag_name}, $token->{attributes});          
4194                !!!next-token;          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
4195                redo B;            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4196              } elsif ($token->{tag_name} eq 'frame') {              !!!parse-error (type => 'in frameset:#character');
4197                !!!insert-element ($token->{tag_name}, $token->{attributes});            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
4198                pop @{$self->{open_elements}};              !!!parse-error (type => 'after frameset:#character');
4199                !!!next-token;            } else { # "after html frameset"
4200                redo B;              !!!parse-error (type => 'after html:#character');
4201              } elsif ($token->{tag_name} eq 'noframes') {  
4202                $in_body->($insert_to_current);              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4203                redo B;              ## Reprocess in the "main" phase, "after frameset"...
4204              } 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 {  
             #  
4205            }            }
4206                        
4207            if (defined $token->{tag_name}) {            ## Ignore the token.
4208              if (length $token->{data}) {
4209                ## reprocess the rest of characters
4210              } else {
4211                !!!next-token;
4212              }
4213              redo B;
4214            }
4215            
4216            die qq[$0: Character "$token->{data}"];
4217          } elsif ($token->{type} == START_TAG_TOKEN) {
4218            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4219              !!!parse-error (type => 'after html:'.$token->{tag_name});
4220    
4221              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4222              ## Process in the "main" phase, "after frameset" insertion mode...
4223            }
4224    
4225            if ($token->{tag_name} eq 'frameset' and
4226                $self->{insertion_mode} == IN_FRAMESET_IM) {
4227              !!!insert-element ($token->{tag_name}, $token->{attributes});
4228              !!!next-token;
4229              redo B;
4230            } elsif ($token->{tag_name} eq 'frame' and
4231                     $self->{insertion_mode} == IN_FRAMESET_IM) {
4232              !!!insert-element ($token->{tag_name}, $token->{attributes});
4233              pop @{$self->{open_elements}};
4234              !!!next-token;
4235              redo B;
4236            } elsif ($token->{tag_name} eq 'noframes') {
4237              ## NOTE: As if in body.
4238              $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
4239              redo B;
4240            } else {
4241              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4242              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset:'.$token->{tag_name});
4243            } else {            } else {
4244              !!!parse-error (type => 'in frameset:#'.$token->{type});              !!!parse-error (type => 'after frameset:'.$token->{tag_name});
4245            }            }
4246            ## Ignore the token            ## Ignore the token
4247            !!!next-token;            !!!next-token;
4248            redo B;            redo B;
4249          } elsif ($self->{insertion_mode} eq 'after frameset') {          }
4250            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_TAG_TOKEN) {
4251              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4252                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            !!!parse-error (type => 'after html:/'.$token->{tag_name});
4253    
4254                unless (length $token->{data}) {            $self->{insertion_mode} = AFTER_FRAMESET_IM;
4255                  !!!next-token;            ## Process in the "main" phase, "after frameset" insertion mode...
4256                  redo B;          }
               }  
             }  
4257    
4258              #          if ($token->{tag_name} eq 'frameset' and
4259            } elsif ($token->{type} eq 'comment') {              $self->{insertion_mode} == IN_FRAMESET_IM) {
4260              my $comment = $self->{document}->create_comment ($token->{data});            if ($self->{open_elements}->[-1]->[1] eq 'html' and
4261              $self->{open_elements}->[-1]->[0]->append_child ($comment);                @{$self->{open_elements}} == 1) {
4262                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4263                ## Ignore the token
4264              !!!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 {  
               #  
             }  
4265            } else {            } else {
4266              #              pop @{$self->{open_elements}};
4267                !!!next-token;
4268            }            }
4269              
4270            if (defined $token->{tag_name}) {            if (not defined $self->{inner_html_node} and
4271              !!!parse-error (type => 'after frameset:'.$token->{tag_name});                $self->{open_elements}->[-1]->[1] ne 'frameset') {
4272                $self->{insertion_mode} = AFTER_FRAMESET_IM;
4273              }
4274              redo B;
4275            } elsif ($token->{tag_name} eq 'html' and
4276                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
4277              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
4278              !!!next-token;
4279              redo B;
4280            } else {
4281              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4282                !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
4283            } else {            } else {
4284              !!!parse-error (type => 'after frameset:#'.$token->{type});              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
4285            }            }
4286            ## Ignore the token            ## Ignore the token
4287            !!!next-token;            !!!next-token;
4288            redo B;            redo B;
4289            }
4290          } else {
4291            die "$0: $token->{type}: Unknown token type";
4292          }
4293    
4294          ## ISSUE: An issue in spec here
4295        } else {
4296          die "$0: $self->{insertion_mode}: Unknown insertion mode";
4297        }
4298    
4299            ## ISSUE: An issue in spec there      ## "in body" insertion mode
4300        if ($token->{type} == START_TAG_TOKEN) {
4301          if ($token->{tag_name} eq 'script') {
4302            ## NOTE: This is an "as if in head" code clone
4303            $script_start_tag->($insert);
4304            redo B;
4305          } elsif ($token->{tag_name} eq 'style') {
4306            ## NOTE: This is an "as if in head" code clone
4307            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4308            redo B;
4309          } elsif ({
4310                    base => 1, link => 1,
4311                   }->{$token->{tag_name}}) {
4312            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4313            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4314            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4315            !!!next-token;
4316            redo B;
4317          } elsif ($token->{tag_name} eq 'meta') {
4318            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4319            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4320            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4321    
4322            unless ($self->{confident}) {
4323              my $charset;
4324              if ($token->{attributes}->{charset}) { ## TODO: And if supported
4325                $charset = $token->{attributes}->{charset}->{value};
4326              }
4327              if ($token->{attributes}->{'http-equiv'}) {
4328                ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
4329                if ($token->{attributes}->{'http-equiv'}->{value}
4330                    =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
4331                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4332                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4333                  $charset = defined $1 ? $1 : defined $2 ? $2 : $3;
4334                } ## TODO: And if supported
4335              }
4336              ## TODO: Change the encoding
4337            }
4338    
4339            !!!next-token;
4340            redo B;
4341          } elsif ($token->{tag_name} eq 'title') {
4342            !!!parse-error (type => 'in body:title');
4343            ## NOTE: This is an "as if in head" code clone
4344            $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {
4345              if (defined $self->{head_element}) {
4346                $self->{head_element}->append_child ($_[0]);
4347              } else {
4348                $insert->($_[0]);
4349              }
4350            });
4351            redo B;
4352          } elsif ($token->{tag_name} eq 'body') {
4353            !!!parse-error (type => 'in body:body');
4354                  
4355            if (@{$self->{open_elements}} == 1 or
4356                $self->{open_elements}->[1]->[1] ne 'body') {
4357              ## Ignore the token
4358          } else {          } else {
4359            die "$0: $self->{insertion_mode}: Unknown insertion mode";            my $body_el = $self->{open_elements}->[1]->[0];
4360              for my $attr_name (keys %{$token->{attributes}}) {
4361                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
4362                  $body_el->set_attribute_ns
4363                    (undef, [undef, $attr_name],
4364                     $token->{attributes}->{$attr_name}->{value});
4365                }
4366              }
4367          }          }
       }  
     } 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  
4368          !!!next-token;          !!!next-token;
4369          redo B;          redo B;
4370        } elsif ($token->{type} eq 'comment') {        } elsif ({
4371          my $comment = $self->{document}->create_comment ($token->{data});                  address => 1, blockquote => 1, center => 1, dir => 1,
4372          $self->{document}->append_child ($comment);                  div => 1, dl => 1, fieldset => 1, listing => 1,
4373                    menu => 1, ol => 1, p => 1, ul => 1,
4374                    pre => 1,
4375                   }->{$token->{tag_name}}) {
4376            ## has a p element in scope
4377            INSCOPE: for (reverse @{$self->{open_elements}}) {
4378              if ($_->[1] eq 'p') {
4379                !!!back-token;
4380                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4381                redo B;
4382              } elsif ({
4383                        table => 1, caption => 1, td => 1, th => 1,
4384                        button => 1, marquee => 1, object => 1, html => 1,
4385                       }->{$_->[1]}) {
4386                last INSCOPE;
4387              }
4388            } # INSCOPE
4389              
4390            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4391            if ($token->{tag_name} eq 'pre') {
4392              !!!next-token;
4393              if ($token->{type} == CHARACTER_TOKEN) {
4394                $token->{data} =~ s/^\x0A//;
4395                unless (length $token->{data}) {
4396                  !!!next-token;
4397                }
4398              }
4399            } else {
4400              !!!next-token;
4401            }
4402            redo B;
4403          } elsif ($token->{tag_name} eq 'form') {
4404            if (defined $self->{form_element}) {
4405              !!!parse-error (type => 'in form:form');
4406              ## Ignore the token
4407              !!!next-token;
4408              redo B;
4409            } else {
4410              ## has a p element in scope
4411              INSCOPE: for (reverse @{$self->{open_elements}}) {
4412                if ($_->[1] eq 'p') {
4413                  !!!back-token;
4414                  $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4415                  redo B;
4416                } elsif ({
4417                          table => 1, caption => 1, td => 1, th => 1,
4418                          button => 1, marquee => 1, object => 1, html => 1,
4419                         }->{$_->[1]}) {
4420                  last INSCOPE;
4421                }
4422              } # INSCOPE
4423                
4424              !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4425              $self->{form_element} = $self->{open_elements}->[-1]->[0];
4426              !!!next-token;
4427              redo B;
4428            }
4429          } elsif ($token->{tag_name} eq 'li') {
4430            ## has a p element in scope
4431            INSCOPE: for (reverse @{$self->{open_elements}}) {
4432              if ($_->[1] eq 'p') {
4433                !!!back-token;
4434                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4435                redo B;
4436              } elsif ({
4437                        table => 1, caption => 1, td => 1, th => 1,
4438                        button => 1, marquee => 1, object => 1, html => 1,
4439                       }->{$_->[1]}) {
4440                last INSCOPE;
4441              }
4442            } # INSCOPE
4443              
4444            ## Step 1
4445            my $i = -1;
4446            my $node = $self->{open_elements}->[$i];
4447            LI: {
4448              ## Step 2
4449              if ($node->[1] eq 'li') {
4450                if ($i != -1) {
4451                  !!!parse-error (type => 'end tag missing:'.
4452                                  $self->{open_elements}->[-1]->[1]);
4453                }
4454                splice @{$self->{open_elements}}, $i;
4455                last LI;
4456              }
4457              
4458              ## Step 3
4459              if (not $formatting_category->{$node->[1]} and
4460                  #not $phrasing_category->{$node->[1]} and
4461                  ($special_category->{$node->[1]} or
4462                   $scoping_category->{$node->[1]}) and
4463                  $node->[1] ne 'address' and $node->[1] ne 'div') {
4464                last LI;
4465              }
4466              
4467              ## Step 4
4468              $i--;
4469              $node = $self->{open_elements}->[$i];
4470              redo LI;
4471            } # LI
4472              
4473            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4474          !!!next-token;          !!!next-token;
4475          redo B;          redo B;
4476        } elsif ($token->{type} eq 'character') {        } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {
4477          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          ## has a p element in scope
4478            my $data = $1;          INSCOPE: for (reverse @{$self->{open_elements}}) {
4479            ## As if in the main phase.            if ($_->[1] eq 'p') {
4480            ## NOTE: The insertion mode in the main phase              !!!back-token;
4481            ## just before the phase has been changed to the trailing              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4482            ## end phase is either "after body" or "after frameset".              redo B;
4483            $reconstruct_active_formatting_elements->($insert_to_current)            } elsif ({
4484              if $phase eq 'main';                      table => 1, caption => 1, td => 1, th => 1,
4485                        button => 1, marquee => 1, object => 1, html => 1,
4486                       }->{$_->[1]}) {
4487                last INSCOPE;
4488              }
4489            } # INSCOPE
4490              
4491            ## Step 1
4492            my $i = -1;
4493            my $node = $self->{open_elements}->[$i];
4494            LI: {
4495              ## Step 2
4496              if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {
4497                if ($i != -1) {
4498                  !!!parse-error (type => 'end tag missing:'.
4499                                  $self->{open_elements}->[-1]->[1]);
4500                }
4501                splice @{$self->{open_elements}}, $i;
4502                last LI;
4503              }
4504              
4505              ## Step 3
4506              if (not $formatting_category->{$node->[1]} and
4507                  #not $phrasing_category->{$node->[1]} and
4508                  ($special_category->{$node->[1]} or
4509                   $scoping_category->{$node->[1]}) and
4510                  $node->[1] ne 'address' and $node->[1] ne 'div') {
4511                last LI;
4512              }
4513              
4514              ## Step 4
4515              $i--;
4516              $node = $self->{open_elements}->[$i];
4517              redo LI;
4518            } # LI
4519              
4520            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4521            !!!next-token;
4522            redo B;
4523          } elsif ($token->{tag_name} eq 'plaintext') {
4524            ## has a p element in scope
4525            INSCOPE: for (reverse @{$self->{open_elements}}) {
4526              if ($_->[1] eq 'p') {
4527                !!!back-token;
4528                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4529                redo B;
4530              } elsif ({
4531                        table => 1, caption => 1, td => 1, th => 1,
4532                        button => 1, marquee => 1, object => 1, html => 1,
4533                       }->{$_->[1]}) {
4534                last INSCOPE;
4535              }
4536            } # INSCOPE
4537              
4538            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4539              
4540            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
4541              
4542            !!!next-token;
4543            redo B;
4544          } elsif ({
4545                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4546                   }->{$token->{tag_name}}) {
4547            ## has a p element in scope
4548            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4549              my $node = $self->{open_elements}->[$_];
4550              if ($node->[1] eq 'p') {
4551                !!!back-token;
4552                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4553                redo B;
4554              } elsif ({
4555                        table => 1, caption => 1, td => 1, th => 1,
4556                        button => 1, marquee => 1, object => 1, html => 1,
4557                       }->{$node->[1]}) {
4558                last INSCOPE;
4559              }
4560            } # INSCOPE
4561              
4562            ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>
4563            ## has an element in scope
4564            #my $i;
4565            #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4566            #  my $node = $self->{open_elements}->[$_];
4567            #  if ({
4568            #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4569            #      }->{$node->[1]}) {
4570            #    $i = $_;
4571            #    last INSCOPE;
4572            #  } elsif ({
4573            #            table => 1, caption => 1, td => 1, th => 1,
4574            #            button => 1, marquee => 1, object => 1, html => 1,
4575            #           }->{$node->[1]}) {
4576            #    last INSCOPE;
4577            #  }
4578            #} # INSCOPE
4579            #  
4580            #if (defined $i) {
4581            #  !!! parse-error (type => 'in hn:hn');
4582            #  splice @{$self->{open_elements}}, $i;
4583            #}
4584              
4585            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4586              
4587            !!!next-token;
4588            redo B;
4589          } elsif ($token->{tag_name} eq 'a') {
4590            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
4591              my $node = $active_formatting_elements->[$i];
4592              if ($node->[1] eq 'a') {
4593                !!!parse-error (type => 'in a:a');
4594                
4595                !!!back-token;
4596                $token = {type => END_TAG_TOKEN, tag_name => 'a'};
4597                $formatting_end_tag->($token->{tag_name});
4598                
4599                AFE2: for (reverse 0..$#$active_formatting_elements) {
4600                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
4601                    splice @$active_formatting_elements, $_, 1;
4602                    last AFE2;
4603                  }
4604                } # AFE2
4605                OE: for (reverse 0..$#{$self->{open_elements}}) {
4606                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
4607                    splice @{$self->{open_elements}}, $_, 1;
4608                    last OE;
4609                  }
4610                } # OE
4611                last AFE;
4612              } elsif ($node->[0] eq '#marker') {
4613                last AFE;
4614              }
4615            } # AFE
4616                        
4617            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);          $reconstruct_active_formatting_elements->($insert_to_current);
4618    
4619            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4620            push @$active_formatting_elements, $self->{open_elements}->[-1];
4621    
4622            !!!next-token;
4623            redo B;
4624          } elsif ({
4625                    b => 1, big => 1, em => 1, font => 1, i => 1,
4626                    s => 1, small => 1, strile => 1,
4627                    strong => 1, tt => 1, u => 1,
4628                   }->{$token->{tag_name}}) {
4629            $reconstruct_active_formatting_elements->($insert_to_current);
4630            
4631            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4632            push @$active_formatting_elements, $self->{open_elements}->[-1];
4633            
4634            !!!next-token;
4635            redo B;
4636          } elsif ($token->{tag_name} eq 'nobr') {
4637            $reconstruct_active_formatting_elements->($insert_to_current);
4638    
4639            ## has a |nobr| element in scope
4640            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4641              my $node = $self->{open_elements}->[$_];
4642              if ($node->[1] eq 'nobr') {
4643                !!!parse-error (type => 'not closed:nobr');
4644                !!!back-token;
4645                $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};
4646                redo B;
4647              } elsif ({
4648                        table => 1, caption => 1, td => 1, th => 1,
4649                        button => 1, marquee => 1, object => 1, html => 1,
4650                       }->{$node->[1]}) {
4651                last INSCOPE;
4652              }
4653            } # INSCOPE
4654            
4655            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4656            push @$active_formatting_elements, $self->{open_elements}->[-1];
4657            
4658            !!!next-token;
4659            redo B;
4660          } elsif ($token->{tag_name} eq 'button') {
4661            ## has a button element in scope
4662            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4663              my $node = $self->{open_elements}->[$_];
4664              if ($node->[1] eq 'button') {
4665                !!!parse-error (type => 'in button:button');
4666                !!!back-token;
4667                $token = {type => END_TAG_TOKEN, tag_name => 'button'};
4668                redo B;
4669              } elsif ({
4670                        table => 1, caption => 1, td => 1, th => 1,
4671                        button => 1, marquee => 1, object => 1, html => 1,
4672                       }->{$node->[1]}) {
4673                last INSCOPE;
4674              }
4675            } # INSCOPE
4676              
4677            $reconstruct_active_formatting_elements->($insert_to_current);
4678                        
4679            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4680            push @$active_formatting_elements, ['#marker', ''];
4681    
4682            !!!next-token;
4683            redo B;
4684          } elsif ($token->{tag_name} eq 'marquee' or
4685                   $token->{tag_name} eq 'object') {
4686            $reconstruct_active_formatting_elements->($insert_to_current);
4687            
4688            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4689            push @$active_formatting_elements, ['#marker', ''];
4690            
4691            !!!next-token;
4692            redo B;
4693          } elsif ($token->{tag_name} eq 'xmp') {
4694            $reconstruct_active_formatting_elements->($insert_to_current);
4695            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4696            redo B;
4697          } elsif ($token->{tag_name} eq 'table') {
4698            ## has a p element in scope
4699            INSCOPE: for (reverse @{$self->{open_elements}}) {
4700              if ($_->[1] eq 'p') {
4701                !!!back-token;
4702                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4703                redo B;
4704              } elsif ({
4705                        table => 1, caption => 1, td => 1, th => 1,
4706                        button => 1, marquee => 1, object => 1, html => 1,
4707                       }->{$_->[1]}) {
4708                last INSCOPE;
4709              }
4710            } # INSCOPE
4711              
4712            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4713              
4714            $self->{insertion_mode} = IN_TABLE_IM;
4715              
4716            !!!next-token;
4717            redo B;
4718          } elsif ({
4719                    area => 1, basefont => 1, bgsound => 1, br => 1,
4720                    embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
4721                    image => 1,
4722                   }->{$token->{tag_name}}) {
4723            if ($token->{tag_name} eq 'image') {
4724              !!!parse-error (type => 'image');
4725              $token->{tag_name} = 'img';
4726            }
4727    
4728            ## NOTE: There is an "as if <br>" code clone.
4729            $reconstruct_active_formatting_elements->($insert_to_current);
4730            
4731            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4732            pop @{$self->{open_elements}};
4733            
4734            !!!next-token;
4735            redo B;
4736          } elsif ($token->{tag_name} eq 'hr') {
4737            ## has a p element in scope
4738            INSCOPE: for (reverse @{$self->{open_elements}}) {
4739              if ($_->[1] eq 'p') {
4740                !!!back-token;
4741                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4742                redo B;
4743              } elsif ({
4744                        table => 1, caption => 1, td => 1, th => 1,
4745                        button => 1, marquee => 1, object => 1, html => 1,
4746                       }->{$_->[1]}) {
4747                last INSCOPE;
4748              }
4749            } # INSCOPE
4750              
4751            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4752            pop @{$self->{open_elements}};
4753              
4754            !!!next-token;
4755            redo B;
4756          } elsif ($token->{tag_name} eq 'input') {
4757            $reconstruct_active_formatting_elements->($insert_to_current);
4758            
4759            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4760            ## TODO: associate with $self->{form_element} if defined
4761            pop @{$self->{open_elements}};
4762            
4763            !!!next-token;
4764            redo B;
4765          } elsif ($token->{tag_name} eq 'isindex') {
4766            !!!parse-error (type => 'isindex');
4767            
4768            if (defined $self->{form_element}) {
4769              ## Ignore the token
4770              !!!next-token;
4771              redo B;
4772            } else {
4773              my $at = $token->{attributes};
4774              my $form_attrs;
4775              $form_attrs->{action} = $at->{action} if $at->{action};
4776              my $prompt_attr = $at->{prompt};
4777              $at->{name} = {name => 'name', value => 'isindex'};
4778              delete $at->{action};
4779              delete $at->{prompt};
4780              my @tokens = (
4781                            {type => START_TAG_TOKEN, tag_name => 'form',
4782                             attributes => $form_attrs},
4783                            {type => START_TAG_TOKEN, tag_name => 'hr'},
4784                            {type => START_TAG_TOKEN, tag_name => 'p'},
4785                            {type => START_TAG_TOKEN, tag_name => 'label'},
4786                           );
4787              if ($prompt_attr) {
4788                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};
4789              } else {
4790                push @tokens, {type => CHARACTER_TOKEN,
4791                               data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
4792                ## TODO: make this configurable
4793              }
4794              push @tokens,
4795                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},
4796                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
4797                            {type => END_TAG_TOKEN, tag_name => 'label'},
4798                            {type => END_TAG_TOKEN, tag_name => 'p'},
4799                            {type => START_TAG_TOKEN, tag_name => 'hr'},
4800                            {type => END_TAG_TOKEN, tag_name => 'form'};
4801              $token = shift @tokens;
4802              !!!back-token (@tokens);
4803              redo B;
4804            }
4805          } elsif ($token->{tag_name} eq 'textarea') {
4806            my $tag_name = $token->{tag_name};
4807            my $el;
4808            !!!create-element ($el, $token->{tag_name}, $token->{attributes});
4809            
4810            ## TODO: $self->{form_element} if defined
4811            $self->{content_model} = RCDATA_CONTENT_MODEL;
4812            delete $self->{escape}; # MUST
4813            
4814            $insert->($el);
4815            
4816            my $text = '';
4817            !!!next-token;
4818            if ($token->{type} == CHARACTER_TOKEN) {
4819              $token->{data} =~ s/^\x0A//;
4820            unless (length $token->{data}) {            unless (length $token->{data}) {
4821              !!!next-token;              !!!next-token;
             redo B;  
4822            }            }
4823          }          }
4824            while ($token->{type} == CHARACTER_TOKEN) {
4825              $text .= $token->{data};
4826              !!!next-token;
4827            }
4828            if (length $text) {
4829              $el->manakai_append_text ($text);
4830            }
4831            
4832            $self->{content_model} = PCDATA_CONTENT_MODEL;
4833            
4834            if ($token->{type} == END_TAG_TOKEN and
4835                $token->{tag_name} eq $tag_name) {
4836              ## Ignore the token
4837            } else {
4838              !!!parse-error (type => 'in RCDATA:#'.$token->{type});
4839            }
4840            !!!next-token;
4841            redo B;
4842          } elsif ({
4843                    iframe => 1,
4844                    noembed => 1,
4845                    noframes => 1,
4846                    noscript => 0, ## TODO: 1 if scripting is enabled
4847                   }->{$token->{tag_name}}) {
4848            ## NOTE: There are two "as if in body" code clones.
4849            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4850            redo B;
4851          } elsif ($token->{tag_name} eq 'select') {
4852            $reconstruct_active_formatting_elements->($insert_to_current);
4853            
4854            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4855            
4856            $self->{insertion_mode} = IN_SELECT_IM;
4857            !!!next-token;
4858            redo B;
4859          } elsif ({
4860                    caption => 1, col => 1, colgroup => 1, frame => 1,
4861                    frameset => 1, head => 1, option => 1, optgroup => 1,
4862                    tbody => 1, td => 1, tfoot => 1, th => 1,
4863                    thead => 1, tr => 1,
4864                   }->{$token->{tag_name}}) {
4865            !!!parse-error (type => 'in body:'.$token->{tag_name});
4866            ## Ignore the token
4867            !!!next-token;
4868            redo B;
4869            
4870            ## ISSUE: An issue on HTML5 new elements in the spec.
4871          } else {
4872            $reconstruct_active_formatting_elements->($insert_to_current);
4873            
4874            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4875            
4876            !!!next-token;
4877            redo B;
4878          }
4879        } elsif ($token->{type} == END_TAG_TOKEN) {
4880          if ($token->{tag_name} eq 'body') {
4881            if (@{$self->{open_elements}} > 1 and
4882                $self->{open_elements}->[1]->[1] eq 'body') {
4883              for (@{$self->{open_elements}}) {
4884                unless ({
4885                           dd => 1, dt => 1, li => 1, p => 1, td => 1,
4886                           th => 1, tr => 1, body => 1, html => 1,
4887                         tbody => 1, tfoot => 1, thead => 1,
4888                        }->{$_->[1]}) {
4889                  !!!parse-error (type => 'not closed:'.$_->[1]);
4890                }
4891              }
4892    
4893          !!!parse-error (type => 'after html:#character');            $self->{insertion_mode} = AFTER_BODY_IM;
4894          $phase = 'main';            !!!next-token;
4895          ## reprocess            redo B;
4896            } else {
4897              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4898              ## Ignore the token
4899              !!!next-token;
4900              redo B;
4901            }
4902          } elsif ($token->{tag_name} eq 'html') {
4903            if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {
4904              ## ISSUE: There is an issue in the spec.
4905              if ($self->{open_elements}->[-1]->[1] ne 'body') {
4906                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
4907              }
4908              $self->{insertion_mode} = AFTER_BODY_IM;
4909              ## reprocess
4910              redo B;
4911            } else {
4912              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4913              ## Ignore the token
4914              !!!next-token;
4915              redo B;
4916            }
4917          } elsif ({
4918                    address => 1, blockquote => 1, center => 1, dir => 1,
4919                    div => 1, dl => 1, fieldset => 1, listing => 1,
4920                    menu => 1, ol => 1, pre => 1, ul => 1,
4921                    p => 1,
4922                    dd => 1, dt => 1, li => 1,
4923                    button => 1, marquee => 1, object => 1,
4924                   }->{$token->{tag_name}}) {
4925            ## has an element in scope
4926            my $i;
4927            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4928              my $node = $self->{open_elements}->[$_];
4929              if ($node->[1] eq $token->{tag_name}) {
4930                ## generate implied end tags
4931                if ({
4932                     dd => ($token->{tag_name} ne 'dd'),
4933                     dt => ($token->{tag_name} ne 'dt'),
4934                     li => ($token->{tag_name} ne 'li'),
4935                     p => ($token->{tag_name} ne 'p'),
4936                     td => 1, th => 1, tr => 1,
4937                     tbody => 1, tfoot=> 1, thead => 1,
4938                    }->{$self->{open_elements}->[-1]->[1]}) {
4939                  !!!back-token;
4940                  $token = {type => END_TAG_TOKEN,
4941                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4942                  redo B;
4943                }
4944                $i = $_;
4945                last INSCOPE unless $token->{tag_name} eq 'p';
4946              } elsif ({
4947                        table => 1, caption => 1, td => 1, th => 1,
4948                        button => 1, marquee => 1, object => 1, html => 1,
4949                       }->{$node->[1]}) {
4950                last INSCOPE;
4951              }
4952            } # INSCOPE
4953            
4954            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
4955              if (defined $i) {
4956                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4957              } else {
4958                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4959              }
4960            }
4961            
4962            if (defined $i) {
4963              splice @{$self->{open_elements}}, $i;
4964            } elsif ($token->{tag_name} eq 'p') {
4965              ## As if <p>, then reprocess the current token
4966              my $el;
4967              !!!create-element ($el, 'p');
4968              $insert->($el);
4969            }
4970            $clear_up_to_marker->()
4971              if {
4972                button => 1, marquee => 1, object => 1,
4973              }->{$token->{tag_name}};
4974            !!!next-token;
4975          redo B;          redo B;
4976        } elsif ($token->{type} eq 'start tag' or        } elsif ($token->{tag_name} eq 'form') {
4977                 $token->{type} eq 'end tag') {          ## has an element in scope
4978          !!!parse-error (type => 'after html:'.$token->{tag_name});          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4979          $phase = 'main';            my $node = $self->{open_elements}->[$_];
4980          ## reprocess            if ($node->[1] eq $token->{tag_name}) {
4981                ## generate implied end tags
4982                if ({
4983                     dd => 1, dt => 1, li => 1, p => 1,
4984                     td => 1, th => 1, tr => 1,
4985                     tbody => 1, tfoot=> 1, thead => 1,
4986                    }->{$self->{open_elements}->[-1]->[1]}) {
4987                  !!!back-token;
4988                  $token = {type => END_TAG_TOKEN,
4989                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4990                  redo B;
4991                }
4992                last INSCOPE;
4993              } elsif ({
4994                        table => 1, caption => 1, td => 1, th => 1,
4995                        button => 1, marquee => 1, object => 1, html => 1,
4996                       }->{$node->[1]}) {
4997                last INSCOPE;
4998              }
4999            } # INSCOPE
5000            
5001            if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
5002              pop @{$self->{open_elements}};
5003            } else {
5004              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5005            }
5006    
5007            undef $self->{form_element};
5008            !!!next-token;
5009            redo B;
5010          } elsif ({
5011                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5012                   }->{$token->{tag_name}}) {
5013            ## has an element in scope
5014            my $i;
5015            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5016              my $node = $self->{open_elements}->[$_];
5017              if ({
5018                   h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5019                  }->{$node->[1]}) {
5020                ## generate implied end tags
5021                if ({
5022                     dd => 1, dt => 1, li => 1, p => 1,
5023                     td => 1, th => 1, tr => 1,
5024                     tbody => 1, tfoot=> 1, thead => 1,
5025                    }->{$self->{open_elements}->[-1]->[1]}) {
5026                  !!!back-token;
5027                  $token = {type => END_TAG_TOKEN,
5028                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5029                  redo B;
5030                }
5031                $i = $_;
5032                last INSCOPE;
5033              } elsif ({
5034                        table => 1, caption => 1, td => 1, th => 1,
5035                        button => 1, marquee => 1, object => 1, html => 1,
5036                       }->{$node->[1]}) {
5037                last INSCOPE;
5038              }
5039            } # INSCOPE
5040            
5041            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5042              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5043            }
5044            
5045            splice @{$self->{open_elements}}, $i if defined $i;
5046            !!!next-token;
5047            redo B;
5048          } elsif ({
5049                    a => 1,
5050                    b => 1, big => 1, em => 1, font => 1, i => 1,
5051                    nobr => 1, s => 1, small => 1, strile => 1,
5052                    strong => 1, tt => 1, u => 1,
5053                   }->{$token->{tag_name}}) {
5054            $formatting_end_tag->($token->{tag_name});
5055            redo B;
5056          } elsif ($token->{tag_name} eq 'br') {
5057            !!!parse-error (type => 'unmatched end tag:br');
5058    
5059            ## As if <br>
5060            $reconstruct_active_formatting_elements->($insert_to_current);
5061            
5062            my $el;
5063            !!!create-element ($el, 'br');
5064            $insert->($el);
5065            
5066            ## Ignore the token.
5067            !!!next-token;
5068            redo B;
5069          } elsif ({
5070                    caption => 1, col => 1, colgroup => 1, frame => 1,
5071                    frameset => 1, head => 1, option => 1, optgroup => 1,
5072                    tbody => 1, td => 1, tfoot => 1, th => 1,
5073                    thead => 1, tr => 1,
5074                    area => 1, basefont => 1, bgsound => 1,
5075                    embed => 1, hr => 1, iframe => 1, image => 1,
5076                    img => 1, input => 1, isindex => 1, noembed => 1,
5077                    noframes => 1, param => 1, select => 1, spacer => 1,
5078                    table => 1, textarea => 1, wbr => 1,
5079                    noscript => 0, ## TODO: if scripting is enabled
5080                   }->{$token->{tag_name}}) {
5081            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5082            ## Ignore the token
5083            !!!next-token;
5084          redo B;          redo B;
5085        } elsif ($token->{type} eq 'end-of-file') {          
5086          ## Stop parsing          ## ISSUE: Issue on HTML5 new elements in spec
5087          last B;          
5088        } else {        } else {
5089          die "$0: $token->{type}: Unknown token";          ## Step 1
5090            my $node_i = -1;
5091            my $node = $self->{open_elements}->[$node_i];
5092    
5093            ## Step 2
5094            S2: {
5095              if ($node->[1] eq $token->{tag_name}) {
5096                ## Step 1
5097                ## generate implied end tags
5098                if ({
5099                     dd => 1, dt => 1, li => 1, p => 1,
5100                     td => 1, th => 1, tr => 1,
5101                     tbody => 1, tfoot => 1, thead => 1,
5102                    }->{$self->{open_elements}->[-1]->[1]}) {
5103                  !!!back-token;
5104                  $token = {type => END_TAG_TOKEN,
5105                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5106                  redo B;
5107                }
5108            
5109                ## Step 2
5110                if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
5111                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5112                }
5113                
5114                ## Step 3
5115                splice @{$self->{open_elements}}, $node_i;
5116    
5117                !!!next-token;
5118                last S2;
5119              } else {
5120                ## Step 3
5121                if (not $formatting_category->{$node->[1]} and
5122                    #not $phrasing_category->{$node->[1]} and
5123                    ($special_category->{$node->[1]} or
5124                     $scoping_category->{$node->[1]})) {
5125                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5126                  ## Ignore the token
5127                  !!!next-token;
5128                  last S2;
5129                }
5130              }
5131              
5132              ## Step 4
5133              $node_i--;
5134              $node = $self->{open_elements}->[$node_i];
5135              
5136              ## Step 5;
5137              redo S2;
5138            } # S2
5139            redo B;
5140        }        }
5141      }      }
5142        redo B;
5143    } # B    } # B
5144    
5145      ## NOTE: The "trailing end" phase in HTML5 is split into
5146      ## two insertion modes: "after html body" and "after html frameset".
5147      ## NOTE: States in the main stage is preserved while
5148      ## the parser stays in the trailing end phase. # MUST
5149    
5150    ## Stop parsing # MUST    ## Stop parsing # MUST
5151        
5152    ## TODO: script stuffs    ## TODO: script stuffs
# Line 4793  sub set_inner_html ($$$) { Line 5180  sub set_inner_html ($$$) {
5180      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
5181    
5182      ## Step 1 # MUST      ## Step 1 # MUST
5183      my $doc = $node->owner_document->implementation->create_document;      my $this_doc = $node->owner_document;
5184      ## TODO: Mark as HTML document      my $doc = $this_doc->implementation->create_document;
5185        $doc->manakai_is_html (1);
5186      my $p = $class->new;      my $p = $class->new;
5187      $p->{document} = $doc;      $p->{document} = $doc;
5188    
# Line 4804  sub set_inner_html ($$$) { Line 5192  sub set_inner_html ($$$) {
5192      my $column = 0;      my $column = 0;
5193      $p->{set_next_input_character} = sub {      $p->{set_next_input_character} = sub {
5194        my $self = shift;        my $self = shift;
5195    
5196          pop @{$self->{prev_input_character}};
5197          unshift @{$self->{prev_input_character}}, $self->{next_input_character};
5198    
5199        $self->{next_input_character} = -1 and return if $i >= length $$s;        $self->{next_input_character} = -1 and return if $i >= length $$s;
5200        $self->{next_input_character} = ord substr $$s, $i++, 1;        $self->{next_input_character} = ord substr $$s, $i++, 1;
5201        $column++;        $column++;
5202          
5203        if ($self->{next_input_character} == 0x000D) { # CR        if ($self->{next_input_character} == 0x000A) { # LF
5204          if ($i >= length $$s) {          $line++;
5205            #          $column = 0;
5206          } else {        } elsif ($self->{next_input_character} == 0x000D) { # CR
5207            my $next_char = ord substr $$s, $i++, 1;          $i++ if substr ($$s, $i, 1) eq "\x0A";
           if ($next_char == 0x000A) { # LF  
             #  
           } else {  
             push @{$self->{char}}, $next_char;  
           }  
         }  
5208          $self->{next_input_character} = 0x000A; # LF # MUST          $self->{next_input_character} = 0x000A; # LF # MUST
5209          $line++;          $line++;
5210          $column = -1;          $column = 0;
5211        } elsif ($self->{next_input_character} > 0x10FFFF) {        } elsif ($self->{next_input_character} > 0x10FFFF) {
5212          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
5213        } elsif ($self->{next_input_character} == 0x0000) { # NULL        } elsif ($self->{next_input_character} == 0x0000) { # NULL
5214            !!!parse-error (type => 'NULL');
5215          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
5216        }        }
5217      };      };
5218        $p->{prev_input_character} = [-1, -1, -1];
5219        $p->{next_input_character} = -1;
5220            
5221      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
5222        my (%opt) = @_;        my (%opt) = @_;
# Line 4842  sub set_inner_html ($$$) { Line 5231  sub set_inner_html ($$$) {
5231    
5232      ## Step 2      ## Step 2
5233      my $node_ln = $node->local_name;      my $node_ln = $node->local_name;
5234      $p->{content_model_flag} = {      $p->{content_model} = {
5235        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
5236        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
5237        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
5238        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
5239        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
5240        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
5241        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
5242        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
5243        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
5244        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
5245      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
5246         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
5247            unless defined $p->{content_model};
5248            ## ISSUE: What is "the name of the element"? local name?
5249    
5250      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $node_ln];
5251    
# Line 4906  sub set_inner_html ($$$) { Line 5297  sub set_inner_html ($$$) {
5297      ## Step 12 # MUST      ## Step 12 # MUST
5298      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
5299      for (@cn) {      for (@cn) {
5300          $this_doc->adopt_node ($_);
5301        $node->append_child ($_);        $node->append_child ($_);
5302      }      }
5303      ## ISSUE: adopt_node? mutation events?      ## ISSUE: mutation events?
5304    
5305      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
5306    } else {    } else {
# Line 4953  sub get_inner_html ($$$) { Line 5345  sub get_inner_html ($$$) {
5345            
5346      my $nt = $child->node_type;      my $nt = $child->node_type;
5347      if ($nt == 1) { # Element      if ($nt == 1) { # Element
5348        my $tag_name = lc $child->tag_name; ## ISSUE: Definition of "lowercase"        my $tag_name = $child->tag_name; ## TODO: manakai_tag_name
5349        $s .= '<' . $tag_name;        $s .= '<' . $tag_name;
5350          ## NOTE: Non-HTML case:
5351        ## ISSUE: Non-html elements        ## <http://permalink.gmane.org/gmane.org.w3c.whatwg.discuss/11191>
5352    
5353        my @attrs = @{$child->attributes}; # sort order MUST be stable        my @attrs = @{$child->attributes}; # sort order MUST be stable
5354        for my $attr (@attrs) { # order is implementation dependent        for my $attr (@attrs) { # order is implementation dependent
5355          my $attr_name = lc $attr->name; ## ISSUE: Definition of "lowercase"          my $attr_name = $attr->name; ## TODO: manakai_name
5356          $s .= ' ' . $attr_name . '="';          $s .= ' ' . $attr_name . '="';
5357          my $attr_value = $attr->value;          my $attr_value = $attr->value;
5358          ## escape          ## escape
# Line 4979  sub get_inner_html ($$$) { Line 5371  sub get_inner_html ($$$) {
5371          spacer => 1, wbr => 1,          spacer => 1, wbr => 1,
5372        }->{$tag_name};        }->{$tag_name};
5373    
5374          $s .= "\x0A" if $tag_name eq 'pre' or $tag_name eq 'textarea';
5375    
5376        if (not $in_cdata and {        if (not $in_cdata and {
5377          style => 1, script => 1, xmp => 1, iframe => 1,          style => 1, script => 1, xmp => 1, iframe => 1,
5378          noembed => 1, noframes => 1, noscript => 1,          noembed => 1, noframes => 1, noscript => 1,
5379            plaintext => 1,
5380        }->{$tag_name}) {        }->{$tag_name}) {
5381          unshift @node, 'cdata-out';          unshift @node, 'cdata-out';
5382          $in_cdata = 1;          $in_cdata = 1;

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.55

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24