File: html.t

package info (click to toggle)
perl 5.20.2-3%2Bdeb8u11
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 102,964 kB
  • sloc: perl: 555,553; ansic: 214,041; sh: 38,121; pascal: 8,783; cpp: 3,895; makefile: 2,393; xml: 2,325; yacc: 1,741
file content (179 lines) | stat: -rw-r--r-- 4,861 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/local/bin/perl -w

use Test::More tests => 33;

END { ok $loaded; }
use CGI ( ':standard', '-no_debug', '*h3', 'start_table' );
$loaded = 1;
$CGI::Util::SORT_ATTRIBUTES= 1;
ok 1;

BEGIN {
    $| = 1;
    if ( $] > 5.006 ) {

        # no utf8
        require utf8;    # we contain Latin-1
        utf8->unimport;
    }
}

######################### End of black magic.

my $CRLF = "\015\012";
if ( $^O eq 'VMS' ) {
    $CRLF = "\n";        # via web server carriage is inserted automatically
}
if ( ord("\t") != 9 ) {    # EBCDIC?
    $CRLF = "\r\n";
}

# util
sub test {
    local ($^W) = 0;
    my ( undef, $true, $msg ) = @_;
    ok $true => $msg;
}

# all the automatic tags
is h1(), '<h1 />', "single tag";

is h1('fred'), '<h1>fred</h1>', "open/close tag";

is h1( 'fred', 'agnes', 'maura' ), '<h1>fred agnes maura</h1>',
  "open/close tag multiple";

is h1( { -align => 'CENTER' }, 'fred' ), '<h1 align="CENTER">fred</h1>',
  "open/close tag with attribute";

is h1( { -align => undef }, 'fred' ), '<h1 align>fred</h1>',
  "open/close tag with orphan attribute";

is h1( { -align => 'CENTER' }, [ 'fred', 'agnes' ] ),
  '<h1 align="CENTER">fred</h1> <h1 align="CENTER">agnes</h1>',
  "distributive tag with attribute";

{
    local $" = '-';

    is h1( 'fred', 'agnes', 'maura' ), '<h1>fred-agnes-maura</h1>',
      "open/close tag \$\" interpolation";

}

is header(), "Content-Type: text/html; charset=ISO-8859-1${CRLF}${CRLF}",
  "header()";

is header( -type => 'image/gif', -charset => '' ), "Content-Type: image/gif${CRLF}${CRLF}",
  "header()";

is header( -type => 'image/gif', -status => '500 Sucks' ),
  "Status: 500 Sucks${CRLF}Content-Type: image/gif${CRLF}${CRLF}", "header()";
 
# return to normal 
charset( 'ISO-8859-1' );

like header( -nph => 1 ),
  qr!HTTP/1.0 200 OK${CRLF}Server: cmdline${CRLF}Date:.+${CRLF}Content-Type: text/html; charset=ISO-8859-1${CRLF}${CRLF}!,
  "header()";

is start_html(), <<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>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
END

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 charset="utf-8" src="foo.js" type="text/javascript"></script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
END

for my $v (qw/ 2.0 3.2 4.0 4.01 /) {
    local $CGI::XHTML = 1;
    is
      start_html( -dtd => "-//IETF//DTD HTML $v//FR", -lang => 'fr' ),
      <<"END", 'start_html()';
<!DOCTYPE html
	PUBLIC "-//IETF//DTD HTML $v//FR">
<html lang="fr"><head><title>Untitled Document</title>
</head>
<body>
END
}

is
  start_html( -dtd => "-//IETF//DTD HTML 9.99//FR", -lang => 'fr' ),
  <<"END", 'start_html()';
<!DOCTYPE html
	PUBLIC "-//IETF//DTD HTML 9.99//FR">
<html xmlns="http://www.w3.org/1999/xhtml" lang="fr" xml:lang="fr">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
END

my $cookie =
  cookie( -name => 'fred', -value => [ 'chocolate', 'chip' ], -path => '/' );

is $cookie, 'fred=chocolate&chip; path=/', "cookie()";

my $h = header( -Cookie => $cookie );

like $h,
  qr!^Set-Cookie: fred=chocolate&chip\; path=/${CRLF}Date:.*${CRLF}Content-Type: text/html; charset=ISO-8859-1${CRLF}${CRLF}!s,
  "header(-cookie)";

is start_h3, '<h3>';

is end_h3, '</h3>';

is start_table( { -border => undef } ), '<table border>';
is h1( escapeHTML("this is <not> \x8bright\x9b") ),
  '<h1>this is &lt;not&gt; &#8249;right&#8250;</h1>';

charset('utf-8');

is h1( escapeHTML("this is <not> \x8bright\x9b") ),
  ord("\t") == 9
  ? '<h1>this is &lt;not&gt; right</h1>'
  : '<h1>this is &lt;not&gt; right</h1>';

is i( p('hello there') ), '<i><p>hello there</p></i>';

my $q = CGI->new;
is $q->h1('hi'), '<h1>hi</h1>';

$q->autoEscape(1);

is $q->p( { title => "hello world&egrave;" }, 'hello &aacute;' ),
  '<p title="hello world&amp;egrave;">hello &aacute;</p>';

$q->autoEscape(0);

is $q->p( { title => "hello world&egrave;" }, 'hello &aacute;' ),
  '<p title="hello world&egrave;">hello &aacute;</p>';

is p( { title => "hello world&egrave;" }, 'hello &aacute;' ),
  '<p title="hello world&amp;egrave;">hello &aacute;</p>';

is header( -type => 'image/gif', -charset => 'UTF-8' ),
  "Content-Type: image/gif; charset=UTF-8${CRLF}${CRLF}", "header()";