/[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.11 - (hide annotations) (download) (as text)
Sun Jan 27 08:58:05 2008 UTC (17 years, 5 months ago) by wakaba
Branch: MAIN
Changes since 1.10: +9 -5 lines
File MIME type: application/x-troff
++ whatpm/t/ChangeLog	27 Jan 2008 08:57:57 -0000
	* CSS-Parser-1.t: 'overflow-x' and 'overflow-y' are added.

	* css-visual.dat: New test data for 'overflow', 'overflow-x',
	and 'overflow-y' are added.

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

++ whatpm/Whatpm/CSS/ChangeLog	27 Jan 2008 08:56:42 -0000
	* Parser.pm ($one_keyword_parser): More accurate error location
	reporting.
	('overflow-x', 'overflow-y'): Implemented.
	('overflow'): Reimplemented as a shorthand.

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 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     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 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     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.11 | overflow-x: visible
284     | overflow-y: visible
285 wakaba 1.6 | padding-bottom: 0px
286     | padding-left: 0px
287     | padding-right: 0px
288     | padding-top: 0px];
289 wakaba 1.1 }
290    
291 wakaba 1.4 sub get_parser ($) {
292     my $parse_mode = shift;
293    
294 wakaba 1.1 my $p = Whatpm::CSS::Parser->new;
295 wakaba 1.4
296     if ($parse_mode eq 'q') {
297     $p->{unitless_px} = 1;
298     $p->{hashless_color} = 1;
299     }
300 wakaba 1.1
301     $p->{prop}->{$_} = 1 for (@longhand, @shorthand);
302     $p->{prop_value}->{display}->{$_} = 1 for qw/
303     block inline inline-block inline-table list-item none
304     table table-caption table-cell table-column table-column-group
305     table-header-group table-footer-group table-row table-row-group
306     /;
307     $p->{prop_value}->{position}->{$_} = 1 for qw/
308     absolute fixed relative static
309     /;
310     $p->{prop_value}->{float}->{$_} = 1 for qw/
311     left right none
312     /;
313     $p->{prop_value}->{clear}->{$_} = 1 for qw/
314     left right none both
315     /;
316     $p->{prop_value}->{direction}->{ltr} = 1;
317     $p->{prop_value}->{direction}->{rtl} = 1;
318     $p->{prop_value}->{'unicode-bidi'}->{$_} = 1 for qw/
319     normal bidi-override embed
320     /;
321 wakaba 1.11 for my $prop_name (qw/overflow overflow-x overflow-y/) {
322     $p->{prop_value}->{$prop_name}->{$_} = 1 for qw/
323     visible hidden scroll auto -webkit-marquee -moz-hidden-unscrollable
324     /;
325     }
326 wakaba 1.1 $p->{prop_value}->{visibility}->{$_} = 1 for qw/
327     visible hidden collapse
328     /;
329     $p->{prop_value}->{'list-style-type'}->{$_} = 1 for qw/
330     disc circle square decimal decimal-leading-zero
331     lower-roman upper-roman lower-greek lower-latin
332     upper-latin armenian georgian lower-alpha upper-alpha none
333     /;
334     $p->{prop_value}->{'list-style-position'}->{outside} = 1;
335     $p->{prop_value}->{'list-style-position'}->{inside} = 1;
336     $p->{prop_value}->{'page-break-before'}->{$_} = 1 for qw/
337     auto always avoid left right
338     /;
339     $p->{prop_value}->{'page-break-after'}->{$_} = 1 for qw/
340     auto always avoid left right
341     /;
342     $p->{prop_value}->{'page-break-inside'}->{auto} = 1;
343     $p->{prop_value}->{'page-break-inside'}->{avoid} = 1;
344     $p->{prop_value}->{'background-repeat'}->{$_} = 1 for qw/
345     repeat repeat-x repeat-y no-repeat
346     /;
347     $p->{prop_value}->{'background-attachment'}->{scroll} = 1;
348     $p->{prop_value}->{'background-attachment'}->{fixed} = 1;
349     $p->{prop_value}->{'font-style'}->{normal} = 1;
350     $p->{prop_value}->{'font-style'}->{italic} = 1;
351     $p->{prop_value}->{'font-style'}->{oblique} = 1;
352     $p->{prop_value}->{'font-variant'}->{normal} = 1;
353     $p->{prop_value}->{'font-variant'}->{'small-caps'} = 1;
354     $p->{prop_value}->{'text-align'}->{$_} = 1 for qw/
355     left right center justify begin end
356     /;
357     $p->{prop_value}->{'text-transform'}->{$_} = 1 for qw/
358     capitalize uppercase lowercase none
359     /;
360     $p->{prop_value}->{'white-space'}->{$_} = 1 for qw/
361     normal pre nowrap pre-line pre-wrap
362     /;
363     $p->{prop_value}->{'text-decoration'}->{$_} = 1 for qw/
364     none blink underline overline line-through
365     /;
366     $p->{prop_value}->{'caption-side'}->{$_} = 1 for qw/
367     top bottom
368     /;
369     $p->{prop_value}->{'table-layout'}->{auto} = 1;
370     $p->{prop_value}->{'table-layout'}->{fixed} = 1;
371     $p->{prop_value}->{'border-collapse'}->{collapase} = 1;
372     $p->{prop_value}->{'border-collapse'}->{separate} = 1;
373     $p->{prop_value}->{'empty-cells'}->{show} = 1;
374     $p->{prop_value}->{'empty-cells'}->{hide} = 1;
375     $p->{prop_value}->{cursor}->{$_} = 1 for qw/
376     auto crosshair default pointer move e-resize ne-resize nw-resize n-resize
377     se-resize sw-resize s-resize w-resize text wait help progress
378     /;
379     for my $prop (qw/border-top-style border-left-style
380     border-bottom-style border-right-style outline-style/) {
381     $p->{prop_value}->{$prop}->{$_} = 1 for qw/
382     none hidden dotted dashed solid double groove ridge inset outset
383     /;
384     }
385     for my $prop (qw/color background-color
386     border-bottom-color border-left-color border-right-color
387     border-top-color border-color/) {
388     $p->{prop_value}->{$prop}->{transparent} = 1;
389     $p->{prop_value}->{$prop}->{flavor} = 1;
390     $p->{prop_value}->{$prop}->{'-manakai-default'} = 1;
391     }
392     $p->{prop_value}->{'outline-color'}->{invert} = 1;
393     $p->{prop_value}->{'outline-color'}->{'-manakai-invert-or-currentcolor'} = 1;
394     $p->{pseudo_class}->{$_} = 1 for qw/
395     active checked disabled empty enabled first-child first-of-type
396     focus hover indeterminate last-child last-of-type link only-child
397     only-of-type root target visited
398     lang nth-child nth-last-child nth-of-type nth-last-of-type not
399     -manakai-contains -manakai-current
400     /;
401     $p->{pseudo_element}->{$_} = 1 for qw/
402     after before first-letter first-line
403     /;
404    
405     my $css_options = {
406     prop => $p->{prop},
407     prop_value => $p->{prop_value},
408     pseudo_class => $p->{pseudo_class},
409     pseudo_element => $p->{pseudo_element},
410     };
411    
412     $p->{href} = 'thismessage:/';
413    
414     return ($p, $css_options);
415     } # get_parser
416    
417     sub serialize_cssom ($) {
418     my $ss = shift;
419    
420     if (defined $ss) {
421     if ($ss->isa ('Message::IF::CSSStyleSheet')) {
422     my $v = '';
423     for my $rule (@{$ss->css_rules}) {
424     my $indent = '';
425     if ($rule->type == $rule->STYLE_RULE) {
426     $v .= '| ' . $indent . '<' . $rule->selector_text . ">\n";
427     $v .= serialize_style ($rule->style, $indent . ' ');
428     } else {
429     die "Rule type @{[$rule->type]} is not supported";
430     }
431     }
432     return $v;
433     } else {
434     return '(' . (ref $ss) . ')';
435     }
436     } else {
437     return '(undef)';
438     }
439     } # serialize_cssom
440    
441     sub get_computed_style ($$$$$$) {
442     my ($all_test, $doc_id, $selectors, $dom, $css_options, $ss) = @_;
443    
444     my $doc = $all_test->{document}->{$doc_id}->{document};
445     unless ($doc) {
446     die "Test document $doc_id is not defined";
447     }
448    
449 wakaba 1.6 my $element = $doc->query_selector ($selectors);
450 wakaba 1.1 unless ($element) {
451     die "Element $selectors not found in document $doc_id";
452     }
453    
454     my $window = Message::DOM::Window->___new ($dom);
455     $window->___set_css_options ($css_options);
456     $window->___set_user_style_sheets ([$ss]);
457     $window->set_document ($doc);
458    
459 wakaba 1.3 my $style = $element->manakai_computed_style;
460 wakaba 1.1 return ($window, $style);
461     } # get_computed_style
462    
463     sub serialize_style ($$) {
464     my ($style, $indent) = @_;
465    
466     ## TODO: check @$style
467    
468     my @v;
469     for (map {get_dom_names ($_)} @shorthand, @longhand) {
470     my $dom = $_->[1];
471     push @v, [$_->[0], $dom, $style->$dom,
472     $style->get_property_priority ($_->[0])];
473     $v[-1]->[3] = ' !' . $v[-1]->[3] if length $v[-1]->[3];
474     }
475     return join '', map {"| $indent$_->[0]: $_->[2]$_->[3]\n"}
476     sort {$a->[0] cmp $b->[0]} grep {length $_->[2]} @v;
477     } # serialize_style
478    
479     sub get_dom_names ($) {
480     my $dom_name = $_[0];
481     if ($_[0] eq 'float') {
482     return ([$_[0] => 'css_float'], [$_[0] => 'style_float']);
483     }
484    
485     $dom_name =~ tr/-/_/;
486     return ([$_[0] => $dom_name]);
487     } # get_dom_names
488    
489     sub apply_diff ($$$) {
490     my ($actual, $expected, $diff) = @_;
491     my @actual = split /[\x0D\x0A]+/, $actual;
492     my @expected = split /[\x0D\x0A]+/, $expected;
493     my @diff = split /[\x0D\x0A]+/, $diff;
494     for (@diff) {
495     if (s/^-//) {
496     push @actual, $_;
497     } elsif (s/^\+//) {
498     push @expected, $_;
499     } else {
500     die "Invalid diff line: $_";
501     }
502     }
503     $actual = join "\n", sort {$a cmp $b} @actual;
504     $expected = join "\n", sort {$a cmp $b} @expected;
505     ($actual, $expected);
506     } # apply_diff

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24