/[suikacvs]/markup/html/whatpm/t/CSS-Parser-1.t
Suika

Contents of /markup/html/whatpm/t/CSS-Parser-1.t

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.13 - (hide annotations) (download) (as text)
Mon Jan 28 13:13:24 2008 UTC (18 years, 6 months ago) by wakaba
Branch: MAIN
Changes since 1.12: +3 -2 lines
File MIME type: application/x-troff
++ whatpm/t/ChangeLog	28 Jan 2008 13:13:16 -0000
2008-01-28  Wakaba  <wakaba@suika.fam.cx>

	* CSS-Parser-1.t: 'content' added to the list of supported
	property and the list of initial values.

	* css-generated.dat: Tests for 'content' are added.

2008-01-27  Wakaba  <wakaba@suika.fam.cx>

	* CSS-Parser-1.t: Loads test files as UTF-8.

	* css-generated.dat: Semi-real-world test data for 'quotes' are added.

++ whatpm/Whatpm/CSS/ChangeLog	28 Jan 2008 13:11:50 -0000
2008-01-28  Wakaba  <wakaba@suika.fam.cx>

	* Parser.pm ($default_serializer): New 'CONTENT' type supported.
	('content'): Implemented.

1 wakaba 1.1 #!/usr/bin/perl
2     use strict;
3    
4     use lib qw[/home/wakaba/work/manakai2/lib]; ## TODO: ...
5    
6     use Test;
7    
8     BEGIN { plan tests => 548 }
9    
10     require Whatpm::CSS::Parser;
11     require Message::DOM::Window;
12    
13     require Message::DOM::DOMImplementation;
14     my $dom = Message::DOM::DOMImplementation->new;
15    
16     my $DefaultComputed;
17     my $DefaultComputedText;
18    
19     for my $file_name (map {"t/$_"} qw(
20     css-1.dat
21 wakaba 1.4 css-visual.dat
22 wakaba 1.6 css-generated.dat
23 wakaba 1.9 css-paged.dat
24     css-text.dat
25 wakaba 1.7 css-font.dat
26 wakaba 1.10 css-table.dat
27     css-interactive.dat
28 wakaba 1.1 )) {
29     print "# $file_name\n";
30 wakaba 1.13 open my $file, '<:utf8', $file_name or die "$0: $file_name: $!";
31 wakaba 1.1
32     my $all_test = {document => {}, test => []};
33     my $test;
34     my $mode = 'data';
35     my $doc_id;
36     my $selectors;
37     while (<$file>) {
38     s/\x0D\x0A/\x0A/;
39     if (/^#data$/) {
40     undef $test;
41     $test->{data} = '';
42     push @{$all_test->{test}}, $test;
43     $mode = 'data';
44     } elsif (/#(csstext|cssom)$/) {
45     $test->{$1} = '';
46     $mode = $1;
47     } elsif (/#(computed(?>text)?) (\S+) (.+)$/) {
48     $test->{$1}->{$doc_id = $2}->{$selectors = $3} = '';
49     $mode = $1;
50     } elsif (/^#html (\S+)$/) {
51     undef $test;
52     $test->{format} = 'html';
53     $test->{data} = '';
54     $all_test->{document}->{$1} = $test;
55     $mode = 'data';
56 wakaba 1.2 } elsif (/^#errors$/) {
57     $test->{errors} = [];
58     $mode = 'errors';
59     $test->{data} =~ s/\x0D?\x0A\z//;
60 wakaba 1.4 } elsif (/^#option q$/) {
61     $test->{option}->{parse_mode} = 'q';
62 wakaba 1.1 } elsif (defined $test->{data} and /^$/) {
63     undef $test;
64     } else {
65     if ({data => 1, cssom => 1, csstext => 1}->{$mode}) {
66     $test->{$mode} .= $_;
67     } elsif ($mode eq 'computed' or $mode eq 'computedtext') {
68     $test->{$mode}->{$doc_id}->{$selectors} .= $_;
69 wakaba 1.2 } elsif ($mode eq 'errors') {
70     tr/\x0D\x0A//d;
71     push @{$test->{errors}}, $_;
72 wakaba 1.1 } else {
73     die "Line $.: $_";
74     }
75     }
76     }
77    
78     for my $data (values %{$all_test->{document}}) {
79     if ($data->{format} eq 'html') {
80     my $doc = $dom->create_document;
81     $doc->manakai_is_html (1);
82     $doc->inner_html ($data->{data});
83     $data->{document} = $doc;
84     } else {
85     die "Test data format $data->{format} is not supported";
86     }
87     }
88    
89     for my $test (@{$all_test->{test}}) {
90 wakaba 1.4 my ($p, $css_options) = get_parser ($test->{option}->{parse_mode});
91 wakaba 1.1
92 wakaba 1.2 my @actual_error;
93 wakaba 1.1 $p->{onerror} = sub {
94     my (%opt) = @_;
95 wakaba 1.2 my $uri = ${$opt{uri}};
96     $uri =~ s[^thismessage:/][];
97     push @actual_error, join ';',
98     $uri, $opt{token}->{line}, $opt{token}->{column},
99     $opt{level},
100     $opt{type} . (defined $opt{value} ? ','.$opt{value} : '');
101 wakaba 1.1 };
102    
103     my $ss = $p->parse_char_string ($test->{data});
104 wakaba 1.2
105     ok ((join "\n", @actual_error), (join "\n", @{$test->{errors} or []}),
106     "#result ($test->{data})");
107 wakaba 1.1
108     if (defined $test->{cssom}) {
109     my $actual = serialize_cssom ($ss);
110     ok $actual, $test->{cssom}, "#cssom ($test->{data})";
111     }
112    
113     if (defined $test->{csstext}) {
114     my $actual = $ss->css_text;
115     ok $actual, $test->{csstext}, "#csstext ($test->{data})";
116     }
117    
118     for my $doc_id (keys %{$test->{computed} or {}}) {
119     for my $selectors (keys %{$test->{computed}->{$doc_id}}) {
120     my ($window, $style) = get_computed_style
121     ($all_test, $doc_id, $selectors, $dom, $css_options, $ss);
122     ## NOTE: $window is the root object, so that we must keep it
123     ## referenced in this block.
124    
125     my $actual = serialize_style ($style, '');
126     my $expected = $DefaultComputed;
127     my $diff = $test->{computed}->{$doc_id}->{$selectors};
128     ($actual, $expected) = apply_diff ($actual, $expected, $diff);
129     ok $actual, $expected,
130     "#computed $doc_id $selectors ($test->{data})";
131     }
132     }
133    
134     for my $doc_id (keys %{$test->{computedtext} or {}}) {
135     for my $selectors (keys %{$test->{computedtext}->{$doc_id}}) {
136     my ($window, $style) = get_computed_style
137     ($all_test, $doc_id, $selectors, $dom, $css_options, $ss);
138     ## NOTE: $window is the root object, so that we must keep it
139     ## referenced in this block.
140    
141     my $actual = $style->css_text;
142     my $expected = $DefaultComputedText;
143     my $diff = $test->{computedtext}->{$doc_id}->{$selectors};
144     ($actual, $expected) = apply_diff ($actual, $expected, $diff);
145     "#computedtext $doc_id $selectors ($test->{data})";
146     ok $actual, $expected,
147     "#computedtext $doc_id $selectors ($test->{data})";
148     }
149     }
150     }
151     }
152    
153     my @longhand;
154     my @shorthand;
155     BEGIN {
156     @longhand = qw/
157     background-attachment background-color background-image
158     background-position-x background-position-y
159     background-repeat border-bottom-color
160     border-bottom-style border-bottom-width border-collapse
161     border-left-color
162     border-left-style border-left-width border-right-color
163     border-right-style border-right-width
164     -manakai-border-spacing-x -manakai-border-spacing-y
165     border-top-color border-top-style border-top-width bottom
166 wakaba 1.13 caption-side clear color content cursor direction display empty-cells float
167 wakaba 1.1 font-family font-size font-style font-variant font-weight height left
168     letter-spacing line-height
169     list-style-image list-style-position list-style-type
170     margin-bottom margin-left margin-right margin-top
171     max-height max-width min-height min-width opacity -moz-opacity
172 wakaba 1.11 orphans outline-color outline-style outline-width overflow-x overflow-y
173 wakaba 1.1 padding-bottom padding-left padding-right padding-top
174     page-break-after page-break-before page-break-inside
175 wakaba 1.12 position quotes right table-layout
176 wakaba 1.1 text-align text-decoration text-indent text-transform
177     top unicode-bidi vertical-align visibility white-space width widows
178     word-spacing z-index
179     /;
180     @shorthand = qw/
181     background background-position
182     border border-color border-style border-width border-spacing
183     border-top border-right border-bottom border-left
184 wakaba 1.11 font list-style margin outline overflow padding
185 wakaba 1.1 /;
186     $DefaultComputedText = q[ border-spacing: 0px;
187     background: transparent none repeat scroll 0% 0%;
188     border: 0px none -manakai-default;
189     border-collapse: separate;
190     bottom: auto;
191     caption-side: top;
192     clear: none;
193     color: -manakai-default;
194 wakaba 1.13 content: normal;
195 wakaba 1.1 cursor: auto;
196     direction: ltr;
197     display: inline;
198     empty-cells: show;
199     float: none;
200     font-family: -manakai-default;
201     font-size: 16px;
202     font-style: normal;
203     font-variant: normal;
204     font-weight: 400;
205     height: auto;
206     left: auto;
207     letter-spacing: normal;
208     line-height: normal;
209     list-style-image: none;
210     list-style-position: outside;
211     list-style-type: disc;
212 wakaba 1.5 margin: 0px;
213 wakaba 1.1 max-height: none;
214     max-width: none;
215     min-height: 0px;
216     min-width: 0px;
217     opacity: 1;
218     orphans: 2;
219     outline: 0px none invert;
220     overflow: visible;
221 wakaba 1.6 padding: 0px;
222 wakaba 1.1 page-break-after: auto;
223     page-break-before: auto;
224     page-break-inside: auto;
225     position: static;
226 wakaba 1.12 quotes: -manakai-default;
227 wakaba 1.1 right: auto;
228     table-layout: auto;
229     text-align: begin;
230     text-decoration: none;
231     text-indent: 0px;
232     text-transform: none;
233     top: auto;
234     unicode-bidi: normal;
235     vertical-align: baseline;
236     visibility: visible;
237     white-space: normal;
238     widows: 2;
239     width: auto;
240     word-spacing: normal;
241     z-index: auto;
242     ];
243     $DefaultComputed = $DefaultComputedText;
244     $DefaultComputed =~ s/^ /| /gm;
245     $DefaultComputed =~ s/;$//gm;
246     $DefaultComputed .= q[| -manakai-border-spacing-x: 0px
247     | -manakai-border-spacing-y: 0px
248     | -moz-opacity: 1
249     | background-attachment: scroll
250     | background-color: transparent
251     | background-image: none
252 wakaba 1.8 | background-position: 0% 0%
253 wakaba 1.1 | background-position-x: 0%
254     | background-position-y: 0%
255     | background-repeat: repeat
256     | border-top: 0px none -manakai-default
257     | border-right: 0px none -manakai-default
258     | border-bottom: 0px none -manakai-default
259     | border-left: 0px none -manakai-default
260     | border-bottom-color: -manakai-default
261     | border-bottom-style: none
262     | border-bottom-width: 0px
263     | border-left-color: -manakai-default
264     | border-left-style: none
265     | border-left-width: 0px
266     | border-right-color: -manakai-default
267     | border-right-style: none
268     | border-right-width: 0px
269     | border-top-color: -manakai-default
270     | border-top-style: none
271     | border-top-width: 0px
272     | border-color: -manakai-default
273     | border-style: none
274     | border-width: 0px
275     | float: none
276 wakaba 1.7 | font: 400 16px -manakai-default
277 wakaba 1.6 | list-style: disc none outside
278 wakaba 1.5 | margin-top: 0px
279     | margin-right: 0px
280     | margin-bottom: 0px
281     | margin-left: 0px
282 wakaba 1.1 | outline-color: invert
283     | outline-style: none
284     | outline-width: 0px
285 wakaba 1.11 | overflow-x: visible
286     | overflow-y: visible
287 wakaba 1.6 | padding-bottom: 0px
288     | padding-left: 0px
289     | padding-right: 0px
290     | padding-top: 0px];
291 wakaba 1.1 }
292    
293 wakaba 1.4 sub get_parser ($) {
294     my $parse_mode = shift;
295    
296 wakaba 1.1 my $p = Whatpm::CSS::Parser->new;
297 wakaba 1.4
298     if ($parse_mode eq 'q') {
299     $p->{unitless_px} = 1;
300     $p->{hashless_color} = 1;
301     }
302 wakaba 1.1
303     $p->{prop}->{$_} = 1 for (@longhand, @shorthand);
304     $p->{prop_value}->{display}->{$_} = 1 for qw/
305     block inline inline-block inline-table list-item none
306     table table-caption table-cell table-column table-column-group
307     table-header-group table-footer-group table-row table-row-group
308     /;
309     $p->{prop_value}->{position}->{$_} = 1 for qw/
310     absolute fixed relative static
311     /;
312     $p->{prop_value}->{float}->{$_} = 1 for qw/
313     left right none
314     /;
315     $p->{prop_value}->{clear}->{$_} = 1 for qw/
316     left right none both
317     /;
318     $p->{prop_value}->{direction}->{ltr} = 1;
319     $p->{prop_value}->{direction}->{rtl} = 1;
320     $p->{prop_value}->{'unicode-bidi'}->{$_} = 1 for qw/
321     normal bidi-override embed
322     /;
323 wakaba 1.11 for my $prop_name (qw/overflow overflow-x overflow-y/) {
324     $p->{prop_value}->{$prop_name}->{$_} = 1 for qw/
325     visible hidden scroll auto -webkit-marquee -moz-hidden-unscrollable
326     /;
327     }
328 wakaba 1.1 $p->{prop_value}->{visibility}->{$_} = 1 for qw/
329     visible hidden collapse
330     /;
331     $p->{prop_value}->{'list-style-type'}->{$_} = 1 for qw/
332     disc circle square decimal decimal-leading-zero
333     lower-roman upper-roman lower-greek lower-latin
334     upper-latin armenian georgian lower-alpha upper-alpha none
335     /;
336     $p->{prop_value}->{'list-style-position'}->{outside} = 1;
337     $p->{prop_value}->{'list-style-position'}->{inside} = 1;
338     $p->{prop_value}->{'page-break-before'}->{$_} = 1 for qw/
339     auto always avoid left right
340     /;
341     $p->{prop_value}->{'page-break-after'}->{$_} = 1 for qw/
342     auto always avoid left right
343     /;
344     $p->{prop_value}->{'page-break-inside'}->{auto} = 1;
345     $p->{prop_value}->{'page-break-inside'}->{avoid} = 1;
346     $p->{prop_value}->{'background-repeat'}->{$_} = 1 for qw/
347     repeat repeat-x repeat-y no-repeat
348     /;
349     $p->{prop_value}->{'background-attachment'}->{scroll} = 1;
350     $p->{prop_value}->{'background-attachment'}->{fixed} = 1;
351     $p->{prop_value}->{'font-style'}->{normal} = 1;
352     $p->{prop_value}->{'font-style'}->{italic} = 1;
353     $p->{prop_value}->{'font-style'}->{oblique} = 1;
354     $p->{prop_value}->{'font-variant'}->{normal} = 1;
355     $p->{prop_value}->{'font-variant'}->{'small-caps'} = 1;
356     $p->{prop_value}->{'text-align'}->{$_} = 1 for qw/
357     left right center justify begin end
358     /;
359     $p->{prop_value}->{'text-transform'}->{$_} = 1 for qw/
360     capitalize uppercase lowercase none
361     /;
362     $p->{prop_value}->{'white-space'}->{$_} = 1 for qw/
363     normal pre nowrap pre-line pre-wrap
364     /;
365     $p->{prop_value}->{'text-decoration'}->{$_} = 1 for qw/
366     none blink underline overline line-through
367     /;
368     $p->{prop_value}->{'caption-side'}->{$_} = 1 for qw/
369     top bottom
370     /;
371     $p->{prop_value}->{'table-layout'}->{auto} = 1;
372     $p->{prop_value}->{'table-layout'}->{fixed} = 1;
373     $p->{prop_value}->{'border-collapse'}->{collapase} = 1;
374     $p->{prop_value}->{'border-collapse'}->{separate} = 1;
375     $p->{prop_value}->{'empty-cells'}->{show} = 1;
376     $p->{prop_value}->{'empty-cells'}->{hide} = 1;
377     $p->{prop_value}->{cursor}->{$_} = 1 for qw/
378     auto crosshair default pointer move e-resize ne-resize nw-resize n-resize
379     se-resize sw-resize s-resize w-resize text wait help progress
380     /;
381     for my $prop (qw/border-top-style border-left-style
382     border-bottom-style border-right-style outline-style/) {
383     $p->{prop_value}->{$prop}->{$_} = 1 for qw/
384     none hidden dotted dashed solid double groove ridge inset outset
385     /;
386     }
387     for my $prop (qw/color background-color
388     border-bottom-color border-left-color border-right-color
389     border-top-color border-color/) {
390     $p->{prop_value}->{$prop}->{transparent} = 1;
391     $p->{prop_value}->{$prop}->{flavor} = 1;
392     $p->{prop_value}->{$prop}->{'-manakai-default'} = 1;
393     }
394     $p->{prop_value}->{'outline-color'}->{invert} = 1;
395     $p->{prop_value}->{'outline-color'}->{'-manakai-invert-or-currentcolor'} = 1;
396     $p->{pseudo_class}->{$_} = 1 for qw/
397     active checked disabled empty enabled first-child first-of-type
398     focus hover indeterminate last-child last-of-type link only-child
399     only-of-type root target visited
400     lang nth-child nth-last-child nth-of-type nth-last-of-type not
401     -manakai-contains -manakai-current
402     /;
403     $p->{pseudo_element}->{$_} = 1 for qw/
404     after before first-letter first-line
405     /;
406    
407     my $css_options = {
408     prop => $p->{prop},
409     prop_value => $p->{prop_value},
410     pseudo_class => $p->{pseudo_class},
411     pseudo_element => $p->{pseudo_element},
412     };
413    
414     $p->{href} = 'thismessage:/';
415    
416     return ($p, $css_options);
417     } # get_parser
418    
419     sub serialize_cssom ($) {
420     my $ss = shift;
421    
422     if (defined $ss) {
423     if ($ss->isa ('Message::IF::CSSStyleSheet')) {
424     my $v = '';
425     for my $rule (@{$ss->css_rules}) {
426     my $indent = '';
427     if ($rule->type == $rule->STYLE_RULE) {
428     $v .= '| ' . $indent . '<' . $rule->selector_text . ">\n";
429     $v .= serialize_style ($rule->style, $indent . ' ');
430     } else {
431     die "Rule type @{[$rule->type]} is not supported";
432     }
433     }
434     return $v;
435     } else {
436     return '(' . (ref $ss) . ')';
437     }
438     } else {
439     return '(undef)';
440     }
441     } # serialize_cssom
442    
443     sub get_computed_style ($$$$$$) {
444     my ($all_test, $doc_id, $selectors, $dom, $css_options, $ss) = @_;
445    
446     my $doc = $all_test->{document}->{$doc_id}->{document};
447     unless ($doc) {
448     die "Test document $doc_id is not defined";
449     }
450    
451 wakaba 1.6 my $element = $doc->query_selector ($selectors);
452 wakaba 1.1 unless ($element) {
453     die "Element $selectors not found in document $doc_id";
454     }
455    
456     my $window = Message::DOM::Window->___new ($dom);
457     $window->___set_css_options ($css_options);
458     $window->___set_user_style_sheets ([$ss]);
459     $window->set_document ($doc);
460    
461 wakaba 1.3 my $style = $element->manakai_computed_style;
462 wakaba 1.1 return ($window, $style);
463     } # get_computed_style
464    
465     sub serialize_style ($$) {
466     my ($style, $indent) = @_;
467    
468     ## TODO: check @$style
469    
470     my @v;
471     for (map {get_dom_names ($_)} @shorthand, @longhand) {
472     my $dom = $_->[1];
473     push @v, [$_->[0], $dom, $style->$dom,
474     $style->get_property_priority ($_->[0])];
475     $v[-1]->[3] = ' !' . $v[-1]->[3] if length $v[-1]->[3];
476     }
477     return join '', map {"| $indent$_->[0]: $_->[2]$_->[3]\n"}
478     sort {$a->[0] cmp $b->[0]} grep {length $_->[2]} @v;
479     } # serialize_style
480    
481     sub get_dom_names ($) {
482     my $dom_name = $_[0];
483     if ($_[0] eq 'float') {
484     return ([$_[0] => 'css_float'], [$_[0] => 'style_float']);
485     }
486    
487     $dom_name =~ tr/-/_/;
488     return ([$_[0] => $dom_name]);
489     } # get_dom_names
490    
491     sub apply_diff ($$$) {
492     my ($actual, $expected, $diff) = @_;
493     my @actual = split /[\x0D\x0A]+/, $actual;
494     my @expected = split /[\x0D\x0A]+/, $expected;
495     my @diff = split /[\x0D\x0A]+/, $diff;
496     for (@diff) {
497     if (s/^-//) {
498     push @actual, $_;
499     } elsif (s/^\+//) {
500     push @expected, $_;
501     } else {
502     die "Invalid diff line: $_";
503     }
504     }
505     $actual = join "\n", sort {$a cmp $b} @actual;
506     $expected = join "\n", sort {$a cmp $b} @expected;
507     ($actual, $expected);
508     } # apply_diff

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24