/[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.10 - (hide annotations) (download) (as text)
Sun Jan 27 08:09:12 2008 UTC (17 years, 5 months ago) by wakaba
Branch: MAIN
Changes since 1.9: +2 -0 lines
File MIME type: application/x-troff
++ whatpm/t/ChangeLog	27 Jan 2008 08:09:01 -0000
	* CSS-Parser-1.t: Files |css-table.dat| and |css-interactive.dat|
	are added.

	* css-table.dat: New test file.

	* css-interactive.dat: New test file.

	* css-font.dat: New test data for 'font-size' are added.

	* css-text.dat: New test data for 'text-indent', 'letter-spacing',
	and 'word-spacing' are added.

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

++ whatpm/Whatpm/CSS/ChangeLog	27 Jan 2008 08:07:41 -0000
	* Parser.pm ('letter-spacing' parse): Support for '+'.
	('border-spacing' serialize_multiple): Revised taking into
	account 'import' and 'inherit'.
	('border-spacing' parse): Support for '+'.

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

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     open my $file, '<', $file_name or die "$0: $file_name: $!";
31    
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     caption-side clear color cursor direction display empty-cells float
167     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     orphans outline-color outline-style outline-width overflow
173     padding-bottom padding-left padding-right padding-top
174     page-break-after page-break-before page-break-inside
175     position right table-layout
176     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     font list-style margin outline padding
185     /;
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     cursor: auto;
195     direction: ltr;
196     display: inline;
197     empty-cells: show;
198     float: none;
199     font-family: -manakai-default;
200     font-size: 16px;
201     font-style: normal;
202     font-variant: normal;
203     font-weight: 400;
204     height: auto;
205     left: auto;
206     letter-spacing: normal;
207     line-height: normal;
208     list-style-image: none;
209     list-style-position: outside;
210     list-style-type: disc;
211 wakaba 1.5 margin: 0px;
212 wakaba 1.1 max-height: none;
213     max-width: none;
214     min-height: 0px;
215     min-width: 0px;
216     opacity: 1;
217     orphans: 2;
218     outline: 0px none invert;
219     overflow: visible;
220 wakaba 1.6 padding: 0px;
221 wakaba 1.1 page-break-after: auto;
222     page-break-before: auto;
223     page-break-inside: auto;
224     position: static;
225     right: auto;
226     table-layout: auto;
227     text-align: begin;
228     text-decoration: none;
229     text-indent: 0px;
230     text-transform: none;
231     top: auto;
232     unicode-bidi: normal;
233     vertical-align: baseline;
234     visibility: visible;
235     white-space: normal;
236     widows: 2;
237     width: auto;
238     word-spacing: normal;
239     z-index: auto;
240     ];
241     $DefaultComputed = $DefaultComputedText;
242     $DefaultComputed =~ s/^ /| /gm;
243     $DefaultComputed =~ s/;$//gm;
244     $DefaultComputed .= q[| -manakai-border-spacing-x: 0px
245     | -manakai-border-spacing-y: 0px
246     | -moz-opacity: 1
247     | background-attachment: scroll
248     | background-color: transparent
249     | background-image: none
250 wakaba 1.8 | background-position: 0% 0%
251 wakaba 1.1 | background-position-x: 0%
252     | background-position-y: 0%
253     | background-repeat: repeat
254     | border-top: 0px none -manakai-default
255     | border-right: 0px none -manakai-default
256     | border-bottom: 0px none -manakai-default
257     | border-left: 0px none -manakai-default
258     | border-bottom-color: -manakai-default
259     | border-bottom-style: none
260     | border-bottom-width: 0px
261     | border-left-color: -manakai-default
262     | border-left-style: none
263     | border-left-width: 0px
264     | border-right-color: -manakai-default
265     | border-right-style: none
266     | border-right-width: 0px
267     | border-top-color: -manakai-default
268     | border-top-style: none
269     | border-top-width: 0px
270     | border-color: -manakai-default
271     | border-style: none
272     | border-width: 0px
273     | float: none
274 wakaba 1.7 | font: 400 16px -manakai-default
275 wakaba 1.6 | list-style: disc none outside
276 wakaba 1.5 | margin-top: 0px
277     | margin-right: 0px
278     | margin-bottom: 0px
279     | margin-left: 0px
280 wakaba 1.1 | outline-color: invert
281     | outline-style: none
282     | outline-width: 0px
283 wakaba 1.6 | padding-bottom: 0px
284     | padding-left: 0px
285     | padding-right: 0px
286     | padding-top: 0px];
287 wakaba 1.1 }
288    
289 wakaba 1.4 sub get_parser ($) {
290     my $parse_mode = shift;
291    
292 wakaba 1.1 my $p = Whatpm::CSS::Parser->new;
293 wakaba 1.4
294     if ($parse_mode eq 'q') {
295     $p->{unitless_px} = 1;
296     $p->{hashless_color} = 1;
297     }
298 wakaba 1.1
299     $p->{prop}->{$_} = 1 for (@longhand, @shorthand);
300     $p->{prop_value}->{display}->{$_} = 1 for qw/
301     block inline inline-block inline-table list-item none
302     table table-caption table-cell table-column table-column-group
303     table-header-group table-footer-group table-row table-row-group
304     /;
305     $p->{prop_value}->{position}->{$_} = 1 for qw/
306     absolute fixed relative static
307     /;
308     $p->{prop_value}->{float}->{$_} = 1 for qw/
309     left right none
310     /;
311     $p->{prop_value}->{clear}->{$_} = 1 for qw/
312     left right none both
313     /;
314     $p->{prop_value}->{direction}->{ltr} = 1;
315     $p->{prop_value}->{direction}->{rtl} = 1;
316     $p->{prop_value}->{'unicode-bidi'}->{$_} = 1 for qw/
317     normal bidi-override embed
318     /;
319     $p->{prop_value}->{overflow}->{$_} = 1 for qw/
320     visible hidden scroll auto
321     /;
322     $p->{prop_value}->{visibility}->{$_} = 1 for qw/
323     visible hidden collapse
324     /;
325     $p->{prop_value}->{'list-style-type'}->{$_} = 1 for qw/
326     disc circle square decimal decimal-leading-zero
327     lower-roman upper-roman lower-greek lower-latin
328     upper-latin armenian georgian lower-alpha upper-alpha none
329     /;
330     $p->{prop_value}->{'list-style-position'}->{outside} = 1;
331     $p->{prop_value}->{'list-style-position'}->{inside} = 1;
332     $p->{prop_value}->{'page-break-before'}->{$_} = 1 for qw/
333     auto always avoid left right
334     /;
335     $p->{prop_value}->{'page-break-after'}->{$_} = 1 for qw/
336     auto always avoid left right
337     /;
338     $p->{prop_value}->{'page-break-inside'}->{auto} = 1;
339     $p->{prop_value}->{'page-break-inside'}->{avoid} = 1;
340     $p->{prop_value}->{'background-repeat'}->{$_} = 1 for qw/
341     repeat repeat-x repeat-y no-repeat
342     /;
343     $p->{prop_value}->{'background-attachment'}->{scroll} = 1;
344     $p->{prop_value}->{'background-attachment'}->{fixed} = 1;
345     $p->{prop_value}->{'font-style'}->{normal} = 1;
346     $p->{prop_value}->{'font-style'}->{italic} = 1;
347     $p->{prop_value}->{'font-style'}->{oblique} = 1;
348     $p->{prop_value}->{'font-variant'}->{normal} = 1;
349     $p->{prop_value}->{'font-variant'}->{'small-caps'} = 1;
350     $p->{prop_value}->{'text-align'}->{$_} = 1 for qw/
351     left right center justify begin end
352     /;
353     $p->{prop_value}->{'text-transform'}->{$_} = 1 for qw/
354     capitalize uppercase lowercase none
355     /;
356     $p->{prop_value}->{'white-space'}->{$_} = 1 for qw/
357     normal pre nowrap pre-line pre-wrap
358     /;
359     $p->{prop_value}->{'text-decoration'}->{$_} = 1 for qw/
360     none blink underline overline line-through
361     /;
362     $p->{prop_value}->{'caption-side'}->{$_} = 1 for qw/
363     top bottom
364     /;
365     $p->{prop_value}->{'table-layout'}->{auto} = 1;
366     $p->{prop_value}->{'table-layout'}->{fixed} = 1;
367     $p->{prop_value}->{'border-collapse'}->{collapase} = 1;
368     $p->{prop_value}->{'border-collapse'}->{separate} = 1;
369     $p->{prop_value}->{'empty-cells'}->{show} = 1;
370     $p->{prop_value}->{'empty-cells'}->{hide} = 1;
371     $p->{prop_value}->{cursor}->{$_} = 1 for qw/
372     auto crosshair default pointer move e-resize ne-resize nw-resize n-resize
373     se-resize sw-resize s-resize w-resize text wait help progress
374     /;
375     for my $prop (qw/border-top-style border-left-style
376     border-bottom-style border-right-style outline-style/) {
377     $p->{prop_value}->{$prop}->{$_} = 1 for qw/
378     none hidden dotted dashed solid double groove ridge inset outset
379     /;
380     }
381     for my $prop (qw/color background-color
382     border-bottom-color border-left-color border-right-color
383     border-top-color border-color/) {
384     $p->{prop_value}->{$prop}->{transparent} = 1;
385     $p->{prop_value}->{$prop}->{flavor} = 1;
386     $p->{prop_value}->{$prop}->{'-manakai-default'} = 1;
387     }
388     $p->{prop_value}->{'outline-color'}->{invert} = 1;
389     $p->{prop_value}->{'outline-color'}->{'-manakai-invert-or-currentcolor'} = 1;
390     $p->{pseudo_class}->{$_} = 1 for qw/
391     active checked disabled empty enabled first-child first-of-type
392     focus hover indeterminate last-child last-of-type link only-child
393     only-of-type root target visited
394     lang nth-child nth-last-child nth-of-type nth-last-of-type not
395     -manakai-contains -manakai-current
396     /;
397     $p->{pseudo_element}->{$_} = 1 for qw/
398     after before first-letter first-line
399     /;
400    
401     my $css_options = {
402     prop => $p->{prop},
403     prop_value => $p->{prop_value},
404     pseudo_class => $p->{pseudo_class},
405     pseudo_element => $p->{pseudo_element},
406     };
407    
408     $p->{href} = 'thismessage:/';
409    
410     return ($p, $css_options);
411     } # get_parser
412    
413     sub serialize_cssom ($) {
414     my $ss = shift;
415    
416     if (defined $ss) {
417     if ($ss->isa ('Message::IF::CSSStyleSheet')) {
418     my $v = '';
419     for my $rule (@{$ss->css_rules}) {
420     my $indent = '';
421     if ($rule->type == $rule->STYLE_RULE) {
422     $v .= '| ' . $indent . '<' . $rule->selector_text . ">\n";
423     $v .= serialize_style ($rule->style, $indent . ' ');
424     } else {
425     die "Rule type @{[$rule->type]} is not supported";
426     }
427     }
428     return $v;
429     } else {
430     return '(' . (ref $ss) . ')';
431     }
432     } else {
433     return '(undef)';
434     }
435     } # serialize_cssom
436    
437     sub get_computed_style ($$$$$$) {
438     my ($all_test, $doc_id, $selectors, $dom, $css_options, $ss) = @_;
439    
440     my $doc = $all_test->{document}->{$doc_id}->{document};
441     unless ($doc) {
442     die "Test document $doc_id is not defined";
443     }
444    
445 wakaba 1.6 my $element = $doc->query_selector ($selectors);
446 wakaba 1.1 unless ($element) {
447     die "Element $selectors not found in document $doc_id";
448     }
449    
450     my $window = Message::DOM::Window->___new ($dom);
451     $window->___set_css_options ($css_options);
452     $window->___set_user_style_sheets ([$ss]);
453     $window->set_document ($doc);
454    
455 wakaba 1.3 my $style = $element->manakai_computed_style;
456 wakaba 1.1 return ($window, $style);
457     } # get_computed_style
458    
459     sub serialize_style ($$) {
460     my ($style, $indent) = @_;
461    
462     ## TODO: check @$style
463    
464     my @v;
465     for (map {get_dom_names ($_)} @shorthand, @longhand) {
466     my $dom = $_->[1];
467     push @v, [$_->[0], $dom, $style->$dom,
468     $style->get_property_priority ($_->[0])];
469     $v[-1]->[3] = ' !' . $v[-1]->[3] if length $v[-1]->[3];
470     }
471     return join '', map {"| $indent$_->[0]: $_->[2]$_->[3]\n"}
472     sort {$a->[0] cmp $b->[0]} grep {length $_->[2]} @v;
473     } # serialize_style
474    
475     sub get_dom_names ($) {
476     my $dom_name = $_[0];
477     if ($_[0] eq 'float') {
478     return ([$_[0] => 'css_float'], [$_[0] => 'style_float']);
479     }
480    
481     $dom_name =~ tr/-/_/;
482     return ([$_[0] => $dom_name]);
483     } # get_dom_names
484    
485     sub apply_diff ($$$) {
486     my ($actual, $expected, $diff) = @_;
487     my @actual = split /[\x0D\x0A]+/, $actual;
488     my @expected = split /[\x0D\x0A]+/, $expected;
489     my @diff = split /[\x0D\x0A]+/, $diff;
490     for (@diff) {
491     if (s/^-//) {
492     push @actual, $_;
493     } elsif (s/^\+//) {
494     push @expected, $_;
495     } else {
496     die "Invalid diff line: $_";
497     }
498     }
499     $actual = join "\n", sort {$a cmp $b} @actual;
500     $expected = join "\n", sort {$a cmp $b} @expected;
501     ($actual, $expected);
502     } # apply_diff

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24