Skip to content

Commit

Permalink
Add support for the 'charset' attribute to the '<script>' tag.
Browse files Browse the repository at this point in the history
    RT#62907, Thanks to Fabrice Metge
  • Loading branch information
markstos committed Nov 20, 2010
1 parent 8a43b8a commit d99997c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
21 changes: 11 additions & 10 deletions lib/CGI.pm
Expand Up @@ -1861,20 +1861,20 @@ sub _script {
my (@scripts) = ref($script) eq 'ARRAY' ? @$script : ($script);
for $script (@scripts) {
my($src,$code,$language);
if (ref($script)) { # script is a hash
($src,$code,$type) =
rearrange(['SRC','CODE',['LANGUAGE','TYPE']],
'-foo'=>'bar', # a trick to allow the '-' to be omitted
ref($script) eq 'ARRAY' ? @$script : %$script);
my($src,$code,$language,$charset);
if (ref($script)) { # script is a hash
($src,$code,$type,$charset) =
rearrange(['SRC','CODE',['LANGUAGE','TYPE'],'CHARSET'],
'-foo'=>'bar', # a trick to allow the '-' to be omitted
ref($script) eq 'ARRAY' ? @$script : %$script);
$type ||= 'text/javascript';
unless ($type =~ m!\w+/\w+!) {
$type =~ s/[\d.]+$//;
$type = "text/$type";
}
} else {
($src,$code,$type) = ('',$script, 'text/javascript');
}
} else {
($src,$code,$type,$charset) = ('',$script, 'text/javascript', '');
}
my $comment = '//'; # javascript by default
$comment = '#' if $type=~/perl|tcl/i;
Expand All @@ -1892,6 +1892,7 @@ sub _script {
my(@satts);
push(@satts,'src'=>$src) if $src;
push(@satts,'type'=>$type);
push(@satts,'charset'=>$charset) if ($src && $charset);
$code = $cdata_start . $code . $cdata_end if defined $code;
push(@result,$self->script({@satts},$code || ''));
}
Expand Down Expand Up @@ -5487,7 +5488,7 @@ Use the B<-noScript> parameter to pass some HTML text that will be displayed on
browsers that do not have JavaScript (or browsers where JavaScript is turned
off).
The <script> tag, has several attributes including "type" and src.
The <script> tag, has several attributes including "type", "src" and "charset".
The latter is particularly interesting, as it allows you to keep the
JavaScript code in a file or CGI script rather than cluttering up each
page with the source. To use these attributes pass a HASH reference
Expand Down
6 changes: 5 additions & 1 deletion t/html.t
Expand Up @@ -85,13 +85,17 @@ is start_html(), <<END, "start_html()";
<body>
END

is start_html( -Title => 'The world of foo' ), <<END, "start_html()";
is start_html(
-Title => 'The world of foo' ,
-Script => [ {-src=> 'foo.js', -charset=>'utf-8'} ],
), <<END, "start_html()";
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
<head>
<title>The world of foo</title>
<script src="foo.js" charset="utf-8" type="text/javascript"></script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
Expand Down

0 comments on commit d99997c

Please sign in to comment.