/[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.35 by wakaba, Mon Jul 16 03:21:04 2007 UTC revision 1.162 by wakaba, Thu Sep 11 09:12:27 2008 UTC
# Line 1  Line 1 
1  package Whatpm::HTML;  package Whatpm::HTML;
2  use strict;  use strict;
3  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
4    use Error qw(:try);
5    
6  ## ISSUE:  ## ISSUE:
7  ## var doc = implementation.createDocument (null, null, null);  ## var doc = implementation.createDocument (null, null, null);
8  ## doc.write ('');  ## doc.write ('');
9  ## alert (doc.compatMode);  ## alert (doc.compatMode);
10    
11  ## ISSUE: HTML5 revision 967 says that the encoding layer MUST NOT  require IO::Handle;
12  ## strip BOM and the HTML layer MUST ignore it.  Whether we can do it  
13  ## is not yet clear.  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
14  ## "{U+FEFF}..." in UTF-16BE/UTF-16LE is three or four characters?  my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
15  ## "{U+FEFF}..." in GB18030?  my $SVG_NS = q<http://www.w3.org/2000/svg>;
16    my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
17  my $permitted_slash_tag_name = {  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
18    base => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
19    link => 1,  
20    meta => 1,  sub A_EL () { 0b1 }
21    hr => 1,  sub ADDRESS_EL () { 0b10 }
22    br => 1,  sub BODY_EL () { 0b100 }
23    img=> 1,  sub BUTTON_EL () { 0b1000 }
24    embed => 1,  sub CAPTION_EL () { 0b10000 }
25    param => 1,  sub DD_EL () { 0b100000 }
26    area => 1,  sub DIV_EL () { 0b1000000 }
27    col => 1,  sub DT_EL () { 0b10000000 }
28    input => 1,  sub FORM_EL () { 0b100000000 }
29    sub FORMATTING_EL () { 0b1000000000 }
30    sub FRAMESET_EL () { 0b10000000000 }
31    sub HEADING_EL () { 0b100000000000 }
32    sub HTML_EL () { 0b1000000000000 }
33    sub LI_EL () { 0b10000000000000 }
34    sub NOBR_EL () { 0b100000000000000 }
35    sub OPTION_EL () { 0b1000000000000000 }
36    sub OPTGROUP_EL () { 0b10000000000000000 }
37    sub P_EL () { 0b100000000000000000 }
38    sub SELECT_EL () { 0b1000000000000000000 }
39    sub TABLE_EL () { 0b10000000000000000000 }
40    sub TABLE_CELL_EL () { 0b100000000000000000000 }
41    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
42    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
43    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
44    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
45    sub FOREIGN_EL () { 0b10000000000000000000000000 }
46    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
47    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
48    sub RUBY_EL () { 0b10000000000000000000000000000 }
49    sub RUBY_COMPONENT_EL () { 0b100000000000000000000000000000 }
50    
51    sub TABLE_ROWS_EL () {
52      TABLE_EL |
53      TABLE_ROW_EL |
54      TABLE_ROW_GROUP_EL
55    }
56    
57    ## NOTE: Used in "generate implied end tags" algorithm.
58    ## NOTE: There is a code where a modified version of END_TAG_OPTIONAL_EL
59    ## is used in "generate implied end tags" implementation (search for the
60    ## function mae).
61    sub END_TAG_OPTIONAL_EL () {
62      DD_EL |
63      DT_EL |
64      LI_EL |
65      P_EL |
66      RUBY_COMPONENT_EL
67    }
68    
69    ## NOTE: Used in </body> and EOF algorithms.
70    sub ALL_END_TAG_OPTIONAL_EL () {
71      DD_EL |
72      DT_EL |
73      LI_EL |
74      P_EL |
75    
76      BODY_EL |
77      HTML_EL |
78      TABLE_CELL_EL |
79      TABLE_ROW_EL |
80      TABLE_ROW_GROUP_EL
81    }
82    
83    sub SCOPING_EL () {
84      BUTTON_EL |
85      CAPTION_EL |
86      HTML_EL |
87      TABLE_EL |
88      TABLE_CELL_EL |
89      MISC_SCOPING_EL
90    }
91    
92    sub TABLE_SCOPING_EL () {
93      HTML_EL |
94      TABLE_EL
95    }
96    
97    sub TABLE_ROWS_SCOPING_EL () {
98      HTML_EL |
99      TABLE_ROW_GROUP_EL
100    }
101    
102    sub TABLE_ROW_SCOPING_EL () {
103      HTML_EL |
104      TABLE_ROW_EL
105    }
106    
107    sub SPECIAL_EL () {
108      ADDRESS_EL |
109      BODY_EL |
110      DIV_EL |
111    
112      DD_EL |
113      DT_EL |
114      LI_EL |
115      P_EL |
116    
117      FORM_EL |
118      FRAMESET_EL |
119      HEADING_EL |
120      OPTION_EL |
121      OPTGROUP_EL |
122      SELECT_EL |
123      TABLE_ROW_EL |
124      TABLE_ROW_GROUP_EL |
125      MISC_SPECIAL_EL
126    }
127    
128    my $el_category = {
129      a => A_EL | FORMATTING_EL,
130      address => ADDRESS_EL,
131      applet => MISC_SCOPING_EL,
132      area => MISC_SPECIAL_EL,
133      b => FORMATTING_EL,
134      base => MISC_SPECIAL_EL,
135      basefont => MISC_SPECIAL_EL,
136      bgsound => MISC_SPECIAL_EL,
137      big => FORMATTING_EL,
138      blockquote => MISC_SPECIAL_EL,
139      body => BODY_EL,
140      br => MISC_SPECIAL_EL,
141      button => BUTTON_EL,
142      caption => CAPTION_EL,
143      center => MISC_SPECIAL_EL,
144      col => MISC_SPECIAL_EL,
145      colgroup => MISC_SPECIAL_EL,
146      dd => DD_EL,
147      dir => MISC_SPECIAL_EL,
148      div => DIV_EL,
149      dl => MISC_SPECIAL_EL,
150      dt => DT_EL,
151      em => FORMATTING_EL,
152      embed => MISC_SPECIAL_EL,
153      fieldset => MISC_SPECIAL_EL,
154      font => FORMATTING_EL,
155      form => FORM_EL,
156      frame => MISC_SPECIAL_EL,
157      frameset => FRAMESET_EL,
158      h1 => HEADING_EL,
159      h2 => HEADING_EL,
160      h3 => HEADING_EL,
161      h4 => HEADING_EL,
162      h5 => HEADING_EL,
163      h6 => HEADING_EL,
164      head => MISC_SPECIAL_EL,
165      hr => MISC_SPECIAL_EL,
166      html => HTML_EL,
167      i => FORMATTING_EL,
168      iframe => MISC_SPECIAL_EL,
169      img => MISC_SPECIAL_EL,
170      input => MISC_SPECIAL_EL,
171      isindex => MISC_SPECIAL_EL,
172      li => LI_EL,
173      link => MISC_SPECIAL_EL,
174      listing => MISC_SPECIAL_EL,
175      marquee => MISC_SCOPING_EL,
176      menu => MISC_SPECIAL_EL,
177      meta => MISC_SPECIAL_EL,
178      nobr => NOBR_EL | FORMATTING_EL,
179      noembed => MISC_SPECIAL_EL,
180      noframes => MISC_SPECIAL_EL,
181      noscript => MISC_SPECIAL_EL,
182      object => MISC_SCOPING_EL,
183      ol => MISC_SPECIAL_EL,
184      optgroup => OPTGROUP_EL,
185      option => OPTION_EL,
186      p => P_EL,
187      param => MISC_SPECIAL_EL,
188      plaintext => MISC_SPECIAL_EL,
189      pre => MISC_SPECIAL_EL,
190      rp => RUBY_COMPONENT_EL,
191      rt => RUBY_COMPONENT_EL,
192      ruby => RUBY_EL,
193      s => FORMATTING_EL,
194      script => MISC_SPECIAL_EL,
195      select => SELECT_EL,
196      small => FORMATTING_EL,
197      spacer => MISC_SPECIAL_EL,
198      strike => FORMATTING_EL,
199      strong => FORMATTING_EL,
200      style => MISC_SPECIAL_EL,
201      table => TABLE_EL,
202      tbody => TABLE_ROW_GROUP_EL,
203      td => TABLE_CELL_EL,
204      textarea => MISC_SPECIAL_EL,
205      tfoot => TABLE_ROW_GROUP_EL,
206      th => TABLE_CELL_EL,
207      thead => TABLE_ROW_GROUP_EL,
208      title => MISC_SPECIAL_EL,
209      tr => TABLE_ROW_EL,
210      tt => FORMATTING_EL,
211      u => FORMATTING_EL,
212      ul => MISC_SPECIAL_EL,
213      wbr => MISC_SPECIAL_EL,
214    };
215    
216    my $el_category_f = {
217      $MML_NS => {
218        'annotation-xml' => MML_AXML_EL,
219        mi => FOREIGN_FLOW_CONTENT_EL,
220        mo => FOREIGN_FLOW_CONTENT_EL,
221        mn => FOREIGN_FLOW_CONTENT_EL,
222        ms => FOREIGN_FLOW_CONTENT_EL,
223        mtext => FOREIGN_FLOW_CONTENT_EL,
224      },
225      $SVG_NS => {
226        foreignObject => FOREIGN_FLOW_CONTENT_EL,
227        desc => FOREIGN_FLOW_CONTENT_EL,
228        title => FOREIGN_FLOW_CONTENT_EL,
229      },
230      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
231  };  };
232    
233    my $svg_attr_name = {
234      attributename => 'attributeName',
235      attributetype => 'attributeType',
236      basefrequency => 'baseFrequency',
237      baseprofile => 'baseProfile',
238      calcmode => 'calcMode',
239      clippathunits => 'clipPathUnits',
240      contentscripttype => 'contentScriptType',
241      contentstyletype => 'contentStyleType',
242      diffuseconstant => 'diffuseConstant',
243      edgemode => 'edgeMode',
244      externalresourcesrequired => 'externalResourcesRequired',
245      filterres => 'filterRes',
246      filterunits => 'filterUnits',
247      glyphref => 'glyphRef',
248      gradienttransform => 'gradientTransform',
249      gradientunits => 'gradientUnits',
250      kernelmatrix => 'kernelMatrix',
251      kernelunitlength => 'kernelUnitLength',
252      keypoints => 'keyPoints',
253      keysplines => 'keySplines',
254      keytimes => 'keyTimes',
255      lengthadjust => 'lengthAdjust',
256      limitingconeangle => 'limitingConeAngle',
257      markerheight => 'markerHeight',
258      markerunits => 'markerUnits',
259      markerwidth => 'markerWidth',
260      maskcontentunits => 'maskContentUnits',
261      maskunits => 'maskUnits',
262      numoctaves => 'numOctaves',
263      pathlength => 'pathLength',
264      patterncontentunits => 'patternContentUnits',
265      patterntransform => 'patternTransform',
266      patternunits => 'patternUnits',
267      pointsatx => 'pointsAtX',
268      pointsaty => 'pointsAtY',
269      pointsatz => 'pointsAtZ',
270      preservealpha => 'preserveAlpha',
271      preserveaspectratio => 'preserveAspectRatio',
272      primitiveunits => 'primitiveUnits',
273      refx => 'refX',
274      refy => 'refY',
275      repeatcount => 'repeatCount',
276      repeatdur => 'repeatDur',
277      requiredextensions => 'requiredExtensions',
278      requiredfeatures => 'requiredFeatures',
279      specularconstant => 'specularConstant',
280      specularexponent => 'specularExponent',
281      spreadmethod => 'spreadMethod',
282      startoffset => 'startOffset',
283      stddeviation => 'stdDeviation',
284      stitchtiles => 'stitchTiles',
285      surfacescale => 'surfaceScale',
286      systemlanguage => 'systemLanguage',
287      tablevalues => 'tableValues',
288      targetx => 'targetX',
289      targety => 'targetY',
290      textlength => 'textLength',
291      viewbox => 'viewBox',
292      viewtarget => 'viewTarget',
293      xchannelselector => 'xChannelSelector',
294      ychannelselector => 'yChannelSelector',
295      zoomandpan => 'zoomAndPan',
296    };
297    
298    my $foreign_attr_xname = {
299      'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
300      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
301      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
302      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
303      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
304      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
305      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
306      'xml:base' => [$XML_NS, ['xml', 'base']],
307      'xml:lang' => [$XML_NS, ['xml', 'lang']],
308      'xml:space' => [$XML_NS, ['xml', 'space']],
309      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
310      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
311    };
312    
313    ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
314    
315  my $c1_entity_char = {  my $c1_entity_char = {
316    0x80 => 0x20AC,    0x80 => 0x20AC,
317    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 62  my $c1_entity_char = { Line 347  my $c1_entity_char = {
347    0x9F => 0x0178,    0x9F => 0x0178,
348  }; # $c1_entity_char  }; # $c1_entity_char
349    
350  my $special_category = {  sub parse_byte_string ($$$$;$) {
351    address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,    my $self = shift;
352    blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,    my $charset_name = shift;
353    dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,    open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
354    form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,    return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
355    h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,  } # parse_byte_string
356    img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,  
357    menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,  sub parse_byte_stream ($$$$;$$) {
358    ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,    # my ($self, $charset_name, $byte_stream, $doc, $onerror, $get_wrapper) = @_;
359    pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,    my $self = ref $_[0] ? shift : shift->new;
360    textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,    my $charset_name = shift;
361  };    my $byte_stream = $_[0];
 my $scoping_category = {  
   button => 1, caption => 1, html => 1, marquee => 1, object => 1,  
   table => 1, td => 1, th => 1,  
 };  
 my $formatting_category = {  
   a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,  
   s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,  
 };  
 # $phrasing_category: all other elements  
362    
363  sub parse_string ($$$;$) {    my $onerror = $_[2] || sub {
364    my $self = shift->new;      my (%opt) = @_;
365    my $s = \$_[0];      warn "Parse error ($opt{type})\n";
366      };
367      $self->{parse_error} = $onerror; # updated later by parse_char_string
368    
369      my $get_wrapper = $_[3] || sub ($) {
370        return $_[0]; # $_[0] = byte stream handle, returned = arg to char handle
371      };
372    
373      ## HTML5 encoding sniffing algorithm
374      require Message::Charset::Info;
375      my $charset;
376      my $buffer;
377      my ($char_stream, $e_status);
378    
379      SNIFFING: {
380        ## NOTE: By setting |allow_fallback| option true when the
381        ## |get_decode_handle| method is invoked, we ignore what the HTML5
382        ## spec requires, i.e. unsupported encoding should be ignored.
383          ## TODO: We should not do this unless the parser is invoked
384          ## in the conformance checking mode, in which this behavior
385          ## would be useful.
386    
387        ## Step 1
388        if (defined $charset_name) {
389          $charset = Message::Charset::Info->get_by_html_name ($charset_name);
390              ## TODO: Is this ok?  Transfer protocol's parameter should be
391              ## interpreted in its semantics?
392    
393          ## ISSUE: Unsupported encoding is not ignored according to the spec.
394          ($char_stream, $e_status) = $charset->get_decode_handle
395              ($byte_stream, allow_error_reporting => 1,
396               allow_fallback => 1);
397          if ($char_stream) {
398            $self->{confident} = 1;
399            last SNIFFING;
400          } else {
401            ## TODO: unsupported error
402          }
403        }
404    
405        ## Step 2
406        my $byte_buffer = '';
407        for (1..1024) {
408          my $char = $byte_stream->getc;
409          last unless defined $char;
410          $byte_buffer .= $char;
411        } ## TODO: timeout
412    
413        ## Step 3
414        if ($byte_buffer =~ /^\xFE\xFF/) {
415          $charset = Message::Charset::Info->get_by_html_name ('utf-16be');
416          ($char_stream, $e_status) = $charset->get_decode_handle
417              ($byte_stream, allow_error_reporting => 1,
418               allow_fallback => 1, byte_buffer => \$byte_buffer);
419          $self->{confident} = 1;
420          last SNIFFING;
421        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
422          $charset = Message::Charset::Info->get_by_html_name ('utf-16le');
423          ($char_stream, $e_status) = $charset->get_decode_handle
424              ($byte_stream, allow_error_reporting => 1,
425               allow_fallback => 1, byte_buffer => \$byte_buffer);
426          $self->{confident} = 1;
427          last SNIFFING;
428        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
429          $charset = Message::Charset::Info->get_by_html_name ('utf-8');
430          ($char_stream, $e_status) = $charset->get_decode_handle
431              ($byte_stream, allow_error_reporting => 1,
432               allow_fallback => 1, byte_buffer => \$byte_buffer);
433          $self->{confident} = 1;
434          last SNIFFING;
435        }
436    
437        ## Step 4
438        ## TODO: <meta charset>
439    
440        ## Step 5
441        ## TODO: from history
442    
443        ## Step 6
444        require Whatpm::Charset::UniversalCharDet;
445        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
446            ($byte_buffer);
447        if (defined $charset_name) {
448          $charset = Message::Charset::Info->get_by_html_name ($charset_name);
449    
450          ## ISSUE: Unsupported encoding is not ignored according to the spec.
451          require Whatpm::Charset::DecodeHandle;
452          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
453              ($byte_stream);
454          ($char_stream, $e_status) = $charset->get_decode_handle
455              ($buffer, allow_error_reporting => 1,
456               allow_fallback => 1, byte_buffer => \$byte_buffer);
457          if ($char_stream) {
458            $buffer->{buffer} = $byte_buffer;
459            !!!parse-error (type => 'sniffing:chardet',
460                            text => $charset_name,
461                            level => $self->{level}->{info},
462                            layer => 'encode',
463                            line => 1, column => 1);
464            $self->{confident} = 0;
465            last SNIFFING;
466          }
467        }
468    
469        ## Step 7: default
470        ## TODO: Make this configurable.
471        $charset = Message::Charset::Info->get_by_html_name ('windows-1252');
472            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
473            ## detectable in the step 6.
474        require Whatpm::Charset::DecodeHandle;
475        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
476            ($byte_stream);
477        ($char_stream, $e_status)
478            = $charset->get_decode_handle ($buffer,
479                                           allow_error_reporting => 1,
480                                           allow_fallback => 1,
481                                           byte_buffer => \$byte_buffer);
482        $buffer->{buffer} = $byte_buffer;
483        !!!parse-error (type => 'sniffing:default',
484                        text => 'windows-1252',
485                        level => $self->{level}->{info},
486                        line => 1, column => 1,
487                        layer => 'encode');
488        $self->{confident} = 0;
489      } # SNIFFING
490    
491      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
492        $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
493        !!!parse-error (type => 'chardecode:fallback',
494                        #text => $self->{input_encoding},
495                        level => $self->{level}->{uncertain},
496                        line => 1, column => 1,
497                        layer => 'encode');
498      } elsif (not ($e_status &
499                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
500        $self->{input_encoding} = $charset->get_iana_name;
501        !!!parse-error (type => 'chardecode:no error',
502                        text => $self->{input_encoding},
503                        level => $self->{level}->{uncertain},
504                        line => 1, column => 1,
505                        layer => 'encode');
506      } else {
507        $self->{input_encoding} = $charset->get_iana_name;
508      }
509    
510      $self->{change_encoding} = sub {
511        my $self = shift;
512        $charset_name = shift;
513        my $token = shift;
514    
515        $charset = Message::Charset::Info->get_by_html_name ($charset_name);
516        ($char_stream, $e_status) = $charset->get_decode_handle
517            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
518             byte_buffer => \ $buffer->{buffer});
519        
520        if ($char_stream) { # if supported
521          ## "Change the encoding" algorithm:
522    
523          ## Step 1    
524          if ($charset->{category} &
525              Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
526            $charset = Message::Charset::Info->get_by_html_name ('utf-8');
527            ($char_stream, $e_status) = $charset->get_decode_handle
528                ($byte_stream,
529                 byte_buffer => \ $buffer->{buffer});
530          }
531          $charset_name = $charset->get_iana_name;
532          
533          ## Step 2
534          if (defined $self->{input_encoding} and
535              $self->{input_encoding} eq $charset_name) {
536            !!!parse-error (type => 'charset label:matching',
537                            text => $charset_name,
538                            level => $self->{level}->{info});
539            $self->{confident} = 1;
540            return;
541          }
542    
543          !!!parse-error (type => 'charset label detected',
544                          text => $self->{input_encoding},
545                          value => $charset_name,
546                          level => $self->{level}->{warn},
547                          token => $token);
548          
549          ## Step 3
550          # if (can) {
551            ## change the encoding on the fly.
552            #$self->{confident} = 1;
553            #return;
554          # }
555          
556          ## Step 4
557          throw Whatpm::HTML::RestartParser ();
558        }
559      }; # $self->{change_encoding}
560    
561      my $char_onerror = sub {
562        my (undef, $type, %opt) = @_;
563        !!!parse-error (layer => 'encode',
564                        %opt, type => $type,
565                        line => $self->{line}, column => $self->{column} + 1);
566        if ($opt{octets}) {
567          ${$opt{octets}} = "\x{FFFD}"; # relacement character
568        }
569      };
570    
571      my $wrapped_char_stream = $get_wrapper->($char_stream);
572      $wrapped_char_stream->onerror ($char_onerror);
573    
574      my @args = @_; shift @args; # $s
575      my $return;
576      try {
577        $return = $self->parse_char_stream ($wrapped_char_stream, @args);  
578      } catch Whatpm::HTML::RestartParser with {
579        ## NOTE: Invoked after {change_encoding}.
580    
581        if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
582          $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
583          !!!parse-error (type => 'chardecode:fallback',
584                          level => $self->{level}->{uncertain},
585                          #text => $self->{input_encoding},
586                          line => 1, column => 1,
587                          layer => 'encode');
588        } elsif (not ($e_status &
589                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
590          $self->{input_encoding} = $charset->get_iana_name;
591          !!!parse-error (type => 'chardecode:no error',
592                          text => $self->{input_encoding},
593                          level => $self->{level}->{uncertain},
594                          line => 1, column => 1,
595                          layer => 'encode');
596        } else {
597          $self->{input_encoding} = $charset->get_iana_name;
598        }
599        $self->{confident} = 1;
600    
601        $wrapped_char_stream = $get_wrapper->($char_stream);
602        $wrapped_char_stream->onerror ($char_onerror);
603    
604        $return = $self->parse_char_stream ($wrapped_char_stream, @args);
605      };
606      return $return;
607    } # parse_byte_stream
608    
609    ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
610    ## and the HTML layer MUST ignore it.  However, we does strip BOM in
611    ## the encoding layer and the HTML layer does not ignore any U+FEFF,
612    ## because the core part of our HTML parser expects a string of character,
613    ## not a string of bytes or code units or anything which might contain a BOM.
614    ## Therefore, any parser interface that accepts a string of bytes,
615    ## such as |parse_byte_string| in this module, must ensure that it does
616    ## strip the BOM and never strip any ZWNBSP.
617    
618    sub parse_char_string ($$$;$$) {
619      #my ($self, $s, $doc, $onerror, $get_wrapper) = @_;
620      my $self = shift;
621      require utf8;
622      my $s = ref $_[0] ? $_[0] : \($_[0]);
623      open my $input, '<' . (utf8::is_utf8 ($$s) ? ':utf8' : ''), $s;
624      if ($_[3]) {
625        $input = $_[3]->($input);
626      }
627      return $self->parse_char_stream ($input, @_[1..$#_]);
628    } # parse_char_string
629    *parse_string = \&parse_char_string; ## NOTE: Alias for backward compatibility.
630    
631    sub parse_char_stream ($$$;$) {
632      my $self = ref $_[0] ? shift : shift->new;
633      my $input = $_[0];
634    $self->{document} = $_[1];    $self->{document} = $_[1];
635      @{$self->{document}->child_nodes} = ();
636    
637    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
638    
639      $self->{confident} = 1 unless exists $self->{confident};
640      $self->{document}->input_encoding ($self->{input_encoding})
641          if defined $self->{input_encoding};
642    
643    my $i = 0;    my $i = 0;
644    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
645    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
646    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
647      my $self = shift;      my $self = shift;
648    
649      pop @{$self->{prev_input_character}};      pop @{$self->{prev_char}};
650      unshift @{$self->{prev_input_character}}, $self->{next_input_character};      unshift @{$self->{prev_char}}, $self->{next_char};
651    
652      $self->{next_input_character} = -1 and return if $i >= length $$s;      my $char;
653      $self->{next_input_character} = ord substr $$s, $i++, 1;      if (defined $self->{next_next_char}) {
654      $column++;        $char = $self->{next_next_char};
655          delete $self->{next_next_char};
656        } else {
657          $char = $input->getc;
658        }
659        $self->{next_char} = -1 and return unless defined $char;
660        $self->{next_char} = ord $char;
661    
662        ($self->{line_prev}, $self->{column_prev})
663            = ($self->{line}, $self->{column});
664        $self->{column}++;
665            
666      if ($self->{next_input_character} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
667        $line++;        !!!cp ('j1');
668        $column = 0;        $self->{line}++;
669      } elsif ($self->{next_input_character} == 0x000D) { # CR        $self->{column} = 0;
670        $i++ if substr ($$s, $i, 1) eq "\x0A";      } elsif ($self->{next_char} == 0x000D) { # CR
671        $self->{next_input_character} = 0x000A; # LF # MUST        !!!cp ('j2');
672        $line++;        my $next = $input->getc;
673        $column = 0;        if (defined $next and $next ne "\x0A") {
674      } elsif ($self->{next_input_character} > 0x10FFFF) {          $self->{next_next_char} = $next;
675        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        }
676      } elsif ($self->{next_input_character} == 0x0000) { # NULL        $self->{next_char} = 0x000A; # LF # MUST
677          $self->{line}++;
678          $self->{column} = 0;
679        } elsif ($self->{next_char} > 0x10FFFF) {
680          !!!cp ('j3');
681          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
682        } elsif ($self->{next_char} == 0x0000) { # NULL
683          !!!cp ('j4');
684        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
685        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
686        } elsif ($self->{next_char} <= 0x0008 or
687                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
688                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
689                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
690                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
691                 {
692                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
693                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
694                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
695                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
696                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
697                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
698                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
699                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
700                  0x10FFFE => 1, 0x10FFFF => 1,
701                 }->{$self->{next_char}}) {
702          !!!cp ('j5');
703          if ($self->{next_char} < 0x10000) {
704            !!!parse-error (type => 'control char',
705                            text => (sprintf 'U+%04X', $self->{next_char}));
706          } else {
707            !!!parse-error (type => 'control char',
708                            text => (sprintf 'U-%08X', $self->{next_char}));
709          }
710      }      }
711    };    };
712    $self->{prev_input_character} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
713    $self->{next_input_character} = -1;    $self->{next_char} = -1;
714    
715    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
716      my (%opt) = @_;      my (%opt) = @_;
717      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
718        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
719        warn "Parse error ($opt{type}) at line $line column $column\n";
720    };    };
721    $self->{parse_error} = sub {    $self->{parse_error} = sub {
722      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
723    };    };
724    
725    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 135  sub parse_string ($$$;$) { Line 727  sub parse_string ($$$;$) {
727    $self->_construct_tree;    $self->_construct_tree;
728    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
729    
730      delete $self->{parse_error}; # remove loop
731    
732    return $self->{document};    return $self->{document};
733  } # parse_string  } # parse_char_stream
734    
735  sub new ($) {  sub new ($) {
736    my $class = shift;    my $class = shift;
737    my $self = bless {}, $class;    my $self = bless {
738    $self->{set_next_input_character} = sub {      level => {must => 'm',
739      $self->{next_input_character} = -1;                should => 's',
740                  warn => 'w',
741                  info => 'i',
742                  uncertain => 'u'},
743      }, $class;
744      $self->{set_next_char} = sub {
745        $self->{next_char} = -1;
746    };    };
747    $self->{parse_error} = sub {    $self->{parse_error} = sub {
748      #      #
749    };    };
750      $self->{change_encoding} = sub {
751        # if ($_[0] is a supported encoding) {
752        #   run "change the encoding" algorithm;
753        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
754        # }
755      };
756      $self->{application_cache_selection} = sub {
757        #
758      };
759    return $self;    return $self;
760  } # new  } # new
761    
762    sub CM_ENTITY () { 0b001 } # & markup in data
763    sub CM_LIMITED_MARKUP () { 0b010 } # < markup in data (limited)
764    sub CM_FULL_MARKUP () { 0b100 } # < markup in data (any)
765    
766    sub PLAINTEXT_CONTENT_MODEL () { 0 }
767    sub CDATA_CONTENT_MODEL () { CM_LIMITED_MARKUP }
768    sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
769    sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
770    
771    sub DATA_STATE () { 0 }
772    sub ENTITY_DATA_STATE () { 1 }
773    sub TAG_OPEN_STATE () { 2 }
774    sub CLOSE_TAG_OPEN_STATE () { 3 }
775    sub TAG_NAME_STATE () { 4 }
776    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
777    sub ATTRIBUTE_NAME_STATE () { 6 }
778    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
779    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
780    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
781    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
782    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
783    sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
784    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
785    sub COMMENT_START_STATE () { 14 }
786    sub COMMENT_START_DASH_STATE () { 15 }
787    sub COMMENT_STATE () { 16 }
788    sub COMMENT_END_STATE () { 17 }
789    sub COMMENT_END_DASH_STATE () { 18 }
790    sub BOGUS_COMMENT_STATE () { 19 }
791    sub DOCTYPE_STATE () { 20 }
792    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
793    sub DOCTYPE_NAME_STATE () { 22 }
794    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
795    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
796    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
797    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
798    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
799    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
800    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
801    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
802    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
803    sub BOGUS_DOCTYPE_STATE () { 32 }
804    sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
805    sub SELF_CLOSING_START_TAG_STATE () { 34 }
806    sub CDATA_BLOCK_STATE () { 35 }
807    
808    sub DOCTYPE_TOKEN () { 1 }
809    sub COMMENT_TOKEN () { 2 }
810    sub START_TAG_TOKEN () { 3 }
811    sub END_TAG_TOKEN () { 4 }
812    sub END_OF_FILE_TOKEN () { 5 }
813    sub CHARACTER_TOKEN () { 6 }
814    
815    sub AFTER_HTML_IMS () { 0b100 }
816    sub HEAD_IMS ()       { 0b1000 }
817    sub BODY_IMS ()       { 0b10000 }
818    sub BODY_TABLE_IMS () { 0b100000 }
819    sub TABLE_IMS ()      { 0b1000000 }
820    sub ROW_IMS ()        { 0b10000000 }
821    sub BODY_AFTER_IMS () { 0b100000000 }
822    sub FRAME_IMS ()      { 0b1000000000 }
823    sub SELECT_IMS ()     { 0b10000000000 }
824    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
825        ## NOTE: "in foreign content" insertion mode is special; it is combined
826        ## with the secondary insertion mode.  In this parser, they are stored
827        ## together in the bit-or'ed form.
828    
829    ## NOTE: "initial" and "before html" insertion modes have no constants.
830    
831    ## NOTE: "after after body" insertion mode.
832    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
833    
834    ## NOTE: "after after frameset" insertion mode.
835    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
836    
837    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
838    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
839    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
840    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
841    sub IN_BODY_IM () { BODY_IMS }
842    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
843    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
844    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
845    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
846    sub IN_TABLE_IM () { TABLE_IMS }
847    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
848    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
849    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
850    sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
851    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
852    sub IN_COLUMN_GROUP_IM () { 0b10 }
853    
854  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
855    
856  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
857    my $self = shift;    my $self = shift;
858    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
859    $self->{content_model_flag} = 'PCDATA'; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
860    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
861    undef $self->{current_attribute};    undef $self->{current_attribute};
862    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
863    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
864      delete $self->{self_closing};
865    $self->{char} = [];    $self->{char} = [];
866    # $self->{next_input_character}    # $self->{next_char}
867    !!!next-input-character;    !!!next-input-character;
868    $self->{token} = [];    $self->{token} = [];
869    # $self->{escape}    # $self->{escape}
870  } # _initialize_tokenizer  } # _initialize_tokenizer
871    
872  ## A token has:  ## A token has:
873  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
874  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
875  ##   ->{name} (DOCTYPE, start tag (tag name), end tag (tag name))  ##   ->{name} (DOCTYPE_TOKEN)
876  ##   ->{public_identifier} (DOCTYPE)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
877  ##   ->{system_identifier} (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
878  ##   ->{correct} == 1 or 0 (DOCTYPE)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
879  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
880  ##   ->{data} (comment, character)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
881    ##        ->{name}
882    ##        ->{value}
883    ##        ->{has_reference} == 1 or 0
884    ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
885    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
886    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
887    ##     while the token is pushed back to the stack.
888    
889  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
890    
# Line 185  sub _initialize_tokenizer ($) { Line 894  sub _initialize_tokenizer ($) {
894  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
895  ## and removed from the list.  ## and removed from the list.
896    
897    ## NOTE: HTML5 "Writing HTML documents" section, applied to
898    ## documents and not to user agents and conformance checkers,
899    ## contains some requirements that are not detected by the
900    ## parsing algorithm:
901    ## - Some requirements on character encoding declarations. ## TODO
902    ## - "Elements MUST NOT contain content that their content model disallows."
903    ##   ... Some are parse error, some are not (will be reported by c.c.).
904    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
905    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
906    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
907    
908    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
909    ## be detected by the HTML5 parsing algorithm:
910    ## - Text,
911    
912  sub _get_next_token ($) {  sub _get_next_token ($) {
913    my $self = shift;    my $self = shift;
914    
915      if ($self->{self_closing}) {
916        !!!parse-error (type => 'nestc', token => $self->{current_token});
917        ## NOTE: The |self_closing| flag is only set by start tag token.
918        ## In addition, when a start tag token is emitted, it is always set to
919        ## |current_token|.
920        delete $self->{self_closing};
921      }
922    
923    if (@{$self->{token}}) {    if (@{$self->{token}}) {
924        $self->{self_closing} = $self->{token}->[0]->{self_closing};
925      return shift @{$self->{token}};      return shift @{$self->{token}};
926    }    }
927    
928    A: {    A: {
929      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
930        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_char} == 0x0026) { # &
931          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
932              $self->{content_model_flag} eq 'RCDATA') {              not $self->{escape}) {
933            $self->{state} = 'entity data';            !!!cp (1);
934              $self->{state} = ENTITY_DATA_STATE;
935            !!!next-input-character;            !!!next-input-character;
936            redo A;            redo A;
937          } else {          } else {
938              !!!cp (2);
939            #            #
940          }          }
941        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
942          if ($self->{content_model_flag} eq 'RCDATA' or          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
             $self->{content_model_flag} eq 'CDATA') {  
943            unless ($self->{escape}) {            unless ($self->{escape}) {
944              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_char}->[0] == 0x002D and # -
945                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_char}->[1] == 0x0021 and # !
946                  $self->{prev_input_character}->[2] == 0x003C) { # <                  $self->{prev_char}->[2] == 0x003C) { # <
947                  !!!cp (3);
948                $self->{escape} = 1;                $self->{escape} = 1;
949                } else {
950                  !!!cp (4);
951              }              }
952              } else {
953                !!!cp (5);
954            }            }
955          }          }
956                    
957          #          #
958        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_char} == 0x003C) { # <
959          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
960              (($self->{content_model_flag} eq 'CDATA' or              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
               $self->{content_model_flag} eq 'RCDATA') and  
961               not $self->{escape})) {               not $self->{escape})) {
962            $self->{state} = 'tag open';            !!!cp (6);
963              $self->{state} = TAG_OPEN_STATE;
964            !!!next-input-character;            !!!next-input-character;
965            redo A;            redo A;
966          } else {          } else {
967              !!!cp (7);
968            #            #
969          }          }
970        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
971          if ($self->{escape} and          if ($self->{escape} and
972              ($self->{content_model_flag} eq 'RCDATA' or              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
973               $self->{content_model_flag} eq 'CDATA')) {            if ($self->{prev_char}->[0] == 0x002D and # -
974            if ($self->{prev_input_character}->[0] == 0x002D and # -                $self->{prev_char}->[1] == 0x002D) { # -
975                $self->{prev_input_character}->[1] == 0x002D) { # -              !!!cp (8);
976              delete $self->{escape};              delete $self->{escape};
977              } else {
978                !!!cp (9);
979            }            }
980            } else {
981              !!!cp (10);
982          }          }
983                    
984          #          #
985        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
986          !!!emit ({type => 'end-of-file'});          !!!cp (11);
987            !!!emit ({type => END_OF_FILE_TOKEN,
988                      line => $self->{line}, column => $self->{column}});
989          last A; ## TODO: ok?          last A; ## TODO: ok?
990          } else {
991            !!!cp (12);
992        }        }
993        # Anything else        # Anything else
994        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
995                     data => chr $self->{next_input_character}};                     data => chr $self->{next_char},
996                       line => $self->{line}, column => $self->{column},
997                      };
998        ## Stay in the data state        ## Stay in the data state
999        !!!next-input-character;        !!!next-input-character;
1000    
1001        !!!emit ($token);        !!!emit ($token);
1002    
1003        redo A;        redo A;
1004      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
1005        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
1006    
1007          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
1008                
1009        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
1010    
1011        $self->{state} = 'data';        $self->{state} = DATA_STATE;
1012        # next-input-character is already done        # next-input-character is already done
1013    
1014        unless (defined $token) {        unless (defined $token) {
1015          !!!emit ({type => 'character', data => '&'});          !!!cp (13);
1016            !!!emit ({type => CHARACTER_TOKEN, data => '&',
1017                      line => $l, column => $c,
1018                     });
1019        } else {        } else {
1020            !!!cp (14);
1021          !!!emit ($token);          !!!emit ($token);
1022        }        }
1023    
1024        redo A;        redo A;
1025      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
1026        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1027            $self->{content_model_flag} eq 'CDATA') {          if ($self->{next_char} == 0x002F) { # /
1028          if ($self->{next_input_character} == 0x002F) { # /            !!!cp (15);
1029            !!!next-input-character;            !!!next-input-character;
1030            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
1031            redo A;            redo A;
1032          } else {          } else {
1033              !!!cp (16);
1034            ## reconsume            ## reconsume
1035            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1036    
1037            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1038                        line => $self->{line_prev},
1039                        column => $self->{column_prev},
1040                       });
1041    
1042            redo A;            redo A;
1043          }          }
1044        } elsif ($self->{content_model_flag} eq 'PCDATA') {        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
1045          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_char} == 0x0021) { # !
1046            $self->{state} = 'markup declaration open';            !!!cp (17);
1047              $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
1048            !!!next-input-character;            !!!next-input-character;
1049            redo A;            redo A;
1050          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_char} == 0x002F) { # /
1051            $self->{state} = 'close tag open';            !!!cp (18);
1052              $self->{state} = CLOSE_TAG_OPEN_STATE;
1053            !!!next-input-character;            !!!next-input-character;
1054            redo A;            redo A;
1055          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
1056                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_char} <= 0x005A) { # A..Z
1057              !!!cp (19);
1058            $self->{current_token}            $self->{current_token}
1059              = {type => 'start tag',              = {type => START_TAG_TOKEN,
1060                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
1061            $self->{state} = 'tag name';                 line => $self->{line_prev},
1062                   column => $self->{column_prev}};
1063              $self->{state} = TAG_NAME_STATE;
1064            !!!next-input-character;            !!!next-input-character;
1065            redo A;            redo A;
1066          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
1067                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
1068            $self->{current_token} = {type => 'start tag',            !!!cp (20);
1069                              tag_name => chr ($self->{next_input_character})};            $self->{current_token} = {type => START_TAG_TOKEN,
1070            $self->{state} = 'tag name';                                      tag_name => chr ($self->{next_char}),
1071                                        line => $self->{line_prev},
1072                                        column => $self->{column_prev}};
1073              $self->{state} = TAG_NAME_STATE;
1074            !!!next-input-character;            !!!next-input-character;
1075            redo A;            redo A;
1076          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
1077            !!!parse-error (type => 'empty start tag');            !!!cp (21);
1078            $self->{state} = 'data';            !!!parse-error (type => 'empty start tag',
1079                              line => $self->{line_prev},
1080                              column => $self->{column_prev});
1081              $self->{state} = DATA_STATE;
1082            !!!next-input-character;            !!!next-input-character;
1083    
1084            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1085                        line => $self->{line_prev},
1086                        column => $self->{column_prev},
1087                       });
1088    
1089            redo A;            redo A;
1090          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
1091            !!!parse-error (type => 'pio');            !!!cp (22);
1092            $self->{state} = 'bogus comment';            !!!parse-error (type => 'pio',
1093            ## $self->{next_input_character} is intentionally left as is                            line => $self->{line_prev},
1094                              column => $self->{column_prev});
1095              $self->{state} = BOGUS_COMMENT_STATE;
1096              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1097                                        line => $self->{line_prev},
1098                                        column => $self->{column_prev},
1099                                       };
1100              ## $self->{next_char} is intentionally left as is
1101            redo A;            redo A;
1102          } else {          } else {
1103            !!!parse-error (type => 'bare stago');            !!!cp (23);
1104            $self->{state} = 'data';            !!!parse-error (type => 'bare stago',
1105                              line => $self->{line_prev},
1106                              column => $self->{column_prev});
1107              $self->{state} = DATA_STATE;
1108            ## reconsume            ## reconsume
1109    
1110            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1111                        line => $self->{line_prev},
1112                        column => $self->{column_prev},
1113                       });
1114    
1115            redo A;            redo A;
1116          }          }
1117        } else {        } else {
1118          die "$0: $self->{content_model_flag}: Unknown content model flag";          die "$0: $self->{content_model} in tag open";
1119        }        }
1120      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1121        if ($self->{content_model_flag} eq 'RCDATA' or        my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1122            $self->{content_model_flag} eq 'CDATA') {        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1123          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
1124    
1125            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
1126            my @next_char;            my @next_char;
1127            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
1128              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
1129              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
1130              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
1131              if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {              if ($self->{next_char} == $c or $self->{next_char} == $C) {
1132                  !!!cp (24);
1133                !!!next-input-character;                !!!next-input-character;
1134                next TAGNAME;                next TAGNAME;
1135              } else {              } else {
1136                $self->{next_input_character} = shift @next_char; # reconsume                !!!cp (25);
1137                  $self->{next_char} = shift @next_char; # reconsume
1138                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
1139                $self->{state} = 'data';                $self->{state} = DATA_STATE;
1140    
1141                !!!emit ({type => 'character', data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</',
1142                            line => $l, column => $c,
1143                           });
1144        
1145                redo A;                redo A;
1146              }              }
1147            }            }
1148            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
1149                
1150            unless ($self->{next_input_character} == 0x0009 or # HT            unless ($self->{next_char} == 0x0009 or # HT
1151                    $self->{next_input_character} == 0x000A or # LF                    $self->{next_char} == 0x000A or # LF
1152                    $self->{next_input_character} == 0x000B or # VT                    $self->{next_char} == 0x000B or # VT
1153                    $self->{next_input_character} == 0x000C or # FF                    $self->{next_char} == 0x000C or # FF
1154                    $self->{next_input_character} == 0x0020 or # SP                    $self->{next_char} == 0x0020 or # SP
1155                    $self->{next_input_character} == 0x003E or # >                    $self->{next_char} == 0x003E or # >
1156                    $self->{next_input_character} == 0x002F or # /                    $self->{next_char} == 0x002F or # /
1157                    $self->{next_input_character} == -1) {                    $self->{next_char} == -1) {
1158              $self->{next_input_character} = shift @next_char; # reconsume              !!!cp (26);
1159                $self->{next_char} = shift @next_char; # reconsume
1160              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1161              $self->{state} = 'data';              $self->{state} = DATA_STATE;
1162              !!!emit ({type => 'character', data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1163                          line => $l, column => $c,
1164                         });
1165              redo A;              redo A;
1166            } else {            } else {
1167              $self->{next_input_character} = shift @next_char;              !!!cp (27);
1168                $self->{next_char} = shift @next_char;
1169              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1170              # and consume...              # and consume...
1171            }            }
1172          } else {          } else {
1173            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
1174              !!!cp (28);
1175            # next-input-character is already done            # next-input-character is already done
1176            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1177            !!!emit ({type => 'character', data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</',
1178                        line => $l, column => $c,
1179                       });
1180            redo A;            redo A;
1181          }          }
1182        }        }
1183                
1184        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_char} and
1185            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1186          $self->{current_token} = {type => 'end tag',          !!!cp (29);
1187                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{current_token}
1188          $self->{state} = 'tag name';              = {type => END_TAG_TOKEN,
1189          !!!next-input-character;                 tag_name => chr ($self->{next_char} + 0x0020),
1190          redo A;                 line => $l, column => $c};
1191        } elsif (0x0061 <= $self->{next_input_character} and          $self->{state} = TAG_NAME_STATE;
1192                 $self->{next_input_character} <= 0x007A) { # a..z          !!!next-input-character;
1193          $self->{current_token} = {type => 'end tag',          redo A;
1194                            tag_name => chr ($self->{next_input_character})};        } elsif (0x0061 <= $self->{next_char} and
1195          $self->{state} = 'tag name';                 $self->{next_char} <= 0x007A) { # a..z
1196          !!!next-input-character;          !!!cp (30);
1197          redo A;          $self->{current_token} = {type => END_TAG_TOKEN,
1198        } elsif ($self->{next_input_character} == 0x003E) { # >                                    tag_name => chr ($self->{next_char}),
1199          !!!parse-error (type => 'empty end tag');                                    line => $l, column => $c};
1200          $self->{state} = 'data';          $self->{state} = TAG_NAME_STATE;
1201            !!!next-input-character;
1202            redo A;
1203          } elsif ($self->{next_char} == 0x003E) { # >
1204            !!!cp (31);
1205            !!!parse-error (type => 'empty end tag',
1206                            line => $self->{line_prev}, ## "<" in "</>"
1207                            column => $self->{column_prev} - 1);
1208            $self->{state} = DATA_STATE;
1209          !!!next-input-character;          !!!next-input-character;
1210          redo A;          redo A;
1211        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1212            !!!cp (32);
1213          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
1214          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1215          # reconsume          # reconsume
1216    
1217          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1218                      line => $l, column => $c,
1219                     });
1220    
1221          redo A;          redo A;
1222        } else {        } else {
1223            !!!cp (33);
1224          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1225          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
1226          ## $self->{next_input_character} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1227          redo A;                                    line => $self->{line_prev}, # "<" of "</"
1228        }                                    column => $self->{column_prev} - 1,
1229      } elsif ($self->{state} eq 'tag name') {                                   };
1230        if ($self->{next_input_character} == 0x0009 or # HT          ## $self->{next_char} is intentionally left as is
1231            $self->{next_input_character} == 0x000A or # LF          redo A;
1232            $self->{next_input_character} == 0x000B or # VT        }
1233            $self->{next_input_character} == 0x000C or # FF      } elsif ($self->{state} == TAG_NAME_STATE) {
1234            $self->{next_input_character} == 0x0020) { # SP        if ($self->{next_char} == 0x0009 or # HT
1235          $self->{state} = 'before attribute name';            $self->{next_char} == 0x000A or # LF
1236          !!!next-input-character;            $self->{next_char} == 0x000B or # VT
1237          redo A;            $self->{next_char} == 0x000C or # FF
1238        } elsif ($self->{next_input_character} == 0x003E) { # >            $self->{next_char} == 0x0020) { # SP
1239          if ($self->{current_token}->{type} eq 'start tag') {          !!!cp (34);
1240            $self->{current_token}->{first_start_tag}          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1241                = not defined $self->{last_emitted_start_tag_name};          !!!next-input-character;
1242            redo A;
1243          } elsif ($self->{next_char} == 0x003E) { # >
1244            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1245              !!!cp (35);
1246            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1247          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1248            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1249            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1250              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
1251            }            #  !!! cp (36);
1252              #  !!! parse-error (type => 'end tag attribute');
1253              #} else {
1254                !!!cp (37);
1255              #}
1256          } else {          } else {
1257            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1258          }          }
1259          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1260          !!!next-input-character;          !!!next-input-character;
1261    
1262          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1263    
1264          redo A;          redo A;
1265        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1266                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1267          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
1268            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
1269            # start tag or end tag            # start tag or end tag
1270          ## Stay in this state          ## Stay in this state
1271          !!!next-input-character;          !!!next-input-character;
1272          redo A;          redo A;
1273        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1274          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1275          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1276            $self->{current_token}->{first_start_tag}            !!!cp (39);
               = not defined $self->{last_emitted_start_tag_name};  
1277            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1278          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1279            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1280            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1281              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
1282            }            #  !!! cp (40);
1283              #  !!! parse-error (type => 'end tag attribute');
1284              #} else {
1285                !!!cp (41);
1286              #}
1287          } else {          } else {
1288            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1289          }          }
1290          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1291          # reconsume          # reconsume
1292    
1293          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1294    
1295          redo A;          redo A;
1296        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1297            !!!cp (42);
1298            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1299          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} eq 'start tag' and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = 'before attribute name';  
         # next-input-character is already done  
1300          redo A;          redo A;
1301        } else {        } else {
1302          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
1303            $self->{current_token}->{tag_name} .= chr $self->{next_char};
1304            # start tag or end tag            # start tag or end tag
1305          ## Stay in the state          ## Stay in the state
1306          !!!next-input-character;          !!!next-input-character;
1307          redo A;          redo A;
1308        }        }
1309      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
1310        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1311            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1312            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1313            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1314            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1315            !!!cp (45);
1316          ## Stay in the state          ## Stay in the state
1317          !!!next-input-character;          !!!next-input-character;
1318          redo A;          redo A;
1319        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1320          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1321            $self->{current_token}->{first_start_tag}            !!!cp (46);
               = not defined $self->{last_emitted_start_tag_name};  
1322            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1323          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1324            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1325            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1326                !!!cp (47);
1327              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1328              } else {
1329                !!!cp (48);
1330            }            }
1331          } else {          } else {
1332            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1333          }          }
1334          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1335          !!!next-input-character;          !!!next-input-character;
1336    
1337          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1338    
1339          redo A;          redo A;
1340        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1341                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1342          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
1343                                value => ''};          $self->{current_attribute}
1344          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1345                   value => '',
1346                   line => $self->{line}, column => $self->{column}};
1347            $self->{state} = ATTRIBUTE_NAME_STATE;
1348            !!!next-input-character;
1349            redo A;
1350          } elsif ($self->{next_char} == 0x002F) { # /
1351            !!!cp (50);
1352            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1353          !!!next-input-character;          !!!next-input-character;
1354          redo A;          redo A;
1355        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == -1) {
         !!!next-input-character;  
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} eq 'start tag' and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         ## Stay in the state  
         # next-input-character is already done  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
1356          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1357          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1358            $self->{current_token}->{first_start_tag}            !!!cp (52);
               = not defined $self->{last_emitted_start_tag_name};  
1359            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1360          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1361            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1362            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1363                !!!cp (53);
1364              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1365              } else {
1366                !!!cp (54);
1367            }            }
1368          } else {          } else {
1369            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1370          }          }
1371          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1372          # reconsume          # reconsume
1373    
1374          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1375    
1376          redo A;          redo A;
1377        } else {        } else {
1378          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
1379                                value => ''};               0x0022 => 1, # "
1380          $self->{state} = 'attribute name';               0x0027 => 1, # '
1381                 0x003D => 1, # =
1382                }->{$self->{next_char}}) {
1383              !!!cp (55);
1384              !!!parse-error (type => 'bad attribute name');
1385            } else {
1386              !!!cp (56);
1387            }
1388            $self->{current_attribute}
1389                = {name => chr ($self->{next_char}),
1390                   value => '',
1391                   line => $self->{line}, column => $self->{column}};
1392            $self->{state} = ATTRIBUTE_NAME_STATE;
1393          !!!next-input-character;          !!!next-input-character;
1394          redo A;          redo A;
1395        }        }
1396      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
1397        my $before_leave = sub {        my $before_leave = sub {
1398          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1399              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1400            !!!parse-error (type => 'dupulicate attribute');            !!!cp (57);
1401              !!!parse-error (type => 'duplicate attribute', text => $self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1402            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1403          } else {          } else {
1404              !!!cp (58);
1405            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
1406              = $self->{current_attribute};              = $self->{current_attribute};
1407          }          }
1408        }; # $before_leave        }; # $before_leave
1409    
1410        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1411            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1412            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1413            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1414            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1415            !!!cp (59);
1416          $before_leave->();          $before_leave->();
1417          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1418          !!!next-input-character;          !!!next-input-character;
1419          redo A;          redo A;
1420        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1421            !!!cp (60);
1422          $before_leave->();          $before_leave->();
1423          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1424          !!!next-input-character;          !!!next-input-character;
1425          redo A;          redo A;
1426        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1427          $before_leave->();          $before_leave->();
1428          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1429            $self->{current_token}->{first_start_tag}            !!!cp (61);
               = not defined $self->{last_emitted_start_tag_name};  
1430            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1431          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1432            $self->{content_model_flag} = 'PCDATA'; # MUST            !!!cp (62);
1433              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1434            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1435              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1436            }            }
1437          } else {          } else {
1438            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1439          }          }
1440          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1441          !!!next-input-character;          !!!next-input-character;
1442    
1443          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1444    
1445          redo A;          redo A;
1446        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1447                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1448          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
1449            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
1450          ## Stay in the state          ## Stay in the state
1451          !!!next-input-character;          !!!next-input-character;
1452          redo A;          redo A;
1453        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1454            !!!cp (64);
1455          $before_leave->();          $before_leave->();
1456            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1457          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} eq 'start tag' and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = 'before attribute name';  
         # next-input-character is already done  
1458          redo A;          redo A;
1459        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1460          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1461          $before_leave->();          $before_leave->();
1462          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1463            $self->{current_token}->{first_start_tag}            !!!cp (66);
               = not defined $self->{last_emitted_start_tag_name};  
1464            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1465          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1466            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1467            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1468                !!!cp (67);
1469              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1470              } else {
1471                ## NOTE: This state should never be reached.
1472                !!!cp (68);
1473            }            }
1474          } else {          } else {
1475            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1476          }          }
1477          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1478          # reconsume          # reconsume
1479    
1480          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1481    
1482          redo A;          redo A;
1483        } else {        } else {
1484          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x0022 or # "
1485                $self->{next_char} == 0x0027) { # '
1486              !!!cp (69);
1487              !!!parse-error (type => 'bad attribute name');
1488            } else {
1489              !!!cp (70);
1490            }
1491            $self->{current_attribute}->{name} .= chr ($self->{next_char});
1492          ## Stay in the state          ## Stay in the state
1493          !!!next-input-character;          !!!next-input-character;
1494          redo A;          redo A;
1495        }        }
1496      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1497        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1498            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1499            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1500            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1501            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1502            !!!cp (71);
1503          ## Stay in the state          ## Stay in the state
1504          !!!next-input-character;          !!!next-input-character;
1505          redo A;          redo A;
1506        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1507          $self->{state} = 'before attribute value';          !!!cp (72);
1508            $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1509          !!!next-input-character;          !!!next-input-character;
1510          redo A;          redo A;
1511        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1512          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1513            $self->{current_token}->{first_start_tag}            !!!cp (73);
               = not defined $self->{last_emitted_start_tag_name};  
1514            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1515          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1516            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1517            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1518                !!!cp (74);
1519              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1520              } else {
1521                ## NOTE: This state should never be reached.
1522                !!!cp (75);
1523            }            }
1524          } else {          } else {
1525            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1526          }          }
1527          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1528          !!!next-input-character;          !!!next-input-character;
1529    
1530          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1531    
1532          redo A;          redo A;
1533        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1534                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1535          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
1536                                value => ''};          $self->{current_attribute}
1537          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1538                   value => '',
1539                   line => $self->{line}, column => $self->{column}};
1540            $self->{state} = ATTRIBUTE_NAME_STATE;
1541            !!!next-input-character;
1542            redo A;
1543          } elsif ($self->{next_char} == 0x002F) { # /
1544            !!!cp (77);
1545            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1546          !!!next-input-character;          !!!next-input-character;
1547          redo A;          redo A;
1548        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == -1) {
         !!!next-input-character;  
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} eq 'start tag' and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
           ## TODO: Different error type for <aa / bb> than <aa/>  
         }  
         $self->{state} = 'before attribute name';  
         # next-input-character is already done  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
1549          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1550          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1551            $self->{current_token}->{first_start_tag}            !!!cp (79);
               = not defined $self->{last_emitted_start_tag_name};  
1552            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1553          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1554            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1555            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1556                !!!cp (80);
1557              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1558              } else {
1559                ## NOTE: This state should never be reached.
1560                !!!cp (81);
1561            }            }
1562          } else {          } else {
1563            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1564          }          }
1565          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1566          # reconsume          # reconsume
1567    
1568          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1569    
1570          redo A;          redo A;
1571        } else {        } else {
1572          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ($self->{next_char} == 0x0022 or # "
1573                                value => ''};              $self->{next_char} == 0x0027) { # '
1574          $self->{state} = 'attribute name';            !!!cp (78);
1575              !!!parse-error (type => 'bad attribute name');
1576            } else {
1577              !!!cp (82);
1578            }
1579            $self->{current_attribute}
1580                = {name => chr ($self->{next_char}),
1581                   value => '',
1582                   line => $self->{line}, column => $self->{column}};
1583            $self->{state} = ATTRIBUTE_NAME_STATE;
1584          !!!next-input-character;          !!!next-input-character;
1585          redo A;                  redo A;        
1586        }        }
1587      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1588        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1589            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1590            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1591            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1592            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1593            !!!cp (83);
1594          ## Stay in the state          ## Stay in the state
1595          !!!next-input-character;          !!!next-input-character;
1596          redo A;          redo A;
1597        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1598          $self->{state} = 'attribute value (double-quoted)';          !!!cp (84);
1599            $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1600          !!!next-input-character;          !!!next-input-character;
1601          redo A;          redo A;
1602        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1603          $self->{state} = 'attribute value (unquoted)';          !!!cp (85);
1604            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1605          ## reconsume          ## reconsume
1606          redo A;          redo A;
1607        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1608          $self->{state} = 'attribute value (single-quoted)';          !!!cp (86);
1609            $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1610          !!!next-input-character;          !!!next-input-character;
1611          redo A;          redo A;
1612        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1613          if ($self->{current_token}->{type} eq 'start tag') {          !!!parse-error (type => 'empty unquoted attribute value');
1614            $self->{current_token}->{first_start_tag}          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1615                = not defined $self->{last_emitted_start_tag_name};            !!!cp (87);
1616            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1617          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1618            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1619            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1620                !!!cp (88);
1621              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1622              } else {
1623                ## NOTE: This state should never be reached.
1624                !!!cp (89);
1625            }            }
1626          } else {          } else {
1627            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1628          }          }
1629          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1630          !!!next-input-character;          !!!next-input-character;
1631    
1632          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1633    
1634          redo A;          redo A;
1635        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1636          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1637          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1638            $self->{current_token}->{first_start_tag}            !!!cp (90);
               = not defined $self->{last_emitted_start_tag_name};  
1639            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1640          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1641            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1642            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1643                !!!cp (91);
1644              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1645              } else {
1646                ## NOTE: This state should never be reached.
1647                !!!cp (92);
1648            }            }
1649          } else {          } else {
1650            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1651          }          }
1652          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1653          ## reconsume          ## reconsume
1654    
1655          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1656    
1657          redo A;          redo A;
1658        } else {        } else {
1659          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x003D) { # =
1660          $self->{state} = 'attribute value (unquoted)';            !!!cp (93);
1661              !!!parse-error (type => 'bad attribute value');
1662            } else {
1663              !!!cp (94);
1664            }
1665            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1666            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1667          !!!next-input-character;          !!!next-input-character;
1668          redo A;          redo A;
1669        }        }
1670      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1671        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1672          $self->{state} = 'before attribute name';          !!!cp (95);
1673            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1674          !!!next-input-character;          !!!next-input-character;
1675          redo A;          redo A;
1676        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1677          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          !!!cp (96);
1678          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1679            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1680          !!!next-input-character;          !!!next-input-character;
1681          redo A;          redo A;
1682        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1683          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1684          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1685            $self->{current_token}->{first_start_tag}            !!!cp (97);
               = not defined $self->{last_emitted_start_tag_name};  
1686            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1687          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1688            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1689            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1690                !!!cp (98);
1691              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1692              } else {
1693                ## NOTE: This state should never be reached.
1694                !!!cp (99);
1695            }            }
1696          } else {          } else {
1697            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1698          }          }
1699          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1700          ## reconsume          ## reconsume
1701    
1702          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1703    
1704          redo A;          redo A;
1705        } else {        } else {
1706          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1707            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1708          ## Stay in the state          ## Stay in the state
1709          !!!next-input-character;          !!!next-input-character;
1710          redo A;          redo A;
1711        }        }
1712      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1713        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1714          $self->{state} = 'before attribute name';          !!!cp (101);
1715            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1716          !!!next-input-character;          !!!next-input-character;
1717          redo A;          redo A;
1718        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1719          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          !!!cp (102);
1720          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1721            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1722          !!!next-input-character;          !!!next-input-character;
1723          redo A;          redo A;
1724        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1725          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1726          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1727            $self->{current_token}->{first_start_tag}            !!!cp (103);
               = not defined $self->{last_emitted_start_tag_name};  
1728            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1729          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1730            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1731            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1732                !!!cp (104);
1733              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1734              } else {
1735                ## NOTE: This state should never be reached.
1736                !!!cp (105);
1737            }            }
1738          } else {          } else {
1739            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1740          }          }
1741          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1742          ## reconsume          ## reconsume
1743    
1744          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1745    
1746          redo A;          redo A;
1747        } else {        } else {
1748          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1749            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1750          ## Stay in the state          ## Stay in the state
1751          !!!next-input-character;          !!!next-input-character;
1752          redo A;          redo A;
1753        }        }
1754      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1755        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1756            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1757            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1758            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1759            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1760          $self->{state} = 'before attribute name';          !!!cp (107);
1761          !!!next-input-character;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1762          redo A;          !!!next-input-character;
1763        } elsif ($self->{next_input_character} == 0x0026) { # &          redo A;
1764          $self->{last_attribute_value_state} = 'attribute value (unquoted)';        } elsif ($self->{next_char} == 0x0026) { # &
1765          $self->{state} = 'entity in attribute value';          !!!cp (108);
1766          !!!next-input-character;          $self->{last_attribute_value_state} = $self->{state};
1767          redo A;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1768        } elsif ($self->{next_input_character} == 0x003E) { # >          !!!next-input-character;
1769          if ($self->{current_token}->{type} eq 'start tag') {          redo A;
1770            $self->{current_token}->{first_start_tag}        } elsif ($self->{next_char} == 0x003E) { # >
1771                = not defined $self->{last_emitted_start_tag_name};          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1772              !!!cp (109);
1773            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1774          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1775            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1776            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1777                !!!cp (110);
1778              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1779              } else {
1780                ## NOTE: This state should never be reached.
1781                !!!cp (111);
1782            }            }
1783          } else {          } else {
1784            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1785          }          }
1786          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1787          !!!next-input-character;          !!!next-input-character;
1788    
1789          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1790    
1791          redo A;          redo A;
1792        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1793          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1794          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1795            $self->{current_token}->{first_start_tag}            !!!cp (112);
               = not defined $self->{last_emitted_start_tag_name};  
1796            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1797          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1798            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1799            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1800                !!!cp (113);
1801              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1802              } else {
1803                ## NOTE: This state should never be reached.
1804                !!!cp (114);
1805            }            }
1806          } else {          } else {
1807            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1808          }          }
1809          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1810          ## reconsume          ## reconsume
1811    
1812          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1813    
1814          redo A;          redo A;
1815        } else {        } else {
1816          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1817                 0x0022 => 1, # "
1818                 0x0027 => 1, # '
1819                 0x003D => 1, # =
1820                }->{$self->{next_char}}) {
1821              !!!cp (115);
1822              !!!parse-error (type => 'bad attribute value');
1823            } else {
1824              !!!cp (116);
1825            }
1826            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1827          ## Stay in the state          ## Stay in the state
1828          !!!next-input-character;          !!!next-input-character;
1829          redo A;          redo A;
1830        }        }
1831      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1832        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);        my $token = $self->_tokenize_attempt_to_consume_an_entity
1833              (1,
1834               $self->{last_attribute_value_state}
1835                 == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "
1836               $self->{last_attribute_value_state}
1837                 == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '
1838               -1);
1839    
1840        unless (defined $token) {        unless (defined $token) {
1841            !!!cp (117);
1842          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1843        } else {        } else {
1844            !!!cp (118);
1845          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1846            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1847          ## ISSUE: spec says "append the returned character token to the current attribute's value"          ## ISSUE: spec says "append the returned character token to the current attribute's value"
1848        }        }
1849    
1850        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1851        # next-input-character is already done        # next-input-character is already done
1852        redo A;        redo A;
1853      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1854          if ($self->{next_char} == 0x0009 or # HT
1855              $self->{next_char} == 0x000A or # LF
1856              $self->{next_char} == 0x000B or # VT
1857              $self->{next_char} == 0x000C or # FF
1858              $self->{next_char} == 0x0020) { # SP
1859            !!!cp (118);
1860            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1861            !!!next-input-character;
1862            redo A;
1863          } elsif ($self->{next_char} == 0x003E) { # >
1864            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1865              !!!cp (119);
1866              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1867            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1868              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1869              if ($self->{current_token}->{attributes}) {
1870                !!!cp (120);
1871                !!!parse-error (type => 'end tag attribute');
1872              } else {
1873                ## NOTE: This state should never be reached.
1874                !!!cp (121);
1875              }
1876            } else {
1877              die "$0: $self->{current_token}->{type}: Unknown token type";
1878            }
1879            $self->{state} = DATA_STATE;
1880            !!!next-input-character;
1881    
1882            !!!emit ($self->{current_token}); # start tag or end tag
1883    
1884            redo A;
1885          } elsif ($self->{next_char} == 0x002F) { # /
1886            !!!cp (122);
1887            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1888            !!!next-input-character;
1889            redo A;
1890          } elsif ($self->{next_char} == -1) {
1891            !!!parse-error (type => 'unclosed tag');
1892            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1893              !!!cp (122.3);
1894              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1895            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1896              if ($self->{current_token}->{attributes}) {
1897                !!!cp (122.1);
1898                !!!parse-error (type => 'end tag attribute');
1899              } else {
1900                ## NOTE: This state should never be reached.
1901                !!!cp (122.2);
1902              }
1903            } else {
1904              die "$0: $self->{current_token}->{type}: Unknown token type";
1905            }
1906            $self->{state} = DATA_STATE;
1907            ## Reconsume.
1908            !!!emit ($self->{current_token}); # start tag or end tag
1909            redo A;
1910          } else {
1911            !!!cp ('124.1');
1912            !!!parse-error (type => 'no space between attributes');
1913            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1914            ## reconsume
1915            redo A;
1916          }
1917        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1918          if ($self->{next_char} == 0x003E) { # >
1919            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1920              !!!cp ('124.2');
1921              !!!parse-error (type => 'nestc', token => $self->{current_token});
1922              ## TODO: Different type than slash in start tag
1923              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1924              if ($self->{current_token}->{attributes}) {
1925                !!!cp ('124.4');
1926                !!!parse-error (type => 'end tag attribute');
1927              } else {
1928                !!!cp ('124.5');
1929              }
1930              ## TODO: Test |<title></title/>|
1931            } else {
1932              !!!cp ('124.3');
1933              $self->{self_closing} = 1;
1934            }
1935    
1936            $self->{state} = DATA_STATE;
1937            !!!next-input-character;
1938    
1939            !!!emit ($self->{current_token}); # start tag or end tag
1940    
1941            redo A;
1942          } elsif ($self->{next_char} == -1) {
1943            !!!parse-error (type => 'unclosed tag');
1944            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1945              !!!cp (124.7);
1946              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1947            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1948              if ($self->{current_token}->{attributes}) {
1949                !!!cp (124.5);
1950                !!!parse-error (type => 'end tag attribute');
1951              } else {
1952                ## NOTE: This state should never be reached.
1953                !!!cp (124.6);
1954              }
1955            } else {
1956              die "$0: $self->{current_token}->{type}: Unknown token type";
1957            }
1958            $self->{state} = DATA_STATE;
1959            ## Reconsume.
1960            !!!emit ($self->{current_token}); # start tag or end tag
1961            redo A;
1962          } else {
1963            !!!cp ('124.4');
1964            !!!parse-error (type => 'nestc');
1965            ## TODO: This error type is wrong.
1966            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1967            ## Reconsume.
1968            redo A;
1969          }
1970        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1971        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1972                
1973        my $token = {type => 'comment', data => ''};        ## NOTE: Set by the previous state
1974          #my $token = {type => COMMENT_TOKEN, data => ''};
1975    
1976        BC: {        BC: {
1977          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
1978            $self->{state} = 'data';            !!!cp (124);
1979              $self->{state} = DATA_STATE;
1980            !!!next-input-character;            !!!next-input-character;
1981    
1982            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1983    
1984            redo A;            redo A;
1985          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_char} == -1) {
1986            $self->{state} = 'data';            !!!cp (125);
1987              $self->{state} = DATA_STATE;
1988            ## reconsume            ## reconsume
1989    
1990            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1991    
1992            redo A;            redo A;
1993          } else {          } else {
1994            $token->{data} .= chr ($self->{next_input_character});            !!!cp (126);
1995              $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1996            !!!next-input-character;            !!!next-input-character;
1997            redo BC;            redo BC;
1998          }          }
1999        } # BC        } # BC
2000      } elsif ($self->{state} eq 'markup declaration open') {  
2001          die "$0: _get_next_token: unexpected case [BC]";
2002        } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
2003        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
2004    
2005          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
2006    
2007        my @next_char;        my @next_char;
2008        push @next_char, $self->{next_input_character};        push @next_char, $self->{next_char};
2009                
2010        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2011          !!!next-input-character;          !!!next-input-character;
2012          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
2013          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
2014            $self->{current_token} = {type => 'comment', data => ''};            !!!cp (127);
2015            $self->{state} = 'comment start';            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2016                                        line => $l, column => $c,
2017                                       };
2018              $self->{state} = COMMENT_START_STATE;
2019            !!!next-input-character;            !!!next-input-character;
2020            redo A;            redo A;
2021            } else {
2022              !!!cp (128);
2023          }          }
2024        } elsif ($self->{next_input_character} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
2025                 $self->{next_input_character} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
2026          !!!next-input-character;          !!!next-input-character;
2027          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
2028          if ($self->{next_input_character} == 0x004F or # O          if ($self->{next_char} == 0x004F or # O
2029              $self->{next_input_character} == 0x006F) { # o              $self->{next_char} == 0x006F) { # o
2030            !!!next-input-character;            !!!next-input-character;
2031            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
2032            if ($self->{next_input_character} == 0x0043 or # C            if ($self->{next_char} == 0x0043 or # C
2033                $self->{next_input_character} == 0x0063) { # c                $self->{next_char} == 0x0063) { # c
2034              !!!next-input-character;              !!!next-input-character;
2035              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
2036              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
2037                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
2038                !!!next-input-character;                !!!next-input-character;
2039                push @next_char, $self->{next_input_character};                push @next_char, $self->{next_char};
2040                if ($self->{next_input_character} == 0x0059 or # Y                if ($self->{next_char} == 0x0059 or # Y
2041                    $self->{next_input_character} == 0x0079) { # y                    $self->{next_char} == 0x0079) { # y
2042                  !!!next-input-character;                  !!!next-input-character;
2043                  push @next_char, $self->{next_input_character};                  push @next_char, $self->{next_char};
2044                  if ($self->{next_input_character} == 0x0050 or # P                  if ($self->{next_char} == 0x0050 or # P
2045                      $self->{next_input_character} == 0x0070) { # p                      $self->{next_char} == 0x0070) { # p
2046                    !!!next-input-character;                    !!!next-input-character;
2047                    push @next_char, $self->{next_input_character};                    push @next_char, $self->{next_char};
2048                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_char} == 0x0045 or # E
2049                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_char} == 0x0065) { # e
2050                      ## ISSUE: What a stupid code this is!                      !!!cp (129);
2051                      $self->{state} = 'DOCTYPE';                      ## TODO: What a stupid code this is!
2052                        $self->{state} = DOCTYPE_STATE;
2053                        $self->{current_token} = {type => DOCTYPE_TOKEN,
2054                                                  quirks => 1,
2055                                                  line => $l, column => $c,
2056                                                 };
2057                      !!!next-input-character;                      !!!next-input-character;
2058                      redo A;                      redo A;
2059                      } else {
2060                        !!!cp (130);
2061                      }
2062                    } else {
2063                      !!!cp (131);
2064                    }
2065                  } else {
2066                    !!!cp (132);
2067                  }
2068                } else {
2069                  !!!cp (133);
2070                }
2071              } else {
2072                !!!cp (134);
2073              }
2074            } else {
2075              !!!cp (135);
2076            }
2077          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2078                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2079                   $self->{next_char} == 0x005B) { # [
2080            !!!next-input-character;
2081            push @next_char, $self->{next_char};
2082            if ($self->{next_char} == 0x0043) { # C
2083              !!!next-input-character;
2084              push @next_char, $self->{next_char};
2085              if ($self->{next_char} == 0x0044) { # D
2086                !!!next-input-character;
2087                push @next_char, $self->{next_char};
2088                if ($self->{next_char} == 0x0041) { # A
2089                  !!!next-input-character;
2090                  push @next_char, $self->{next_char};
2091                  if ($self->{next_char} == 0x0054) { # T
2092                    !!!next-input-character;
2093                    push @next_char, $self->{next_char};
2094                    if ($self->{next_char} == 0x0041) { # A
2095                      !!!next-input-character;
2096                      push @next_char, $self->{next_char};
2097                      if ($self->{next_char} == 0x005B) { # [
2098                        !!!cp (135.1);
2099                        $self->{state} = CDATA_BLOCK_STATE;
2100                        !!!next-input-character;
2101                        redo A;
2102                      } else {
2103                        !!!cp (135.2);
2104                    }                    }
2105                    } else {
2106                      !!!cp (135.3);
2107                  }                  }
2108                  } else {
2109                    !!!cp (135.4);                
2110                }                }
2111                } else {
2112                  !!!cp (135.5);
2113              }              }
2114              } else {
2115                !!!cp (135.6);
2116            }            }
2117            } else {
2118              !!!cp (135.7);
2119          }          }
2120          } else {
2121            !!!cp (136);
2122        }        }
2123    
2124        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment');
2125        $self->{next_input_character} = shift @next_char;        $self->{next_char} = shift @next_char;
2126        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
2127        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
2128          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2129                                    line => $l, column => $c,
2130                                   };
2131        redo A;        redo A;
2132                
2133        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
2134        ## 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?
2135      } elsif ($self->{state} eq 'comment start') {      } elsif ($self->{state} == COMMENT_START_STATE) {
2136        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2137          $self->{state} = 'comment start dash';          !!!cp (137);
2138            $self->{state} = COMMENT_START_DASH_STATE;
2139          !!!next-input-character;          !!!next-input-character;
2140          redo A;          redo A;
2141        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2142            !!!cp (138);
2143          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2144          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2145          !!!next-input-character;          !!!next-input-character;
2146    
2147          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2148    
2149          redo A;          redo A;
2150        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2151            !!!cp (139);
2152          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2153          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2154          ## reconsume          ## reconsume
2155    
2156          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2157    
2158          redo A;          redo A;
2159        } else {        } else {
2160            !!!cp (140);
2161          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
2162              .= chr ($self->{next_input_character});              .= chr ($self->{next_char});
2163          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
2164          !!!next-input-character;          !!!next-input-character;
2165          redo A;          redo A;
2166        }        }
2167      } elsif ($self->{state} eq 'comment start dash') {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
2168        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2169          $self->{state} = 'comment end';          !!!cp (141);
2170            $self->{state} = COMMENT_END_STATE;
2171          !!!next-input-character;          !!!next-input-character;
2172          redo A;          redo A;
2173        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2174            !!!cp (142);
2175          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2176          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2177          !!!next-input-character;          !!!next-input-character;
2178    
2179          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2180    
2181          redo A;          redo A;
2182        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2183            !!!cp (143);
2184          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2185          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2186          ## reconsume          ## reconsume
2187    
2188          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2189    
2190          redo A;          redo A;
2191        } else {        } else {
2192            !!!cp (144);
2193          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
2194              .= '-' . chr ($self->{next_input_character});              .= '-' . chr ($self->{next_char});
2195          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
2196          !!!next-input-character;          !!!next-input-character;
2197          redo A;          redo A;
2198        }        }
2199      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_STATE) {
2200        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2201          $self->{state} = 'comment end dash';          !!!cp (145);
2202            $self->{state} = COMMENT_END_DASH_STATE;
2203          !!!next-input-character;          !!!next-input-character;
2204          redo A;          redo A;
2205        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2206            !!!cp (146);
2207          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2208          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2209          ## reconsume          ## reconsume
2210    
2211          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2212    
2213          redo A;          redo A;
2214        } else {        } else {
2215          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (147);
2216            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2217          ## Stay in the state          ## Stay in the state
2218          !!!next-input-character;          !!!next-input-character;
2219          redo A;          redo A;
2220        }        }
2221      } elsif ($self->{state} eq 'comment end dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
2222        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2223          $self->{state} = 'comment end';          !!!cp (148);
2224            $self->{state} = COMMENT_END_STATE;
2225          !!!next-input-character;          !!!next-input-character;
2226          redo A;          redo A;
2227        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2228            !!!cp (149);
2229          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2230          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2231          ## reconsume          ## reconsume
2232    
2233          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2234    
2235          redo A;          redo A;
2236        } else {        } else {
2237          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
2238          $self->{state} = 'comment';          $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
2239            $self->{state} = COMMENT_STATE;
2240          !!!next-input-character;          !!!next-input-character;
2241          redo A;          redo A;
2242        }        }
2243      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
2244        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2245          $self->{state} = 'data';          !!!cp (151);
2246            $self->{state} = DATA_STATE;
2247          !!!next-input-character;          !!!next-input-character;
2248    
2249          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2250    
2251          redo A;          redo A;
2252        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2253          !!!parse-error (type => 'dash in comment');          !!!cp (152);
2254            !!!parse-error (type => 'dash in comment',
2255                            line => $self->{line_prev},
2256                            column => $self->{column_prev});
2257          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2258          ## Stay in the state          ## Stay in the state
2259          !!!next-input-character;          !!!next-input-character;
2260          redo A;          redo A;
2261        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2262            !!!cp (153);
2263          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2264          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2265          ## reconsume          ## reconsume
2266    
2267          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2268    
2269          redo A;          redo A;
2270        } else {        } else {
2271          !!!parse-error (type => 'dash in comment');          !!!cp (154);
2272          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          !!!parse-error (type => 'dash in comment',
2273          $self->{state} = 'comment';                          line => $self->{line_prev},
2274                            column => $self->{column_prev});
2275            $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2276            $self->{state} = COMMENT_STATE;
2277          !!!next-input-character;          !!!next-input-character;
2278          redo A;          redo A;
2279        }        }
2280      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
2281        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2282            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2283            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2284            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2285            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2286          $self->{state} = 'before DOCTYPE name';          !!!cp (155);
2287            $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2288          !!!next-input-character;          !!!next-input-character;
2289          redo A;          redo A;
2290        } else {        } else {
2291            !!!cp (156);
2292          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
2293          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2294          ## reconsume          ## reconsume
2295          redo A;          redo A;
2296        }        }
2297      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
2298        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2299            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2300            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2301            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2302            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2303            !!!cp (157);
2304          ## Stay in the state          ## Stay in the state
2305          !!!next-input-character;          !!!next-input-character;
2306          redo A;          redo A;
2307        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2308            !!!cp (158);
2309          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2310          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2311          !!!next-input-character;          !!!next-input-character;
2312    
2313          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2314    
2315          redo A;          redo A;
2316        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2317            !!!cp (159);
2318          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2319          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2320          ## reconsume          ## reconsume
2321    
2322          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2323    
2324          redo A;          redo A;
2325        } else {        } else {
2326          $self->{current_token}          !!!cp (160);
2327              = {type => 'DOCTYPE',          $self->{current_token}->{name} = chr $self->{next_char};
2328                 name => chr ($self->{next_input_character}),          delete $self->{current_token}->{quirks};
                correct => 1};  
2329  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2330          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
2331          !!!next-input-character;          !!!next-input-character;
2332          redo A;          redo A;
2333        }        }
2334      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
2335  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
2336        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2337            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2338            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2339            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2340            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2341          $self->{state} = 'after DOCTYPE name';          !!!cp (161);
2342            $self->{state} = AFTER_DOCTYPE_NAME_STATE;
2343          !!!next-input-character;          !!!next-input-character;
2344          redo A;          redo A;
2345        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2346          $self->{state} = 'data';          !!!cp (162);
2347            $self->{state} = DATA_STATE;
2348          !!!next-input-character;          !!!next-input-character;
2349    
2350          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2351    
2352          redo A;          redo A;
2353        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2354            !!!cp (163);
2355          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2356          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2357          ## reconsume          ## reconsume
2358    
2359          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2360          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2361    
2362          redo A;          redo A;
2363        } else {        } else {
2364            !!!cp (164);
2365          $self->{current_token}->{name}          $self->{current_token}->{name}
2366            .= chr ($self->{next_input_character}); # DOCTYPE            .= chr ($self->{next_char}); # DOCTYPE
2367          ## Stay in the state          ## Stay in the state
2368          !!!next-input-character;          !!!next-input-character;
2369          redo A;          redo A;
2370        }        }
2371      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
2372        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2373            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2374            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2375            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2376            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2377            !!!cp (165);
2378          ## Stay in the state          ## Stay in the state
2379          !!!next-input-character;          !!!next-input-character;
2380          redo A;          redo A;
2381        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2382          $self->{state} = 'data';          !!!cp (166);
2383            $self->{state} = DATA_STATE;
2384          !!!next-input-character;          !!!next-input-character;
2385    
2386          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2387    
2388          redo A;          redo A;
2389        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2390            !!!cp (167);
2391          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2392          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2393          ## reconsume          ## reconsume
2394    
2395          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2396          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2397    
2398          redo A;          redo A;
2399        } elsif ($self->{next_input_character} == 0x0050 or # P        } elsif ($self->{next_char} == 0x0050 or # P
2400                 $self->{next_input_character} == 0x0070) { # p                 $self->{next_char} == 0x0070) { # p
2401          !!!next-input-character;          !!!next-input-character;
2402          if ($self->{next_input_character} == 0x0055 or # U          if ($self->{next_char} == 0x0055 or # U
2403              $self->{next_input_character} == 0x0075) { # u              $self->{next_char} == 0x0075) { # u
2404            !!!next-input-character;            !!!next-input-character;
2405            if ($self->{next_input_character} == 0x0042 or # B            if ($self->{next_char} == 0x0042 or # B
2406                $self->{next_input_character} == 0x0062) { # b                $self->{next_char} == 0x0062) { # b
2407              !!!next-input-character;              !!!next-input-character;
2408              if ($self->{next_input_character} == 0x004C or # L              if ($self->{next_char} == 0x004C or # L
2409                  $self->{next_input_character} == 0x006C) { # l                  $self->{next_char} == 0x006C) { # l
2410                !!!next-input-character;                !!!next-input-character;
2411                if ($self->{next_input_character} == 0x0049 or # I                if ($self->{next_char} == 0x0049 or # I
2412                    $self->{next_input_character} == 0x0069) { # i                    $self->{next_char} == 0x0069) { # i
2413                  !!!next-input-character;                  !!!next-input-character;
2414                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_char} == 0x0043 or # C
2415                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_char} == 0x0063) { # c
2416                    $self->{state} = 'before DOCTYPE public identifier';                    !!!cp (168);
2417                      $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2418                    !!!next-input-character;                    !!!next-input-character;
2419                    redo A;                    redo A;
2420                    } else {
2421                      !!!cp (169);
2422                  }                  }
2423                  } else {
2424                    !!!cp (170);
2425                }                }
2426                } else {
2427                  !!!cp (171);
2428              }              }
2429              } else {
2430                !!!cp (172);
2431            }            }
2432            } else {
2433              !!!cp (173);
2434          }          }
2435    
2436          #          #
2437        } elsif ($self->{next_input_character} == 0x0053 or # S        } elsif ($self->{next_char} == 0x0053 or # S
2438                 $self->{next_input_character} == 0x0073) { # s                 $self->{next_char} == 0x0073) { # s
2439          !!!next-input-character;          !!!next-input-character;
2440          if ($self->{next_input_character} == 0x0059 or # Y          if ($self->{next_char} == 0x0059 or # Y
2441              $self->{next_input_character} == 0x0079) { # y              $self->{next_char} == 0x0079) { # y
2442            !!!next-input-character;            !!!next-input-character;
2443            if ($self->{next_input_character} == 0x0053 or # S            if ($self->{next_char} == 0x0053 or # S
2444                $self->{next_input_character} == 0x0073) { # s                $self->{next_char} == 0x0073) { # s
2445              !!!next-input-character;              !!!next-input-character;
2446              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
2447                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
2448                !!!next-input-character;                !!!next-input-character;
2449                if ($self->{next_input_character} == 0x0045 or # E                if ($self->{next_char} == 0x0045 or # E
2450                    $self->{next_input_character} == 0x0065) { # e                    $self->{next_char} == 0x0065) { # e
2451                  !!!next-input-character;                  !!!next-input-character;
2452                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_char} == 0x004D or # M
2453                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_char} == 0x006D) { # m
2454                    $self->{state} = 'before DOCTYPE system identifier';                    !!!cp (174);
2455                      $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2456                    !!!next-input-character;                    !!!next-input-character;
2457                    redo A;                    redo A;
2458                    } else {
2459                      !!!cp (175);
2460                  }                  }
2461                  } else {
2462                    !!!cp (176);
2463                }                }
2464                } else {
2465                  !!!cp (177);
2466              }              }
2467              } else {
2468                !!!cp (178);
2469            }            }
2470            } else {
2471              !!!cp (179);
2472          }          }
2473    
2474          #          #
2475        } else {        } else {
2476            !!!cp (180);
2477          !!!next-input-character;          !!!next-input-character;
2478          #          #
2479        }        }
2480    
2481        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
2482        $self->{state} = 'bogus DOCTYPE';        $self->{current_token}->{quirks} = 1;
2483    
2484          $self->{state} = BOGUS_DOCTYPE_STATE;
2485        # next-input-character is already done        # next-input-character is already done
2486        redo A;        redo A;
2487      } elsif ($self->{state} eq 'before DOCTYPE public identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2488        if ({        if ({
2489              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2490              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2491            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2492            !!!cp (181);
2493          ## Stay in the state          ## Stay in the state
2494          !!!next-input-character;          !!!next-input-character;
2495          redo A;          redo A;
2496        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_char} eq 0x0022) { # "
2497            !!!cp (182);
2498          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2499          $self->{state} = 'DOCTYPE public identifier (double-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2500          !!!next-input-character;          !!!next-input-character;
2501          redo A;          redo A;
2502        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_char} eq 0x0027) { # '
2503            !!!cp (183);
2504          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2505          $self->{state} = 'DOCTYPE public identifier (single-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2506          !!!next-input-character;          !!!next-input-character;
2507          redo A;          redo A;
2508        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_char} eq 0x003E) { # >
2509            !!!cp (184);
2510          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
2511    
2512          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2513          !!!next-input-character;          !!!next-input-character;
2514    
2515          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2516          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2517    
2518          redo A;          redo A;
2519        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2520            !!!cp (185);
2521          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2522    
2523          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2524          ## reconsume          ## reconsume
2525    
2526          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2527          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2528    
2529          redo A;          redo A;
2530        } else {        } else {
2531            !!!cp (186);
2532          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
2533          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2534    
2535            $self->{state} = BOGUS_DOCTYPE_STATE;
2536          !!!next-input-character;          !!!next-input-character;
2537          redo A;          redo A;
2538        }        }
2539      } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2540        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2541          $self->{state} = 'after DOCTYPE public identifier';          !!!cp (187);
2542            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2543          !!!next-input-character;          !!!next-input-character;
2544          redo A;          redo A;
2545        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2546            !!!cp (188);
2547          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2548    
2549          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2550            !!!next-input-character;
2551    
2552            $self->{current_token}->{quirks} = 1;
2553            !!!emit ($self->{current_token}); # DOCTYPE
2554    
2555            redo A;
2556          } elsif ($self->{next_char} == -1) {
2557            !!!cp (189);
2558            !!!parse-error (type => 'unclosed PUBLIC literal');
2559    
2560            $self->{state} = DATA_STATE;
2561          ## reconsume          ## reconsume
2562    
2563          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2564          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2565    
2566          redo A;          redo A;
2567        } else {        } else {
2568            !!!cp (190);
2569          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2570              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2571          ## Stay in the state          ## Stay in the state
2572          !!!next-input-character;          !!!next-input-character;
2573          redo A;          redo A;
2574        }        }
2575      } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2576        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2577          $self->{state} = 'after DOCTYPE public identifier';          !!!cp (191);
2578            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2579            !!!next-input-character;
2580            redo A;
2581          } elsif ($self->{next_char} == 0x003E) { # >
2582            !!!cp (192);
2583            !!!parse-error (type => 'unclosed PUBLIC literal');
2584    
2585            $self->{state} = DATA_STATE;
2586          !!!next-input-character;          !!!next-input-character;
2587    
2588            $self->{current_token}->{quirks} = 1;
2589            !!!emit ($self->{current_token}); # DOCTYPE
2590    
2591          redo A;          redo A;
2592        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2593            !!!cp (193);
2594          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2595    
2596          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2597          ## reconsume          ## reconsume
2598    
2599          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2600          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2601    
2602          redo A;          redo A;
2603        } else {        } else {
2604            !!!cp (194);
2605          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2606              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2607          ## Stay in the state          ## Stay in the state
2608          !!!next-input-character;          !!!next-input-character;
2609          redo A;          redo A;
2610        }        }
2611      } elsif ($self->{state} eq 'after DOCTYPE public identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2612        if ({        if ({
2613              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2614              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2615            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2616            !!!cp (195);
2617          ## Stay in the state          ## Stay in the state
2618          !!!next-input-character;          !!!next-input-character;
2619          redo A;          redo A;
2620        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2621            !!!cp (196);
2622          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2623          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2624          !!!next-input-character;          !!!next-input-character;
2625          redo A;          redo A;
2626        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2627            !!!cp (197);
2628          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2629          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2630          !!!next-input-character;          !!!next-input-character;
2631          redo A;          redo A;
2632        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2633          $self->{state} = 'data';          !!!cp (198);
2634            $self->{state} = DATA_STATE;
2635          !!!next-input-character;          !!!next-input-character;
2636    
2637          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2638    
2639          redo A;          redo A;
2640        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2641            !!!cp (199);
2642          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2643    
2644          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2645          ## reconsume          ## reconsume
2646    
2647          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2648          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2649    
2650          redo A;          redo A;
2651        } else {        } else {
2652            !!!cp (200);
2653          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
2654          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;