/[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.12 - (hide annotations) (download) (as text)
Sun Jan 27 10:14:52 2008 UTC (17 years, 5 months ago) by wakaba
Branch: MAIN
Changes since 1.11: +2 -1 lines
File MIME type: application/x-troff
++ whatpm/t/ChangeLog	27 Jan 2008 10:14:45 -0000
	* CSS-Parser-1.t: 'quotes' added.

	* css-generated.dat: Test data for 'quotes' are added.

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

++ whatpm/Whatpm/CSS/ChangeLog	27 Jan 2008 10:14:15 -0000
	* Parser.pm ($default_serializer): 'QUOTES' type supported.
	('quotes'): Implemented.

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24