最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - duplicate `rel` attribute emitted by Perl 5.18.2's CGI `start_html` - Stack Overflow

programmeradmin1浏览0评论

In a program I'm using a rather sophisticated parameter for CGI's start_html, but that results in a duplicate rel attribute for the CSS passed via -style.

Example code:

#!/usr/bin/perl
use strict;
use warnings;
use CGI;

my $attr = {
    '-script' => [
        {
            '-src' => '.js',
            '-type' => 'text/javascript'
        },
        {
            '-src' => '.js',
            '-type' => 'text/javascript'
        },
        {
            '-type' => 'text/javascript',
            '-src' => '.js'
        }
    ],
    '-noscript' => '<link type="text/css" href=".css" id="3.css" title="No Script Additions" rel="stylesheet" />',
    '-encoding' => 'utf-8',
    '-author' => 'Me &lt;[email protected]&gt;',
    '-lang' => 'de-DE',
    '-style' => [
        {
            '-id' => '2.css',
            '-type' => 'text/css',
            '-src' => '.css',
            '-rel' => 'stylesheet alternate',
            '-title' => 'JavaScript Additions'
        }
    ],
    '-meta' => {
            'copyright' => 'Copyright ...',
            'keywords' => 'bla',
            'description' => 'Application to ...'
    },
    '-title' => '[ldapdir-3.8] Login'
};
my $query = CGI->new;

print $query->start_html($attr), "\n";

Output:

<!DOCTYPE html
        PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
         ".dtd">
<html xmlns="; lang="de-DE" xml:lang="de-DE">
<head>
<title>[ldapdir-3.8] Login</title>
<link rev="made" href="mailto:Me%20%26lt%3Bmy%40e.mail%26gt%3B" />
<meta name="copyright" content="Copyright ..." />
<meta name="description" content="Application to ..." />
<meta name="keywords" content="bla" />
<link rel="stylesheet" type="text/css" href=".css" id="2.css" rel="stylesheet alternate" title="JavaScript Additions"/>
<script src=".js" type="text/javascript"></script>
<script src=".js" type="text/javascript"></script>
<script src=".js" type="text/javascript"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<noscript>
<link type="text/css" href=".css" id="3.css" title="No Script Additions" rel="stylesheet" />
</noscript>

</head>
<body>

I tend to believe it's a bug in $CGI::VERSION='3.63'. How can I fix it?

In a program I'm using a rather sophisticated parameter for CGI's start_html, but that results in a duplicate rel attribute for the CSS passed via -style.

Example code:

#!/usr/bin/perl
use strict;
use warnings;
use CGI;

my $attr = {
    '-script' => [
        {
            '-src' => 'https://server.domain./app/js/0.js',
            '-type' => 'text/javascript'
        },
        {
            '-src' => 'https://server.domain./app/js/2.js',
            '-type' => 'text/javascript'
        },
        {
            '-type' => 'text/javascript',
            '-src' => 'https://server.domain./app/js/3.js'
        }
    ],
    '-noscript' => '<link type="text/css" href="https://server.domain./app/style/3.css" id="3.css" title="No Script Additions" rel="stylesheet" />',
    '-encoding' => 'utf-8',
    '-author' => 'Me &lt;[email protected]&gt;',
    '-lang' => 'de-DE',
    '-style' => [
        {
            '-id' => '2.css',
            '-type' => 'text/css',
            '-src' => 'https://server.domain./app/style/2.css',
            '-rel' => 'stylesheet alternate',
            '-title' => 'JavaScript Additions'
        }
    ],
    '-meta' => {
            'copyright' => 'Copyright ...',
            'keywords' => 'bla',
            'description' => 'Application to ...'
    },
    '-title' => '[ldapdir-3.8] Login'
};
my $query = CGI->new;

print $query->start_html($attr), "\n";

Output:

<!DOCTYPE html
        PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
         "http://www.w3./TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3./1999/xhtml" lang="de-DE" xml:lang="de-DE">
<head>
<title>[ldapdir-3.8] Login</title>
<link rev="made" href="mailto:Me%20%26lt%3Bmy%40e.mail%26gt%3B" />
<meta name="copyright" content="Copyright ..." />
<meta name="description" content="Application to ..." />
<meta name="keywords" content="bla" />
<link rel="stylesheet" type="text/css" href="https://server.domain./app/style/2.css" id="2.css" rel="stylesheet alternate" title="JavaScript Additions"/>
<script src="https://server.domain./app/js/0.js" type="text/javascript"></script>
<script src="https://server.domain./app/js/2.js" type="text/javascript"></script>
<script src="https://server.domain./app/js/3.js" type="text/javascript"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<noscript>
<link type="text/css" href="https://server.domain./app/style/3.css" id="3.css" title="No Script Additions" rel="stylesheet" />
</noscript>

</head>
<body>

I tend to believe it's a bug in $CGI::VERSION='3.63'. How can I fix it?

Share Improve this question edited Nov 20, 2024 at 9:03 U. Windl asked Nov 19, 2024 at 11:42 U. WindlU. Windl 4,48936 silver badges63 bronze badges 9
  • By defining -style on top level of the %attr array you already defined the "stylesheet". You do not need to define -rel manually. – White Owl Commented Nov 19, 2024 at 13:32
  • So is the solution to use -link instead of -style? – ikegami Commented Nov 19, 2024 at 15:09
  • The CGI module has been removed from core for being obsolete (the author's own words in the docs), and the HTML generation functions are no longer maintained, as described here: metacpan./pod/… – TLP Commented Nov 19, 2024 at 15:32
  • @ikegami So you are saying -style is just an alias for -link having pre-set '-rel' => 'stylesheet'? – U. Windl Commented Nov 20, 2024 at 6:03
  • @TLP I agree that CGI's HTML generation is primitive and not perfect, but people have built around those, so simply dropping it is a no-go IMHO. I'd probably build my own mostly compatible version of CGI then. That way you can rely on its existence in the future. – U. Windl Commented Nov 20, 2024 at 6:14
 |  Show 4 more comments

1 Answer 1

Reset to default 3

After reading LIMITED SUPPORT FOR CASCADING STYLE SHEETS I changed the attributes like this (so I dropped -rel and -type while adding '-alternate' => 1):

# ...
    '-style' => [
        {
            '-id' => '2.css',
            '-alternate' => 1,
            '-src' => 'https://server.domain./app/style/2.css',
            '-title' => 'JavaScript Additions'
        }
    ],
# ...

and the resulting HTML was

<!-- ... -->
<link rel="alternate stylesheet" type="text/css" href="https://server.domain./app/style/2.css" title="JavaScript Additions" id="2.css"/>
<!-- ... -->

and the duplicate rel was gone.

发布评论

评论列表(0)

  1. 暂无评论