-sub extract_attributes ($;$) {
- my($s, $lc) = @_;
- my %attr;
- $s = $1 if $s =~ /^<\S+(.*)\/\S$/s # XML-style self-closing tags
- || $s =~ /^<\S+(.*)\S$/s; # SGML-style tags
-
- for (my $i = 0; $s =~ /^(?:$re_directive_control)?\s+(?:$re_directive_control)?(?:([a-zA-Z][-a-zA-Z0-9]*)\s*=\s*)?('((?:$re_directive|[^'])*)'|"((?:$re_directive|[^"])*)"|((?:$re_directive|[^\s<>])+))/os;) {
- my($key, $val, $val_orig, $rest)
- = ($1, (defined $3? $3: defined $4? $4: $5), $2, $');
- $i += 1;
- $attr{+lc($key)} = [$key, $val, $val_orig, $i];
- $s = $rest;
- if ($val =~ /$re_tmpl_include/os) {
- warn "Warning: TMPL_INCLUDE in attribute"
- . (defined $lc? " near line $lc": '') . ": $val_orig\n";
- } elsif ($val =~ /$re_tmpl_var/os && $val !~ /$re_tmpl_var_escaped/os) {
- # FIXME: we probably should not warn if key is "onclick" etc? :-/
- my $suggest = ($key =~ /^(?:action|archive|background|cite|classid|codebase|data|datasrc|for|href|longdesc|profile|src|usemap)$/? 'URL': 'HTML');
- warn_pedantic \$pedantic_tmpl_var_use_in_nonpedantic_mode_p,
- "Suggest ESCAPE=$suggest for TMPL_VAR in attribute \"$key\""
- . (defined $lc? " near line $lc": '') . ": $val_orig"
- if $pedantic_p || !$pedantic_tmpl_var_use_in_nonpedantic_mode_p;
- } elsif ($val_orig !~ /^['"]/) {
- my $t = $val; $t =~ s/$re_directive_control//os;
- warn_pedantic \$pedantic_attribute_error_in_nonpedantic_mode_p,
- "Unquoted attribute contains character(s) that should be quoted"
- . (defined $lc? " near line $lc": '') . ": $val_orig"
- if $t =~ /[^-\.A-Za-z0-9]/s;
- }
- }
- my $s2 = $s; $s2 =~ s/$re_tmpl_endif_endloop//g; # for the next check
- if ($s2 =~ /\S/s) { # should never happen
- if ($s =~ /^([^\n]*)\n/s) { # this is even worse
- warn "Error: Completely confused while extracting attributes"
- . (defined $lc? " near line $lc": '') . ": $1\n";
- warn "Error: " . (scalar split(/\n/, $s) - 1) . " more line(s) not shown.\n";
- $fatal_p = 1;
- } else {
- warn "Warning: Strange attribute syntax"
- . (defined $lc? " near line $lc": '') . ": $s\n";
- }
- }
- return \%attr;
-}
-
-sub next_token_internal (*) {
- my($h) = @_;
- my($it, $kind);
- my $eof_p = 0;
- if (!defined $readahead || !length $readahead) {
- my $next = scalar <$h>;
- $eof_p = !defined $next;
- if (!$eof_p) {
- $lc += 1;
- $readahead .= $next;
- }
- }
- $lc_0 = $lc; # remember line number of first line
- if ($eof_p && !length $readahead) { # nothing left to do
- ;
- } elsif ($readahead =~ /^\s+/s) { # whitespace
- ($kind, $it, $readahead) = (KIND_TEXT, $&, $');
- # FIXME the following (the [<\s] part) is an unreliable HACK :-(
- } elsif ($readahead =~ /^(?:[^<]|<[<\s])+/s) { # non-space normal text
- ($kind, $it, $readahead) = (KIND_TEXT, $&, $');
- warn "Warning: Unescaped < near line $lc_0: $it\n"
- if !$cdata_mode_p && $it =~ /</s;
- } else { # tag/declaration/processing instruction
- my $ok_p = 0;
- for (;;) {
- if ($cdata_mode_p) {
- if ($readahead =~ /^$cdata_close/) {
- ($kind, $it, $readahead) = (KIND_TAG, $&, $');
- $ok_p = 1;
- } else {
- ($kind, $it, $readahead) = (KIND_TEXT, $readahead, undef);
- $ok_p = 1;
- }
- } elsif ($readahead =~ /^$re_tag_compat/os) {
- ($kind, $it, $readahead) = (KIND_TAG, "$1>", $3);
- $ok_p = 1;
- warn "Warning: SGML \"closed start tag\" notation near line $lc_0: $1<\n" if $2 eq '';
- } elsif ($readahead =~ /^<!--(?:(?!-->).)*-->/s) {
- ($kind, $it, $readahead) = (KIND_COMMENT, $&, $');
- $ok_p = 1;
- warn "Warning: Syntax error in comment at line $lc_0: $&\n";
- $syntaxerror_p = 1;
- }
- last if $ok_p;
- my $next = scalar <$h>;
- $eof_p = !defined $next;
- last if $eof_p;
- $lc += 1;
- $readahead .= $next;
- }
- if ($kind ne KIND_TAG) {
- ;
- } elsif ($it =~ /^<!/) {
- $kind = KIND_DECL;
- $kind = KIND_COMMENT if $it =~ /^<!--(?:(?!-->).)*-->/;
- } elsif ($it =~ /^<\?/) {
- $kind = KIND_PI;
- }
- if ($it =~ /^$re_directive/ios && !$cdata_mode_p) {
- $kind = KIND_DIRECTIVE;
- }
- if (!$ok_p && $eof_p) {
- ($kind, $it, $readahead) = (KIND_UNKNOWN, $readahead, undef);
- $syntaxerror_p = 1;
- }
- }
- warn "Warning: Unrecognizable token found near line $lc_0: $it\n"
- if $kind eq KIND_UNKNOWN;
- return defined $it? (wantarray? ($kind, $it):
- [$kind, $it]): undef;
-}
-
-sub next_token (*) {
- my($h) = @_;
- my $it;
- if (!$cdata_mode_p) {
- $it = next_token_internal($h);
- if (defined $it && $it->[0] eq KIND_TAG) { # FIXME
- ($cdata_mode_p, $cdata_close) = (1, "</$1\\s*>")
- if $it->[1] =~ /^<(script|style|textarea)\b/i; #FIXME
- push @$it, extract_attributes($it->[1], $lc_0); #FIXME
- }
- } else {
- for ($it = '';;) {
- my $lc_prev = $lc;
- my $next = next_token_internal($h);
- last if !defined $next;
- if (defined $next && $next->[1] =~ /$cdata_close/i) { #FIXME
- ($lc, $readahead) = ($lc_prev, $next->[1] . $readahead); #FIXME
- $cdata_mode_p = 0;
- }
- last unless $cdata_mode_p;
- $it .= $next->[1]; #FIXME
- }
- $it = [KIND_CDATA, $it]; #FIXME
- $cdata_close = undef;
- }
- return defined $it? (wantarray? @$it: $it): undef;