[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]

[PATCH] CGI.pm DTD and script "type" attributes




These are the changes I needed to make to CGI.pm to have output validate
cleanly at validator.w3.org/, namely adding the "type" attribute to
<script> tags generated during start_html and specifying a full DTD,
including URI.

Here's the patch, if anyone cares for it.

-Aaron

--- /usr/lib/perl5/5.00503/CGI_old.pm	Mon Sep 13 21:06:11 1999
+++ /usr/lib/perl5/5.00503/CGI.pm	Tue Jan 11 18:00:53 2000
@@ -31,7 +31,8 @@
 
     # Change this to the preferred DTD to print in start_html()
     # or use default_dtd('text of DTD to use');
-    $DEFAULT_DTD = '-//IETF//DTD HTML//EN';
+    $DEFAULT_DTD = [ '-//W3C//DTD HTML 4.01 Transitional//EN',
+		     'http://www.w3.org/TR/html4/loose.dtd' ] ;
 
     # Set this to 1 to enable NPH scripts
     # or: 
@@ -1342,8 +1343,20 @@
     $title = $self->escapeHTML($title || 'Untitled Document');
     $author = $self->escape($author);
     my(@result);
-    $dtd = $DEFAULT_DTD unless $dtd && $dtd =~ m|^-//|;
-    push(@result,qq(<!DOCTYPE HTML PUBLIC "$dtd">)) if $dtd;
+    if ($dtd) {
+        if (ref $dtd && $ref eq 'ARRAY') {
+            $dtd = $DEFAULT_DTD unless $dtd->[0] =~ m|^-//|;
+        } else {
+            $dtd = $DEFAULT_DTD unless $dtd =~ m|^-//|;
+        }
+    } else {
+        $dtd = $DEFAULT_DTD;
+    }
+    if (ref($dtd) && ref($dtd) eq 'ARRAY') {
+        push(@result,qq(<!DOCTYPE HTML PUBLIC "$dtd->[0]"\n\t"$dtd->[1]">));
+    } else {
+        push(@result,qq(<!DOCTYPE HTML PUBLIC "$dtd">));
+    }
     push(@result,"<HTML><HEAD><TITLE>$title</TITLE>");
     push(@result,"<LINK REV=MADE HREF=\"mailto:$author\">") if defined $author;
 
@@ -1408,21 +1421,32 @@
     foreach $script (@scripts) {
 	my($src,$code,$language);
 	if (ref($script)) { # script is a hash
-	    ($src,$code,$language) =
-		$self->rearrange([SRC,CODE,LANGUAGE],
+	    ($src,$code,$language, $type) =
+		$self->rearrange([SRC,CODE,LANGUAGE,TYPE],
 				 '-foo'=>'bar',	# a trick to allow the '-' to be omitted
 				 ref($script) eq 'ARRAY' ? @$script : %$script);
-	    
+            # User may not have specified language
+            $language ||= 'JavaScript';
+            unless (defined $type) {
+                $type = lc $language;
+                # strip '1.2' from 'javascript1.2'
+                $type =~ s/^(\D+).*$/text\/$1/;
+            }
 	} else {
-	    ($src,$code,$language) = ('',$script,'JavaScript');
+	    ($src,$code,$language, $type) = ('',$script,'JavaScript', 'text/javascript');
 	}
 	my(@satts);
 	push(@satts,'src'=>$src) if $src;
-	push(@satts,'language'=>$language || 'JavaScript');
+	push(@satts,'language'=>$language);
+        push(@satts,'type'=>$type);
 	$code = "<!-- Hide script\n$code\n// End script hiding -->"
-	    if $code && $language=~/javascript/i;
+	    if $code && $type=~/javascript/i;
+	$code = "<!-- Hide script\n$code\n\# End script hiding -->"
+	    if $code && $type=~/perl/i;
 	$code = "<!-- Hide script\n$code\n\# End script hiding -->"
-	    if $code && $language=~/perl/i;
+	    if $code && $type=~/tcl/i;
+        $code = "<!-- Hide script\n$code\n' End script hiding -->"
+            if $code && $type=~/vbscript/i;
 	push(@result,script({@satts},$code || ''));
     }
     @result;
@@ -2757,8 +2781,12 @@
 ####
 'default_dtd' => <<'END_OF_FUNC',
 sub default_dtd {
-    my ($self,$param) = self_or_CGI(@_);
-    $CGI::DEFAULT_DTD = $param if defined($param);
+    my ($self,$param,$param2) = self_or_CGI(@_);
+    if (defined $param2 && defined $param) {
+        $CGI::DEFAULT_DTD = [ $param, $param2 ];
+    } elsif (defined $param) {
+        $CGI::DEFAULT_DTD = $param;
+    }
     return $CGI::DEFAULT_DTD;
 }
 END_OF_FUNC


Follow-Ups from:
Lincoln Stein <lstein@cshl.org>

[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]