/[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.16 - (hide annotations) (download) (as text)
Sun Feb 3 06:00:40 2008 UTC (17 years, 5 months ago) by wakaba
Branch: MAIN
Changes since 1.15: +28 -7 lines
File MIME type: application/x-troff
++ whatpm/t/ChangeLog	3 Feb 2008 06:00:37 -0000
2008-02-03  Wakaba  <wakaba@suika.fam.cx>

	* CSS-Parser-1.t: Typo fixed.  New properties added.

	* css-font.dat, css-table.dat, css-paged.dat, css-generated.dat,
	css-visual.dat: New test data for
	newly supported properties are added.

++ whatpm/Whatpm/CSS/ChangeLog	3 Feb 2008 05:59:12 -0000
2008-02-03  Wakaba  <wakaba@suika.fam.cx>

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24