/[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.20 - (hide annotations) (download) (as text)
Mon Feb 11 00:32:10 2008 UTC (17 years, 5 months ago) by wakaba
Branch: MAIN
Changes since 1.19: +6 -0 lines
File MIME type: application/x-troff
++ whatpm/t/ChangeLog	11 Feb 2008 00:32:04 -0000
2008-02-11  Wakaba  <wakaba@suika.fam.cx>

	* css-4.dat: New test file.

	* CSS-Parser-1.t: |css-4.dat| added.  Support for '@charset'
	and '@import'.

++ whatpm/Whatpm/CSS/ChangeLog	11 Feb 2008 00:31:35 -0000
2008-02-11  Wakaba  <wakaba@suika.fam.cx>

	* Parser.pm (parse_char_string): Precious '@charset' error
	reporting.

2008-02-10  Wakaba  <wakaba@suika.fam.cx>

	* Parser.pm (parse_char_string): '@import' support.

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24