/[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.11 by wakaba, Sat Jun 23 03:53:35 2007 UTC revision 1.35 by wakaba, Mon Jul 16 03:21:04 2007 UTC
# Line 2  package Whatpm::HTML; Line 2  package Whatpm::HTML;
2  use strict;  use strict;
3  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
4    
5  ## This is an early version of an HTML parser.  ## ISSUE:
6    ## var doc = implementation.createDocument (null, null, null);
7    ## doc.write ('');
8    ## alert (doc.compatMode);
9    
10    ## ISSUE: HTML5 revision 967 says that the encoding layer MUST NOT
11    ## strip BOM and the HTML layer MUST ignore it.  Whether we can do it
12    ## is not yet clear.
13    ## "{U+FEFF}..." in UTF-16BE/UTF-16LE is three or four characters?
14    ## "{U+FEFF}..." in GB18030?
15    
16  my $permitted_slash_tag_name = {  my $permitted_slash_tag_name = {
17    base => 1,    base => 1,
# Line 18  my $permitted_slash_tag_name = { Line 27  my $permitted_slash_tag_name = {
27    input => 1,    input => 1,
28  };  };
29    
 my $entity_char = {  
   AElig => "\x{00C6}",  
   Aacute => "\x{00C1}",  
   Acirc => "\x{00C2}",  
   Agrave => "\x{00C0}",  
   Alpha => "\x{0391}",  
   Aring => "\x{00C5}",  
   Atilde => "\x{00C3}",  
   Auml => "\x{00C4}",  
   Beta => "\x{0392}",  
   Ccedil => "\x{00C7}",  
   Chi => "\x{03A7}",  
   Dagger => "\x{2021}",  
   Delta => "\x{0394}",  
   ETH => "\x{00D0}",  
   Eacute => "\x{00C9}",  
   Ecirc => "\x{00CA}",  
   Egrave => "\x{00C8}",  
   Epsilon => "\x{0395}",  
   Eta => "\x{0397}",  
   Euml => "\x{00CB}",  
   Gamma => "\x{0393}",  
   Iacute => "\x{00CD}",  
   Icirc => "\x{00CE}",  
   Igrave => "\x{00CC}",  
   Iota => "\x{0399}",  
   Iuml => "\x{00CF}",  
   Kappa => "\x{039A}",  
   Lambda => "\x{039B}",  
   Mu => "\x{039C}",  
   Ntilde => "\x{00D1}",  
   Nu => "\x{039D}",  
   OElig => "\x{0152}",  
   Oacute => "\x{00D3}",  
   Ocirc => "\x{00D4}",  
   Ograve => "\x{00D2}",  
   Omega => "\x{03A9}",  
   Omicron => "\x{039F}",  
   Oslash => "\x{00D8}",  
   Otilde => "\x{00D5}",  
   Ouml => "\x{00D6}",  
   Phi => "\x{03A6}",  
   Pi => "\x{03A0}",  
   Prime => "\x{2033}",  
   Psi => "\x{03A8}",  
   Rho => "\x{03A1}",  
   Scaron => "\x{0160}",  
   Sigma => "\x{03A3}",  
   THORN => "\x{00DE}",  
   Tau => "\x{03A4}",  
   Theta => "\x{0398}",  
   Uacute => "\x{00DA}",  
   Ucirc => "\x{00DB}",  
   Ugrave => "\x{00D9}",  
   Upsilon => "\x{03A5}",  
   Uuml => "\x{00DC}",  
   Xi => "\x{039E}",  
   Yacute => "\x{00DD}",  
   Yuml => "\x{0178}",  
   Zeta => "\x{0396}",  
   aacute => "\x{00E1}",  
   acirc => "\x{00E2}",  
   acute => "\x{00B4}",  
   aelig => "\x{00E6}",  
   agrave => "\x{00E0}",  
   alefsym => "\x{2135}",  
   alpha => "\x{03B1}",  
   amp => "\x{0026}",  
   AMP => "\x{0026}",  
   and => "\x{2227}",  
   ang => "\x{2220}",  
   apos => "\x{0027}",  
   aring => "\x{00E5}",  
   asymp => "\x{2248}",  
   atilde => "\x{00E3}",  
   auml => "\x{00E4}",  
   bdquo => "\x{201E}",  
   beta => "\x{03B2}",  
   brvbar => "\x{00A6}",  
   bull => "\x{2022}",  
   cap => "\x{2229}",  
   ccedil => "\x{00E7}",  
   cedil => "\x{00B8}",  
   cent => "\x{00A2}",  
   chi => "\x{03C7}",  
   circ => "\x{02C6}",  
   clubs => "\x{2663}",  
   cong => "\x{2245}",  
   copy => "\x{00A9}",  
   COPY => "\x{00A9}",  
   crarr => "\x{21B5}",  
   cup => "\x{222A}",  
   curren => "\x{00A4}",  
   dArr => "\x{21D3}",  
   dagger => "\x{2020}",  
   darr => "\x{2193}",  
   deg => "\x{00B0}",  
   delta => "\x{03B4}",  
   diams => "\x{2666}",  
   divide => "\x{00F7}",  
   eacute => "\x{00E9}",  
   ecirc => "\x{00EA}",  
   egrave => "\x{00E8}",  
   empty => "\x{2205}",  
   emsp => "\x{2003}",  
   ensp => "\x{2002}",  
   epsilon => "\x{03B5}",  
   equiv => "\x{2261}",  
   eta => "\x{03B7}",  
   eth => "\x{00F0}",  
   euml => "\x{00EB}",  
   euro => "\x{20AC}",  
   exist => "\x{2203}",  
   fnof => "\x{0192}",  
   forall => "\x{2200}",  
   frac12 => "\x{00BD}",  
   frac14 => "\x{00BC}",  
   frac34 => "\x{00BE}",  
   frasl => "\x{2044}",  
   gamma => "\x{03B3}",  
   ge => "\x{2265}",  
   gt => "\x{003E}",  
   GT => "\x{003E}",  
   hArr => "\x{21D4}",  
   harr => "\x{2194}",  
   hearts => "\x{2665}",  
   hellip => "\x{2026}",  
   iacute => "\x{00ED}",  
   icirc => "\x{00EE}",  
   iexcl => "\x{00A1}",  
   igrave => "\x{00EC}",  
   image => "\x{2111}",  
   infin => "\x{221E}",  
   int => "\x{222B}",  
   iota => "\x{03B9}",  
   iquest => "\x{00BF}",  
   isin => "\x{2208}",  
   iuml => "\x{00EF}",  
   kappa => "\x{03BA}",  
   lArr => "\x{21D0}",  
   lambda => "\x{03BB}",  
   lang => "\x{2329}",  
   laquo => "\x{00AB}",  
   larr => "\x{2190}",  
   lceil => "\x{2308}",  
   ldquo => "\x{201C}",  
   le => "\x{2264}",  
   lfloor => "\x{230A}",  
   lowast => "\x{2217}",  
   loz => "\x{25CA}",  
   lrm => "\x{200E}",  
   lsaquo => "\x{2039}",  
   lsquo => "\x{2018}",  
   lt => "\x{003C}",  
   LT => "\x{003C}",  
   macr => "\x{00AF}",  
   mdash => "\x{2014}",  
   micro => "\x{00B5}",  
   middot => "\x{00B7}",  
   minus => "\x{2212}",  
   mu => "\x{03BC}",  
   nabla => "\x{2207}",  
   nbsp => "\x{00A0}",  
   ndash => "\x{2013}",  
   ne => "\x{2260}",  
   ni => "\x{220B}",  
   not => "\x{00AC}",  
   notin => "\x{2209}",  
   nsub => "\x{2284}",  
   ntilde => "\x{00F1}",  
   nu => "\x{03BD}",  
   oacute => "\x{00F3}",  
   ocirc => "\x{00F4}",  
   oelig => "\x{0153}",  
   ograve => "\x{00F2}",  
   oline => "\x{203E}",  
   omega => "\x{03C9}",  
   omicron => "\x{03BF}",  
   oplus => "\x{2295}",  
   or => "\x{2228}",  
   ordf => "\x{00AA}",  
   ordm => "\x{00BA}",  
   oslash => "\x{00F8}",  
   otilde => "\x{00F5}",  
   otimes => "\x{2297}",  
   ouml => "\x{00F6}",  
   para => "\x{00B6}",  
   part => "\x{2202}",  
   permil => "\x{2030}",  
   perp => "\x{22A5}",  
   phi => "\x{03C6}",  
   pi => "\x{03C0}",  
   piv => "\x{03D6}",  
   plusmn => "\x{00B1}",  
   pound => "\x{00A3}",  
   prime => "\x{2032}",  
   prod => "\x{220F}",  
   prop => "\x{221D}",  
   psi => "\x{03C8}",  
   quot => "\x{0022}",  
   QUOT => "\x{0022}",  
   rArr => "\x{21D2}",  
   radic => "\x{221A}",  
   rang => "\x{232A}",  
   raquo => "\x{00BB}",  
   rarr => "\x{2192}",  
   rceil => "\x{2309}",  
   rdquo => "\x{201D}",  
   real => "\x{211C}",  
   reg => "\x{00AE}",  
   REG => "\x{00AE}",  
   rfloor => "\x{230B}",  
   rho => "\x{03C1}",  
   rlm => "\x{200F}",  
   rsaquo => "\x{203A}",  
   rsquo => "\x{2019}",  
   sbquo => "\x{201A}",  
   scaron => "\x{0161}",  
   sdot => "\x{22C5}",  
   sect => "\x{00A7}",  
   shy => "\x{00AD}",  
   sigma => "\x{03C3}",  
   sigmaf => "\x{03C2}",  
   sim => "\x{223C}",  
   spades => "\x{2660}",  
   sub => "\x{2282}",  
   sube => "\x{2286}",  
   sum => "\x{2211}",  
   sup => "\x{2283}",  
   sup1 => "\x{00B9}",  
   sup2 => "\x{00B2}",  
   sup3 => "\x{00B3}",  
   supe => "\x{2287}",  
   szlig => "\x{00DF}",  
   tau => "\x{03C4}",  
   there4 => "\x{2234}",  
   theta => "\x{03B8}",  
   thetasym => "\x{03D1}",  
   thinsp => "\x{2009}",  
   thorn => "\x{00FE}",  
   tilde => "\x{02DC}",  
   times => "\x{00D7}",  
   trade => "\x{2122}",  
   uArr => "\x{21D1}",  
   uacute => "\x{00FA}",  
   uarr => "\x{2191}",  
   ucirc => "\x{00FB}",  
   ugrave => "\x{00F9}",  
   uml => "\x{00A8}",  
   upsih => "\x{03D2}",  
   upsilon => "\x{03C5}",  
   uuml => "\x{00FC}",  
   weierp => "\x{2118}",  
   xi => "\x{03BE}",  
   yacute => "\x{00FD}",  
   yen => "\x{00A5}",  
   yuml => "\x{00FF}",  
   zeta => "\x{03B6}",  
   zwj => "\x{200D}",  
   zwnj => "\x{200C}",  
 }; # $entity_char  
   
30  my $c1_entity_char = {  my $c1_entity_char = {
31    0x80 => 0x20AC,    0x80 => 0x20AC,
32    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 349  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++;
# Line 357  sub parse_string ($$$;$) { Line 108  sub parse_string ($$$;$) {
108        $line++;        $line++;
109        $column = 0;        $column = 0;
110      } elsif ($self->{next_input_character} == 0x000D) { # CR      } elsif ($self->{next_input_character} == 0x000D) { # CR
111        if ($i >= length $$s) {        $i++ if substr ($$s, $i, 1) eq "\x0A";
         #  
       } else {  
         my $next_char = ord substr $$s, $i++, 1;  
         if ($next_char == 0x000A) { # LF  
           #  
         } else {  
           push @{$self->{char}}, $next_char;  
         }  
       }  
112        $self->{next_input_character} = 0x000A; # LF # MUST        $self->{next_input_character} = 0x000A; # LF # MUST
113        $line++;        $line++;
114        $column = 0;        $column = 0;
# Line 377  sub parse_string ($$$;$) { Line 119  sub parse_string ($$$;$) {
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 420  sub _initialize_tokenizer ($) { Line 164  sub _initialize_tokenizer ($) {
164    # $self->{next_input_character}    # $self->{next_input_character}
165    !!!next-input-character;    !!!next-input-character;
166    $self->{token} = [];    $self->{token} = [];
167      # $self->{escape}
168  } # _initialize_tokenizer  } # _initialize_tokenizer
169    
170  ## A token has:  ## A token has:
171  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',
172  ##       'character', or 'end-of-file'  ##       'character', or 'end-of-file'
173  ##   ->{name} (DOCTYPE, start tag (tagname), end tag (tagname))  ##   ->{name} (DOCTYPE, start tag (tag name), end tag (tag name))
174      ## ISSUE: the spec need s/tagname/tag name/  ##   ->{public_identifier} (DOCTYPE)
175  ##   ->{error} == 1 or 0 (DOCTYPE)  ##   ->{system_identifier} (DOCTYPE)
176    ##   ->{correct} == 1 or 0 (DOCTYPE)
177  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{attributes} isa HASH (start tag, end tag)
178  ##   ->{data} (comment, character)  ##   ->{data} (comment, character)
179    
 ## Macros  
 ##   Macros MUST be preceded by three EXCLAMATION MARKs.  
 ##   emit ($token)  
 ##     Emits the specified token.  
   
180  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
181    
182  ## Before each step, UA MAY check to see if either one of the scripts in  ## Before each step, UA MAY check to see if either one of the scripts in
# Line 461  sub _get_next_token ($) { Line 202  sub _get_next_token ($) {
202          } else {          } else {
203            #            #
204          }          }
205          } elsif ($self->{next_input_character} == 0x002D) { # -
206            if ($self->{content_model_flag} eq 'RCDATA' or
207                $self->{content_model_flag} eq 'CDATA') {
208              unless ($self->{escape}) {
209                if ($self->{prev_input_character}->[0] == 0x002D and # -
210                    $self->{prev_input_character}->[1] == 0x0021 and # !
211                    $self->{prev_input_character}->[2] == 0x003C) { # <
212                  $self->{escape} = 1;
213                }
214              }
215            }
216            
217            #
218        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_input_character} == 0x003C) { # <
219          if ($self->{content_model_flag} ne 'PLAINTEXT') {          if ($self->{content_model_flag} eq 'PCDATA' or
220                (($self->{content_model_flag} eq 'CDATA' or
221                  $self->{content_model_flag} eq 'RCDATA') and
222                 not $self->{escape})) {
223            $self->{state} = 'tag open';            $self->{state} = 'tag open';
224            !!!next-input-character;            !!!next-input-character;
225            redo A;            redo A;
226          } else {          } else {
227            #            #
228          }          }
229          } elsif ($self->{next_input_character} == 0x003E) { # >
230            if ($self->{escape} and
231                ($self->{content_model_flag} eq 'RCDATA' or
232                 $self->{content_model_flag} eq 'CDATA')) {
233              if ($self->{prev_input_character}->[0] == 0x002D and # -
234                  $self->{prev_input_character}->[1] == 0x002D) { # -
235                delete $self->{escape};
236              }
237            }
238            
239            #
240        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
241          !!!emit ({type => 'end-of-file'});          !!!emit ({type => 'end-of-file'});
242          last A; ## TODO: ok?          last A; ## TODO: ok?
# Line 485  sub _get_next_token ($) { Line 253  sub _get_next_token ($) {
253      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} eq 'entity data') {
254        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
255                
256        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);
257    
258        $self->{state} = 'data';        $self->{state} = 'data';
259        # next-input-character is already done        # next-input-character is already done
# Line 564  sub _get_next_token ($) { Line 332  sub _get_next_token ($) {
332      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} eq 'close tag open') {
333        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model_flag} eq 'RCDATA' or
334            $self->{content_model_flag} eq 'CDATA') {            $self->{content_model_flag} eq 'CDATA') {
335          my @next_char;          if (defined $self->{last_emitted_start_tag_name}) {
336          TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
337              my @next_char;
338              TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
339                push @next_char, $self->{next_input_character};
340                my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
341                my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
342                if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {
343                  !!!next-input-character;
344                  next TAGNAME;
345                } else {
346                  $self->{next_input_character} = shift @next_char; # reconsume
347                  !!!back-next-input-character (@next_char);
348                  $self->{state} = 'data';
349    
350                  !!!emit ({type => 'character', data => '</'});
351      
352                  redo A;
353                }
354              }
355            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_input_character};
356            my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);        
357            my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;            unless ($self->{next_input_character} == 0x0009 or # HT
358            if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {                    $self->{next_input_character} == 0x000A or # LF
359              !!!next-input-character;                    $self->{next_input_character} == 0x000B or # VT
360              next TAGNAME;                    $self->{next_input_character} == 0x000C or # FF
361            } else {                    $self->{next_input_character} == 0x0020 or # SP
362              !!!parse-error (type => 'unmatched end tag');                    $self->{next_input_character} == 0x003E or # >
363                      $self->{next_input_character} == 0x002F or # /
364                      $self->{next_input_character} == -1) {
365              $self->{next_input_character} = shift @next_char; # reconsume              $self->{next_input_character} = shift @next_char; # reconsume
366              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
367              $self->{state} = 'data';              $self->{state} = 'data';
   
368              !!!emit ({type => 'character', data => '</'});              !!!emit ({type => 'character', data => '</'});
   
369              redo A;              redo A;
370              } else {
371                $self->{next_input_character} = shift @next_char;
372                !!!back-next-input-character (@next_char);
373                # and consume...
374            }            }
375          }          } else {
376          push @next_char, $self->{next_input_character};            ## No start tag token has ever been emitted
377                  # 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);  
378            $self->{state} = 'data';            $self->{state} = 'data';
   
379            !!!emit ({type => 'character', data => '</'});            !!!emit ({type => 'character', data => '</'});
   
380            redo A;            redo A;
         } else {  
           $self->{next_input_character} = shift @next_char;  
           !!!back-next-input-character (@next_char);  
           # and consume...  
381          }          }
382        }        }
383                
# Line 653  sub _get_next_token ($) { Line 425  sub _get_next_token ($) {
425          redo A;          redo A;
426        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
427          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
428              $self->{current_token}->{first_start_tag}
429                  = not defined $self->{last_emitted_start_tag_name};
430            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
431          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
432            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model_flag} = 'PCDATA'; # MUST
# Line 666  sub _get_next_token ($) { Line 440  sub _get_next_token ($) {
440          !!!next-input-character;          !!!next-input-character;
441    
442          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
443    
444          redo A;          redo A;
445        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 676  sub _get_next_token ($) { Line 449  sub _get_next_token ($) {
449          ## Stay in this state          ## Stay in this state
450          !!!next-input-character;          !!!next-input-character;
451          redo A;          redo A;
452        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
453          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
454          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
455              $self->{current_token}->{first_start_tag}
456                  = not defined $self->{last_emitted_start_tag_name};
457            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
458          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
459            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model_flag} = 'PCDATA'; # MUST
# Line 693  sub _get_next_token ($) { Line 467  sub _get_next_token ($) {
467          # reconsume          # reconsume
468    
469          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
470    
471          redo A;          redo A;
472        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
# Line 727  sub _get_next_token ($) { Line 500  sub _get_next_token ($) {
500          redo A;          redo A;
501        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
502          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
503              $self->{current_token}->{first_start_tag}
504                  = not defined $self->{last_emitted_start_tag_name};
505            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
506          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
507            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model_flag} = 'PCDATA'; # MUST
# Line 740  sub _get_next_token ($) { Line 515  sub _get_next_token ($) {
515          !!!next-input-character;          !!!next-input-character;
516    
517          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
518    
519          redo A;          redo A;
520        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 763  sub _get_next_token ($) { Line 537  sub _get_next_token ($) {
537          ## Stay in the state          ## Stay in the state
538          # next-input-character is already done          # next-input-character is already done
539          redo A;          redo A;
540        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
541          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
542          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
543              $self->{current_token}->{first_start_tag}
544                  = not defined $self->{last_emitted_start_tag_name};
545            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
546          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
547            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model_flag} = 'PCDATA'; # MUST
# Line 780  sub _get_next_token ($) { Line 555  sub _get_next_token ($) {
555          # reconsume          # reconsume
556    
557          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
558    
559          redo A;          redo A;
560        } else {        } else {
# Line 819  sub _get_next_token ($) { Line 593  sub _get_next_token ($) {
593        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
594          $before_leave->();          $before_leave->();
595          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
596              $self->{current_token}->{first_start_tag}
597                  = not defined $self->{last_emitted_start_tag_name};
598            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
599          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
600            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model_flag} = 'PCDATA'; # MUST
# Line 832  sub _get_next_token ($) { Line 608  sub _get_next_token ($) {
608          !!!next-input-character;          !!!next-input-character;
609    
610          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
611    
612          redo A;          redo A;
613        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 855  sub _get_next_token ($) { Line 630  sub _get_next_token ($) {
630          $self->{state} = 'before attribute name';          $self->{state} = 'before attribute name';
631          # next-input-character is already done          # next-input-character is already done
632          redo A;          redo A;
633        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
634          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
635          $before_leave->();          $before_leave->();
636          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
637              $self->{current_token}->{first_start_tag}
638                  = not defined $self->{last_emitted_start_tag_name};
639            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
640          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
641            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model_flag} = 'PCDATA'; # MUST
# Line 873  sub _get_next_token ($) { Line 649  sub _get_next_token ($) {
649          # reconsume          # reconsume
650    
651          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
652    
653          redo A;          redo A;
654        } else {        } else {
# Line 897  sub _get_next_token ($) { Line 672  sub _get_next_token ($) {
672          redo A;          redo A;
673        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
674          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
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} eq 'end tag') {
679            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model_flag} = 'PCDATA'; # MUST
# Line 910  sub _get_next_token ($) { Line 687  sub _get_next_token ($) {
687          !!!next-input-character;          !!!next-input-character;
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        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 929  sub _get_next_token ($) { Line 705  sub _get_next_token ($) {
705            #            #
706          } else {          } else {
707            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
708              ## TODO: Different error type for <aa / bb> than <aa/>
709          }          }
710          $self->{state} = 'before attribute name';          $self->{state} = 'before attribute name';
711          # next-input-character is already done          # next-input-character is already done
712          redo A;          redo A;
713        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
714          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
715          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
716              $self->{current_token}->{first_start_tag}
717                  = not defined $self->{last_emitted_start_tag_name};
718            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
719          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
720            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model_flag} = 'PCDATA'; # MUST
# Line 950  sub _get_next_token ($) { Line 728  sub _get_next_token ($) {
728          # reconsume          # reconsume
729    
730          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
731    
732          redo A;          redo A;
733        } else {        } else {
# Line 983  sub _get_next_token ($) { Line 760  sub _get_next_token ($) {
760          redo A;          redo A;
761        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
762          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
763              $self->{current_token}->{first_start_tag}
764                  = not defined $self->{last_emitted_start_tag_name};
765            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
766          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
767            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model_flag} = 'PCDATA'; # MUST
# Line 996  sub _get_next_token ($) { Line 775  sub _get_next_token ($) {
775          !!!next-input-character;          !!!next-input-character;
776    
777          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
778    
779          redo A;          redo A;
780        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
781          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
782          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
783              $self->{current_token}->{first_start_tag}
784                  = not defined $self->{last_emitted_start_tag_name};
785            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
786          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
787            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model_flag} = 'PCDATA'; # MUST
# Line 1016  sub _get_next_token ($) { Line 795  sub _get_next_token ($) {
795          ## reconsume          ## reconsume
796    
797          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
798    
799          redo A;          redo A;
800        } else {        } else {
# Line 1038  sub _get_next_token ($) { Line 816  sub _get_next_token ($) {
816        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
817          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
818          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
819              $self->{current_token}->{first_start_tag}
820                  = not defined $self->{last_emitted_start_tag_name};
821            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
822          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
823            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model_flag} = 'PCDATA'; # MUST
# Line 1051  sub _get_next_token ($) { Line 831  sub _get_next_token ($) {
831          ## reconsume          ## reconsume
832    
833          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
834    
835          redo A;          redo A;
836        } else {        } else {
# Line 1073  sub _get_next_token ($) { Line 852  sub _get_next_token ($) {
852        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
853          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
854          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
855              $self->{current_token}->{first_start_tag}
856                  = not defined $self->{last_emitted_start_tag_name};
857            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
858          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
859            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model_flag} = 'PCDATA'; # MUST
# Line 1086  sub _get_next_token ($) { Line 867  sub _get_next_token ($) {
867          ## reconsume          ## reconsume
868    
869          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
870    
871          redo A;          redo A;
872        } else {        } else {
# Line 1111  sub _get_next_token ($) { Line 891  sub _get_next_token ($) {
891          redo A;          redo A;
892        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
893          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
894              $self->{current_token}->{first_start_tag}
895                  = not defined $self->{last_emitted_start_tag_name};
896            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
897          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
898            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model_flag} = 'PCDATA'; # MUST
# Line 1124  sub _get_next_token ($) { Line 906  sub _get_next_token ($) {
906          !!!next-input-character;          !!!next-input-character;
907    
908          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
909    
910          redo A;          redo A;
911        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
912          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
913          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
914              $self->{current_token}->{first_start_tag}
915                  = not defined $self->{last_emitted_start_tag_name};
916            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
917          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
918            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model_flag} = 'PCDATA'; # MUST
# Line 1144  sub _get_next_token ($) { Line 926  sub _get_next_token ($) {
926          ## reconsume          ## reconsume
927    
928          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
929    
930          redo A;          redo A;
931        } else {        } else {
# Line 1154  sub _get_next_token ($) { Line 935  sub _get_next_token ($) {
935          redo A;          redo A;
936        }        }
937      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} eq 'entity in attribute value') {
938        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);
939    
940        unless (defined $token) {        unless (defined $token) {
941          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
# Line 1203  sub _get_next_token ($) { Line 984  sub _get_next_token ($) {
984          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_input_character};
985          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_input_character} == 0x002D) { # -
986            $self->{current_token} = {type => 'comment', data => ''};            $self->{current_token} = {type => 'comment', data => ''};
987            $self->{state} = 'comment';            $self->{state} = 'comment start';
988            !!!next-input-character;            !!!next-input-character;
989            redo A;            redo A;
990          }          }
# Line 1245  sub _get_next_token ($) { Line 1026  sub _get_next_token ($) {
1026          }          }
1027        }        }
1028    
1029        !!!parse-error (type => 'bogus comment open');        !!!parse-error (type => 'bogus comment');
1030        $self->{next_input_character} = shift @next_char;        $self->{next_input_character} = shift @next_char;
1031        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1032        $self->{state} = 'bogus comment';        $self->{state} = 'bogus comment';
# Line 1253  sub _get_next_token ($) { Line 1034  sub _get_next_token ($) {
1034                
1035        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1036        ## 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?
1037        } elsif ($self->{state} eq 'comment start') {
1038          if ($self->{next_input_character} == 0x002D) { # -
1039            $self->{state} = 'comment start dash';
1040            !!!next-input-character;
1041            redo A;
1042          } elsif ($self->{next_input_character} == 0x003E) { # >
1043            !!!parse-error (type => 'bogus comment');
1044            $self->{state} = 'data';
1045            !!!next-input-character;
1046    
1047            !!!emit ($self->{current_token}); # comment
1048    
1049            redo A;
1050          } elsif ($self->{next_input_character} == -1) {
1051            !!!parse-error (type => 'unclosed comment');
1052            $self->{state} = 'data';
1053            ## reconsume
1054    
1055            !!!emit ($self->{current_token}); # comment
1056    
1057            redo A;
1058          } else {
1059            $self->{current_token}->{data} # comment
1060                .= chr ($self->{next_input_character});
1061            $self->{state} = 'comment';
1062            !!!next-input-character;
1063            redo A;
1064          }
1065        } elsif ($self->{state} eq 'comment start dash') {
1066          if ($self->{next_input_character} == 0x002D) { # -
1067            $self->{state} = 'comment end';
1068            !!!next-input-character;
1069            redo A;
1070          } elsif ($self->{next_input_character} == 0x003E) { # >
1071            !!!parse-error (type => 'bogus comment');
1072            $self->{state} = 'data';
1073            !!!next-input-character;
1074    
1075            !!!emit ($self->{current_token}); # comment
1076    
1077            redo A;
1078          } elsif ($self->{next_input_character} == -1) {
1079            !!!parse-error (type => 'unclosed comment');
1080            $self->{state} = 'data';
1081            ## reconsume
1082    
1083            !!!emit ($self->{current_token}); # comment
1084    
1085            redo A;
1086          } else {
1087            $self->{current_token}->{data} # comment
1088                .= '-' . chr ($self->{next_input_character});
1089            $self->{state} = 'comment';
1090            !!!next-input-character;
1091            redo A;
1092          }
1093      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} eq 'comment') {
1094        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1095          $self->{state} = 'comment dash';          $self->{state} = 'comment end dash';
1096          !!!next-input-character;          !!!next-input-character;
1097          redo A;          redo A;
1098        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
# Line 1264  sub _get_next_token ($) { Line 1101  sub _get_next_token ($) {
1101          ## reconsume          ## reconsume
1102    
1103          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1104    
1105          redo A;          redo A;
1106        } else {        } else {
# Line 1273  sub _get_next_token ($) { Line 1109  sub _get_next_token ($) {
1109          !!!next-input-character;          !!!next-input-character;
1110          redo A;          redo A;
1111        }        }
1112      } elsif ($self->{state} eq 'comment dash') {      } elsif ($self->{state} eq 'comment end dash') {
1113        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1114          $self->{state} = 'comment end';          $self->{state} = 'comment end';
1115          !!!next-input-character;          !!!next-input-character;
# Line 1284  sub _get_next_token ($) { Line 1120  sub _get_next_token ($) {
1120          ## reconsume          ## reconsume
1121    
1122          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1123    
1124          redo A;          redo A;
1125        } else {        } else {
# Line 1299  sub _get_next_token ($) { Line 1134  sub _get_next_token ($) {
1134          !!!next-input-character;          !!!next-input-character;
1135    
1136          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1137    
1138          redo A;          redo A;
1139        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_input_character} == 0x002D) { # -
# Line 1314  sub _get_next_token ($) { Line 1148  sub _get_next_token ($) {
1148          ## reconsume          ## reconsume
1149    
1150          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1151    
1152          redo A;          redo A;
1153        } else {        } else {
# Line 1348  sub _get_next_token ($) { Line 1181  sub _get_next_token ($) {
1181          ## Stay in the state          ## Stay in the state
1182          !!!next-input-character;          !!!next-input-character;
1183          redo A;          redo A;
       } elsif (0x0061 <= $self->{next_input_character} and  
                $self->{next_input_character} <= 0x007A) { # a..z  
 ## ISSUE: "Set the token's name name to the" in the spec  
         $self->{current_token} = {type => 'DOCTYPE',  
                           name => chr ($self->{next_input_character} - 0x0020),  
                           error => 1};  
         $self->{state} = 'DOCTYPE name';  
         !!!next-input-character;  
         redo A;  
1184        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1185          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1186          $self->{state} = 'data';          $self->{state} = 'data';
1187          !!!next-input-character;          !!!next-input-character;
1188    
1189          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ({type => 'DOCTYPE'}); # incorrect
1190    
1191          redo A;          redo A;
1192        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
# Line 1370  sub _get_next_token ($) { Line 1194  sub _get_next_token ($) {
1194          $self->{state} = 'data';          $self->{state} = 'data';
1195          ## reconsume          ## reconsume
1196    
1197          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ({type => 'DOCTYPE'}); # incorrect
1198    
1199          redo A;          redo A;
1200        } else {        } else {
1201          $self->{current_token} = {type => 'DOCTYPE',          $self->{current_token}
1202                            name => chr ($self->{next_input_character}),              = {type => 'DOCTYPE',
1203                            error => 1};                 name => chr ($self->{next_input_character}),
1204                   correct => 1};
1205  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
1206          $self->{state} = 'DOCTYPE name';          $self->{state} = 'DOCTYPE name';
1207          !!!next-input-character;          !!!next-input-character;
1208          redo A;          redo A;
1209        }        }
1210      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} eq 'DOCTYPE name') {
1211    ## ISSUE: Redundant "First," in the spec.
1212        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1213            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1214            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1215            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1216            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
         $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE  
1217          $self->{state} = 'after DOCTYPE name';          $self->{state} = 'after DOCTYPE name';
1218          !!!next-input-character;          !!!next-input-character;
1219          redo A;          redo A;
1220        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
         $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE  
1221          $self->{state} = 'data';          $self->{state} = 'data';
1222          !!!next-input-character;          !!!next-input-character;
1223    
1224          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1225    
1226          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;  
1227        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1228          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
         $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE  
1229          $self->{state} = 'data';          $self->{state} = 'data';
1230          ## reconsume          ## reconsume
1231    
1232          !!!emit ($self->{current_token});          delete $self->{current_token}->{correct};
1233          undef $self->{current_token};          !!!emit ($self->{current_token}); # DOCTYPE
1234    
1235          redo A;          redo A;
1236        } else {        } else {
1237          $self->{current_token}->{name}          $self->{current_token}->{name}
1238            .= chr ($self->{next_input_character}); # DOCTYPE            .= chr ($self->{next_input_character}); # DOCTYPE
         #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');  
1239          ## Stay in the state          ## Stay in the state
1240          !!!next-input-character;          !!!next-input-character;
1241          redo A;          redo A;
# Line 1440  sub _get_next_token ($) { Line 1254  sub _get_next_token ($) {
1254          !!!next-input-character;          !!!next-input-character;
1255    
1256          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1257    
1258          redo A;          redo A;
1259        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
# Line 1448  sub _get_next_token ($) { Line 1261  sub _get_next_token ($) {
1261          $self->{state} = 'data';          $self->{state} = 'data';
1262          ## reconsume          ## reconsume
1263    
1264            delete $self->{current_token}->{correct};
1265          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1266    
1267          redo A;          redo A;
1268          } elsif ($self->{next_input_character} == 0x0050 or # P
1269                   $self->{next_input_character} == 0x0070) { # p
1270            !!!next-input-character;
1271            if ($self->{next_input_character} == 0x0055 or # U
1272                $self->{next_input_character} == 0x0075) { # u
1273              !!!next-input-character;
1274              if ($self->{next_input_character} == 0x0042 or # B
1275                  $self->{next_input_character} == 0x0062) { # b
1276                !!!next-input-character;
1277                if ($self->{next_input_character} == 0x004C or # L
1278                    $self->{next_input_character} == 0x006C) { # l
1279                  !!!next-input-character;
1280                  if ($self->{next_input_character} == 0x0049 or # I
1281                      $self->{next_input_character} == 0x0069) { # i
1282                    !!!next-input-character;
1283                    if ($self->{next_input_character} == 0x0043 or # C
1284                        $self->{next_input_character} == 0x0063) { # c
1285                      $self->{state} = 'before DOCTYPE public identifier';
1286                      !!!next-input-character;
1287                      redo A;
1288                    }
1289                  }
1290                }
1291              }
1292            }
1293    
1294            #
1295          } elsif ($self->{next_input_character} == 0x0053 or # S
1296                   $self->{next_input_character} == 0x0073) { # s
1297            !!!next-input-character;
1298            if ($self->{next_input_character} == 0x0059 or # Y
1299                $self->{next_input_character} == 0x0079) { # y
1300              !!!next-input-character;
1301              if ($self->{next_input_character} == 0x0053 or # S
1302                  $self->{next_input_character} == 0x0073) { # s
1303                !!!next-input-character;
1304                if ($self->{next_input_character} == 0x0054 or # T
1305                    $self->{next_input_character} == 0x0074) { # t
1306                  !!!next-input-character;
1307                  if ($self->{next_input_character} == 0x0045 or # E
1308                      $self->{next_input_character} == 0x0065) { # e
1309                    !!!next-input-character;
1310                    if ($self->{next_input_character} == 0x004D or # M
1311                        $self->{next_input_character} == 0x006D) { # m
1312                      $self->{state} = 'before DOCTYPE system identifier';
1313                      !!!next-input-character;
1314                      redo A;
1315                    }
1316                  }
1317                }
1318              }
1319            }
1320    
1321            #
1322        } else {        } else {
1323          !!!parse-error (type => 'string after DOCTYPE name');          !!!next-input-character;
1324          $self->{current_token}->{error} = 1; # DOCTYPE          #
1325          }
1326    
1327          !!!parse-error (type => 'string after DOCTYPE name');
1328          $self->{state} = 'bogus DOCTYPE';
1329          # next-input-character is already done
1330          redo A;
1331        } elsif ($self->{state} eq 'before DOCTYPE public identifier') {
1332          if ({
1333                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1334                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1335              }->{$self->{next_input_character}}) {
1336            ## Stay in the state
1337            !!!next-input-character;
1338            redo A;
1339          } elsif ($self->{next_input_character} eq 0x0022) { # "
1340            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1341            $self->{state} = 'DOCTYPE public identifier (double-quoted)';
1342            !!!next-input-character;
1343            redo A;
1344          } elsif ($self->{next_input_character} eq 0x0027) { # '
1345            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1346            $self->{state} = 'DOCTYPE public identifier (single-quoted)';
1347            !!!next-input-character;
1348            redo A;
1349          } elsif ($self->{next_input_character} eq 0x003E) { # >
1350            !!!parse-error (type => 'no PUBLIC literal');
1351    
1352            $self->{state} = 'data';
1353            !!!next-input-character;
1354    
1355            delete $self->{current_token}->{correct};
1356            !!!emit ($self->{current_token}); # DOCTYPE
1357    
1358            redo A;
1359          } elsif ($self->{next_input_character} == -1) {
1360            !!!parse-error (type => 'unclosed DOCTYPE');
1361    
1362            $self->{state} = 'data';
1363            ## reconsume
1364    
1365            delete $self->{current_token}->{correct};
1366            !!!emit ($self->{current_token}); # DOCTYPE
1367    
1368            redo A;
1369          } else {
1370            !!!parse-error (type => 'string after PUBLIC');
1371            $self->{state} = 'bogus DOCTYPE';
1372            !!!next-input-character;
1373            redo A;
1374          }
1375        } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {
1376          if ($self->{next_input_character} == 0x0022) { # "
1377            $self->{state} = 'after DOCTYPE public identifier';
1378            !!!next-input-character;
1379            redo A;
1380          } elsif ($self->{next_input_character} == -1) {
1381            !!!parse-error (type => 'unclosed PUBLIC literal');
1382    
1383            $self->{state} = 'data';
1384            ## reconsume
1385    
1386            delete $self->{current_token}->{correct};
1387            !!!emit ($self->{current_token}); # DOCTYPE
1388    
1389            redo A;
1390          } else {
1391            $self->{current_token}->{public_identifier} # DOCTYPE
1392                .= chr $self->{next_input_character};
1393            ## Stay in the state
1394            !!!next-input-character;
1395            redo A;
1396          }
1397        } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {
1398          if ($self->{next_input_character} == 0x0027) { # '
1399            $self->{state} = 'after DOCTYPE public identifier';
1400            !!!next-input-character;
1401            redo A;
1402          } elsif ($self->{next_input_character} == -1) {
1403            !!!parse-error (type => 'unclosed PUBLIC literal');
1404    
1405            $self->{state} = 'data';
1406            ## reconsume
1407    
1408            delete $self->{current_token}->{correct};
1409            !!!emit ($self->{current_token}); # DOCTYPE
1410    
1411            redo A;
1412          } else {
1413            $self->{current_token}->{public_identifier} # DOCTYPE
1414                .= chr $self->{next_input_character};
1415            ## Stay in the state
1416            !!!next-input-character;
1417            redo A;
1418          }
1419        } elsif ($self->{state} eq 'after DOCTYPE public identifier') {
1420          if ({
1421                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1422                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1423              }->{$self->{next_input_character}}) {
1424            ## Stay in the state
1425            !!!next-input-character;
1426            redo A;
1427          } elsif ($self->{next_input_character} == 0x0022) { # "
1428            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1429            $self->{state} = 'DOCTYPE system identifier (double-quoted)';
1430            !!!next-input-character;
1431            redo A;
1432          } elsif ($self->{next_input_character} == 0x0027) { # '
1433            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1434            $self->{state} = 'DOCTYPE system identifier (single-quoted)';
1435            !!!next-input-character;
1436            redo A;
1437          } elsif ($self->{next_input_character} == 0x003E) { # >
1438            $self->{state} = 'data';
1439            !!!next-input-character;
1440    
1441            !!!emit ($self->{current_token}); # DOCTYPE
1442    
1443            redo A;
1444          } elsif ($self->{next_input_character} == -1) {
1445            !!!parse-error (type => 'unclosed DOCTYPE');
1446    
1447            $self->{state} = 'data';
1448            ## reconsume
1449    
1450            delete $self->{current_token}->{correct};
1451            !!!emit ($self->{current_token}); # DOCTYPE
1452    
1453            redo A;
1454          } else {
1455            !!!parse-error (type => 'string after PUBLIC literal');
1456            $self->{state} = 'bogus DOCTYPE';
1457            !!!next-input-character;
1458            redo A;
1459          }
1460        } elsif ($self->{state} eq 'before DOCTYPE system identifier') {
1461          if ({
1462                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1463                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1464              }->{$self->{next_input_character}}) {
1465            ## Stay in the state
1466            !!!next-input-character;
1467            redo A;
1468          } elsif ($self->{next_input_character} == 0x0022) { # "
1469            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1470            $self->{state} = 'DOCTYPE system identifier (double-quoted)';
1471            !!!next-input-character;
1472            redo A;
1473          } elsif ($self->{next_input_character} == 0x0027) { # '
1474            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1475            $self->{state} = 'DOCTYPE system identifier (single-quoted)';
1476            !!!next-input-character;
1477            redo A;
1478          } elsif ($self->{next_input_character} == 0x003E) { # >
1479            !!!parse-error (type => 'no SYSTEM literal');
1480            $self->{state} = 'data';
1481            !!!next-input-character;
1482    
1483            delete $self->{current_token}->{correct};
1484            !!!emit ($self->{current_token}); # DOCTYPE
1485    
1486            redo A;
1487          } elsif ($self->{next_input_character} == -1) {
1488            !!!parse-error (type => 'unclosed DOCTYPE');
1489    
1490            $self->{state} = 'data';
1491            ## reconsume
1492    
1493            delete $self->{current_token}->{correct};
1494            !!!emit ($self->{current_token}); # DOCTYPE
1495    
1496            redo A;
1497          } else {
1498            !!!parse-error (type => 'string after SYSTEM');
1499            $self->{state} = 'bogus DOCTYPE';
1500            !!!next-input-character;
1501            redo A;
1502          }
1503        } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {
1504          if ($self->{next_input_character} == 0x0022) { # "
1505            $self->{state} = 'after DOCTYPE system identifier';
1506            !!!next-input-character;
1507            redo A;
1508          } elsif ($self->{next_input_character} == -1) {
1509            !!!parse-error (type => 'unclosed SYSTEM literal');
1510    
1511            $self->{state} = 'data';
1512            ## reconsume
1513    
1514            delete $self->{current_token}->{correct};
1515            !!!emit ($self->{current_token}); # DOCTYPE
1516    
1517            redo A;
1518          } else {
1519            $self->{current_token}->{system_identifier} # DOCTYPE
1520                .= chr $self->{next_input_character};
1521            ## Stay in the state
1522            !!!next-input-character;
1523            redo A;
1524          }
1525        } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {
1526          if ($self->{next_input_character} == 0x0027) { # '
1527            $self->{state} = 'after DOCTYPE system identifier';
1528            !!!next-input-character;
1529            redo A;
1530          } elsif ($self->{next_input_character} == -1) {
1531            !!!parse-error (type => 'unclosed SYSTEM literal');
1532    
1533            $self->{state} = 'data';
1534            ## reconsume
1535    
1536            delete $self->{current_token}->{correct};
1537            !!!emit ($self->{current_token}); # DOCTYPE
1538    
1539            redo A;
1540          } else {
1541            $self->{current_token}->{system_identifier} # DOCTYPE
1542                .= chr $self->{next_input_character};
1543            ## Stay in the state
1544            !!!next-input-character;
1545            redo A;
1546          }
1547        } elsif ($self->{state} eq 'after DOCTYPE system identifier') {
1548          if ({
1549                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1550                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1551              }->{$self->{next_input_character}}) {
1552            ## Stay in the state
1553            !!!next-input-character;
1554            redo A;
1555          } elsif ($self->{next_input_character} == 0x003E) { # >
1556            $self->{state} = 'data';
1557            !!!next-input-character;
1558    
1559            !!!emit ($self->{current_token}); # DOCTYPE
1560    
1561            redo A;
1562          } elsif ($self->{next_input_character} == -1) {
1563            !!!parse-error (type => 'unclosed DOCTYPE');
1564    
1565            $self->{state} = 'data';
1566            ## reconsume
1567    
1568            delete $self->{current_token}->{correct};
1569            !!!emit ($self->{current_token}); # DOCTYPE
1570    
1571            redo A;
1572          } else {
1573            !!!parse-error (type => 'string after SYSTEM literal');
1574          $self->{state} = 'bogus DOCTYPE';          $self->{state} = 'bogus DOCTYPE';
1575          !!!next-input-character;          !!!next-input-character;
1576          redo A;          redo A;
# Line 1464  sub _get_next_token ($) { Line 1580  sub _get_next_token ($) {
1580          $self->{state} = 'data';          $self->{state} = 'data';
1581          !!!next-input-character;          !!!next-input-character;
1582    
1583            delete $self->{current_token}->{correct};
1584          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1585    
1586          redo A;          redo A;
1587        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
# Line 1473  sub _get_next_token ($) { Line 1589  sub _get_next_token ($) {
1589          $self->{state} = 'data';          $self->{state} = 'data';
1590          ## reconsume          ## reconsume
1591    
1592            delete $self->{current_token}->{correct};
1593          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1594    
1595          redo A;          redo A;
1596        } else {        } else {
# Line 1490  sub _get_next_token ($) { Line 1606  sub _get_next_token ($) {
1606    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
1607  } # _get_next_token  } # _get_next_token
1608    
1609  sub _tokenize_attempt_to_consume_an_entity ($) {  sub _tokenize_attempt_to_consume_an_entity ($$) {
1610    my $self = shift;    my ($self, $in_attr) = @_;
1611      
1612    if ($self->{next_input_character} == 0x0023) { # #    if ({
1613           0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
1614           0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
1615          }->{$self->{next_input_character}}) {
1616        ## Don't consume
1617        ## No error
1618        return undef;
1619      } elsif ($self->{next_input_character} == 0x0023) { # #
1620      !!!next-input-character;      !!!next-input-character;
1621      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_input_character} == 0x0078 or # x
1622          $self->{next_input_character} == 0x0058) { # X          $self->{next_input_character} == 0x0058) { # X
1623        my $num;        my $code;
1624        X: {        X: {
1625          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_input_character};
1626          !!!next-input-character;          !!!next-input-character;
1627          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_input_character} and
1628              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_input_character} <= 0x0039) { # 0..9
1629            $num ||= 0;            $code ||= 0;
1630            $num *= 0x10;            $code *= 0x10;
1631            $num += $self->{next_input_character} - 0x0030;            $code += $self->{next_input_character} - 0x0030;
1632            redo X;            redo X;
1633          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_input_character} and
1634                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_input_character} <= 0x0066) { # a..f
1635            ## ISSUE: the spec says U+0078, which is apparently incorrect            $code ||= 0;
1636            $num ||= 0;            $code *= 0x10;
1637            $num *= 0x10;            $code += $self->{next_input_character} - 0x0060 + 9;
           $num += $self->{next_input_character} - 0x0060 + 9;  
1638            redo X;            redo X;
1639          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_input_character} and
1640                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_input_character} <= 0x0046) { # A..F
1641            ## ISSUE: the spec says U+0058, which is apparently incorrect            $code ||= 0;
1642            $num ||= 0;            $code *= 0x10;
1643            $num *= 0x10;            $code += $self->{next_input_character} - 0x0040 + 9;
           $num += $self->{next_input_character} - 0x0040 + 9;  
1644            redo X;            redo X;
1645          } elsif (not defined $num) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
1646            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro');
1647            $self->{next_input_character} = 0x0023; # #            $self->{next_input_character} = 0x0023; # #
1648            !!!back-next-input-character ($x_char);            !!!back-next-input-character ($x_char);
# Line 1532  sub _tokenize_attempt_to_consume_an_enti Line 1653  sub _tokenize_attempt_to_consume_an_enti
1653            !!!parse-error (type => 'no refc');            !!!parse-error (type => 'no refc');
1654          }          }
1655    
1656          ## TODO: check the definition for |a valid Unicode character|.          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
1657          ## <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8189>            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
1658          if ($num > 1114111 or $num == 0) {            $code = 0xFFFD;
1659            $num = 0xFFFD; # REPLACEMENT CHARACTER          } elsif ($code > 0x10FFFF) {
1660            ## ISSUE: Why this is not an error?            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
1661          } elsif (0x80 <= $num and $num <= 0x9F) {            $code = 0xFFFD;
1662            !!!parse-error (type => sprintf 'c1 entity:U+%04X', $num);          } elsif ($code == 0x000D) {
1663            $num = $c1_entity_char->{$num};            !!!parse-error (type => 'CR character reference');
1664              $code = 0x000A;
1665            } elsif (0x80 <= $code and $code <= 0x9F) {
1666              !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
1667              $code = $c1_entity_char->{$code};
1668          }          }
1669    
1670          return {type => 'character', data => chr $num};          return {type => 'character', data => chr $code};
1671        } # X        } # X
1672      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_input_character} and
1673               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_input_character} <= 0x0039) { # 0..9
# Line 1563  sub _tokenize_attempt_to_consume_an_enti Line 1688  sub _tokenize_attempt_to_consume_an_enti
1688          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc');
1689        }        }
1690    
1691        ## TODO: check the definition for |a valid Unicode character|.        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
1692        if ($code > 1114111 or $code == 0) {          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
1693          $code = 0xFFFD; # REPLACEMENT CHARACTER          $code = 0xFFFD;
1694          ## ISSUE: Why this is not an error?        } elsif ($code > 0x10FFFF) {
1695            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
1696            $code = 0xFFFD;
1697          } elsif ($code == 0x000D) {
1698            !!!parse-error (type => 'CR character reference');
1699            $code = 0x000A;
1700        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
1701          !!!parse-error (type => sprintf 'c1 entity:U+%04X', $code);          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
1702          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
1703        }        }
1704                
# Line 1588  sub _tokenize_attempt_to_consume_an_enti Line 1718  sub _tokenize_attempt_to_consume_an_enti
1718    
1719      my $value = $entity_name;      my $value = $entity_name;
1720      my $match;      my $match;
1721        require Whatpm::_NamedEntityList;
1722        our $EntityChar;
1723    
1724      while (length $entity_name < 10 and      while (length $entity_name < 10 and
1725             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
1726             ((0x0041 <= $self->{next_input_character} and             ((0x0041 <= $self->{next_input_character} and # a
1727               $self->{next_input_character} <= 0x005A) or               $self->{next_input_character} <= 0x005A) or # x
1728              (0x0061 <= $self->{next_input_character} and              (0x0061 <= $self->{next_input_character} and # a
1729               $self->{next_input_character} <= 0x007A) or               $self->{next_input_character} <= 0x007A) or # z
1730              (0x0030 <= $self->{next_input_character} and              (0x0030 <= $self->{next_input_character} and # 0
1731               $self->{next_input_character} <= 0x0039))) {               $self->{next_input_character} <= 0x0039) or # 9
1732                $self->{next_input_character} == 0x003B)) { # ;
1733        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_input_character};
1734        if (defined $entity_char->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
1735          $value = $entity_char->{$entity_name};          if ($self->{next_input_character} == 0x003B) { # ;
1736          $match = 1;            $value = $EntityChar->{$entity_name};
1737              $match = 1;
1738              !!!next-input-character;
1739              last;
1740            } elsif (not $in_attr) {
1741              $value = $EntityChar->{$entity_name};
1742              $match = -1;
1743            } else {
1744              $value .= chr $self->{next_input_character};
1745            }
1746        } else {        } else {
1747          $value .= chr $self->{next_input_character};          $value .= chr $self->{next_input_character};
1748        }        }
1749        !!!next-input-character;        !!!next-input-character;
1750      }      }
1751            
1752      if ($match) {      if ($match > 0) {
1753        if ($self->{next_input_character} == 0x003B) { # ;        return {type => 'character', data => $value};
1754          !!!next-input-character;      } elsif ($match < 0) {
1755        } else {        !!!parse-error (type => 'no refc');
         !!!parse-error (type => 'refc');  
       }  
   
1756        return {type => 'character', data => $value};        return {type => 'character', data => $value};
1757      } else {      } else {
1758        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero');
1759        ## NOTE: No characters are consumed in the spec.        ## NOTE: No characters are consumed in the spec.
1760        !!!back-token ({type => 'character', data => $value});        return {type => 'character', data => '&'.$value};
       return undef;  
1761      }      }
1762    } else {    } else {
1763      ## no characters are consumed      ## no characters are consumed
# Line 1634  sub _initialize_tree_constructor ($) { Line 1772  sub _initialize_tree_constructor ($) {
1772    $self->{document}->strict_error_checking (0);    $self->{document}->strict_error_checking (0);
1773    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
1774    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
1775    ## TODO: Mark the Document as an HTML document # MUST    $self->{document}->manakai_is_html (1); # MUST
1776  } # _initialize_tree_constructor  } # _initialize_tree_constructor
1777    
1778  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 1674  sub _construct_tree ($) { Line 1812  sub _construct_tree ($) {
1812    
1813  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
1814    my $self = shift;    my $self = shift;
1815    B: {    INITIAL: {
1816        if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} eq 'DOCTYPE') {
1817          if ($token->{error}) {        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
1818            ## ISSUE: Spec currently left this case undefined.        ## error, switch to a conformance checking mode for another
1819            !!!parse-error (type => 'bogus DOCTYPE');        ## language.
1820          }        my $doctype_name = $token->{name};
1821          my $doctype = $self->{document}->create_document_type_definition        $doctype_name = '' unless defined $doctype_name;
1822            ($token->{name});        $doctype_name =~ tr/a-z/A-Z/;
1823          $self->{document}->append_child ($doctype);        if (not defined $token->{name} or # <!DOCTYPE>
1824          #$phase = 'root element';            defined $token->{public_identifier} or
1825          !!!next-token;            defined $token->{system_identifier}) {
1826          #redo B;          !!!parse-error (type => 'not HTML5');
1827          return;        } elsif ($doctype_name ne 'HTML') {
1828        } elsif ({          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
1829                  comment => 1,          !!!parse-error (type => 'not HTML5');
1830                  'start tag' => 1,        }
1831                  'end tag' => 1,        
1832                  'end-of-file' => 1,        my $doctype = $self->{document}->create_document_type_definition
1833                 }->{$token->{type}}) {          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
1834          ## ISSUE: Spec currently left this case undefined.        $doctype->public_id ($token->{public_identifier})
1835          !!!parse-error (type => 'missing DOCTYPE');            if defined $token->{public_identifier};
1836          #$phase = 'root element';        $doctype->system_id ($token->{system_identifier})
1837          ## reprocess            if defined $token->{system_identifier};
1838          #redo B;        ## NOTE: Other DocumentType attributes are null or empty lists.
1839          return;        ## ISSUE: internalSubset = null??
1840        } elsif ($token->{type} eq 'character') {        $self->{document}->append_child ($doctype);
1841          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        
1842            $self->{document}->manakai_append_text ($1);        if (not $token->{correct} or $doctype_name ne 'HTML') {
1843            ## ISSUE: DOM3 Core does not allow Document > Text          $self->{document}->manakai_compat_mode ('quirks');
1844            unless (length $token->{data}) {        } elsif (defined $token->{public_identifier}) {
1845              ## Stay in the phase          my $pubid = $token->{public_identifier};
1846              !!!next-token;          $pubid =~ tr/a-z/A-z/;
1847              redo B;          if ({
1848              "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,
1849              "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,
1850              "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,
1851              "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,
1852              "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,
1853              "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,
1854              "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,
1855              "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,
1856              "-//IETF//DTD HTML 2.0//EN" => 1,
1857              "-//IETF//DTD HTML 2.1E//EN" => 1,
1858              "-//IETF//DTD HTML 3.0//EN" => 1,
1859              "-//IETF//DTD HTML 3.0//EN//" => 1,
1860              "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,
1861              "-//IETF//DTD HTML 3.2//EN" => 1,
1862              "-//IETF//DTD HTML 3//EN" => 1,
1863              "-//IETF//DTD HTML LEVEL 0//EN" => 1,
1864              "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,
1865              "-//IETF//DTD HTML LEVEL 1//EN" => 1,
1866              "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,
1867              "-//IETF//DTD HTML LEVEL 2//EN" => 1,
1868              "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,
1869              "-//IETF//DTD HTML LEVEL 3//EN" => 1,
1870              "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,
1871              "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,
1872              "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,
1873              "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,
1874              "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,
1875              "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,
1876              "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,
1877              "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,
1878              "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,
1879              "-//IETF//DTD HTML STRICT//EN" => 1,
1880              "-//IETF//DTD HTML STRICT//EN//2.0" => 1,
1881              "-//IETF//DTD HTML STRICT//EN//3.0" => 1,
1882              "-//IETF//DTD HTML//EN" => 1,
1883              "-//IETF//DTD HTML//EN//2.0" => 1,
1884              "-//IETF//DTD HTML//EN//3.0" => 1,
1885              "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,
1886              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,
1887              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,
1888              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,
1889              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,
1890              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,
1891              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,
1892              "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,
1893              "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,
1894              "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,
1895              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,
1896              "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,
1897              "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,
1898              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,
1899              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,
1900              "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,
1901              "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,
1902              "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,
1903              "-//W3C//DTD HTML 3.2//EN" => 1,
1904              "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,
1905              "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,
1906              "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,
1907              "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,
1908              "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,
1909              "-//W3C//DTD W3 HTML//EN" => 1,
1910              "-//W3O//DTD W3 HTML 3.0//EN" => 1,
1911              "-//W3O//DTD W3 HTML 3.0//EN//" => 1,
1912              "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,
1913              "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,
1914              "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,
1915              "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,
1916              "HTML" => 1,
1917            }->{$pubid}) {
1918              $self->{document}->manakai_compat_mode ('quirks');
1919            } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or
1920                     $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {
1921              if (defined $token->{system_identifier}) {
1922                $self->{document}->manakai_compat_mode ('quirks');
1923              } else {
1924                $self->{document}->manakai_compat_mode ('limited quirks');
1925            }            }
1926            } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or
1927                     $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {
1928              $self->{document}->manakai_compat_mode ('limited quirks');
1929            }
1930          }
1931          if (defined $token->{system_identifier}) {
1932            my $sysid = $token->{system_identifier};
1933            $sysid =~ tr/A-Z/a-z/;
1934            if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
1935              $self->{document}->manakai_compat_mode ('quirks');
1936            }
1937          }
1938          
1939          ## Go to the root element phase.
1940          !!!next-token;
1941          return;
1942        } elsif ({
1943                  'start tag' => 1,
1944                  'end tag' => 1,
1945                  'end-of-file' => 1,
1946                 }->{$token->{type}}) {
1947          !!!parse-error (type => 'no DOCTYPE');
1948          $self->{document}->manakai_compat_mode ('quirks');
1949          ## Go to the root element phase
1950          ## reprocess
1951          return;
1952        } elsif ($token->{type} eq 'character') {
1953          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
1954            ## Ignore the token
1955    
1956            unless (length $token->{data}) {
1957              ## Stay in the phase
1958              !!!next-token;
1959              redo INITIAL;
1960          }          }
         ## 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";  
1961        }        }
1962      } # B  
1963          !!!parse-error (type => 'no DOCTYPE');
1964          $self->{document}->manakai_compat_mode ('quirks');
1965          ## Go to the root element phase
1966          ## reprocess
1967          return;
1968        } elsif ($token->{type} eq 'comment') {
1969          my $comment = $self->{document}->create_comment ($token->{data});
1970          $self->{document}->append_child ($comment);
1971          
1972          ## Stay in the phase.
1973          !!!next-token;
1974          redo INITIAL;
1975        } else {
1976          die "$0: $token->{type}: Unknown token";
1977        }
1978      } # INITIAL
1979  } # _tree_construction_initial  } # _tree_construction_initial
1980    
1981  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
# Line 1738  sub _tree_construction_root_element ($) Line 1995  sub _tree_construction_root_element ($)
1995          !!!next-token;          !!!next-token;
1996          redo B;          redo B;
1997        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} eq 'character') {
1998          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
1999            $self->{document}->manakai_append_text ($1);            ## Ignore the token.
2000            ## ISSUE: DOM3 Core does not allow Document > Text  
2001            unless (length $token->{data}) {            unless (length $token->{data}) {
2002              ## Stay in the phase              ## Stay in the phase
2003              !!!next-token;              !!!next-token;
# Line 1761  sub _tree_construction_root_element ($) Line 2018  sub _tree_construction_root_element ($)
2018        my $root_element; !!!create-element ($root_element, 'html');        my $root_element; !!!create-element ($root_element, 'html');
2019        $self->{document}->append_child ($root_element);        $self->{document}->append_child ($root_element);
2020        push @{$self->{open_elements}}, [$root_element, 'html'];        push @{$self->{open_elements}}, [$root_element, 'html'];
       #$phase = 'main';  
2021        ## reprocess        ## reprocess
2022        #redo B;        #redo B;
2023        return;        return; ## Go to the main phase.
2024    } # B    } # B
2025  } # _tree_construction_root_element  } # _tree_construction_root_element
2026    
# Line 1780  sub _reset_insertion_mode ($) { Line 2036  sub _reset_insertion_mode ($) {
2036            
2037      ## Step 3      ## Step 3
2038      S3: {      S3: {
2039        $last = 1 if $self->{open_elements}->[0]->[0] eq $node->[0];        ## ISSUE: Oops! "If node is the first node in the stack of open
2040        if (defined $self->{inner_html_node}) {        ## elements, then set last to true. If the context element of the
2041          if ($self->{inner_html_node}->[1] eq 'td' or        ## HTML fragment parsing algorithm is neither a td element nor a
2042              $self->{inner_html_node}->[1] eq 'th') {        ## th element, then set node to the context element. (fragment case)":
2043            #        ## The second "if" is in the scope of the first "if"!?
2044          } else {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
2045            $node = $self->{inner_html_node};          $last = 1;
2046            if (defined $self->{inner_html_node}) {
2047              if ($self->{inner_html_node}->[1] eq 'td' or
2048                  $self->{inner_html_node}->[1] eq 'th') {
2049                #
2050              } else {
2051                $node = $self->{inner_html_node};
2052              }
2053          }          }
2054        }        }
2055            
# Line 1833  sub _reset_insertion_mode ($) { Line 2096  sub _reset_insertion_mode ($) {
2096  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
2097    my $self = shift;    my $self = shift;
2098    
2099    my $phase = 'main';    my $previous_insertion_mode;
2100    
2101    my $active_formatting_elements = [];    my $active_formatting_elements = [];
2102    
# Line 1917  sub _tree_construction_main ($) { Line 2180  sub _tree_construction_main ($) {
2180      }      }
2181    }; # $clear_up_to_marker    }; # $clear_up_to_marker
2182    
2183    my $style_start_tag = sub {    my $parse_rcdata = sub ($$) {
2184      my $style_el; !!!create-element ($style_el, 'style', $token->{attributes});      my ($content_model_flag, $insert) = @_;
2185      ## $self->{insertion_mode} eq 'in head' and ... (always true)  
2186      (($self->{insertion_mode} eq 'in head' and defined $self->{head_element})      ## Step 1
2187       ? $self->{head_element} : $self->{open_elements}->[-1]->[0])      my $start_tag_name = $token->{tag_name};
2188        ->append_child ($style_el);      my $el;
2189      $self->{content_model_flag} = 'CDATA';      !!!create-element ($el, $start_tag_name, $token->{attributes});
2190                  
2191        ## Step 2
2192        $insert->($el); # /context node/->append_child ($el)
2193    
2194        ## Step 3
2195        $self->{content_model_flag} = $content_model_flag; # CDATA or RCDATA
2196        delete $self->{escape}; # MUST
2197    
2198        ## Step 4
2199      my $text = '';      my $text = '';
2200      !!!next-token;      !!!next-token;
2201      while ($token->{type} eq 'character') {      while ($token->{type} eq 'character') { # or until stop tokenizing
2202        $text .= $token->{data};        $text .= $token->{data};
2203        !!!next-token;        !!!next-token;
2204      } # stop if non-character token or tokenizer stops tokenising      }
2205    
2206        ## Step 5
2207      if (length $text) {      if (length $text) {
2208        $style_el->manakai_append_text ($text);        my $text = $self->{document}->create_text_node ($text);
2209          $el->append_child ($text);
2210      }      }
2211        
2212        ## Step 6
2213      $self->{content_model_flag} = 'PCDATA';      $self->{content_model_flag} = 'PCDATA';
2214                  
2215      if ($token->{type} eq 'end tag' and $token->{tag_name} eq 'style') {      ## Step 7
2216        if ($token->{type} eq 'end tag' and $token->{tag_name} eq $start_tag_name) {
2217        ## Ignore the token        ## Ignore the token
2218      } else {      } else {
2219        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in '.$content_model_flag.':#'.$token->{type});
       ## ISSUE: And ignore?  
2220      }      }
2221      !!!next-token;      !!!next-token;
2222    }; # $style_start_tag    }; # $parse_rcdata
2223    
2224    my $script_start_tag = sub {    my $script_start_tag = sub ($) {
2225        my $insert = $_[0];
2226      my $script_el;      my $script_el;
2227      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes});
2228      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
2229    
2230      $self->{content_model_flag} = 'CDATA';      $self->{content_model_flag} = 'CDATA';
2231        delete $self->{escape}; # MUST
2232            
2233      my $text = '';      my $text = '';
2234      !!!next-token;      !!!next-token;
# Line 1979  sub _tree_construction_main ($) { Line 2256  sub _tree_construction_main ($) {
2256      } else {      } else {
2257        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
2258        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
2259          
2260        (($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);  
2261                
2262        ## TODO: insertion point = $old_insertion_point (might be "undefined")        ## TODO: insertion point = $old_insertion_point (might be "undefined")
2263                
# Line 2175  sub _tree_construction_main ($) { Line 2451  sub _tree_construction_main ($) {
2451    }; # $formatting_end_tag    }; # $formatting_end_tag
2452    
2453    my $insert_to_current = sub {    my $insert_to_current = sub {
2454      $self->{open_elements}->[-1]->[0]->append_child (shift);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
2455    }; # $insert_to_current    }; # $insert_to_current
2456    
2457    my $insert_to_foster = sub {    my $insert_to_foster = sub {
# Line 2213  sub _tree_construction_main ($) { Line 2489  sub _tree_construction_main ($) {
2489      my $insert = shift;      my $insert = shift;
2490      if ($token->{type} eq 'start tag') {      if ($token->{type} eq 'start tag') {
2491        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
2492          $script_start_tag->();          ## NOTE: This is an "as if in head" code clone
2493            $script_start_tag->($insert);
2494          return;          return;
2495        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
2496          $style_start_tag->();          ## NOTE: This is an "as if in head" code clone
2497            $parse_rcdata->('CDATA', $insert);
2498          return;          return;
2499        } elsif ({        } elsif ({
2500                  base => 1, link => 1, meta => 1,                  base => 1, link => 1,
2501                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
2502          !!!parse-error (type => 'in body:'.$token->{tag_name});          ## NOTE: This is an "as if in head" code clone, only "-t" differs
2503          ## NOTE: This is an "as if in head" code clone          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
2504          my $el;          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2505          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!next-token;
2506          if (defined $self->{head_element}) {          return;
2507            $self->{head_element}->append_child ($el);        } elsif ($token->{tag_name} eq 'meta') {
2508          } else {          ## NOTE: This is an "as if in head" code clone, only "-t" differs
2509            $insert->($el);          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
2510            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2511    
2512            unless ($self->{confident}) {
2513              my $charset;
2514              if ($token->{attributes}->{charset}) { ## TODO: And if supported
2515                $charset = $token->{attributes}->{charset}->{value};
2516              }
2517              if ($token->{attributes}->{'http-equiv'}) {
2518                ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
2519                if ($token->{attributes}->{'http-equiv'}->{value}
2520                    =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
2521                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
2522                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
2523                  $charset = defined $1 ? $1 : defined $2 ? $2 : $3;
2524                } ## TODO: And if supported
2525              }
2526              ## TODO: Change the encoding
2527          }          }
2528            
2529          !!!next-token;          !!!next-token;
2530          return;          return;
2531        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
2532          !!!parse-error (type => 'in body:title');          !!!parse-error (type => 'in body:title');
2533          ## NOTE: There is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
2534          my $title_el;          $parse_rcdata->('RCDATA', sub {
2535          !!!create-element ($title_el, 'title', $token->{attributes});            if (defined $self->{head_element}) {
2536          (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])              $self->{head_element}->append_child ($_[0]);
2537            ->append_child ($title_el);            } else {
2538          $self->{content_model_flag} = 'RCDATA';              $insert->($_[0]);
2539                      }
2540          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;  
2541          return;          return;
2542        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
2543          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body');
# Line 2364  sub _tree_construction_main ($) { Line 2640  sub _tree_construction_main ($) {
2640              if ($i != -1) {              if ($i != -1) {
2641                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'end tag missing:'.
2642                                $self->{open_elements}->[-1]->[1]);                                $self->{open_elements}->[-1]->[1]);
               ## TODO: test  
2643              }              }
2644              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
2645              last LI;              last LI;
# Line 2412  sub _tree_construction_main ($) { Line 2687  sub _tree_construction_main ($) {
2687              if ($i != -1) {              if ($i != -1) {
2688                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'end tag missing:'.
2689                                $self->{open_elements}->[-1]->[1]);                                $self->{open_elements}->[-1]->[1]);
               ## TODO: test  
2690              }              }
2691              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
2692              last LI;              last LI;
# Line 2475  sub _tree_construction_main ($) { Line 2749  sub _tree_construction_main ($) {
2749            }            }
2750          } # INSCOPE          } # INSCOPE
2751                        
2752            ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>
2753          ## has an element in scope          ## has an element in scope
2754          my $i;          #my $i;
2755          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
2756            my $node = $self->{open_elements}->[$_];          #  my $node = $self->{open_elements}->[$_];
2757            if ({          #  if ({
2758                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,          #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
2759                }->{$node->[1]}) {          #      }->{$node->[1]}) {
2760              $i = $_;          #    $i = $_;
2761              last INSCOPE;          #    last INSCOPE;
2762            } elsif ({          #  } elsif ({
2763                      table => 1, caption => 1, td => 1, th => 1,          #            table => 1, caption => 1, td => 1, th => 1,
2764                      button => 1, marquee => 1, object => 1, html => 1,          #            button => 1, marquee => 1, object => 1, html => 1,
2765                     }->{$node->[1]}) {          #           }->{$node->[1]}) {
2766              last INSCOPE;          #    last INSCOPE;
2767            }          #  }
2768          } # INSCOPE          #} # INSCOPE
2769                      #  
2770          if (defined $i) {          #if (defined $i) {
2771            !!!parse-error (type => 'in hn:hn');          #  !!! parse-error (type => 'in hn:hn');
2772            splice @{$self->{open_elements}}, $i;          #  splice @{$self->{open_elements}}, $i;
2773          }          #}
2774                        
2775          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
2776                        
# Line 2538  sub _tree_construction_main ($) { Line 2813  sub _tree_construction_main ($) {
2813          return;          return;
2814        } elsif ({        } elsif ({
2815                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
2816                  nobr => 1, s => 1, small => 1, strile => 1,                  s => 1, small => 1, strile => 1,
2817                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
2818                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
2819          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
# Line 2548  sub _tree_construction_main ($) { Line 2823  sub _tree_construction_main ($) {
2823                    
2824          !!!next-token;          !!!next-token;
2825          return;          return;
2826          } elsif ($token->{tag_name} eq 'nobr') {
2827            $reconstruct_active_formatting_elements->($insert_to_current);
2828    
2829            ## has a |nobr| element in scope
2830            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
2831              my $node = $self->{open_elements}->[$_];
2832              if ($node->[1] eq 'nobr') {
2833                !!!parse-error (type => 'not closed:nobr');
2834                !!!back-token;
2835                $token = {type => 'end tag', tag_name => 'nobr'};
2836                return;
2837              } elsif ({
2838                        table => 1, caption => 1, td => 1, th => 1,
2839                        button => 1, marquee => 1, object => 1, html => 1,
2840                       }->{$node->[1]}) {
2841                last INSCOPE;
2842              }
2843            } # INSCOPE
2844            
2845            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
2846            push @$active_formatting_elements, $self->{open_elements}->[-1];
2847            
2848            !!!next-token;
2849            return;
2850        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
2851          ## has a button element in scope          ## has a button element in scope
2852          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 2583  sub _tree_construction_main ($) { Line 2882  sub _tree_construction_main ($) {
2882          return;          return;
2883        } elsif ($token->{tag_name} eq 'xmp') {        } elsif ($token->{tag_name} eq 'xmp') {
2884          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
2885                    $parse_rcdata->('CDATA', $insert);
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         $self->{content_model_flag} = 'CDATA';  
           
         !!!next-token;  
2886          return;          return;
2887        } elsif ($token->{tag_name} eq 'table') {        } elsif ($token->{tag_name} eq 'table') {
2888          ## has a p element in scope          ## has a p element in scope
# Line 2620  sub _tree_construction_main ($) { Line 2914  sub _tree_construction_main ($) {
2914            !!!parse-error (type => 'image');            !!!parse-error (type => 'image');
2915            $token->{tag_name} = 'img';            $token->{tag_name} = 'img';
2916          }          }
2917            
2918            ## NOTE: There is an "as if <br>" code clone.
2919          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
2920                    
2921          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
# Line 2666  sub _tree_construction_main ($) { Line 2961  sub _tree_construction_main ($) {
2961            return;            return;
2962          } else {          } else {
2963            my $at = $token->{attributes};            my $at = $token->{attributes};
2964              my $form_attrs;
2965              $form_attrs->{action} = $at->{action} if $at->{action};
2966              my $prompt_attr = $at->{prompt};
2967            $at->{name} = {name => 'name', value => 'isindex'};            $at->{name} = {name => 'name', value => 'isindex'};
2968              delete $at->{action};
2969              delete $at->{prompt};
2970            my @tokens = (            my @tokens = (
2971                          {type => 'start tag', tag_name => 'form'},                          {type => 'start tag', tag_name => 'form',
2972                             attributes => $form_attrs},
2973                          {type => 'start tag', tag_name => 'hr'},                          {type => 'start tag', tag_name => 'hr'},
2974                          {type => 'start tag', tag_name => 'p'},                          {type => 'start tag', tag_name => 'p'},
2975                          {type => 'start tag', tag_name => 'label'},                          {type => 'start tag', tag_name => 'label'},
2976                          {type => 'character',                         );
2977                           data => 'This is a searchable index. Insert your search keywords here: '}, # SHOULD            if ($prompt_attr) {
2978                          ## TODO: make this configurable              push @tokens, {type => 'character', data => $prompt_attr->{value}};
2979              } else {
2980                push @tokens, {type => 'character',
2981                               data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
2982                ## TODO: make this configurable
2983              }
2984              push @tokens,
2985                          {type => 'start tag', tag_name => 'input', attributes => $at},                          {type => 'start tag', tag_name => 'input', attributes => $at},
2986                          #{type => 'character', data => ''}, # SHOULD                          #{type => 'character', data => ''}, # SHOULD
2987                          {type => 'end tag', tag_name => 'label'},                          {type => 'end tag', tag_name => 'label'},
2988                          {type => 'end tag', tag_name => 'p'},                          {type => 'end tag', tag_name => 'p'},
2989                          {type => 'start tag', tag_name => 'hr'},                          {type => 'start tag', tag_name => 'hr'},
2990                          {type => 'end tag', tag_name => 'form'},                          {type => 'end tag', tag_name => 'form'};
                        );  
2991            $token = shift @tokens;            $token = shift @tokens;
2992            !!!back-token (@tokens);            !!!back-token (@tokens);
2993            return;            return;
2994          }          }
2995        } elsif ({        } elsif ($token->{tag_name} eq 'textarea') {
                 textarea => 1,  
                 iframe => 1,  
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
2996          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
2997          my $el;          my $el;
2998          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $token->{tag_name}, $token->{attributes});
2999                    
3000          if ($token->{tag_name} eq 'textarea') {          ## TODO: $self->{form_element} if defined
3001            ## TODO: $self->{form_element} if defined          $self->{content_model_flag} = 'RCDATA';
3002            $self->{content_model_flag} = 'RCDATA';          delete $self->{escape}; # MUST
         } else {  
           $self->{content_model_flag} = 'CDATA';  
         }  
3003                    
3004          $insert->($el);          $insert->($el);
3005                    
3006          my $text = '';          my $text = '';
3007          if ($token->{tag_name} eq 'textarea') {          !!!next-token;
3008            !!!next-token;          if ($token->{type} eq 'character') {
3009            if ($token->{type} eq 'character') {            $token->{data} =~ s/^\x0A//;
3010              $token->{data} =~ s/^\x0A//;            unless (length $token->{data}) {
3011              unless (length $token->{data}) {              !!!next-token;
               !!!next-token;  
             }  
3012            }            }
         } else {  
           !!!next-token;  
3013          }          }
3014          while ($token->{type} eq 'character') {          while ($token->{type} eq 'character') {
3015            $text .= $token->{data};            $text .= $token->{data};
# Line 2732  sub _tree_construction_main ($) { Line 3025  sub _tree_construction_main ($) {
3025              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
3026            ## Ignore the token            ## Ignore the token
3027          } else {          } else {
3028            if ($token->{tag_name} eq 'textarea') {            !!!parse-error (type => 'in RCDATA:#'.$token->{type});
             !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
           } else {  
             !!!parse-error (type => 'in CDATA:#'.$token->{type});  
           }  
           ## ISSUE: And ignore?  
3029          }          }
3030          !!!next-token;          !!!next-token;
3031          return;          return;
3032          } elsif ({
3033                    iframe => 1,
3034                    noembed => 1,
3035                    noframes => 1,
3036                    noscript => 0, ## TODO: 1 if scripting is enabled
3037                   }->{$token->{tag_name}}) {
3038            $parse_rcdata->('CDATA', $insert);
3039            return;
3040        } elsif ($token->{tag_name} eq 'select') {        } elsif ($token->{tag_name} eq 'select') {
3041          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
3042                    
# Line 2771  sub _tree_construction_main ($) { Line 3067  sub _tree_construction_main ($) {
3067        }        }
3068      } elsif ($token->{type} eq 'end tag') {      } elsif ($token->{type} eq 'end tag') {
3069        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
3070          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {          if (@{$self->{open_elements}} > 1 and
3071            ## ISSUE: There is an issue in the spec.              $self->{open_elements}->[1]->[1] eq 'body') {
3072            if ($self->{open_elements}->[-1]->[1] ne 'body') {            for (@{$self->{open_elements}}) {
3073              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              unless ({
3074                           dd => 1, dt => 1, li => 1, p => 1, td => 1,
3075                           th => 1, tr => 1, body => 1, html => 1,
3076                         tbody => 1, tfoot => 1, thead => 1,
3077                        }->{$_->[1]}) {
3078                  !!!parse-error (type => 'not closed:'.$_->[1]);
3079                }
3080            }            }
3081    
3082            $self->{insertion_mode} = 'after body';            $self->{insertion_mode} = 'after body';
3083            !!!next-token;            !!!next-token;
3084            return;            return;
# Line 2804  sub _tree_construction_main ($) { Line 3107  sub _tree_construction_main ($) {
3107                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
3108                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
3109                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
                 form => 1,  
3110                  p => 1,                  p => 1,
3111                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
3112                  button => 1, marquee => 1, object => 1,                  button => 1, marquee => 1, object => 1,
# Line 2821  sub _tree_construction_main ($) { Line 3123  sub _tree_construction_main ($) {
3123                   li => ($token->{tag_name} ne 'li'),                   li => ($token->{tag_name} ne 'li'),
3124                   p => ($token->{tag_name} ne 'p'),                   p => ($token->{tag_name} ne 'p'),
3125                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
3126                     tbody => 1, tfoot=> 1, thead => 1,
3127                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
3128                !!!back-token;                !!!back-token;
3129                $token = {type => 'end tag',                $token = {type => 'end tag',
# Line 2838  sub _tree_construction_main ($) { Line 3141  sub _tree_construction_main ($) {
3141          } # INSCOPE          } # INSCOPE
3142                    
3143          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
3144            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            if (defined $i) {
3145                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3146              } else {
3147                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3148              }
3149          }          }
3150                    
3151          splice @{$self->{open_elements}}, $i if defined $i;          if (defined $i) {
3152          undef $self->{form_element} if $token->{tag_name} eq 'form';            splice @{$self->{open_elements}}, $i;
3153            } elsif ($token->{tag_name} eq 'p') {
3154              ## As if <p>, then reprocess the current token
3155              my $el;
3156              !!!create-element ($el, 'p');
3157              $insert->($el);
3158            }
3159          $clear_up_to_marker->()          $clear_up_to_marker->()
3160            if {            if {
3161              button => 1, marquee => 1, object => 1,              button => 1, marquee => 1, object => 1,
3162            }->{$token->{tag_name}};            }->{$token->{tag_name}};
3163          !!!next-token;          !!!next-token;
3164          return;          return;
3165          } elsif ($token->{tag_name} eq 'form') {
3166            ## has an element in scope
3167            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3168              my $node = $self->{open_elements}->[$_];
3169              if ($node->[1] eq $token->{tag_name}) {
3170                ## generate implied end tags
3171                if ({
3172                     dd => 1, dt => 1, li => 1, p => 1,
3173                     td => 1, th => 1, tr => 1,
3174                     tbody => 1, tfoot=> 1, thead => 1,
3175                    }->{$self->{open_elements}->[-1]->[1]}) {
3176                  !!!back-token;
3177                  $token = {type => 'end tag',
3178                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3179                  return;
3180                }
3181                last INSCOPE;
3182              } elsif ({
3183                        table => 1, caption => 1, td => 1, th => 1,
3184                        button => 1, marquee => 1, object => 1, html => 1,
3185                       }->{$node->[1]}) {
3186                last INSCOPE;
3187              }
3188            } # INSCOPE
3189            
3190            if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
3191              pop @{$self->{open_elements}};
3192            } else {
3193              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3194            }
3195    
3196            undef $self->{form_element};
3197            !!!next-token;
3198            return;
3199        } elsif ({        } elsif ({
3200                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
3201                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 2863  sub _tree_construction_main ($) { Line 3210  sub _tree_construction_main ($) {
3210              if ({              if ({
3211                   dd => 1, dt => 1, li => 1, p => 1,                   dd => 1, dt => 1, li => 1, p => 1,
3212                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
3213                     tbody => 1, tfoot=> 1, thead => 1,
3214                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
3215                !!!back-token;                !!!back-token;
3216                $token = {type => 'end tag',                $token = {type => 'end tag',
# Line 2893  sub _tree_construction_main ($) { Line 3241  sub _tree_construction_main ($) {
3241                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
3242                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
3243          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token->{tag_name});
3244  ## TODO: <http://html5.org/tools/web-apps-tracker?from=883&to=884>          return;
3245          } elsif ($token->{tag_name} eq 'br') {
3246            !!!parse-error (type => 'unmatched end tag:br');
3247    
3248            ## As if <br>
3249            $reconstruct_active_formatting_elements->($insert_to_current);
3250            
3251            my $el;
3252            !!!create-element ($el, 'br');
3253            $insert->($el);
3254            
3255            ## Ignore the token.
3256            !!!next-token;
3257          return;          return;
3258        } elsif ({        } elsif ({
3259                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
3260                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
3261                  tbody => 1, td => 1, tfoot => 1, th => 1,                  tbody => 1, td => 1, tfoot => 1, th => 1,
3262                  thead => 1, tr => 1,                  thead => 1, tr => 1,
3263                  area => 1, basefont => 1, bgsound => 1, br => 1,                  area => 1, basefont => 1, bgsound => 1,
3264                  embed => 1, hr => 1, iframe => 1, image => 1,                  embed => 1, hr => 1, iframe => 1, image => 1,
3265                  img => 1, input => 1, isindex => 1, noembed => 1,                  img => 1, input => 1, isindex => 1, noembed => 1,
3266                  noframes => 1, param => 1, select => 1, spacer => 1,                  noframes => 1, param => 1, select => 1, spacer => 1,
# Line 2927  sub _tree_construction_main ($) { Line 3287  sub _tree_construction_main ($) {
3287              if ({              if ({
3288                   dd => 1, dt => 1, li => 1, p => 1,                   dd => 1, dt => 1, li => 1, p => 1,
3289                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
3290                     tbody => 1, tfoot=> 1, thead => 1,
3291                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
3292                !!!back-token;                !!!back-token;
3293                $token = {type => 'end tag',                $token = {type => 'end tag',
# Line 2950  sub _tree_construction_main ($) { Line 3311  sub _tree_construction_main ($) {
3311                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
3312                  ($special_category->{$node->[1]} or                  ($special_category->{$node->[1]} or
3313                   $scoping_category->{$node->[1]})) {                   $scoping_category->{$node->[1]})) {
3314                !!!parse-error (type => 'not closed:'.$node->[1]);                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3315                ## Ignore the token                ## Ignore the token
3316                !!!next-token;                !!!next-token;
3317                last S2;                last S2;
# Line 2970  sub _tree_construction_main ($) { Line 3331  sub _tree_construction_main ($) {
3331    }; # $in_body    }; # $in_body
3332    
3333    B: {    B: {
3334      if ($phase eq 'main') {      if ($self->{insertion_mode} ne 'trailing end') {
3335        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} eq 'DOCTYPE') {
3336          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE');
3337          ## Ignore the token          ## Ignore the token
# Line 2979  sub _tree_construction_main ($) { Line 3340  sub _tree_construction_main ($) {
3340          redo B;          redo B;
3341        } elsif ($token->{type} eq 'start tag' and        } elsif ($token->{type} eq 'start tag' and
3342                 $token->{tag_name} eq 'html') {                 $token->{tag_name} eq 'html') {
3343          ## TODO: unless it is the first start tag token, parse-error  ## ISSUE: "aa<html>" is not a parse error.
3344    ## ISSUE: "<html>" in fragment is not a parse error.
3345            unless ($token->{first_start_tag}) {
3346              !!!parse-error (type => 'not first start tag');
3347            }
3348          my $top_el = $self->{open_elements}->[0]->[0];          my $top_el = $self->{open_elements}->[0]->[0];
3349          for my $attr_name (keys %{$token->{attributes}}) {          for my $attr_name (keys %{$token->{attributes}}) {
3350            unless ($top_el->has_attribute_ns (undef, $attr_name)) {            unless ($top_el->has_attribute_ns (undef, $attr_name)) {
# Line 2994  sub _tree_construction_main ($) { Line 3359  sub _tree_construction_main ($) {
3359          ## Generate implied end tags          ## Generate implied end tags
3360          if ({          if ({
3361               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,
3362                 tbody => 1, tfoot=> 1, thead => 1,
3363              }->{$self->{open_elements}->[-1]->[1]}) {              }->{$self->{open_elements}->[-1]->[1]}) {
3364            !!!back-token;            !!!back-token;
3365            $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};            $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};
# Line 3053  sub _tree_construction_main ($) { Line 3419  sub _tree_construction_main ($) {
3419              }              }
3420              redo B;              redo B;
3421            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
3422              if ($token->{tag_name} eq 'html') {              if ({
3423                     head => 1, body => 1, html => 1,
3424                     p => 1, br => 1,
3425                    }->{$token->{tag_name}}) {
3426                ## As if <head>                ## As if <head>
3427                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head');
3428                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
# Line 3063  sub _tree_construction_main ($) { Line 3432  sub _tree_construction_main ($) {
3432                redo B;                redo B;
3433              } else {              } else {
3434                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3435                ## Ignore the token                ## Ignore the token ## ISSUE: An issue in the spec.
3436                !!!next-token;                !!!next-token;
3437                redo B;                redo B;
3438              }              }
3439            } else {            } else {
3440              die "$0: $token->{type}: Unknown type";              die "$0: $token->{type}: Unknown type";
3441            }            }
3442          } elsif ($self->{insertion_mode} eq 'in head') {          } elsif ($self->{insertion_mode} eq 'in head' or
3443                     $self->{insertion_mode} eq 'in head noscript' or
3444                     $self->{insertion_mode} eq 'after head') {
3445            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
3446              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3447                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
# Line 3087  sub _tree_construction_main ($) { Line 3458  sub _tree_construction_main ($) {
3458              !!!next-token;              !!!next-token;
3459              redo B;              redo B;
3460            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
3461              if ($token->{tag_name} eq 'title') {              if ({base => ($self->{insertion_mode} eq 'in head' or
3462                ## NOTE: There is an "as if in head" code clone                            $self->{insertion_mode} eq 'after head'),
3463                my $title_el;                   link => 1}->{$token->{tag_name}}) {
3464                !!!create-element ($title_el, 'title', $token->{attributes});                ## NOTE: There is a "as if in head" code clone.
3465                (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])                if ($self->{insertion_mode} eq 'after head') {
3466                  ->append_child ($title_el);                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3467                $self->{content_model_flag} = 'RCDATA';                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3468                  }
3469                my $text = '';                !!!insert-element ($token->{tag_name}, $token->{attributes});
3470                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3471                  pop @{$self->{open_elements}}
3472                      if $self->{insertion_mode} eq 'after head';
3473                !!!next-token;                !!!next-token;
3474                while ($token->{type} eq 'character') {                redo B;
3475                  $text .= $token->{data};              } elsif ($token->{tag_name} eq 'meta') {
3476                  !!!next-token;                ## NOTE: There is a "as if in head" code clone.
3477                  if ($self->{insertion_mode} eq 'after head') {
3478                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3479                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3480                }                }
3481                if (length $text) {                !!!insert-element ($token->{tag_name}, $token->{attributes});
3482                  $title_el->manakai_append_text ($text);                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3483    
3484                  unless ($self->{confident}) {
3485                    my $charset;
3486                    if ($token->{attributes}->{charset}) { ## TODO: And if supported
3487                      $charset = $token->{attributes}->{charset}->{value};
3488                    }
3489                    if ($token->{attributes}->{'http-equiv'}) {
3490                      ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
3491                      if ($token->{attributes}->{'http-equiv'}->{value}
3492                          =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
3493                              [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
3494                              ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
3495                        $charset = defined $1 ? $1 : defined $2 ? $2 : $3;
3496                      } ## TODO: And if supported
3497                    }
3498                    ## TODO: Change the encoding
3499                }                }
3500                  
3501                $self->{content_model_flag} = 'PCDATA';                ## TODO: Extracting |charset| from |meta|.
3502                                pop @{$self->{open_elements}}
3503                if ($token->{type} eq 'end tag' and                    if $self->{insertion_mode} eq 'after head';
3504                    $token->{tag_name} eq 'title') {                !!!next-token;
3505                  redo B;
3506                } elsif ($token->{tag_name} eq 'title' and
3507                         $self->{insertion_mode} eq 'in head') {
3508                  ## NOTE: There is a "as if in head" code clone.
3509                  if ($self->{insertion_mode} eq 'after head') {
3510                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3511                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3512                  }
3513                  my $parent = defined $self->{head_element} ? $self->{head_element}
3514                      : $self->{open_elements}->[-1]->[0];
3515                  $parse_rcdata->('RCDATA', sub { $parent->append_child ($_[0]) });
3516                  pop @{$self->{open_elements}}
3517                      if $self->{insertion_mode} eq 'after head';
3518                  redo B;
3519                } elsif ($token->{tag_name} eq 'style') {
3520                  ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
3521                  ## insertion mode 'in head')
3522                  ## NOTE: There is a "as if in head" code clone.
3523                  if ($self->{insertion_mode} eq 'after head') {
3524                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3525                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3526                  }
3527                  $parse_rcdata->('CDATA', $insert_to_current);
3528                  pop @{$self->{open_elements}}
3529                      if $self->{insertion_mode} eq 'after head';
3530                  redo B;
3531                } elsif ($token->{tag_name} eq 'noscript') {
3532                  if ($self->{insertion_mode} eq 'in head') {
3533                    ## NOTE: and scripting is disalbed
3534                    !!!insert-element ($token->{tag_name}, $token->{attributes});
3535                    $self->{insertion_mode} = 'in head noscript';
3536                    !!!next-token;
3537                    redo B;
3538                  } elsif ($self->{insertion_mode} eq 'in head noscript') {
3539                    !!!parse-error (type => 'in noscript:noscript');
3540                  ## Ignore the token                  ## Ignore the token
3541                    redo B;
3542                } else {                } else {
3543                  !!!parse-error (type => 'in RCDATA:#'.$token->{type});                  #
                 ## ISSUE: And ignore?  
3544                }                }
3545                } elsif ($token->{tag_name} eq 'head' and
3546                         $self->{insertion_mode} ne 'after head') {
3547                  !!!parse-error (type => 'in head:head'); # or in head noscript
3548                  ## Ignore the token
3549                !!!next-token;                !!!next-token;
3550                redo B;                redo B;
3551              } elsif ($token->{tag_name} eq 'style') {              } elsif ($self->{insertion_mode} ne 'in head noscript' and
3552                $style_start_tag->();                       $token->{tag_name} eq 'script') {
3553                redo B;                if ($self->{insertion_mode} eq 'after head') {
3554              } elsif ($token->{tag_name} eq 'script') {                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3555                $script_start_tag->();                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3556                  }
3557                  ## NOTE: There is a "as if in head" code clone.
3558                  $script_start_tag->($insert_to_current);
3559                  pop @{$self->{open_elements}}
3560                      if $self->{insertion_mode} eq 'after head';
3561                redo B;                redo B;
3562              } elsif ({base => 1, link => 1, meta => 1}->{$token->{tag_name}}) {              } elsif ($self->{insertion_mode} eq 'after head' and
3563                ## NOTE: There are "as if in head" code clones                       $token->{tag_name} eq 'body') {
3564                my $el;                !!!insert-element ('body', $token->{attributes});
3565                !!!create-element ($el, $token->{tag_name}, $token->{attributes});                $self->{insertion_mode} = 'in body';
               (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])  
                 ->append_child ($el);  
   
3566                !!!next-token;                !!!next-token;
3567                redo B;                redo B;
3568              } elsif ($token->{tag_name} eq 'head') {              } elsif ($self->{insertion_mode} eq 'after head' and
3569                !!!parse-error (type => 'in head:head');                       $token->{tag_name} eq 'frameset') {
3570                ## Ignore the token                !!!insert-element ('frameset', $token->{attributes});
3571                  $self->{insertion_mode} = 'in frameset';
3572                !!!next-token;                !!!next-token;
3573                redo B;                redo B;
3574              } else {              } else {
3575                #                #
3576              }              }
3577            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
3578              if ($token->{tag_name} eq 'head') {              if ($self->{insertion_mode} eq 'in head' and
3579                if ($self->{open_elements}->[-1]->[1] eq 'head') {                  $token->{tag_name} eq 'head') {
3580                  pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
               } else {  
                 !!!parse-error (type => 'unmatched end tag:head');  
               }  
3581                $self->{insertion_mode} = 'after head';                $self->{insertion_mode} = 'after head';
3582                !!!next-token;                !!!next-token;
3583                redo B;                redo B;
3584              } elsif ($token->{tag_name} eq 'html') {              } elsif ($self->{insertion_mode} eq 'in head noscript' and
3585                    $token->{tag_name} eq 'noscript') {
3586                  pop @{$self->{open_elements}};
3587                  $self->{insertion_mode} = 'in head';
3588                  !!!next-token;
3589                  redo B;
3590                } elsif ($self->{insertion_mode} eq 'in head' and
3591                         {
3592                          body => 1, html => 1,
3593                          p => 1, br => 1,
3594                         }->{$token->{tag_name}}) {
3595                #                #
3596              } else {              } elsif ($self->{insertion_mode} eq 'in head noscript' and
3597                         {
3598                          p => 1, br => 1,
3599                         }->{$token->{tag_name}}) {
3600                  #
3601                } elsif ($self->{insertion_mode} ne 'after head') {
3602                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3603                ## Ignore the token                ## Ignore the token
3604                !!!next-token;                !!!next-token;
3605                redo B;                redo B;
3606                } else {
3607                  #
3608              }              }
3609            } else {            } else {
3610              #              #
3611            }            }
3612    
3613            if ($self->{open_elements}->[-1]->[1] eq 'head') {            ## As if </head> or </noscript> or <body>
3614              ## As if </head>            if ($self->{insertion_mode} eq 'in head') {
3615                pop @{$self->{open_elements}};
3616                $self->{insertion_mode} = 'after head';
3617              } elsif ($self->{insertion_mode} eq 'in head noscript') {
3618              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
3619                !!!parse-error (type => 'in noscript:'.(defined $token->{tag_name} ? ($token->{type} eq 'end tag' ? '/' : '') . $token->{tag_name} : '#' . $token->{type}));
3620                $self->{insertion_mode} = 'in head';
3621              } else { # 'after head'
3622                !!!insert-element ('body');
3623                $self->{insertion_mode} = 'in body';
3624            }            }
           $self->{insertion_mode} = 'after head';  
3625            ## reprocess            ## reprocess
3626            redo B;            redo B;
3627    
3628            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
         } elsif ($self->{insertion_mode} eq 'after 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;  
               }  
             }  
               
             #  
           } 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;  
3629          } elsif ($self->{insertion_mode} eq 'in body') {          } elsif ($self->{insertion_mode} eq 'in body') {
3630            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
3631              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
# Line 3371  sub _tree_construction_main ($) { Line 3780  sub _tree_construction_main ($) {
3780                if ({                if ({
3781                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3782                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3783                       tbody => 1, tfoot=> 1, thead => 1,
3784                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3785                  !!!back-token; # <table>                  !!!back-token; # <table>
3786                  $token = {type => 'end tag', tag_name => 'table'};                  $token = {type => 'end tag', tag_name => 'table'};
# Line 3419  sub _tree_construction_main ($) { Line 3829  sub _tree_construction_main ($) {
3829                if ({                if ({
3830                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3831                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3832                       tbody => 1, tfoot=> 1, thead => 1,
3833                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3834                  !!!back-token;                  !!!back-token;
3835                  $token = {type => 'end tag',                  $token = {type => 'end tag',
# Line 3502  sub _tree_construction_main ($) { Line 3913  sub _tree_construction_main ($) {
3913                if ({                if ({
3914                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3915                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3916                       tbody => 1, tfoot=> 1, thead => 1,
3917                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3918                  !!!back-token; # <?>                  !!!back-token; # <?>
3919                  $token = {type => 'end tag', tag_name => 'caption'};                  $token = {type => 'end tag', tag_name => 'caption'};
# Line 3552  sub _tree_construction_main ($) { Line 3964  sub _tree_construction_main ($) {
3964                if ({                if ({
3965                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3966                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3967                       tbody => 1, tfoot=> 1, thead => 1,
3968                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3969                  !!!back-token;                  !!!back-token;
3970                  $token = {type => 'end tag',                  $token = {type => 'end tag',
# Line 3599  sub _tree_construction_main ($) { Line 4012  sub _tree_construction_main ($) {
4012                if ({                if ({
4013                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
4014                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
4015                       tbody => 1, tfoot=> 1, thead => 1,
4016                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
4017                  !!!back-token; # </table>                  !!!back-token; # </table>
4018                  $token = {type => 'end tag', tag_name => 'caption'};                  $token = {type => 'end tag', tag_name => 'caption'};
# Line 3864  sub _tree_construction_main ($) { Line 4278  sub _tree_construction_main ($) {
4278                if ({                if ({
4279                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
4280                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
4281                       tbody => 1, tfoot=> 1, thead => 1,
4282                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
4283                  !!!back-token; # <table>                  !!!back-token; # <table>
4284                  $token = {type => 'end tag', tag_name => 'table'};                  $token = {type => 'end tag', tag_name => 'table'};
# Line 4132  sub _tree_construction_main ($) { Line 4547  sub _tree_construction_main ($) {
4547                if ({                if ({
4548                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
4549                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
4550                       tbody => 1, tfoot=> 1, thead => 1,
4551                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
4552                  !!!back-token; # <table>                  !!!back-token; # <table>
4553                  $token = {type => 'end tag', tag_name => 'table'};                  $token = {type => 'end tag', tag_name => 'table'};
# Line 4373  sub _tree_construction_main ($) { Line 4789  sub _tree_construction_main ($) {
4789                     td => ($token->{tag_name} eq 'th'),                     td => ($token->{tag_name} eq 'th'),
4790                     th => ($token->{tag_name} eq 'td'),                     th => ($token->{tag_name} eq 'td'),
4791                     tr => 1,                     tr => 1,
4792                       tbody => 1, tfoot=> 1, thead => 1,
4793                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
4794                  !!!back-token;                  !!!back-token;
4795                  $token = {type => 'end tag',                  $token = {type => 'end tag',
# Line 4624  sub _tree_construction_main ($) { Line 5041  sub _tree_construction_main ($) {
5041          } elsif ($self->{insertion_mode} eq 'after body') {          } elsif ($self->{insertion_mode} eq 'after body') {
5042            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
5043              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5044                  my $data = $1;
5045                ## As if in body                ## As if in body
5046                $reconstruct_active_formatting_elements->($insert_to_current);                $reconstruct_active_formatting_elements->($insert_to_current);
5047                                
5048                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5049    
5050                unless (length $token->{data}) {                unless (length $token->{data}) {
5051                  !!!next-token;                  !!!next-token;
# Line 4653  sub _tree_construction_main ($) { Line 5071  sub _tree_construction_main ($) {
5071                  !!!next-token;                  !!!next-token;
5072                  redo B;                  redo B;
5073                } else {                } else {
5074                  $phase = 'trailing end';                  $previous_insertion_mode = $self->{insertion_mode};
5075                    $self->{insertion_mode} = 'trailing end';
5076                  !!!next-token;                  !!!next-token;
5077                  redo B;                  redo B;
5078                }                }
# Line 4670  sub _tree_construction_main ($) { Line 5089  sub _tree_construction_main ($) {
5089          } elsif ($self->{insertion_mode} eq 'in frameset') {          } elsif ($self->{insertion_mode} eq 'in frameset') {
5090            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
5091              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5092                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5093    
5094                unless (length $token->{data}) {                unless (length $token->{data}) {
5095                  !!!next-token;                  !!!next-token;
# Line 4725  sub _tree_construction_main ($) { Line 5144  sub _tree_construction_main ($) {
5144            }            }
5145                        
5146            if (defined $token->{tag_name}) {            if (defined $token->{tag_name}) {
5147              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset:'.($token->{type} eq 'end tag' ? '/' : '').$token->{tag_name});
5148            } else {            } else {
5149              !!!parse-error (type => 'in frameset:#'.$token->{type});              !!!parse-error (type => 'in frameset:#'.$token->{type});
5150            }            }
# Line 4735  sub _tree_construction_main ($) { Line 5154  sub _tree_construction_main ($) {
5154          } elsif ($self->{insertion_mode} eq 'after frameset') {          } elsif ($self->{insertion_mode} eq 'after frameset') {
5155            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
5156              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5157                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5158    
5159                unless (length $token->{data}) {                unless (length $token->{data}) {
5160                  !!!next-token;                  !!!next-token;
# Line 4743  sub _tree_construction_main ($) { Line 5162  sub _tree_construction_main ($) {
5162                }                }
5163              }              }
5164    
5165              #              if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
5166                  !!!parse-error (type => 'after frameset:#character');
5167    
5168                  ## Ignore the token.
5169                  if (length $token->{data}) {
5170                    ## reprocess the rest of characters
5171                  } else {
5172                    !!!next-token;
5173                  }
5174                  redo B;
5175                }
5176            } elsif ($token->{type} eq 'comment') {            } elsif ($token->{type} eq 'comment') {
5177              my $comment = $self->{document}->create_comment ($token->{data});              my $comment = $self->{document}->create_comment ($token->{data});
5178              $self->{open_elements}->[-1]->[0]->append_child ($comment);              $self->{open_elements}->[-1]->[0]->append_child ($comment);
# Line 4758  sub _tree_construction_main ($) { Line 5187  sub _tree_construction_main ($) {
5187              }              }
5188            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
5189              if ($token->{tag_name} eq 'html') {              if ($token->{tag_name} eq 'html') {
5190                $phase = 'trailing end';                $previous_insertion_mode = $self->{insertion_mode};
5191                  $self->{insertion_mode} = 'trailing end';
5192                !!!next-token;                !!!next-token;
5193                redo B;                redo B;
5194              } else {              } else {
5195                #                #
5196              }              }
5197            } else {            } else {
5198              #              die "$0: $token->{type}: Unknown token type";
5199            }            }
5200                        
5201            if (defined $token->{tag_name}) {            !!!parse-error (type => 'after frameset:'.($token->{tag_name} eq 'end tag' ? '/' : '').$token->{tag_name});
             !!!parse-error (type => 'after frameset:'.$token->{tag_name});  
           } else {  
             !!!parse-error (type => 'after frameset:#'.$token->{type});  
           }  
5202            ## Ignore the token            ## Ignore the token
5203            !!!next-token;            !!!next-token;
5204            redo B;            redo B;
# Line 4782  sub _tree_construction_main ($) { Line 5208  sub _tree_construction_main ($) {
5208            die "$0: $self->{insertion_mode}: Unknown insertion mode";            die "$0: $self->{insertion_mode}: Unknown insertion mode";
5209          }          }
5210        }        }
5211      } elsif ($phase eq 'trailing end') {      } elsif ($self->{insertion_mode} eq 'trailing end') {
5212        ## states in the main stage is preserved yet # MUST        ## states in the main stage is preserved yet # MUST
5213                
5214        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} eq 'DOCTYPE') {
# Line 4802  sub _tree_construction_main ($) { Line 5228  sub _tree_construction_main ($) {
5228            ## NOTE: The insertion mode in the main phase            ## NOTE: The insertion mode in the main phase
5229            ## just before the phase has been changed to the trailing            ## just before the phase has been changed to the trailing
5230            ## end phase is either "after body" or "after frameset".            ## end phase is either "after body" or "after frameset".
5231            $reconstruct_active_formatting_elements->($insert_to_current)            $reconstruct_active_formatting_elements->($insert_to_current);
             if $phase eq 'main';  
5232                        
5233            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);
5234                        
# Line 4814  sub _tree_construction_main ($) { Line 5239  sub _tree_construction_main ($) {
5239          }          }
5240    
5241          !!!parse-error (type => 'after html:#character');          !!!parse-error (type => 'after html:#character');
5242          $phase = 'main';          $self->{insertion_mode} = $previous_insertion_mode;
5243          ## reprocess          ## reprocess
5244          redo B;          redo B;
5245        } elsif ($token->{type} eq 'start tag' or        } elsif ($token->{type} eq 'start tag' or
5246                 $token->{type} eq 'end tag') {                 $token->{type} eq 'end tag') {
5247          !!!parse-error (type => 'after html:'.$token->{tag_name});          !!!parse-error (type => 'after html:'.($token->{type} eq 'end tag' ? '/' : '').$token->{tag_name});
5248          $phase = 'main';          $self->{insertion_mode} = $previous_insertion_mode;
5249          ## reprocess          ## reprocess
5250          redo B;          redo B;
5251        } elsif ($token->{type} eq 'end-of-file') {        } elsif ($token->{type} eq 'end-of-file') {
# Line 4865  sub set_inner_html ($$$) { Line 5290  sub set_inner_html ($$$) {
5290      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
5291    
5292      ## Step 1 # MUST      ## Step 1 # MUST
5293      my $doc = $node->owner_document->implementation->create_document;      my $this_doc = $node->owner_document;
5294      ## TODO: Mark as HTML document      my $doc = $this_doc->implementation->create_document;
5295        $doc->manakai_is_html (1);
5296      my $p = $class->new;      my $p = $class->new;
5297      $p->{document} = $doc;      $p->{document} = $doc;
5298    
# Line 4876  sub set_inner_html ($$$) { Line 5302  sub set_inner_html ($$$) {
5302      my $column = 0;      my $column = 0;
5303      $p->{set_next_input_character} = sub {      $p->{set_next_input_character} = sub {
5304        my $self = shift;        my $self = shift;
5305    
5306          pop @{$self->{prev_input_character}};
5307          unshift @{$self->{prev_input_character}}, $self->{next_input_character};
5308    
5309        $self->{next_input_character} = -1 and return if $i >= length $$s;        $self->{next_input_character} = -1 and return if $i >= length $$s;
5310        $self->{next_input_character} = ord substr $$s, $i++, 1;        $self->{next_input_character} = ord substr $$s, $i++, 1;
5311        $column++;        $column++;
# Line 4884  sub set_inner_html ($$$) { Line 5314  sub set_inner_html ($$$) {
5314          $line++;          $line++;
5315          $column = 0;          $column = 0;
5316        } elsif ($self->{next_input_character} == 0x000D) { # CR        } elsif ($self->{next_input_character} == 0x000D) { # CR
5317          if ($i >= length $$s) {          $i++ if substr ($$s, $i, 1) eq "\x0A";
           #  
         } else {  
           my $next_char = ord substr $$s, $i++, 1;  
           if ($next_char == 0x000A) { # LF  
             #  
           } else {  
             push @{$self->{char}}, $next_char;  
           }  
         }  
5318          $self->{next_input_character} = 0x000A; # LF # MUST          $self->{next_input_character} = 0x000A; # LF # MUST
5319          $line++;          $line++;
5320          $column = 0;          $column = 0;
5321        } elsif ($self->{next_input_character} > 0x10FFFF) {        } elsif ($self->{next_input_character} > 0x10FFFF) {
5322          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
5323        } elsif ($self->{next_input_character} == 0x0000) { # NULL        } elsif ($self->{next_input_character} == 0x0000) { # NULL
5324            !!!parse-error (type => 'NULL');
5325          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
5326        }        }
5327      };      };
5328        $p->{prev_input_character} = [-1, -1, -1];
5329        $p->{next_input_character} = -1;
5330            
5331      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
5332        my (%opt) = @_;        my (%opt) = @_;
# Line 4981  sub set_inner_html ($$$) { Line 5405  sub set_inner_html ($$$) {
5405      ## Step 12 # MUST      ## Step 12 # MUST
5406      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
5407      for (@cn) {      for (@cn) {
5408          $this_doc->adopt_node ($_);
5409        $node->append_child ($_);        $node->append_child ($_);
5410      }      }
5411      ## ISSUE: adopt_node? mutation events?      ## ISSUE: mutation events?
5412    
5413      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
5414    } else {    } else {
# Line 5028  sub get_inner_html ($$$) { Line 5453  sub get_inner_html ($$$) {
5453            
5454      my $nt = $child->node_type;      my $nt = $child->node_type;
5455      if ($nt == 1) { # Element      if ($nt == 1) { # Element
5456        my $tag_name = lc $child->tag_name; ## ISSUE: Definition of "lowercase"        my $tag_name = $child->tag_name; ## TODO: manakai_tag_name
5457        $s .= '<' . $tag_name;        $s .= '<' . $tag_name;
5458          ## NOTE: Non-HTML case:
5459        ## ISSUE: Non-html elements        ## <http://permalink.gmane.org/gmane.org.w3c.whatwg.discuss/11191>
5460    
5461        my @attrs = @{$child->attributes}; # sort order MUST be stable        my @attrs = @{$child->attributes}; # sort order MUST be stable
5462        for my $attr (@attrs) { # order is implementation dependent        for my $attr (@attrs) { # order is implementation dependent
5463          my $attr_name = lc $attr->name; ## ISSUE: Definition of "lowercase"          my $attr_name = $attr->name; ## TODO: manakai_name
5464          $s .= ' ' . $attr_name . '="';          $s .= ' ' . $attr_name . '="';
5465          my $attr_value = $attr->value;          my $attr_value = $attr->value;
5466          ## escape          ## escape
# Line 5054  sub get_inner_html ($$$) { Line 5479  sub get_inner_html ($$$) {
5479          spacer => 1, wbr => 1,          spacer => 1, wbr => 1,
5480        }->{$tag_name};        }->{$tag_name};
5481    
5482          $s .= "\x0A" if $tag_name eq 'pre' or $tag_name eq 'textarea';
5483    
5484        if (not $in_cdata and {        if (not $in_cdata and {
5485          style => 1, script => 1, xmp => 1, iframe => 1,          style => 1, script => 1, xmp => 1, iframe => 1,
5486          noembed => 1, noframes => 1, noscript => 1,          noembed => 1, noframes => 1, noscript => 1,
5487            plaintext => 1,
5488        }->{$tag_name}) {        }->{$tag_name}) {
5489          unshift @node, 'cdata-out';          unshift @node, 'cdata-out';
5490          $in_cdata = 1;          $in_cdata = 1;

Legend:
Removed from v.1.11  
changed lines
  Added in v.1.35

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24