File: parser-tokens.t

package info (click to toggle)
libcss-dom-perl 0.17-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,044 kB
  • sloc: perl: 7,310; makefile: 2
file content (214 lines) | stat: -rw-r--r-- 6,722 bytes parent folder | download
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/perl -T

use strict; use warnings; no warnings qw 'utf8 parenthesis';
our $tests;
BEGIN { ++$INC{'tests.pm'} }
sub tests::VERSION { $tests += pop };
use Test::More;
plan tests => $tests;

use utf8;

use CSS::DOM;
use CSS::DOM::Rule::Import;
use CSS::DOM::Style;
use CSS::DOM::Value::Primitive ':all';

use tests 370; # identifiers
for('_', 'a'..'z', 'A'.."Z", map(chr,0x80..0x100) ,"\x{2003}","\x{3000}"){
	my $style = CSS::DOM::Style::parse($_ . 
		"\\20\\10fFfff-_abcABC\\}\\7d\\7d \\7d\t\\7d\r\n\\7d\n"
		. "\\7d\r\\7d\f\xff\x{2003}\x{100}\\\t"
		. ": 65"
	);
	is $style->getPropertyValue(
		"$_ \x{10ffff}f-_abcABC}}}}}}}}\xff\x{2003}\x{100}\t"
	), 65, 'identifier beginning with ' . (ord()<127 ? $_ :"chr ".ord);

	$style = CSS::DOM::Style::parse("-$_" . 
		"\\20\\10fFfff-_abcABC\\}\\7d\\7d \\7d\t\\7d\r\n\\7d\n"
		. "\\7d\r\\7d\f\xff\x{2003}\x{100}\\\t"
		. ": 65"
	);
	is $style->getPropertyValue(
		"-$_ \x{10ffff}f-_abcABC}}}}}}}}\xff\x{2003}\x{100}\t"
	), 65, 'identifier beginning with -'. (ord()<127 ? $_ :"chr ".ord);
}
{
	my $style = CSS::DOM::Style::parse("--a: 65");
	is $style->cssText, '', 'identifier can\'t begin with --';
	$style = CSS::DOM::Style::parse"-0b:-0b";
	is $style->cssText, '', 'nor with -0';
}

use tests 8; # strings
{
	my $nasty_escaped_string =
		"\\20\\10fFfff-_abcABC\\}\\7d\\7d \\7d\t\\7d\r\n\\7d\n"
		. "\\7d\r\\7d\f\xff\x{2003}\x{100}\\\r\n\\\n\\\r\\\t\\\f"
		. q/\'\"/;
	my $expect =
		qq/ \x{10ffff}f-_abcABC}}}}}}}}\xff\x{2003}\x{100}\t'"/;
	my $rule = new CSS::DOM::Rule::Import;

	$rule->cssText('@import ' . "'$nasty_escaped_string'");
	is $rule->href, $expect, "'...'";
	$rule->cssText('@import ' . qq'"$nasty_escaped_string"');
	is $rule->href, $expect, '"..."';
	$rule->cssText('@import ' . "'$nasty_escaped_string");
	is $rule->href, $expect, "'...";
	$rule->cssText('@import ' . qq'"$nasty_escaped_string');
	is $rule->href, $expect, '"...';
	$rule->cssText('@import ' . q"'\'");
	is $rule->href, "'", q"'\'";
	$rule->cssText('@import ' . q'"\"');
	is $rule->href, '"', '"\"';
	$rule->cssText('@import ' . q"'");
	is $rule->href, "", q"'";
	$rule->cssText('@import ' . q'"');
	is $rule->href, '', '"';
}


# ~~~ once both selectors mean something and getCSSPropertyValue is imple-
#     mented, we need #hash tests

# ~~~ numbers
# ~~~ percent
# ~~~ dimensions


use tests 23; # urls
{
	my $rule = new CSS::DOM::Rule::Import;

	$rule->cssText('@import url(!$#%&][\\\}|{*~foo/bar.gif)');
	is $rule->href, '!$#%&][\}|{*~foo/bar.gif', 'unquoted url';
	$rule->cssText('@import url(/*foo*/foo/bar.gif/*bar)');
	is $rule->href, '/*foo*/foo/bar.gif/*bar', 
		'unquoted url w/"comments"';
	$rule->cssText('@import url("\"\'foo")');
	is $rule->href, q/"'foo/, 'double-quoted url';
	$rule->cssText('@import url(\'\\\'"foo\')');
	is $rule->href, q/'"foo/, 'single-quoted url';
	$rule->cssText("\@import url(\n \t\f\rstuff   \r\n\t \f)");
	is $rule->href, "stuff",
		'unquoted url with ws';
	$rule->cssText("\@import url(\n \t\f\r'stuff'   \r\n\t \f)");
	is $rule->href, "stuff",
		'single-quoted url with ws';
	$rule->cssText(qq'\@import url(\n \t\f\r"stuff"   \r\n\t \f)');
	is $rule->href, "stuff",
		'double-quoted url with ws';
	$rule->cssText('@import '.
		"url(\x{2000}\\2000\r\n\\a\n\\20 0\\20\r\\20\f\\)\\z)"
	);
	is $rule->href, "\x{2000}\x{2000}\n 0  )z",
		'unquoted url with escapes';
	$rule->cssText('@import '.
		"url('\x{2000}\\2000\r\n\\a\n\\20 0\\20\r\\20\f\\)"
		."\\\r\n\\\n\\\r\\\t\\\f\\z')"
	);
	is $rule->href, "\x{2000}\x{2000}\n 0  )\tz",
		'single-quoted  url with escapes';
	$rule->cssText('@import '.
		"url(\"\x{2000}\\2000\r\n\\a\n\\20 0\\20\r\\20\f\\)"
		."\\\r\n\\\n\\\r\\\t\\\f\\z\")"
	);
	is $rule->href, "\x{2000}\x{2000}\n 0  )\tz",
		'double-quoted url with escapes';

	my $style = new CSS::DOM::Style;

	$style->name('url(foo');
	is $style->name, 'url(foo)', 'unquoted, unterminated url';
	$style->name('url(\'goo');
	is $style->name, 'url(\'goo\')',
		'single-quoted, unterminated url';
	$style->name('url("foo');
	is $style->name, 'url("foo")',
		'double-quoted, unterminated url';
	$style->name('url(');
	is $style->name, 'url()', 'blank unquoted, unterminated url';
	$style->name('url(\'');
	is $style->name, 'url(\'\')',
		'blank single-quoted, unterminated url';
	$style->name('url("');
	is $style->name, 'url("")',
		'blank double-quoted, unterminated url';
	$style->name('url(');
	is $style->name, 'url()',
		'unterminated unquoted url, ending with \)';
	$style->name(q"url('\'");
	is $style->name, q"url('\'')",
		'unterminated single-quoted url, ending with \\\'';
	$style->name('url("\"');
	is $style->name, 'url("\"")',
		'unterminated double-quoted url, ending with \"';
	$style->name(q"url('foo'");
	is $style->name, q"url('foo')",
		'single-quoted url without )';
	$style->name('url("foo"');
	is $style->name, 'url("foo")',
		'double-quoted url without )';
	$style->name(q"url('foo' ");
	is $style->name, q"url('foo' )",
		'single-quoted url without ) but with ws';
	$style->name('url("foo" ');
	is $style->name, 'url("foo" )',
		'double-quoted url without ) but with ws';
}

# ~~~ unicode range
# Come to think of it, if we didn’t support this as a separate token,
# U+abcd-U+012a would be interpreted as
# ident [U], delim [+], ident [abcd], delim [-], dim [012a]
# which would still become a CSS_UNKNOWN value whose cssText value returned
# exactly the same. So a test for it would pass whether unirange were a
# token or not. (Or would it become a primitive with a type of CSS_CUSTOM?)

use tests 3; # spaces and comments
{
	my $style = CSS::DOM::Style::parse(
		"name/*fooo*/  :"
		." \t\r\n\f/*etet*/ /**oo**//*oo** * **/\n/*/** /*/"
		. 'value/*eeeee'
	);
	is $style->name, 'value', 'whitespace and comments';
	$style = CSS::DOM::Style::parse(
		"name:valu  /*eeeee "  
	);
	is $style->name, 'valu', 'another ws /**/ test';
	$style = CSS::DOM::Style::parse( "name: /*\n*/valu");
	is $style->name, 'valu', 'multiline comments';
}

# ~~~ function

use tests 6; # <!-- -->
{
	my $sheet = CSS::DOM::parse ' <!--{ name: value }--> @media print{}';
	is join('',map cssText$_,cssRules$sheet),
		"{ name: value }\n\@media print {\n}\n",
		'ignored <!-- -->';
	is CSS::DOM::parse"{}{name: <!-- value; n:v}" =>->
		cssRules->length,
	   1,
		'invalid <!--';
	ok $@, '$@ after invalid <!--';
	is CSS::DOM::parse"{}{name: --> value; n:v}" =>->
		cssRules->length,
	   1,
		'invalid -->';
	ok $@, '$@ after invalid -->';
	is CSS::DOM::Style::parse"name:'<!--value-->",->name,
		"'<!--value-->'", '<!-- --> in a string';
}

use tests 1; # miscellaneous tokens
{
	my $sheet = CSS::DOM::parse  '@foo ()[~=:,./+-]{[("\"';
	is $sheet->cssRules->[0]->cssText,
		'@foo ()[~=:,./+-]{[("\"")]}'. "\n",
		'miscellaneous tokens'
}