/[suikacvs]/markup/html/whatpm/Whatpm/CSS/Parser.pm
Suika

Contents of /markup/html/whatpm/Whatpm/CSS/Parser.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.23 - (hide annotations) (download)
Fri Jan 4 14:43:44 2008 UTC (17 years, 6 months ago) by wakaba
Branch: MAIN
Changes since 1.22: +104 -3 lines
++ whatpm/Whatpm/CSS/ChangeLog	4 Jan 2008 14:43:37 -0000
	* Parser.pm (letter-spacing, word-specing, text-indent,
	outline-width): Implemented.
	(outline-style): Don't allow 'hidden'.

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

1 wakaba 1.1 package Whatpm::CSS::Parser;
2     use strict;
3     use Whatpm::CSS::Tokenizer qw(:token);
4     require Whatpm::CSS::SelectorsParser;
5    
6     sub new ($) {
7 wakaba 1.3 my $self = bless {onerror => sub { }, must_level => 'm',
8 wakaba 1.5 message_level => 'w',
9 wakaba 1.3 unsupported_level => 'unsupported'}, shift;
10 wakaba 1.11 # $self->{base_uri}
11 wakaba 1.18 # $self->{unitless_px} = 1/0
12 wakaba 1.1
13     return $self;
14     } # new
15    
16     sub BEFORE_STATEMENT_STATE () { 0 }
17     sub BEFORE_DECLARATION_STATE () { 1 }
18     sub IGNORED_STATEMENT_STATE () { 2 }
19     sub IGNORED_DECLARATION_STATE () { 3 }
20    
21 wakaba 1.5 our $Prop; ## By CSS property name
22     our $Attr; ## By CSSOM attribute name
23     our $Key; ## By internal key
24    
25 wakaba 1.1 sub parse_char_string ($$) {
26     my $self = $_[0];
27    
28     my $s = $_[1];
29     pos ($s) = 0;
30 wakaba 1.2 my $line = 1;
31     my $column = 0;
32    
33     my $_onerror = $self->{onerror};
34     my $onerror = sub {
35     $_onerror->(@_, line => $line, column => $column);
36     };
37 wakaba 1.1
38     my $tt = Whatpm::CSS::Tokenizer->new;
39 wakaba 1.2 $tt->{onerror} = $onerror;
40 wakaba 1.1 $tt->{get_char} = sub {
41     if (pos $s < length $s) {
42 wakaba 1.2 my $c = ord substr $s, pos ($s)++, 1;
43     if ($c == 0x000A) {
44     $line++;
45     $column = 0;
46     } elsif ($c == 0x000D) {
47     unless (substr ($s, pos ($s), 1) eq "\x0A") {
48     $line++;
49     $column = 0;
50     } else {
51     $column++;
52     }
53     } else {
54     $column++;
55     }
56     return $c;
57 wakaba 1.1 } else {
58     return -1;
59     }
60     }; # $tt->{get_char}
61     $tt->init;
62    
63     my $sp = Whatpm::CSS::SelectorsParser->new;
64 wakaba 1.2 $sp->{onerror} = $onerror;
65 wakaba 1.1 $sp->{must_level} = $self->{must_level};
66 wakaba 1.2 $sp->{pseudo_element} = $self->{pseudo_element};
67     $sp->{pseudo_class} = $self->{pseudo_class};
68 wakaba 1.1
69 wakaba 1.4 my $nsmap = {};
70     $sp->{lookup_namespace_uri} = sub {
71     return $nsmap->{$_[0]}; # $_[0] is '' (default namespace) or prefix
72     }; # $sp->{lookup_namespace_uri}
73 wakaba 1.1
74     ## TODO: Supported pseudo classes and elements...
75    
76     require Message::DOM::CSSStyleSheet;
77     require Message::DOM::CSSRule;
78     require Message::DOM::CSSStyleDeclaration;
79    
80 wakaba 1.11 $self->{base_uri} = $self->{href} unless defined $self->{base_uri};
81    
82 wakaba 1.1 my $state = BEFORE_STATEMENT_STATE;
83     my $t = $tt->get_next_token;
84    
85     my $open_rules = [[]];
86     my $current_rules = $open_rules->[-1];
87     my $current_decls;
88     my $closing_tokens = [];
89 wakaba 1.3 my $charset_allowed = 1;
90 wakaba 1.4 my $namespace_allowed = 1;
91 wakaba 1.1
92     S: {
93     if ($state == BEFORE_STATEMENT_STATE) {
94     $t = $tt->get_next_token
95     while $t->{type} == S_TOKEN or
96     $t->{type} == CDO_TOKEN or
97     $t->{type} == CDC_TOKEN;
98    
99     if ($t->{type} == ATKEYWORD_TOKEN) {
100 wakaba 1.5 if (lc $t->{value} eq 'namespace') { ## TODO: case folding
101 wakaba 1.4 $t = $tt->get_next_token;
102     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
103    
104     my $prefix;
105     if ($t->{type} == IDENT_TOKEN) {
106     $prefix = lc $t->{value};
107     ## TODO: Unicode lowercase
108    
109     $t = $tt->get_next_token;
110     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
111     }
112    
113     if ($t->{type} == STRING_TOKEN or $t->{type} == URI_TOKEN) {
114     my $uri = $t->{value};
115    
116     $t = $tt->get_next_token;
117     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
118    
119     ## ISSUE: On handling of empty namespace URI, Firefox 2 and
120     ## Opera 9 work differently (See SuikaWiki:namespace).
121     ## TODO: We need to check what we do once it is specced.
122    
123     if ($t->{type} == SEMICOLON_TOKEN) {
124     if ($namespace_allowed) {
125     $nsmap->{defined $prefix ? $prefix : ''} = $uri;
126     push @$current_rules,
127     Message::DOM::CSSNamespaceRule->____new ($prefix, $uri);
128     undef $charset_allowed;
129     } else {
130     $onerror->(type => 'at:namespace:not allowed',
131     level => $self->{must_level},
132     token => $t);
133     }
134    
135     $t = $tt->get_next_token;
136     ## Stay in the state.
137     redo S;
138     } else {
139     #
140     }
141     } else {
142     #
143     }
144    
145     $onerror->(type => 'syntax error:at:namespace',
146     level => $self->{must_level},
147     token => $t);
148     #
149 wakaba 1.5 } elsif (lc $t->{value} eq 'charset') { ## TODO: case folding
150 wakaba 1.3 $t = $tt->get_next_token;
151     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
152    
153     if ($t->{type} == STRING_TOKEN) {
154     my $encoding = $t->{value};
155    
156     $t = $tt->get_next_token;
157     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
158    
159     if ($t->{type} == SEMICOLON_TOKEN) {
160     if ($charset_allowed) {
161     push @$current_rules,
162     Message::DOM::CSSCharsetRule->____new ($encoding);
163     undef $charset_allowed;
164     } else {
165     $onerror->(type => 'at:charset:not allowed',
166     level => $self->{must_level},
167     token => $t);
168     }
169    
170     ## TODO: Detect the conformance errors for @charset...
171    
172     $t = $tt->get_next_token;
173     ## Stay in the state.
174     redo S;
175     } else {
176     #
177     }
178     } else {
179     #
180     }
181    
182     $onerror->(type => 'syntax error:at:charset',
183     level => $self->{must_level},
184     token => $t);
185 wakaba 1.4 #
186 wakaba 1.3 ## NOTE: When adding support for new at-rule, insert code
187 wakaba 1.4 ## "undef $charset_allowed" and "undef $namespace_token" as
188     ## appropriate.
189 wakaba 1.3 } else {
190     $onerror->(type => 'not supported:at:'.$t->{value},
191     level => $self->{unsupported_level},
192     token => $t);
193     }
194 wakaba 1.1
195     $t = $tt->get_next_token;
196     $state = IGNORED_STATEMENT_STATE;
197     redo S;
198     } elsif (@$open_rules > 1 and $t->{type} == RBRACE_TOKEN) {
199     pop @$open_rules;
200     ## Stay in the state.
201     $t = $tt->get_next_token;
202     redo S;
203     } elsif ($t->{type} == EOF_TOKEN) {
204     if (@$open_rules > 1) {
205 wakaba 1.2 $onerror->(type => 'syntax error:block not closed',
206     level => $self->{must_level},
207     token => $t);
208 wakaba 1.1 }
209    
210     last S;
211     } else {
212 wakaba 1.3 undef $charset_allowed;
213 wakaba 1.4 undef $namespace_allowed;
214 wakaba 1.3
215 wakaba 1.1 ($t, my $selectors) = $sp->_parse_selectors_with_tokenizer
216     ($tt, LBRACE_TOKEN, $t);
217    
218     $t = $tt->get_next_token
219     while $t->{type} != LBRACE_TOKEN and $t->{type} != EOF_TOKEN;
220    
221     if ($t->{type} == LBRACE_TOKEN) {
222     $current_decls = Message::DOM::CSSStyleDeclaration->____new;
223     my $rs = Message::DOM::CSSStyleRule->____new
224     ($selectors, $current_decls);
225     push @{$current_rules}, $rs if defined $selectors;
226    
227     $state = BEFORE_DECLARATION_STATE;
228     $t = $tt->get_next_token;
229     redo S;
230     } else {
231 wakaba 1.2 $onerror->(type => 'syntax error:after selectors',
232     level => $self->{must_level},
233     token => $t);
234 wakaba 1.1
235     ## Stay in the state.
236     $t = $tt->get_next_token;
237     redo S;
238     }
239     }
240     } elsif ($state == BEFORE_DECLARATION_STATE) {
241     ## NOTE: DELIM? in declaration will be removed:
242     ## <http://csswg.inkedblade.net/spec/css2.1?s=declaration%20delim#issue-2>.
243    
244 wakaba 1.5 my $prop_def;
245     my $prop_value;
246     my $prop_flag;
247 wakaba 1.1 $t = $tt->get_next_token while $t->{type} == S_TOKEN;
248     if ($t->{type} == IDENT_TOKEN) { # property
249 wakaba 1.5 my $prop_name = lc $t->{value}; ## TODO: case folding
250     $t = $tt->get_next_token;
251     if ($t->{type} == COLON_TOKEN) {
252     $t = $tt->get_next_token;
253     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
254    
255     $prop_def = $Prop->{$prop_name};
256 wakaba 1.6 if ($prop_def and $self->{prop}->{$prop_name}) {
257 wakaba 1.5 ($t, $prop_value)
258     = $prop_def->{parse}->($self, $prop_name, $tt, $t, $onerror);
259     if ($prop_value) {
260     ## NOTE: {parse} don't have to consume trailing spaces.
261     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
262    
263     if ($t->{type} == EXCLAMATION_TOKEN) {
264     $t = $tt->get_next_token;
265     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
266     if ($t->{type} == IDENT_TOKEN and
267     lc $t->{value} eq 'important') { ## TODO: case folding
268     $prop_flag = 'important';
269    
270     $t = $tt->get_next_token;
271     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
272    
273     #
274     } else {
275     $onerror->(type => 'syntax error:important',
276     level => $self->{must_level},
277     token => $t);
278    
279     ## Reprocess.
280     $state = IGNORED_DECLARATION_STATE;
281     redo S;
282     }
283     }
284    
285     #
286     } else {
287     ## Syntax error.
288    
289     ## Reprocess.
290     $state = IGNORED_DECLARATION_STATE;
291     redo S;
292     }
293     } else {
294     $onerror->(type => 'not supported:property',
295     level => $self->{unsupported_level},
296     token => $t, value => $prop_name);
297    
298     #
299     $state = IGNORED_DECLARATION_STATE;
300     redo S;
301     }
302     } else {
303     $onerror->(type => 'syntax error:property colon',
304     level => $self->{must_level},
305     token => $t);
306 wakaba 1.1
307 wakaba 1.5 #
308     $state = IGNORED_DECLARATION_STATE;
309     redo S;
310     }
311     }
312    
313     if ($t->{type} == RBRACE_TOKEN) {
314 wakaba 1.1 $t = $tt->get_next_token;
315 wakaba 1.5 $state = BEFORE_STATEMENT_STATE;
316     #redo S;
317     } elsif ($t->{type} == SEMICOLON_TOKEN) {
318 wakaba 1.1 $t = $tt->get_next_token;
319 wakaba 1.5 ## Stay in the state.
320     #redo S;
321 wakaba 1.1 } elsif ($t->{type} == EOF_TOKEN) {
322 wakaba 1.2 $onerror->(type => 'syntax error:ruleset not closed',
323     level => $self->{must_level},
324     token => $t);
325 wakaba 1.1 ## Reprocess.
326     $state = BEFORE_STATEMENT_STATE;
327 wakaba 1.5 #redo S;
328     } else {
329     if ($prop_value) {
330     $onerror->(type => 'syntax error:property semicolon',
331     level => $self->{must_level},
332     token => $t);
333     } else {
334     $onerror->(type => 'syntax error:property name',
335     level => $self->{must_level},
336     token => $t);
337     }
338    
339     #
340     $state = IGNORED_DECLARATION_STATE;
341 wakaba 1.1 redo S;
342     }
343    
344 wakaba 1.7 my $important = (defined $prop_flag and $prop_flag eq 'important');
345     for my $set_prop_name (keys %{$prop_value or {}}) {
346     my $set_prop_def = $Prop->{$set_prop_name};
347     $$current_decls->{$set_prop_def->{key}}
348     = [$prop_value->{$set_prop_name}, $prop_flag]
349     if $important or
350     not $$current_decls->{$set_prop_def->{key}} or
351     not defined $$current_decls->{$set_prop_def->{key}}->[1];
352 wakaba 1.5 }
353 wakaba 1.1 redo S;
354     } elsif ($state == IGNORED_STATEMENT_STATE or
355     $state == IGNORED_DECLARATION_STATE) {
356     if (@$closing_tokens) { ## Something is yet in opening state.
357     if ($t->{type} == EOF_TOKEN) {
358     @$closing_tokens = ();
359     ## Reprocess.
360     $state = $state == IGNORED_STATEMENT_STATE
361     ? BEFORE_STATEMENT_STATE : BEFORE_DECLARATION_STATE;
362     redo S;
363     } elsif ($t->{type} == $closing_tokens->[-1]) {
364     pop @$closing_tokens;
365     if (@$closing_tokens == 0 and
366     $t->{type} == RBRACE_TOKEN and
367     $state == IGNORED_STATEMENT_STATE) {
368     $t = $tt->get_next_token;
369     $state = BEFORE_STATEMENT_STATE;
370     redo S;
371     } else {
372     $t = $tt->get_next_token;
373     ## Stay in the state.
374     redo S;
375     }
376     } else {
377     #
378     }
379     } else {
380     if ($t->{type} == SEMICOLON_TOKEN) {
381     $t = $tt->get_next_token;
382     $state = $state == IGNORED_STATEMENT_STATE
383     ? BEFORE_STATEMENT_STATE : BEFORE_DECLARATION_STATE;
384     redo S;
385     } elsif ($state == IGNORED_DECLARATION_STATE and
386     $t->{type} == RBRACE_TOKEN) {
387     $t = $tt->get_next_token;
388     $state = BEFORE_STATEMENT_STATE;
389     redo S;
390     } elsif ($t->{type} == EOF_TOKEN) {
391     ## Reprocess.
392     $state = $state == IGNORED_STATEMENT_STATE
393     ? BEFORE_STATEMENT_STATE : BEFORE_DECLARATION_STATE;
394     redo S;
395     } else {
396     #
397     }
398     }
399    
400     while (not {
401     EOF_TOKEN, 1,
402     RBRACE_TOKEN, 1,
403     RBRACKET_TOKEN, 1,
404     RPAREN_TOKEN, 1,
405     SEMICOLON_TOKEN, 1,
406     }->{$t->{type}}) {
407     if ($t->{type} == LBRACE_TOKEN) {
408     push @$closing_tokens, RBRACE_TOKEN;
409     } elsif ($t->{type} == LBRACKET_TOKEN) {
410     push @$closing_tokens, RBRACKET_TOKEN;
411     } elsif ($t->{type} == LPAREN_TOKEN or $t->{type} == FUNCTION_TOKEN) {
412     push @$closing_tokens, RPAREN_TOKEN;
413     }
414    
415     $t = $tt->get_next_token;
416     }
417    
418     #
419     ## Stay in the state.
420     redo S;
421     } else {
422     die "$0: parse_char_string: Unknown state: $state";
423     }
424     } # S
425    
426     my $ss = Message::DOM::CSSStyleSheet->____new
427 wakaba 1.11 (manakai_base_uri => $self->{base_uri},
428     css_rules => $open_rules->[0],
429 wakaba 1.1 ## TODO: href
430     ## TODO: owner_node
431     ## TODO: media
432     type => 'text/css', ## TODO: OK?
433     _parser => $self);
434     return $ss;
435     } # parse_char_string
436    
437 wakaba 1.9 my $compute_as_specified = sub ($$$$) {
438     #my ($self, $element, $prop_name, $specified_value) = @_;
439     return $_[3];
440     }; # $compute_as_specified
441    
442 wakaba 1.11 my $default_serializer = sub {
443     my ($self, $prop_name, $value) = @_;
444 wakaba 1.15 if ($value->[0] eq 'NUMBER' or $value->[0] eq 'WEIGHT') {
445     ## TODO: What we currently do for 'font-weight' is different from
446     ## any browser for lighter/bolder cases. We need to fix this, but
447     ## how?
448 wakaba 1.11 return $value->[1]; ## TODO: big or small number cases?
449 wakaba 1.18 } elsif ($value->[0] eq 'DIMENSION') {
450     return $value->[1] . $value->[2]; ## NOTE: This is what browsers do.
451 wakaba 1.22 } elsif ($value->[0] eq 'PERCENTAGE') {
452     return $value->[1] . '%';
453 wakaba 1.11 } elsif ($value->[0] eq 'KEYWORD') {
454     return $value->[1];
455     } elsif ($value->[0] eq 'URI') {
456     ## NOTE: This is what browsers do.
457     return 'url('.$value->[1].')';
458     } elsif ($value->[0] eq 'INHERIT') {
459     return 'inherit';
460 wakaba 1.16 } elsif ($value->[0] eq 'DECORATION') {
461     my @v = ();
462     push @v, 'underline' if $value->[1];
463     push @v, 'overline' if $value->[2];
464     push @v, 'line-through' if $value->[3];
465     push @v, 'blink' if $value->[4];
466     return 'none' unless @v;
467     return join ' ', @v;
468 wakaba 1.11 } else {
469     return undef;
470     }
471     }; # $default_serializer
472    
473 wakaba 1.5 $Prop->{color} = {
474     css => 'color',
475     dom => 'color',
476     key => 'color',
477     parse => sub {
478     my ($self, $prop_name, $tt, $t, $onerror) = @_;
479    
480     if ($t->{type} == IDENT_TOKEN) {
481     if (lc $t->{value} eq 'blue') { ## TODO: case folding
482     $t = $tt->get_next_token;
483 wakaba 1.7 return ($t, {$prop_name => ["RGBA", 0, 0, 255, 1]});
484 wakaba 1.5 } else {
485     #
486     }
487     } else {
488     #
489     }
490    
491     $onerror->(type => 'syntax error:color',
492     level => $self->{must_level},
493     token => $t);
494    
495     return ($t, undef);
496     },
497     serialize => sub {
498     my ($self, $prop_name, $value) = @_;
499     if ($value->[0] eq 'RGBA') { ## TODO: %d? %f?
500     return sprintf 'rgba(%d, %d, %d, %f)', @$value[1, 2, 3, 4];
501     } else {
502     return undef;
503     }
504     },
505 wakaba 1.9 initial => ["KEYWORD", "-manakai-initial-color"], ## NOTE: UA-dependent in CSS 2.1.
506     inherited => 1,
507     compute => $compute_as_specified,
508 wakaba 1.5 };
509     $Attr->{color} = $Prop->{color};
510     $Key->{color} = $Prop->{color};
511    
512 wakaba 1.6 my $one_keyword_parser = sub {
513     my ($self, $prop_name, $tt, $t, $onerror) = @_;
514    
515     if ($t->{type} == IDENT_TOKEN) {
516     my $prop_value = lc $t->{value}; ## TODO: case folding
517     $t = $tt->get_next_token;
518     if ($Prop->{$prop_name}->{keyword}->{$prop_value} and
519     $self->{prop_value}->{$prop_name}->{$prop_value}) {
520 wakaba 1.7 return ($t, {$prop_name => ["KEYWORD", $prop_value]});
521 wakaba 1.6 } elsif ($prop_value eq 'inherit') {
522 wakaba 1.10 return ($t, {$prop_name => ['INHERIT']});
523 wakaba 1.6 }
524     }
525    
526 wakaba 1.7 $onerror->(type => 'syntax error:keyword:'.$prop_name,
527 wakaba 1.6 level => $self->{must_level},
528     token => $t);
529     return ($t, undef);
530     };
531    
532     $Prop->{display} = {
533     css => 'display',
534     dom => 'display',
535     key => 'display',
536     parse => $one_keyword_parser,
537 wakaba 1.11 serialize => $default_serializer,
538 wakaba 1.6 keyword => {
539     block => 1, inline => 1, 'inline-block' => 1, 'inline-table' => 1,
540     'list-item' => 1, none => 1,
541     table => 1, 'table-caption' => 1, 'table-cell' => 1, 'table-column' => 1,
542     'table-column-group' => 1, 'table-header-group' => 1,
543     'table-footer-group' => 1, 'table-row' => 1, 'table-row-group' => 1,
544     },
545 wakaba 1.9 initial => ["KEYWORD", "inline"],
546     #inherited => 0,
547     compute => sub {
548     my ($self, $element, $prop_name, $specified_value) = @_;
549     ## NOTE: CSS 2.1 Section 9.7.
550    
551     ## WARNING: |compute| for 'float' property invoke this CODE
552     ## in some case. Careless modification might cause a infinite loop.
553    
554 wakaba 1.17 if (defined $specified_value and $specified_value->[0] eq 'KEYWORD') {
555 wakaba 1.9 if ($specified_value->[1] eq 'none') {
556     ## Case 1 [CSS 2.1]
557     return $specified_value;
558     } else {
559     my $position = $self->get_computed_value ($element, 'position');
560     if ($position->[0] eq 'KEYWORD' and
561     ($position->[1] eq 'absolute' or
562     $position->[1] eq 'fixed')) {
563     ## Case 2 [CSS 2.1]
564     #
565     } else {
566     my $float = $self->get_computed_value ($element, 'float');
567     if ($float->[0] eq 'KEYWORD' and $float->[1] ne 'none') {
568     ## Caes 3 [CSS 2.1]
569     #
570     } elsif (not defined $element->manakai_parent_element) {
571     ## Case 4 [CSS 2.1]
572     #
573     } else {
574     ## Case 5 [CSS 2.1]
575     return $specified_value;
576     }
577     }
578    
579     return ["KEYWORD",
580     {
581     'inline-table' => 'table',
582     inline => 'block',
583     'run-in' => 'block',
584     'table-row-group' => 'block',
585     'table-column' => 'block',
586     'table-column-group' => 'block',
587     'table-header-group' => 'block',
588     'table-footer-group' => 'block',
589     'table-row' => 'block',
590     'table-cell' => 'block',
591     'table-caption' => 'block',
592     'inline-block' => 'block',
593     }->{$specified_value->[1]} || $specified_value->[1]];
594     }
595     } else {
596     return $specified_value; ## Maybe an error of the implementation.
597     }
598     },
599 wakaba 1.6 };
600     $Attr->{display} = $Prop->{display};
601     $Key->{display} = $Prop->{display};
602    
603     $Prop->{position} = {
604     css => 'position',
605     dom => 'position',
606     key => 'position',
607     parse => $one_keyword_parser,
608 wakaba 1.11 serialize => $default_serializer,
609 wakaba 1.6 keyword => {
610     static => 1, relative => 1, absolute => 1, fixed => 1,
611     },
612 wakaba 1.10 initial => ["KEYWORD", "static"],
613 wakaba 1.9 #inherited => 0,
614     compute => $compute_as_specified,
615 wakaba 1.6 };
616     $Attr->{position} = $Prop->{position};
617     $Key->{position} = $Prop->{position};
618    
619     $Prop->{float} = {
620     css => 'float',
621     dom => 'css_float',
622     key => 'float',
623     parse => $one_keyword_parser,
624 wakaba 1.11 serialize => $default_serializer,
625 wakaba 1.6 keyword => {
626     left => 1, right => 1, none => 1,
627     },
628 wakaba 1.9 initial => ["KEYWORD", "none"],
629     #inherited => 0,
630     compute => sub {
631     my ($self, $element, $prop_name, $specified_value) = @_;
632     ## NOTE: CSS 2.1 Section 9.7.
633    
634     ## WARNING: |compute| for 'display' property invoke this CODE
635     ## in some case. Careless modification might cause a infinite loop.
636    
637 wakaba 1.17 if (defined $specified_value and $specified_value->[0] eq 'KEYWORD') {
638 wakaba 1.9 if ($specified_value->[1] eq 'none') {
639     ## Case 1 [CSS 2.1]
640     return $specified_value;
641     } else {
642     my $position = $self->get_computed_value ($element, 'position');
643     if ($position->[0] eq 'KEYWORD' and
644     ($position->[1] eq 'absolute' or
645     $position->[1] eq 'fixed')) {
646     ## Case 2 [CSS 2.1]
647     return ["KEYWORD", "none"];
648     }
649     }
650     }
651    
652     ## ISSUE: CSS 2.1 section 9.7 and 9.5.1 ('float' definition) disagree
653     ## on computed value of 'float' property.
654    
655     ## Case 3, 4, and 5 [CSS 2.1]
656     return $specified_value;
657     },
658 wakaba 1.6 };
659     $Attr->{css_float} = $Prop->{float};
660     $Attr->{style_float} = $Prop->{float}; ## NOTE: IEism
661     $Key->{float} = $Prop->{float};
662    
663     $Prop->{clear} = {
664     css => 'clear',
665     dom => 'clear',
666     key => 'clear',
667     parse => $one_keyword_parser,
668 wakaba 1.11 serialize => $default_serializer,
669 wakaba 1.6 keyword => {
670     left => 1, right => 1, none => 1, both => 1,
671     },
672 wakaba 1.9 initial => ["KEYWORD", "none"],
673     #inherited => 0,
674     compute => $compute_as_specified,
675 wakaba 1.6 };
676     $Attr->{clear} = $Prop->{clear};
677     $Key->{clear} = $Prop->{clear};
678    
679     $Prop->{direction} = {
680     css => 'direction',
681     dom => 'direction',
682     key => 'direction',
683     parse => $one_keyword_parser,
684 wakaba 1.11 serialize => $default_serializer,
685 wakaba 1.6 keyword => {
686     ltr => 1, rtl => 1,
687     },
688 wakaba 1.9 initial => ["KEYWORD", "ltr"],
689     inherited => 1,
690     compute => $compute_as_specified,
691 wakaba 1.6 };
692     $Attr->{direction} = $Prop->{direction};
693     $Key->{direction} = $Prop->{direction};
694    
695     $Prop->{'unicode-bidi'} = {
696     css => 'unicode-bidi',
697     dom => 'unicode_bidi',
698     key => 'unicode_bidi',
699     parse => $one_keyword_parser,
700 wakaba 1.11 serialize => $default_serializer,
701 wakaba 1.6 keyword => {
702     normal => 1, embed => 1, 'bidi-override' => 1,
703     },
704 wakaba 1.9 initial => ["KEYWORD", "normal"],
705     #inherited => 0,
706     compute => $compute_as_specified,
707 wakaba 1.6 };
708     $Attr->{unicode_bidi} = $Prop->{'unicode-bidi'};
709     $Key->{unicode_bidi} = $Prop->{'unicode-bidi'};
710    
711 wakaba 1.11 $Prop->{overflow} = {
712     css => 'overflow',
713     dom => 'overflow',
714     key => 'overflow',
715     parse => $one_keyword_parser,
716     serialize => $default_serializer,
717     keyword => {
718     visible => 1, hidden => 1, scroll => 1, auto => 1,
719     },
720     initial => ["KEYWORD", "visible"],
721     #inherited => 0,
722     compute => $compute_as_specified,
723     };
724     $Attr->{overflow} = $Prop->{overflow};
725     $Key->{overflow} = $Prop->{overflow};
726    
727     $Prop->{visibility} = {
728     css => 'visibility',
729     dom => 'visibility',
730     key => 'visibility',
731     parse => $one_keyword_parser,
732     serialize => $default_serializer,
733     keyword => {
734     visible => 1, hidden => 1, collapse => 1,
735     },
736     initial => ["KEYWORD", "visible"],
737     #inherited => 0,
738     compute => $compute_as_specified,
739     };
740     $Attr->{visibility} = $Prop->{visibility};
741     $Key->{visibility} = $Prop->{visibility};
742    
743     $Prop->{'list-style-type'} = {
744     css => 'list-style-type',
745     dom => 'list_style_type',
746     key => 'list_style_type',
747     parse => $one_keyword_parser,
748     serialize => $default_serializer,
749     keyword => {
750     qw/
751     disc 1 circle 1 square 1 decimal 1 decimal-leading-zero 1
752     lower-roman 1 upper-roman 1 lower-greek 1 lower-latin 1
753     upper-latin 1 armenian 1 georgian 1 lower-alpha 1 upper-alpha 1
754     none 1
755     /,
756     },
757     initial => ["KEYWORD", 'disc'],
758     inherited => 1,
759     compute => $compute_as_specified,
760     };
761     $Attr->{list_style_type} = $Prop->{'list-style-type'};
762     $Key->{list_style_type} = $Prop->{'list-style-type'};
763    
764     $Prop->{'list-style-position'} = {
765     css => 'list-style-position',
766     dom => 'list_style_position',
767     key => 'list_style_position',
768     parse => $one_keyword_parser,
769     serialize => $default_serializer,
770     keyword => {
771     inside => 1, outside => 1,
772     },
773     initial => ["KEYWORD", 'outside'],
774     inherited => 1,
775     compute => $compute_as_specified,
776     };
777     $Attr->{list_style_position} = $Prop->{'list-style-position'};
778     $Key->{list_style_position} = $Prop->{'list-style-position'};
779    
780 wakaba 1.12 $Prop->{'page-break-before'} = {
781     css => 'page-break-before',
782     dom => 'page_break_before',
783     key => 'page_break_before',
784     parse => $one_keyword_parser,
785     serialize => $default_serializer,
786     keyword => {
787     auto => 1, always => 1, avoid => 1, left => 1, right => 1,
788     },
789     initial => ["KEYWORD", 'auto'],
790 wakaba 1.15 #inherited => 0,
791 wakaba 1.12 compute => $compute_as_specified,
792     };
793     $Attr->{page_break_before} = $Prop->{'page-break-before'};
794     $Key->{page_break_before} = $Prop->{'page-break-before'};
795    
796     $Prop->{'page-break-after'} = {
797     css => 'page-break-after',
798     dom => 'page_break_after',
799     key => 'page_break_after',
800     parse => $one_keyword_parser,
801     serialize => $default_serializer,
802     keyword => {
803     auto => 1, always => 1, avoid => 1, left => 1, right => 1,
804     },
805     initial => ["KEYWORD", 'auto'],
806 wakaba 1.15 #inherited => 0,
807 wakaba 1.12 compute => $compute_as_specified,
808     };
809     $Attr->{page_break_after} = $Prop->{'page-break-after'};
810     $Key->{page_break_after} = $Prop->{'page-break-after'};
811    
812     $Prop->{'page-break-inside'} = {
813     css => 'page-break-inside',
814     dom => 'page_break_inside',
815     key => 'page_break_inside',
816     parse => $one_keyword_parser,
817     serialize => $default_serializer,
818     keyword => {
819     auto => 1, avoid => 1,
820     },
821     initial => ["KEYWORD", 'auto'],
822     inherited => 1,
823     compute => $compute_as_specified,
824     };
825     $Attr->{page_break_inside} = $Prop->{'page-break-inside'};
826     $Key->{page_break_inside} = $Prop->{'page-break-inside'};
827    
828 wakaba 1.15 $Prop->{'background-repeat'} = {
829     css => 'background-repeat',
830     dom => 'background_repeat',
831     key => 'background_repeat',
832     parse => $one_keyword_parser,
833     serialize => $default_serializer,
834     keyword => {
835     repeat => 1, 'repeat-x' => 1, 'repeat-y' => 1, 'no-repeat' => 1,
836     },
837     initial => ["KEYWORD", 'repeat'],
838     #inherited => 0,
839     compute => $compute_as_specified,
840     };
841     $Attr->{background_repeat} = $Prop->{'background-repeat'};
842     $Key->{backgroud_repeat} = $Prop->{'background-repeat'};
843    
844     $Prop->{'background-attachment'} = {
845     css => 'background-attachment',
846     dom => 'background_attachment',
847     key => 'background_attachment',
848     parse => $one_keyword_parser,
849     serialize => $default_serializer,
850     keyword => {
851     scroll => 1, fixed => 1,
852     },
853     initial => ["KEYWORD", 'scroll'],
854     #inherited => 0,
855     compute => $compute_as_specified,
856     };
857     $Attr->{background_attachment} = $Prop->{'background-attachment'};
858     $Key->{backgroud_attachment} = $Prop->{'background-attachment'};
859    
860     $Prop->{'font-style'} = {
861     css => 'font-style',
862 wakaba 1.18 dom => 'font_style',
863     key => 'font_style',
864 wakaba 1.15 parse => $one_keyword_parser,
865     serialize => $default_serializer,
866     keyword => {
867     normal => 1, italic => 1, oblique => 1,
868     },
869     initial => ["KEYWORD", 'normal'],
870     inherited => 1,
871     compute => $compute_as_specified,
872     };
873     $Attr->{font_style} = $Prop->{'font-style'};
874     $Key->{font_style} = $Prop->{'font-style'};
875    
876     $Prop->{'font-variant'} = {
877     css => 'font-variant',
878     dom => 'font_variant',
879     key => 'font_variant',
880     parse => $one_keyword_parser,
881     serialize => $default_serializer,
882     keyword => {
883     normal => 1, 'small-caps' => 1,
884     },
885     initial => ["KEYWORD", 'normal'],
886     inherited => 1,
887     compute => $compute_as_specified,
888     };
889     $Attr->{font_variant} = $Prop->{'font-variant'};
890     $Key->{font_variant} = $Prop->{'font-variant'};
891    
892 wakaba 1.16 $Prop->{'text-align'} = {
893     css => 'text-align',
894     dom => 'text_align',
895     key => 'text_align',
896     parse => $one_keyword_parser,
897     serialize => $default_serializer,
898     keyword => {
899     left => 1, right => 1, center => 1, justify => 1, ## CSS 2
900     begin => 1, end => 1, ## CSS 3
901     },
902     initial => ["KEYWORD", 'begin'],
903     inherited => 1,
904     compute => $compute_as_specified,
905     };
906     $Attr->{text_align} = $Prop->{'text-align'};
907     $Key->{text_align} = $Prop->{'text-align'};
908    
909     $Prop->{'text-transform'} = {
910     css => 'text-transform',
911     dom => 'text_transform',
912     key => 'text_transform',
913     parse => $one_keyword_parser,
914     serialize => $default_serializer,
915     keyword => {
916     capitalize => 1, uppercase => 1, lowercase => 1, none => 1,
917     },
918     initial => ["KEYWORD", 'none'],
919     inherited => 1,
920     compute => $compute_as_specified,
921     };
922     $Attr->{text_transform} = $Prop->{'text-transform'};
923     $Key->{text_transform} = $Prop->{'text-transform'};
924    
925     $Prop->{'white-space'} = {
926     css => 'white-space',
927     dom => 'white_space',
928     key => 'white_space',
929     parse => $one_keyword_parser,
930     serialize => $default_serializer,
931     keyword => {
932     normal => 1, pre => 1, nowrap => 1, 'pre-wrap' => 1, 'pre-line' => 1,
933     },
934     initial => ["KEYWORD", 'normal'],
935     inherited => 1,
936     compute => $compute_as_specified,
937     };
938     $Attr->{white_space} = $Prop->{'white-space'};
939     $Key->{white_space} = $Prop->{'white-space'};
940    
941     $Prop->{'caption-side'} = {
942     css => 'caption-side',
943     dom => 'caption_side',
944     key => 'caption_side',
945     parse => $one_keyword_parser,
946     serialize => $default_serializer,
947     keyword => {
948     top => 1, bottom => 1,
949     },
950     initial => ['KEYWORD', 'top'],
951     inherited => 1,
952     compute => $compute_as_specified,
953     };
954     $Attr->{caption_side} = $Prop->{'caption-side'};
955     $Key->{caption_side} = $Prop->{'caption-side'};
956    
957     $Prop->{'table-layout'} = {
958     css => 'table-layout',
959     dom => 'table_layout',
960     key => 'table_layout',
961     parse => $one_keyword_parser,
962     serialize => $default_serializer,
963     keyword => {
964     auto => 1, fixed => 1,
965     },
966     initial => ['KEYWORD', 'auto'],
967     #inherited => 0,
968     compute => $compute_as_specified,
969     };
970     $Attr->{table_layout} = $Prop->{'table-layout'};
971     $Key->{table_layout} = $Prop->{'table-layout'};
972    
973     $Prop->{'border-collapse'} = {
974     css => 'border-collapse',
975     dom => 'border_collapse',
976     key => 'border_collapse',
977     parse => $one_keyword_parser,
978     serialize => $default_serializer,
979     keyword => {
980     collapse => 1, separate => 1,
981     },
982     initial => ['KEYWORD', 'separate'],
983     inherited => 1,
984     compute => $compute_as_specified,
985     };
986     $Attr->{border_collapse} = $Prop->{'border-collapse'};
987     $Key->{border_collapse} = $Prop->{'border-collapse'};
988    
989     $Prop->{'empty-cells'} = {
990     css => 'empty-cells',
991     dom => 'empty_cells',
992     key => 'empty_cells',
993     parse => $one_keyword_parser,
994     serialize => $default_serializer,
995     keyword => {
996     show => 1, hide => 1,
997     },
998     initial => ['KEYWORD', 'show'],
999     inherited => 1,
1000     compute => $compute_as_specified,
1001     };
1002     $Attr->{empty_cells} = $Prop->{'empty-cells'};
1003     $Key->{empty_cells} = $Prop->{'empty-cells'};
1004    
1005 wakaba 1.11 $Prop->{'z-index'} = {
1006     css => 'z-index',
1007     dom => 'z_index',
1008     key => 'z_index',
1009     parse => sub {
1010     my ($self, $prop_name, $tt, $t, $onerror) = @_;
1011    
1012 wakaba 1.12 my $sign = 1;
1013     if ($t->{type} == MINUS_TOKEN) {
1014     $sign = -1;
1015     $t = $tt->get_next_token;
1016     }
1017    
1018 wakaba 1.11 if ($t->{type} == NUMBER_TOKEN) {
1019     ## ISSUE: See <http://suika.fam.cx/gate/2005/sw/z-index> for
1020     ## browser compatibility issue.
1021     my $value = $t->{number};
1022     $t = $tt->get_next_token;
1023 wakaba 1.12 return ($t, {$prop_name => ["NUMBER", $sign * int ($value / 1)]});
1024     } elsif ($sign > 0 and $t->{type} == IDENT_TOKEN) {
1025 wakaba 1.11 my $value = lc $t->{value}; ## TODO: case
1026     $t = $tt->get_next_token;
1027     if ($value eq 'auto') {
1028     ## NOTE: |z-index| is the default value and therefore it must be
1029     ## supported anyway.
1030     return ($t, {$prop_name => ["KEYWORD", 'auto']});
1031     } elsif ($value eq 'inherit') {
1032     return ($t, {$prop_name => ['INHERIT']});
1033     }
1034     }
1035    
1036     $onerror->(type => 'syntax error:'.$prop_name,
1037     level => $self->{must_level},
1038     token => $t);
1039     return ($t, undef);
1040     },
1041     serialize => $default_serializer,
1042     initial => ['KEYWORD', 'auto'],
1043     #inherited => 0,
1044     compute => $compute_as_specified,
1045     };
1046     $Attr->{z_index} = $Prop->{'z-index'};
1047     $Key->{z_index} = $Prop->{'z-index'};
1048    
1049 wakaba 1.12 $Prop->{orphans} = {
1050     css => 'orphans',
1051     dom => 'orphans',
1052     key => 'orphans',
1053     parse => sub {
1054     my ($self, $prop_name, $tt, $t, $onerror) = @_;
1055    
1056     my $sign = 1;
1057     if ($t->{type} == MINUS_TOKEN) {
1058     $t = $tt->get_next_token;
1059     $sign = -1;
1060     }
1061    
1062     if ($t->{type} == NUMBER_TOKEN) {
1063     ## ISSUE: See <http://suika.fam.cx/gate/2005/sw/orphans> and
1064     ## <http://suika.fam.cx/gate/2005/sw/widows> for
1065     ## browser compatibility issue.
1066     my $value = $t->{number};
1067     $t = $tt->get_next_token;
1068     return ($t, {$prop_name => ["NUMBER", $sign * int ($value / 1)]});
1069     } elsif ($sign > 0 and $t->{type} == IDENT_TOKEN) {
1070     my $value = lc $t->{value}; ## TODO: case
1071     $t = $tt->get_next_token;
1072     if ($value eq 'inherit') {
1073     return ($t, {$prop_name => ['INHERIT']});
1074     }
1075     }
1076    
1077     $onerror->(type => 'syntax error:'.$prop_name,
1078     level => $self->{must_level},
1079     token => $t);
1080     return ($t, undef);
1081     },
1082     serialize => $default_serializer,
1083     initial => ['NUMBER', 2],
1084     inherited => 1,
1085     compute => $compute_as_specified,
1086     };
1087     $Attr->{orphans} = $Prop->{orphans};
1088     $Key->{orphans} = $Prop->{orphans};
1089    
1090     $Prop->{widows} = {
1091     css => 'widows',
1092     dom => 'widows',
1093     key => 'widows',
1094     parse => $Prop->{orphans}->{parse},
1095     serialize => $default_serializer,
1096     initial => ['NUMBER', 2],
1097     inherited => 1,
1098     compute => $compute_as_specified,
1099     };
1100     $Attr->{widows} = $Prop->{widows};
1101     $Key->{widows} = $Prop->{widows};
1102    
1103 wakaba 1.19 my $length_unit = {
1104     em => 1, ex => 1, px => 1,
1105     in => 1, cm => 1, mm => 1, pt => 1, pc => 1,
1106     };
1107    
1108 wakaba 1.18 $Prop->{'font-size'} = {
1109     css => 'font-size',
1110     dom => 'font_size',
1111     key => 'font_size',
1112     parse => sub {
1113     my ($self, $prop_name, $tt, $t, $onerror) = @_;
1114    
1115     my $sign = 1;
1116     if ($t->{type} == MINUS_TOKEN) {
1117     $t = $tt->get_next_token;
1118     $sign = -1;
1119     }
1120    
1121     if ($t->{type} == DIMENSION_TOKEN) {
1122     my $value = $t->{number} * $sign;
1123     my $unit = lc $t->{value}; ## TODO: case
1124     $t = $tt->get_next_token;
1125 wakaba 1.19 if ($length_unit->{$unit} and $value >= 0) {
1126 wakaba 1.18 return ($t, {$prop_name => ['DIMENSION', $value, $unit]});
1127     }
1128     } elsif ($t->{type} == PERCENTAGE_TOKEN) {
1129     my $value = $t->{number} * $sign;
1130     $t = $tt->get_next_token;
1131     return ($t, {$prop_name => ['PERCENTAGE', $value]}) if $value >= 0;
1132 wakaba 1.20 } elsif ($t->{type} == NUMBER_TOKEN and
1133 wakaba 1.19 ($self->{unitless_px} or $t->{number} == 0)) {
1134 wakaba 1.18 my $value = $t->{number} * $sign;
1135     $t = $tt->get_next_token;
1136     return ($t, {$prop_name => ['DIMENSION', $value, 'px']}) if $value >= 0;
1137     } elsif ($sign > 0 and $t->{type} == IDENT_TOKEN) {
1138     my $value = lc $t->{value}; ## TODO: case
1139     $t = $tt->get_next_token;
1140     if ({
1141     'xx-small' => 1, 'x-small' => 1, small => 1, medium => 1,
1142     large => 1, 'x-large' => 1, 'xx-large' => 1,
1143     '-manakai-xxx-large' => 1, # -webkit-xxx-large
1144     larger => 1, smaller => 1,
1145     }->{$value}) {
1146     return ($t, {$prop_name => ['KEYWORD', $value]});
1147     } elsif ($value eq 'inherit') {
1148     return ($t, {$prop_name => ['INHERIT']});
1149     }
1150     }
1151    
1152     $onerror->(type => 'syntax error:'.$prop_name,
1153     level => $self->{must_level},
1154     token => $t);
1155     return ($t, undef);
1156     },
1157     serialize => $default_serializer,
1158     initial => ['KEYWORD', 'medium'],
1159     inherited => 1,
1160     compute => sub {
1161     my ($self, $element, $prop_name, $specified_value) = @_;
1162    
1163     if (defined $specified_value) {
1164     if ($specified_value->[0] eq 'DIMENSION') {
1165     my $unit = $specified_value->[2];
1166     my $value = $specified_value->[1];
1167    
1168     if ($unit eq 'em' or $unit eq 'ex') {
1169     $value *= 0.5 if $unit eq 'ex';
1170     ## TODO: Preferred way to determine the |ex| size is defined
1171     ## in CSS 2.1.
1172    
1173     my $parent_element = $element->manakai_parent_element;
1174     if (defined $parent_element) {
1175     $value *= $self->get_computed_value ($parent_element, $prop_name)
1176     ->[1];
1177     } else {
1178     $value *= $self->{font_size}->[3]; # medium
1179     }
1180     $unit = 'px';
1181     } elsif ({in => 1, cm => 1, mm => 1, pt => 1, pc => 1}->{$unit}) {
1182     ($value *= 12, $unit = 'pc') if $unit eq 'pc';
1183     ($value /= 72, $unit = 'in') if $unit eq 'pt';
1184     ($value *= 2.54, $unit = 'cm') if $unit eq 'in';
1185     ($value *= 10, $unit = 'mm') if $unit eq 'cm';
1186     ($value /= 0.26, $unit = 'px') if $unit eq 'mm';
1187     }
1188 wakaba 1.19 ## else: consistency error
1189 wakaba 1.18
1190     return ['DIMENSION', $value, $unit];
1191     } elsif ($specified_value->[0] eq 'PERCENTAGE') {
1192     my $parent_element = $element->manakai_parent_element;
1193     my $parent_cv;
1194     if (defined $parent_element) {
1195     $parent_cv = $self->get_computed_value
1196     ($parent_element, $prop_name);
1197     } else {
1198     $parent_cv = [undef, $self->{font_size}->[3]];
1199     }
1200     return ['DIMENSION', $parent_cv->[1] * $specified_value->[1] / 100,
1201     'px'];
1202     } elsif ($specified_value->[0] eq 'KEYWORD') {
1203     if ($specified_value->[1] eq 'larger') {
1204     my $parent_element = $element->manakai_parent_element;
1205     if (defined $parent_element) {
1206     my $parent_cv = $self->get_computed_value
1207     ($parent_element, $prop_name);
1208     return ['DIMENSION',
1209     $self->{get_larger_font_size}->($self, $parent_cv->[1]),
1210     'px'];
1211     } else { ## 'larger' relative to 'medium', initial of 'font-size'
1212     return ['DIMENSION', $self->{font_size}->[4], 'px'];
1213     }
1214     } elsif ($specified_value->[1] eq 'smaller') {
1215     my $parent_element = $element->manakai_parent_element;
1216     if (defined $parent_element) {
1217     my $parent_cv = $self->get_computed_value
1218     ($parent_element, $prop_name);
1219     return ['DIMENSION',
1220     $self->{get_smaller_font_size}->($self, $parent_cv->[1]),
1221     'px'];
1222     } else { ## 'smaller' relative to 'medium', initial of 'font-size'
1223     return ['DIMENSION', $self->{font_size}->[2], 'px'];
1224     }
1225     } else {
1226     return ['DIMENSION', $self->{font_size}->[{
1227     'xx-small' => 0,
1228     'x-small' => 1,
1229     small => 2,
1230     medium => 3,
1231     large => 4,
1232     'x-large' => 5,
1233     'xx-large' => 6,
1234     '-manakai-xxx-large' => 7,
1235     }->{$specified_value->[1]}], 'px'];
1236     }
1237     }
1238     }
1239    
1240     return $specified_value;
1241     },
1242     };
1243     $Attr->{font_size} = $Prop->{'font-size'};
1244     $Key->{font_size} = $Prop->{'font-size'};
1245    
1246 wakaba 1.19 my $compute_length = sub {
1247     my ($self, $element, $prop_name, $specified_value) = @_;
1248    
1249     if (defined $specified_value) {
1250     if ($specified_value->[0] eq 'DIMENSION') {
1251     my $unit = $specified_value->[2];
1252     my $value = $specified_value->[1];
1253    
1254     if ($unit eq 'em' or $unit eq 'ex') {
1255     $value *= 0.5 if $unit eq 'ex';
1256     ## TODO: Preferred way to determine the |ex| size is defined
1257     ## in CSS 2.1.
1258    
1259     $value *= $self->get_computed_value ($element, 'font-size')->[1];
1260     $unit = 'px';
1261     } elsif ({in => 1, cm => 1, mm => 1, pt => 1, pc => 1}->{$unit}) {
1262     ($value *= 12, $unit = 'pc') if $unit eq 'pc';
1263     ($value /= 72, $unit = 'in') if $unit eq 'pt';
1264     ($value *= 2.54, $unit = 'cm') if $unit eq 'in';
1265     ($value *= 10, $unit = 'mm') if $unit eq 'cm';
1266     ($value /= 0.26, $unit = 'px') if $unit eq 'mm';
1267     }
1268    
1269     return ['DIMENSION', $value, $unit];
1270     }
1271     }
1272    
1273     return $specified_value;
1274     }; # $compute_length
1275    
1276 wakaba 1.23 $Prop->{'letter-spacing'} = {
1277     css => 'letter-spacing',
1278     dom => 'letter_spacing',
1279     key => 'letter_spacing',
1280     parse => sub {
1281     my ($self, $prop_name, $tt, $t, $onerror) = @_;
1282    
1283     ## NOTE: Used also for 'word-spacing'.
1284    
1285     my $sign = 1;
1286     if ($t->{type} == MINUS_TOKEN) {
1287     $t = $tt->get_next_token;
1288     $sign = -1;
1289     }
1290     my $allow_negative = $Prop->{$prop_name}->{allow_negative};
1291    
1292     if ($t->{type} == DIMENSION_TOKEN) {
1293     my $value = $t->{number} * $sign;
1294     my $unit = lc $t->{value}; ## TODO: case
1295     $t = $tt->get_next_token;
1296     if ($length_unit->{$unit}) {
1297     return ($t, {$prop_name => ['DIMENSION', $value, $unit]});
1298     }
1299     } elsif ($t->{type} == NUMBER_TOKEN and
1300     ($self->{unitless_px} or $t->{number} == 0)) {
1301     my $value = $t->{number} * $sign;
1302     $t = $tt->get_next_token;
1303     return ($t, {$prop_name => ['DIMENSION', $value, 'px']});
1304     } elsif ($sign > 0 and $t->{type} == IDENT_TOKEN) {
1305     my $value = lc $t->{value}; ## TODO: case
1306     $t = $tt->get_next_token;
1307     if ($value eq 'normal') {
1308     return ($t, {$prop_name => ['KEYWORD', $value]});
1309     } elsif ($value eq 'inherit') {
1310     return ($t, {$prop_name => ['INHERIT']});
1311     }
1312     }
1313    
1314     $onerror->(type => 'syntax error:'.$prop_name,
1315     level => $self->{must_level},
1316     token => $t);
1317     return ($t, undef);
1318     },
1319     serialize => $default_serializer,
1320     initial => ['KEYWORD', 'normal'],
1321     inherited => 1,
1322     compute => $compute_length,
1323     };
1324     $Attr->{letter_spacing} = $Prop->{'letter-spacing'};
1325     $Key->{letter_spacing} = $Prop->{'letter-spacing'};
1326    
1327     $Prop->{'word-spacing'} = {
1328     css => 'word-spacing',
1329     dom => 'word_spacing',
1330     key => 'word_spacing',
1331     parse => $Prop->{'letter-spacing'}->{parse},
1332     serialize => $default_serializer,
1333     initial => ['KEYWORD', 'normal'],
1334     inherited => 1,
1335     compute => $compute_length,
1336     };
1337     $Attr->{word_spacing} = $Prop->{'word-spacing'};
1338     $Key->{word_spacing} = $Prop->{'word-spacing'};
1339    
1340 wakaba 1.19 $Prop->{'margin-top'} = {
1341     css => 'margin-top',
1342     dom => 'margin_top',
1343     key => 'margin_top',
1344     parse => sub {
1345     my ($self, $prop_name, $tt, $t, $onerror) = @_;
1346    
1347 wakaba 1.21 ## NOTE: Used for 'margin-top', 'margin-right', 'margin-bottom',
1348 wakaba 1.22 ## 'margin-left', 'top', 'right', 'bottom', 'left', 'padding-top',
1349     ## 'padding-right', 'padding-bottom', 'padding-left',
1350     ## 'border-top-width', 'border-right-width', 'border-bottom-width',
1351 wakaba 1.23 ## 'border-left-width', and 'text-indent'.
1352 wakaba 1.21
1353 wakaba 1.19 my $sign = 1;
1354     if ($t->{type} == MINUS_TOKEN) {
1355     $t = $tt->get_next_token;
1356     $sign = -1;
1357     }
1358 wakaba 1.22 my $allow_negative = $Prop->{$prop_name}->{allow_negative};
1359 wakaba 1.19
1360     if ($t->{type} == DIMENSION_TOKEN) {
1361     my $value = $t->{number} * $sign;
1362     my $unit = lc $t->{value}; ## TODO: case
1363     $t = $tt->get_next_token;
1364 wakaba 1.22 if ($length_unit->{$unit} and ($allow_negative or $value >= 0)) {
1365 wakaba 1.19 return ($t, {$prop_name => ['DIMENSION', $value, $unit]});
1366     }
1367     } elsif ($t->{type} == PERCENTAGE_TOKEN) {
1368     my $value = $t->{number} * $sign;
1369     $t = $tt->get_next_token;
1370 wakaba 1.22 return ($t, {$prop_name => ['PERCENTAGE', $value]})
1371     if $allow_negative or $value >= 0;
1372 wakaba 1.20 } elsif ($t->{type} == NUMBER_TOKEN and
1373 wakaba 1.19 ($self->{unitless_px} or $t->{number} == 0)) {
1374     my $value = $t->{number} * $sign;
1375     $t = $tt->get_next_token;
1376 wakaba 1.22 return ($t, {$prop_name => ['DIMENSION', $value, 'px']})
1377     if $allow_negative or $value >= 0;
1378 wakaba 1.19 } elsif ($sign > 0 and $t->{type} == IDENT_TOKEN) {
1379     my $value = lc $t->{value}; ## TODO: case
1380     $t = $tt->get_next_token;
1381 wakaba 1.22 if ($Prop->{$prop_name}->{keyword}->{$value}) {
1382 wakaba 1.19 return ($t, {$prop_name => ['KEYWORD', $value]});
1383     } elsif ($value eq 'inherit') {
1384     return ($t, {$prop_name => ['INHERIT']});
1385     }
1386     }
1387    
1388     $onerror->(type => 'syntax error:'.$prop_name,
1389     level => $self->{must_level},
1390     token => $t);
1391     return ($t, undef);
1392     },
1393 wakaba 1.22 allow_negative => 1,
1394     keyword => {auto => 1},
1395 wakaba 1.19 serialize => $default_serializer,
1396     initial => ['DIMENSION', 0, 'px'],
1397     #inherited => 0,
1398     compute => $compute_length,
1399     };
1400     $Attr->{margin_top} = $Prop->{'margin-top'};
1401     $Key->{margin_top} = $Prop->{'margin-top'};
1402    
1403     $Prop->{'margin-bottom'} = {
1404     css => 'margin-bottom',
1405     dom => 'margin_bottom',
1406     key => 'margin_bottom',
1407     parse => $Prop->{'margin-top'}->{parse},
1408 wakaba 1.22 allow_negative => 1,
1409     keyword => {auto => 1},
1410 wakaba 1.19 serialize => $default_serializer,
1411     initial => ['DIMENSION', 0, 'px'],
1412     #inherited => 0,
1413     compute => $compute_length,
1414     };
1415     $Attr->{margin_bottom} = $Prop->{'margin-bottom'};
1416     $Key->{margin_bottom} = $Prop->{'margin-bottom'};
1417    
1418     $Prop->{'margin-right'} = {
1419     css => 'margin-right',
1420     dom => 'margin_right',
1421     key => 'margin_right',
1422     parse => $Prop->{'margin-top'}->{parse},
1423 wakaba 1.22 allow_negative => 1,
1424     keyword => {auto => 1},
1425 wakaba 1.19 serialize => $default_serializer,
1426     initial => ['DIMENSION', 0, 'px'],
1427     #inherited => 0,
1428     compute => $compute_length,
1429     };
1430     $Attr->{margin_right} = $Prop->{'margin-right'};
1431     $Key->{margin_right} = $Prop->{'margin-right'};
1432    
1433     $Prop->{'margin-left'} = {
1434     css => 'margin-left',
1435     dom => 'margin_left',
1436     key => 'margin_left',
1437     parse => $Prop->{'margin-top'}->{parse},
1438 wakaba 1.22 allow_negative => 1,
1439     keyword => {auto => 1},
1440 wakaba 1.19 serialize => $default_serializer,
1441     initial => ['DIMENSION', 0, 'px'],
1442     #inherited => 0,
1443     compute => $compute_length,
1444     };
1445     $Attr->{margin_left} = $Prop->{'margin-left'};
1446     $Key->{margin_left} = $Prop->{'margin-left'};
1447    
1448 wakaba 1.21 $Prop->{top} = {
1449     css => 'top',
1450     dom => 'top',
1451     key => 'top',
1452     parse => $Prop->{'margin-top'}->{parse},
1453 wakaba 1.22 allow_negative => 1,
1454     keyword => {auto => 1},
1455 wakaba 1.21 serialize => $default_serializer,
1456     initial => ['KEYWORD', 'auto'],
1457     #inherited => 0,
1458     compute_multiple => sub {
1459     my ($self, $element, $eid, $prop_name) = @_;
1460    
1461     my $pos_value = $self->get_computed_value ($element, 'position');
1462     if (defined $pos_value and $pos_value->[0] eq 'KEYWORD') {
1463     if ($pos_value->[1] eq 'static') {
1464     $self->{computed_value}->{$eid}->{top} = ['KEYWORD', 'auto'];
1465     $self->{computed_value}->{$eid}->{bottom} = ['KEYWORD', 'auto'];
1466     return;
1467     } elsif ($pos_value->[1] eq 'relative') {
1468     my $top_specified = $self->get_specified_value_no_inherit
1469     ($element, 'top');
1470     if (defined $top_specified and
1471     ($top_specified->[0] eq 'DIMENSION' or
1472     $top_specified->[0] eq 'PERCENTAGE')) {
1473     my $tv = $self->{computed_value}->{$eid}->{top}
1474     = $compute_length->($self, $element, 'top', $top_specified);
1475     $self->{computed_value}->{$eid}->{bottom}
1476     = [$tv->[0], -$tv->[1], $tv->[2]];
1477     } else { # top: auto
1478     my $bottom_specified = $self->get_specified_value_no_inherit
1479     ($element, 'bottom');
1480     if (defined $bottom_specified and
1481     ($bottom_specified->[0] eq 'DIMENSION' or
1482     $bottom_specified->[0] eq 'PERCENTAGE')) {
1483     my $tv = $self->{computed_value}->{$eid}->{bottom}
1484     = $compute_length->($self, $element, 'bottom',
1485     $bottom_specified);
1486     $self->{computed_value}->{$eid}->{top}
1487     = [$tv->[0], -$tv->[1], $tv->[2]];
1488     } else { # bottom: auto
1489     $self->{computed_value}->{$eid}->{top} = ['DIMENSION', 0, 'px'];
1490     $self->{computed_value}->{$eid}->{bottom} = ['DIMENSION', 0, 'px'];
1491     }
1492     }
1493     return;
1494     }
1495     }
1496    
1497     my $top_specified = $self->get_specified_value_no_inherit
1498     ($element, 'top');
1499     $self->{computed_value}->{$eid}->{top}
1500     = $compute_length->($self, $element, 'top', $top_specified);
1501     my $bottom_specified = $self->get_specified_value_no_inherit
1502     ($element, 'bottom');
1503     $self->{computed_value}->{$eid}->{bottom}
1504     = $compute_length->($self, $element, 'bottom', $bottom_specified);
1505     },
1506     };
1507     $Attr->{top} = $Prop->{top};
1508     $Key->{top} = $Prop->{top};
1509    
1510     $Prop->{bottom} = {
1511     css => 'bottom',
1512     dom => 'bottom',
1513     key => 'bottom',
1514     parse => $Prop->{'margin-top'}->{parse},
1515 wakaba 1.22 allow_negative => 1,
1516     keyword => {auto => 1},
1517 wakaba 1.21 serialize => $default_serializer,
1518     initial => ['KEYWORD', 'auto'],
1519     #inherited => 0,
1520     compute_multiple => $Prop->{top}->{compute_multiple},
1521     };
1522     $Attr->{bottom} = $Prop->{bottom};
1523     $Key->{bottom} = $Prop->{bottom};
1524    
1525     $Prop->{left} = {
1526     css => 'left',
1527     dom => 'left',
1528     key => 'left',
1529     parse => $Prop->{'margin-top'}->{parse},
1530 wakaba 1.22 allow_negative => 1,
1531     keyword => {auto => 1},
1532 wakaba 1.21 serialize => $default_serializer,
1533     initial => ['KEYWORD', 'auto'],
1534     #inherited => 0,
1535     compute_multiple => sub {
1536     my ($self, $element, $eid, $prop_name) = @_;
1537    
1538     my $pos_value = $self->get_computed_value ($element, 'position');
1539     if (defined $pos_value and $pos_value->[0] eq 'KEYWORD') {
1540     if ($pos_value->[1] eq 'static') {
1541     $self->{computed_value}->{$eid}->{left} = ['KEYWORD', 'auto'];
1542     $self->{computed_value}->{$eid}->{right} = ['KEYWORD', 'auto'];
1543     return;
1544     } elsif ($pos_value->[1] eq 'relative') {
1545     my $left_specified = $self->get_specified_value_no_inherit
1546     ($element, 'left');
1547     if (defined $left_specified and
1548     ($left_specified->[0] eq 'DIMENSION' or
1549     $left_specified->[0] eq 'PERCENTAGE')) {
1550     my $right_specified = $self->get_specified_value_no_inherit
1551     ($element, 'right');
1552     if (defined $right_specified and
1553     ($right_specified->[0] eq 'DIMENSION' or
1554     $right_specified->[0] eq 'PERCENTAGE')) {
1555     my $direction = $self->get_computed_value ($element, 'direction');
1556     if (defined $direction and $direction->[0] eq 'KEYWORD' and
1557     $direction->[0] eq 'ltr') {
1558     my $tv = $self->{computed_value}->{$eid}->{left}
1559     = $compute_length->($self, $element, 'left',
1560     $left_specified);
1561     $self->{computed_value}->{$eid}->{right}
1562     = [$tv->[0], -$tv->[1], $tv->[2]];
1563     } else {
1564     my $tv = $self->{computed_value}->{$eid}->{right}
1565     = $compute_length->($self, $element, 'right',
1566     $right_specified);
1567     $self->{computed_value}->{$eid}->{left}
1568     = [$tv->[0], -$tv->[1], $tv->[2]];
1569     }
1570     } else {
1571     my $tv = $self->{computed_value}->{$eid}->{left}
1572     = $compute_length->($self, $element, 'left', $left_specified);
1573     $self->{computed_value}->{$eid}->{right}
1574     = [$tv->[0], -$tv->[1], $tv->[2]];
1575     }
1576     } else { # left: auto
1577     my $right_specified = $self->get_specified_value_no_inherit
1578     ($element, 'right');
1579     if (defined $right_specified and
1580     ($right_specified->[0] eq 'DIMENSION' or
1581     $right_specified->[0] eq 'PERCENTAGE')) {
1582     my $tv = $self->{computed_value}->{$eid}->{right}
1583     = $compute_length->($self, $element, 'right',
1584     $right_specified);
1585     $self->{computed_value}->{$eid}->{left}
1586     = [$tv->[0], -$tv->[1], $tv->[2]];
1587     } else { # right: auto
1588     $self->{computed_value}->{$eid}->{left} = ['DIMENSION', 0, 'px'];
1589     $self->{computed_value}->{$eid}->{right} = ['DIMENSION', 0, 'px'];
1590     }
1591     }
1592     return;
1593     }
1594     }
1595    
1596     my $left_specified = $self->get_specified_value_no_inherit
1597     ($element, 'left');
1598     $self->{computed_value}->{$eid}->{left}
1599     = $compute_length->($self, $element, 'left', $left_specified);
1600     my $right_specified = $self->get_specified_value_no_inherit
1601     ($element, 'right');
1602     $self->{computed_value}->{$eid}->{right}
1603     = $compute_length->($self, $element, 'right', $right_specified);
1604     },
1605     };
1606     $Attr->{left} = $Prop->{left};
1607     $Key->{left} = $Prop->{left};
1608    
1609     $Prop->{right} = {
1610     css => 'right',
1611     dom => 'right',
1612     key => 'right',
1613     parse => $Prop->{'margin-top'}->{parse},
1614     serialize => $default_serializer,
1615     initial => ['KEYWORD', 'auto'],
1616     #inherited => 0,
1617     compute_multiple => $Prop->{left}->{compute_multiple},
1618     };
1619     $Attr->{right} = $Prop->{right};
1620     $Key->{right} = $Prop->{right};
1621    
1622 wakaba 1.22 $Prop->{width} = {
1623     css => 'width',
1624     dom => 'width',
1625     key => 'width',
1626     parse => $Prop->{'margin-top'}->{parse},
1627     #allow_negative => 0,
1628     keyword => {auto => 1},
1629     serialize => $default_serializer,
1630     initial => ['KEYWORD', 'auto'],
1631     #inherited => 0,
1632     compute => $compute_length,
1633     ## NOTE: See <http://suika.fam.cx/gate/2005/sw/width> for
1634     ## browser compatibility issues.
1635     };
1636     $Attr->{width} = $Prop->{width};
1637     $Key->{width} = $Prop->{width};
1638    
1639     $Prop->{'min-width'} = {
1640     css => 'min-width',
1641     dom => 'min_width',
1642     key => 'min_width',
1643     parse => $Prop->{'margin-top'}->{parse},
1644     #allow_negative => 0,
1645     #keyword => {},
1646     serialize => $default_serializer,
1647     initial => ['DIMENSION', 0, 'px'],
1648     #inherited => 0,
1649     compute => $compute_length,
1650     };
1651     $Attr->{min_width} = $Prop->{'min-width'};
1652     $Key->{min_width} = $Prop->{'min-width'};
1653    
1654     $Prop->{'max-width'} = {
1655     css => 'max-width',
1656     dom => 'max_width',
1657     key => 'max_width',
1658     parse => $Prop->{'margin-top'}->{parse},
1659     #allow_negative => 0,
1660     keyword => {none => 1},
1661     serialize => $default_serializer,
1662     initial => ['KEYWORD', 'none'],
1663     #inherited => 0,
1664     compute => $compute_length,
1665     };
1666     $Attr->{max_width} = $Prop->{'max-width'};
1667     $Key->{max_width} = $Prop->{'max-width'};
1668    
1669     $Prop->{height} = {
1670     css => 'height',
1671     dom => 'height',
1672     key => 'height',
1673     parse => $Prop->{'margin-top'}->{parse},
1674     #allow_negative => 0,
1675     keyword => {auto => 1},
1676     serialize => $default_serializer,
1677     initial => ['KEYWORD', 'auto'],
1678     #inherited => 0,
1679     compute => $compute_length,
1680     ## NOTE: See <http://suika.fam.cx/gate/2005/sw/height> for
1681     ## browser compatibility issues.
1682     };
1683     $Attr->{height} = $Prop->{height};
1684     $Key->{height} = $Prop->{height};
1685    
1686     $Prop->{'min-height'} = {
1687     css => 'min-height',
1688     dom => 'min_height',
1689     key => 'min_height',
1690     parse => $Prop->{'margin-top'}->{parse},
1691     #allow_negative => 0,
1692     #keyword => {},
1693     serialize => $default_serializer,
1694     initial => ['DIMENSION', 0, 'px'],
1695     #inherited => 0,
1696     compute => $compute_length,
1697     };
1698     $Attr->{min_height} = $Prop->{'min-height'};
1699     $Key->{min_height} = $Prop->{'min-height'};
1700    
1701     $Prop->{'max-height'} = {
1702     css => 'max-height',
1703     dom => 'max_height',
1704     key => 'max_height',
1705     parse => $Prop->{'margin-top'}->{parse},
1706     #allow_negative => 0,
1707     keyword => {none => 1},
1708     serialize => $default_serializer,
1709     initial => ['KEYWORD', 'none'],
1710     #inherited => 0,
1711     compute => $compute_length,
1712     };
1713     $Attr->{max_height} = $Prop->{'max-height'};
1714     $Key->{max_height} = $Prop->{'max-height'};
1715    
1716     $Prop->{'line-height'} = {
1717     css => 'line-height',
1718     dom => 'line_height',
1719     key => 'line_height',
1720 wakaba 1.19 parse => sub {
1721     my ($self, $prop_name, $tt, $t, $onerror) = @_;
1722    
1723 wakaba 1.22 ## NOTE: Similar to 'margin-top', but different handling
1724     ## for unitless numbers.
1725    
1726 wakaba 1.19 my $sign = 1;
1727     if ($t->{type} == MINUS_TOKEN) {
1728     $t = $tt->get_next_token;
1729     $sign = -1;
1730     }
1731 wakaba 1.22 my $allow_negative = $Prop->{$prop_name}->{allow_negative};
1732 wakaba 1.19
1733     if ($t->{type} == DIMENSION_TOKEN) {
1734     my $value = $t->{number} * $sign;
1735     my $unit = lc $t->{value}; ## TODO: case
1736     $t = $tt->get_next_token;
1737     if ($length_unit->{$unit} and $value >= 0) {
1738     return ($t, {$prop_name => ['DIMENSION', $value, $unit]});
1739     }
1740     } elsif ($t->{type} == PERCENTAGE_TOKEN) {
1741     my $value = $t->{number} * $sign;
1742     $t = $tt->get_next_token;
1743 wakaba 1.22 return ($t, {$prop_name => ['PERCENTAGE', $value]})
1744     if $value >= 0;
1745     } elsif ($t->{type} == NUMBER_TOKEN) {
1746 wakaba 1.19 my $value = $t->{number} * $sign;
1747     $t = $tt->get_next_token;
1748 wakaba 1.22 return ($t, {$prop_name => ['NUMBER', $value]}) if $value >= 0;
1749 wakaba 1.19 } elsif ($sign > 0 and $t->{type} == IDENT_TOKEN) {
1750     my $value = lc $t->{value}; ## TODO: case
1751     $t = $tt->get_next_token;
1752 wakaba 1.22 if ($value eq 'normal') {
1753     return ($t, {$prop_name => ['KEYWORD', $value]});
1754     } elsif ($value eq 'inherit') {
1755 wakaba 1.19 return ($t, {$prop_name => ['INHERIT']});
1756     }
1757     }
1758    
1759     $onerror->(type => 'syntax error:'.$prop_name,
1760     level => $self->{must_level},
1761     token => $t);
1762     return ($t, undef);
1763     },
1764     serialize => $default_serializer,
1765 wakaba 1.22 initial => ['KEYWORD', 'normal'],
1766     inherited => 1,
1767     compute => $compute_length,
1768     };
1769     $Attr->{line_height} = $Prop->{'line-height'};
1770     $Key->{line_height} = $Prop->{'line-height'};
1771    
1772     $Prop->{'vertical-align'} = {
1773     css => 'vertical-align',
1774     dom => 'vertical_align',
1775     key => 'vertical_align',
1776     parse => $Prop->{'margin-top'}->{parse},
1777     allow_negative => 1,
1778     keyword => {
1779     baseline => 1, sub => 1, super => 1, top => 1, 'text-top' => 1,
1780     middle => 1, bottom => 1, 'text-bottom' => 1,
1781     },
1782     ## NOTE: Currently, we don't support option to select subset of keywords
1783     ## supported by application (i.e.
1784     ## $parser->{prop_value}->{'line-height'->{$keyword}). Should we support
1785     ## it?
1786     serialize => $default_serializer,
1787     initial => ['KEYWORD', 'baseline'],
1788     #inherited => 0,
1789     compute => $compute_length,
1790     ## NOTE: See <http://suika.fam.cx/gate/2005/sw/vertical-align> for
1791     ## browser compatibility issues.
1792     };
1793     $Attr->{vertical_align} = $Prop->{'vertical-align'};
1794     $Key->{vertical_align} = $Prop->{'vertical-align'};
1795    
1796 wakaba 1.23 $Prop->{'text-indent'} = {
1797     css => 'text-indent',
1798     dom => 'text_indent',
1799     key => 'text_indent',
1800     parse => $Prop->{'margin-top'}->{parse},
1801     allow_negative => 1,
1802     keyword => {},
1803     serialize => $default_serializer,
1804     initial => ['DIMENSION', 0, 'px'],
1805     inherited => 1,
1806     compute => $compute_length,
1807     };
1808     $Attr->{text_indent} = $Prop->{'text-indent'};
1809     $Key->{text_indent} = $Prop->{'text-indent'};
1810    
1811 wakaba 1.22 $Prop->{'padding-top'} = {
1812     css => 'padding-top',
1813     dom => 'padding_top',
1814     key => 'padding_top',
1815     parse => $Prop->{'margin-top'}->{parse},
1816     #allow_negative => 0,
1817     #keyword => {},
1818     serialize => $default_serializer,
1819 wakaba 1.19 initial => ['DIMENSION', 0, 'px'],
1820     #inherited => 0,
1821     compute => $compute_length,
1822     };
1823     $Attr->{padding_top} = $Prop->{'padding-top'};
1824     $Key->{padding_top} = $Prop->{'padding-top'};
1825    
1826     $Prop->{'padding-bottom'} = {
1827     css => 'padding-bottom',
1828     dom => 'padding_bottom',
1829     key => 'padding_bottom',
1830     parse => $Prop->{'padding-top'}->{parse},
1831 wakaba 1.22 #allow_negative => 0,
1832     #keyword => {},
1833 wakaba 1.19 serialize => $default_serializer,
1834     initial => ['DIMENSION', 0, 'px'],
1835     #inherited => 0,
1836     compute => $compute_length,
1837     };
1838     $Attr->{padding_bottom} = $Prop->{'padding-bottom'};
1839     $Key->{padding_bottom} = $Prop->{'padding-bottom'};
1840    
1841     $Prop->{'padding-right'} = {
1842     css => 'padding-right',
1843     dom => 'padding_right',
1844     key => 'padding_right',
1845     parse => $Prop->{'padding-top'}->{parse},
1846 wakaba 1.22 #allow_negative => 0,
1847     #keyword => {},
1848 wakaba 1.19 serialize => $default_serializer,
1849     initial => ['DIMENSION', 0, 'px'],
1850     #inherited => 0,
1851     compute => $compute_length,
1852     };
1853     $Attr->{padding_right} = $Prop->{'padding-right'};
1854     $Key->{padding_right} = $Prop->{'padding-right'};
1855    
1856     $Prop->{'padding-left'} = {
1857     css => 'padding-left',
1858     dom => 'padding_left',
1859     key => 'padding_left',
1860     parse => $Prop->{'padding-top'}->{parse},
1861 wakaba 1.22 #allow_negative => 0,
1862     #keyword => {},
1863 wakaba 1.19 serialize => $default_serializer,
1864     initial => ['DIMENSION', 0, 'px'],
1865     #inherited => 0,
1866     compute => $compute_length,
1867     };
1868     $Attr->{padding_left} = $Prop->{'padding-left'};
1869     $Key->{padding_left} = $Prop->{'padding-left'};
1870    
1871 wakaba 1.20 $Prop->{'border-top-width'} = {
1872     css => 'border-top-width',
1873     dom => 'border_top_width',
1874     key => 'border_top_width',
1875 wakaba 1.22 parse => $Prop->{'margin-top'}->{parse},
1876     #allow_negative => 0,
1877     keyword => {thin => 1, medium => 1, thick => 1},
1878 wakaba 1.20 serialize => $default_serializer,
1879     initial => ['KEYWORD', 'medium'],
1880     #inherited => 0,
1881     compute => sub {
1882     my ($self, $element, $prop_name, $specified_value) = @_;
1883    
1884 wakaba 1.23 ## NOTE: Used for 'border-top-width', 'border-right-width',
1885     ## 'border-bottom-width', 'border-right-width', and
1886     ## 'outline-width'.
1887    
1888 wakaba 1.20 my $style_prop = $prop_name;
1889     $style_prop =~ s/width/style/;
1890     my $style = $self->get_computed_value ($element, $style_prop);
1891     if (defined $style and $style->[0] eq 'KEYWORD' and
1892     ($style->[1] eq 'none' or $style->[1] eq 'hidden')) {
1893     return ['DIMENSION', 0, 'px'];
1894     }
1895    
1896     my $value = $compute_length->(@_);
1897     if (defined $value and $value->[0] eq 'KEYWORD') {
1898     if ($value->[1] eq 'thin') {
1899     return ['DIMENSION', 1, 'px']; ## Firefox/Opera
1900     } elsif ($value->[1] eq 'medium') {
1901     return ['DIMENSION', 3, 'px']; ## Firefox/Opera
1902     } elsif ($value->[1] eq 'thick') {
1903     return ['DIMENSION', 5, 'px']; ## Firefox
1904     }
1905     }
1906     return $value;
1907     },
1908 wakaba 1.23 ## NOTE: CSS3 will allow <percentage> as an option in <border-width>.
1909     ## Opera 9 has already implemented it.
1910 wakaba 1.20 };
1911     $Attr->{border_top_width} = $Prop->{'border-top-width'};
1912     $Key->{border_top_width} = $Prop->{'border-top-width'};
1913    
1914     $Prop->{'border-right-width'} = {
1915     css => 'border-right-width',
1916     dom => 'border_right_width',
1917     key => 'border_right_width',
1918     parse => $Prop->{'border-top-width'}->{parse},
1919 wakaba 1.22 #allow_negative => 0,
1920     keyword => {thin => 1, medium => 1, thick => 1},
1921 wakaba 1.20 serialize => $default_serializer,
1922     initial => ['KEYWORD', 'medium'],
1923     #inherited => 0,
1924     compute => $Prop->{'border-top-width'}->{compute},
1925     };
1926     $Attr->{border_right_width} = $Prop->{'border-right-width'};
1927     $Key->{border_right_width} = $Prop->{'border-right-width'};
1928    
1929     $Prop->{'border-bottom-width'} = {
1930     css => 'border-bottom-width',
1931     dom => 'border_bottom_width',
1932     key => 'border_bottom_width',
1933     parse => $Prop->{'border-top-width'}->{parse},
1934 wakaba 1.22 #allow_negative => 0,
1935     keyword => {thin => 1, medium => 1, thick => 1},
1936 wakaba 1.20 serialize => $default_serializer,
1937     initial => ['KEYWORD', 'medium'],
1938     #inherited => 0,
1939     compute => $Prop->{'border-top-width'}->{compute},
1940     };
1941     $Attr->{border_bottom_width} = $Prop->{'border-bottom-width'};
1942     $Key->{border_bottom_width} = $Prop->{'border-bottom-width'};
1943    
1944     $Prop->{'border-left-width'} = {
1945     css => 'border-left-width',
1946     dom => 'border_left_width',
1947     key => 'border_left_width',
1948     parse => $Prop->{'border-top-width'}->{parse},
1949 wakaba 1.22 #allow_negative => 0,
1950     keyword => {thin => 1, medium => 1, thick => 1},
1951 wakaba 1.20 serialize => $default_serializer,
1952     initial => ['KEYWORD', 'medium'],
1953     #inherited => 0,
1954     compute => $Prop->{'border-top-width'}->{compute},
1955     };
1956     $Attr->{border_left_width} = $Prop->{'border-left-width'};
1957     $Key->{border_left_width} = $Prop->{'border-left-width'};
1958    
1959 wakaba 1.23 $Prop->{'outline-width'} = {
1960     css => 'outline-width',
1961     dom => 'outline_width',
1962     key => 'outline_width',
1963     parse => $Prop->{'border-top-width'}->{parse},
1964     #allow_negative => 0,
1965     keyword => {thin => 1, medium => 1, thick => 1},
1966     serialize => $default_serializer,
1967     initial => ['KEYWORD', 'medium'],
1968     #inherited => 0,
1969     compute => $Prop->{'border-top-width'}->{compute},
1970     };
1971     $Attr->{outline_width} = $Prop->{'outline-width'};
1972     $Key->{outline_width} = $Prop->{'outline-width'};
1973    
1974 wakaba 1.15 $Prop->{'font-weight'} = {
1975     css => 'font-weight',
1976     dom => 'font_weight',
1977     key => 'font_weight',
1978     parse => sub {
1979     my ($self, $prop_name, $tt, $t, $onerror) = @_;
1980    
1981     if ($t->{type} == NUMBER_TOKEN) {
1982     ## ISSUE: See <http://suika.fam.cx/gate/2005/sw/font-weight> for
1983     ## browser compatibility issue.
1984     my $value = $t->{number};
1985     $t = $tt->get_next_token;
1986     if ($value % 100 == 0 and 100 <= $value and $value <= 900) {
1987     return ($t, {$prop_name => ['WEIGHT', $value, 0]});
1988     }
1989     } elsif ($t->{type} == IDENT_TOKEN) {
1990     my $value = lc $t->{value}; ## TODO: case
1991     $t = $tt->get_next_token;
1992     if ({
1993     normal => 1, bold => 1, bolder => 1, lighter => 1,
1994     }->{$value}) {
1995     return ($t, {$prop_name => ['KEYWORD', $value]});
1996     } elsif ($value eq 'inherit') {
1997     return ($t, {$prop_name => ['INHERIT']});
1998     }
1999     }
2000    
2001     $onerror->(type => 'syntax error:'.$prop_name,
2002     level => $self->{must_level},
2003     token => $t);
2004     return ($t, undef);
2005     },
2006     serialize => $default_serializer,
2007     initial => ['KEYWORD', 'normal'],
2008     inherited => 1,
2009     compute => sub {
2010     my ($self, $element, $prop_name, $specified_value) = @_;
2011    
2012 wakaba 1.17 if (defined $specified_value and $specified_value->[0] eq 'KEYWORD') {
2013 wakaba 1.15 if ($specified_value->[1] eq 'normal') {
2014     return ['WEIGHT', 400, 0];
2015     } elsif ($specified_value->[1] eq 'bold') {
2016     return ['WEIGHT', 700, 0];
2017     } elsif ($specified_value->[1] eq 'bolder') {
2018     my $parent_element = $element->manakai_parent_element;
2019     if (defined $parent_element) {
2020     my $parent_value = $self->get_cascaded_value
2021     ($parent_element, $prop_name); ## NOTE: What Firefox does.
2022     return ['WEIGHT', $parent_value->[1], $parent_value->[2] + 1];
2023     } else {
2024     return ['WEIGHT', 400, 1];
2025     }
2026     } elsif ($specified_value->[1] eq 'lighter') {
2027     my $parent_element = $element->manakai_parent_element;
2028     if (defined $parent_element) {
2029     my $parent_value = $self->get_cascaded_value
2030     ($parent_element, $prop_name); ## NOTE: What Firefox does.
2031     return ['WEIGHT', $parent_value->[1], $parent_value->[2] - 1];
2032     } else {
2033     return ['WEIGHT', 400, 1];
2034     }
2035     }
2036 wakaba 1.17 #} elsif (defined $specified_value and $specified_value->[0] eq 'WEIGHT') {
2037 wakaba 1.15 #
2038     }
2039    
2040     return $specified_value;
2041     },
2042     };
2043     $Attr->{font_weight} = $Prop->{'font-weight'};
2044     $Key->{font_weight} = $Prop->{'font-weight'};
2045    
2046 wakaba 1.13 my $uri_or_none_parser = sub {
2047 wakaba 1.11 my ($self, $prop_name, $tt, $t, $onerror) = @_;
2048    
2049 wakaba 1.13 if ($t->{type} == URI_TOKEN) {
2050 wakaba 1.11 my $value = $t->{value};
2051     $t = $tt->get_next_token;
2052     return ($t, {$prop_name => ['URI', $value, \($self->{base_uri})]});
2053     } elsif ($t->{type} == IDENT_TOKEN) {
2054     my $value = lc $t->{value}; ## TODO: case
2055     $t = $tt->get_next_token;
2056     if ($value eq 'none') {
2057     ## NOTE: |none| is the default value and therefore it must be
2058     ## supported anyway.
2059     return ($t, {$prop_name => ["KEYWORD", 'none']});
2060     } elsif ($value eq 'inherit') {
2061     return ($t, {$prop_name => ['INHERIT']});
2062     }
2063     ## NOTE: None of Firefox2, WinIE6, and Opera9 support this case.
2064     #} elsif ($t->{type} == URI_INVALID_TOKEN) {
2065     # my $value = $t->{value};
2066     # $t = $tt->get_next_token;
2067     # if ($t->{type} == EOF_TOKEN) {
2068     # $onerror->(type => 'syntax error:eof:'.$prop_name,
2069     # level => $self->{must_level},
2070     # token => $t);
2071     #
2072     # return ($t, {$prop_name => ['URI', $value, \($self->{base_uri})]});
2073     # }
2074     }
2075    
2076     $onerror->(type => 'syntax error:'.$prop_name,
2077     level => $self->{must_level},
2078     token => $t);
2079     return ($t, undef);
2080 wakaba 1.13 }; # $uri_or_none_parser
2081    
2082 wakaba 1.14 my $compute_uri_or_none = sub {
2083 wakaba 1.11 my ($self, $element, $prop_name, $specified_value) = @_;
2084    
2085     if (defined $specified_value and
2086     $specified_value->[0] eq 'URI' and
2087     defined $specified_value->[2]) {
2088     require Message::DOM::DOMImplementation;
2089     return ['URI',
2090     Message::DOM::DOMImplementation->create_uri_reference
2091     ($specified_value->[1])
2092     ->get_absolute_reference (${$specified_value->[2]})
2093     ->get_uri_reference,
2094     $specified_value->[2]];
2095     }
2096    
2097     return $specified_value;
2098 wakaba 1.14 }; # $compute_uri_or_none
2099    
2100     $Prop->{'list-style-image'} = {
2101     css => 'list-style-image',
2102     dom => 'list_style_image',
2103     key => 'list_style_image',
2104     parse => $uri_or_none_parser,
2105     serialize => $default_serializer,
2106     initial => ['KEYWORD', 'none'],
2107     inherited => 1,
2108     compute => $compute_uri_or_none,
2109 wakaba 1.11 };
2110     $Attr->{list_style_image} = $Prop->{'list-style-image'};
2111     $Key->{list_style_image} = $Prop->{'list-style-image'};
2112    
2113 wakaba 1.15 $Prop->{'background-image'} = {
2114     css => 'background-image',
2115     dom => 'background_image',
2116     key => 'background_image',
2117     parse => $uri_or_none_parser,
2118     serialize => $default_serializer,
2119     initial => ['KEYWORD', 'none'],
2120     #inherited => 0,
2121     compute => $compute_uri_or_none,
2122     };
2123     $Attr->{background_image} = $Prop->{'background-image'};
2124     $Key->{background_image} = $Prop->{'background-image'};
2125    
2126 wakaba 1.7 my $border_style_keyword = {
2127     none => 1, hidden => 1, dotted => 1, dashed => 1, solid => 1,
2128     double => 1, groove => 1, ridge => 1, inset => 1, outset => 1,
2129     };
2130    
2131     $Prop->{'border-top-style'} = {
2132     css => 'border-top-style',
2133     dom => 'border_top_style',
2134     key => 'border_top_style',
2135     parse => $one_keyword_parser,
2136 wakaba 1.11 serialize => $default_serializer,
2137 wakaba 1.7 keyword => $border_style_keyword,
2138 wakaba 1.9 initial => ["KEYWORD", "none"],
2139     #inherited => 0,
2140     compute => $compute_as_specified,
2141 wakaba 1.7 };
2142     $Attr->{border_top_style} = $Prop->{'border-top-style'};
2143     $Key->{border_top_style} = $Prop->{'border-top-style'};
2144    
2145     $Prop->{'border-right-style'} = {
2146     css => 'border-right-style',
2147     dom => 'border_right_style',
2148     key => 'border_right_style',
2149     parse => $one_keyword_parser,
2150 wakaba 1.11 serialize => $default_serializer,
2151 wakaba 1.7 keyword => $border_style_keyword,
2152 wakaba 1.9 initial => ["KEYWORD", "none"],
2153     #inherited => 0,
2154     compute => $compute_as_specified,
2155 wakaba 1.7 };
2156     $Attr->{border_right_style} = $Prop->{'border-right-style'};
2157     $Key->{border_right_style} = $Prop->{'border-right-style'};
2158    
2159     $Prop->{'border-bottom-style'} = {
2160     css => 'border-bottom-style',
2161     dom => 'border_bottom_style',
2162     key => 'border_bottom_style',
2163     parse => $one_keyword_parser,
2164 wakaba 1.11 serialize => $default_serializer,
2165 wakaba 1.7 keyword => $border_style_keyword,
2166 wakaba 1.9 initial => ["KEYWORD", "none"],
2167     #inherited => 0,
2168     compute => $compute_as_specified,
2169 wakaba 1.7 };
2170     $Attr->{border_bottom_style} = $Prop->{'border-bottom-style'};
2171     $Key->{border_bottom_style} = $Prop->{'border-bottom-style'};
2172    
2173     $Prop->{'border-left-style'} = {
2174     css => 'border-left-style',
2175     dom => 'border_left_style',
2176     key => 'border_left_style',
2177     parse => $one_keyword_parser,
2178 wakaba 1.11 serialize => $default_serializer,
2179 wakaba 1.7 keyword => $border_style_keyword,
2180 wakaba 1.9 initial => ["KEYWORD", "none"],
2181     #inherited => 0,
2182     compute => $compute_as_specified,
2183 wakaba 1.7 };
2184     $Attr->{border_left_style} = $Prop->{'border-left-style'};
2185     $Key->{border_left_style} = $Prop->{'border-left-style'};
2186    
2187 wakaba 1.16 $Prop->{'outline-style'} = {
2188     css => 'outline-style',
2189     dom => 'outline_style',
2190     key => 'outline_style',
2191     parse => $one_keyword_parser,
2192     serialize => $default_serializer,
2193 wakaba 1.23 keyword => {%$border_style_keyword},
2194 wakaba 1.16 initial => ['KEYWORD', 'none'],
2195     #inherited => 0,
2196     compute => $compute_as_specified,
2197     };
2198     $Attr->{outline_style} = $Prop->{'outline-style'};
2199     $Key->{outline_style} = $Prop->{'outline-style'};
2200 wakaba 1.23 delete $Prop->{'outline-style'}->{keyword}->{hidden};
2201 wakaba 1.16
2202 wakaba 1.15 $Prop->{'font-family'} = {
2203     css => 'font-family',
2204     dom => 'font_family',
2205     key => 'font_family',
2206     parse => sub {
2207     my ($self, $prop_name, $tt, $t, $onerror) = @_;
2208    
2209     ## NOTE: See <http://suika.fam.cx/gate/2005/sw/font-family> for
2210     ## how chaotic browsers are!
2211    
2212     my @prop_value;
2213    
2214     my $font_name = '';
2215     my $may_be_generic = 1;
2216     my $may_be_inherit = 1;
2217     my $has_s = 0;
2218     F: {
2219     if ($t->{type} == IDENT_TOKEN) {
2220     undef $may_be_inherit if $has_s or length $font_name;
2221     undef $may_be_generic if $has_s or length $font_name;
2222     $font_name .= ' ' if $has_s;
2223     $font_name .= $t->{value};
2224     undef $has_s;
2225     $t = $tt->get_next_token;
2226     } elsif ($t->{type} == STRING_TOKEN) {
2227     $font_name .= ' ' if $has_s;
2228     $font_name .= $t->{value};
2229     undef $may_be_inherit;
2230     undef $may_be_generic;
2231     undef $has_s;
2232     $t = $tt->get_next_token;
2233     } elsif ($t->{type} == COMMA_TOKEN) {
2234     if ($may_be_generic and
2235     {
2236     serif => 1, 'sans-serif' => 1, cursive => 1,
2237     fantasy => 1, monospace => 1, '-manakai-default' => 1,
2238     }->{lc $font_name}) { ## TODO: case
2239     push @prop_value, ['KEYWORD', $font_name];
2240     } elsif (not $may_be_generic or length $font_name) {
2241     push @prop_value, ["STRING", $font_name];
2242     }
2243     undef $may_be_inherit;
2244     $may_be_generic = 1;
2245     undef $has_s;
2246     $font_name = '';
2247     $t = $tt->get_next_token;
2248     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
2249     } elsif ($t->{type} == S_TOKEN) {
2250     $has_s = 1;
2251     $t = $tt->get_next_token;
2252     } else {
2253     if ($may_be_generic and
2254     {
2255     serif => 1, 'sans-serif' => 1, cursive => 1,
2256     fantasy => 1, monospace => 1, '-manakai-default' => 1,
2257     }->{lc $font_name}) { ## TODO: case
2258     push @prop_value, ['KEYWORD', $font_name];
2259     } elsif (not $may_be_generic or length $font_name) {
2260     push @prop_value, ['STRING', $font_name];
2261     } else {
2262     $onerror->(type => 'syntax error:'.$prop_name,
2263     level => $self->{must_level},
2264     token => $t);
2265     return ($t, undef);
2266     }
2267     last F;
2268     }
2269     redo F;
2270     } # F
2271    
2272     if ($may_be_inherit and
2273     @prop_value == 1 and
2274     $prop_value[0]->[0] eq 'STRING' and
2275     lc $prop_value[0]->[1] eq 'inherit') { ## TODO: case
2276     return ($t, {$prop_name => ['INHERIT']});
2277     } else {
2278     unshift @prop_value, 'FONT';
2279     return ($t, {$prop_name => \@prop_value});
2280     }
2281     },
2282     serialize => sub {
2283     my ($self, $prop_name, $value) = @_;
2284    
2285     if ($value->[0] eq 'FONT') {
2286     return join ', ', map {
2287     if ($_->[0] eq 'STRING') {
2288     '"'.$_->[1].'"'; ## NOTE: This is what Firefox does.
2289     } elsif ($_->[0] eq 'KEYWORD') {
2290     $_->[1]; ## NOTE: This is what Firefox does.
2291     } else {
2292     ## NOTE: This should be an error.
2293     '""';
2294     }
2295     } @$value[1..$#$value];
2296     } elsif ($value->[0] eq 'INHERIT') {
2297     return 'inherit';
2298     } else {
2299     return undef;
2300     }
2301     },
2302     initial => ['FONT', ['KEYWORD', '-manakai-default']],
2303     inherited => 1,
2304     compute => $compute_as_specified,
2305     };
2306     $Attr->{font_family} = $Prop->{'font-family'};
2307     $Key->{font_family} = $Prop->{'font-family'};
2308    
2309 wakaba 1.17 $Prop->{cursor} = {
2310     css => 'cursor',
2311     dom => 'cursor',
2312     key => 'cursor',
2313     parse => sub {
2314     my ($self, $prop_name, $tt, $t, $onerror) = @_;
2315    
2316     ## NOTE: See <http://suika.fam.cx/gate/2005/sw/cursor> for browser
2317     ## compatibility issues.
2318    
2319     my @prop_value = ('CURSOR');
2320    
2321     F: {
2322     if ($t->{type} == IDENT_TOKEN) {
2323     my $v = lc $t->{value}; ## TODO: case
2324     $t = $tt->get_next_token;
2325     if ($Prop->{$prop_name}->{keyword}->{$v}) {
2326     push @prop_value, ['KEYWORD', $v];
2327     last F;
2328     } elsif ($v eq 'inherit' and @prop_value == 1) {
2329     return ($t, {$prop_name => ['INHERIT']});
2330     } else {
2331     $onerror->(type => 'syntax error:'.$prop_name,
2332     level => $self->{must_level},
2333     token => $t);
2334     return ($t, undef);
2335     }
2336     } elsif ($t->{type} == URI_TOKEN) {
2337     push @prop_value, ['URI', $t->{value}, \($self->{base_uri})];
2338     $t = $tt->get_next_token;
2339     } else {
2340     $onerror->(type => 'syntax error:'.$prop_name,
2341     level => $self->{must_level},
2342     token => $t);
2343     return ($t, undef);
2344     }
2345    
2346     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
2347     if ($t->{type} == COMMA_TOKEN) {
2348     $t = $tt->get_next_token;
2349     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
2350     redo F;
2351     }
2352     } # F
2353    
2354     return ($t, {$prop_name => \@prop_value});
2355     },
2356     serialize => sub {
2357     my ($self, $prop_name, $value) = @_;
2358    
2359     if ($value->[0] eq 'CURSOR') {
2360     return join ', ', map {
2361     if ($_->[0] eq 'URI') {
2362     'url('.$_->[1].')'; ## NOTE: This is what Firefox does.
2363     } elsif ($_->[0] eq 'KEYWORD') {
2364     $_->[1];
2365     } else {
2366     ## NOTE: This should be an error.
2367     '""';
2368     }
2369     } @$value[1..$#$value];
2370     } elsif ($value->[0] eq 'INHERIT') {
2371     return 'inherit';
2372     } else {
2373     return undef;
2374     }
2375     },
2376     keyword => {
2377     auto => 1, crosshair => 1, default => 1, pointer => 1, move => 1,
2378     'e-resize' => 1, 'ne-resize' => 1, 'nw-resize' => 1, 'n-resize' => 1,
2379     'n-resize' => 1, 'se-resize' => 1, 'sw-resize' => 1, 's-resize' => 1,
2380     'w-resize' => 1, text => 1, wait => 1, help => 1, progress => 1,
2381     },
2382     initial => ['CURSOR', ['KEYWORD', 'auto']],
2383     inherited => 1,
2384     compute => sub {
2385     my ($self, $element, $prop_name, $specified_value) = @_;
2386    
2387     if (defined $specified_value and $specified_value->[0] eq 'CURSOR') {
2388     my @new_value = ('CURSOR');
2389     for my $value (@$specified_value[1..$#$specified_value]) {
2390     if ($value->[0] eq 'URI') {
2391     if (defined $value->[2]) {
2392     require Message::DOM::DOMImplementation;
2393     push @new_value, ['URI',
2394     Message::DOM::DOMImplementation
2395     ->create_uri_reference ($value->[1])
2396     ->get_absolute_reference (${$value->[2]})
2397     ->get_uri_reference,
2398     $value->[2]];
2399     } else {
2400     push @new_value, $value;
2401     }
2402     } else {
2403     push @new_value, $value;
2404     }
2405     }
2406     return \@new_value;
2407     }
2408    
2409     return $specified_value;
2410     },
2411     };
2412     $Attr->{cursor} = $Prop->{cursor};
2413     $Key->{cursor} = $Prop->{cursor};
2414    
2415 wakaba 1.7 $Prop->{'border-style'} = {
2416     css => 'border-style',
2417     dom => 'border_style',
2418     parse => sub {
2419     my ($self, $prop_name, $tt, $t, $onerror) = @_;
2420    
2421     my %prop_value;
2422     if ($t->{type} == IDENT_TOKEN) {
2423     my $prop_value = lc $t->{value}; ## TODO: case folding
2424     $t = $tt->get_next_token;
2425     if ($border_style_keyword->{$prop_value} and
2426     $self->{prop_value}->{'border-top-style'}->{$prop_value}) {
2427     $prop_value{'border-top-style'} = ["KEYWORD", $prop_value];
2428     } elsif ($prop_value eq 'inherit') {
2429 wakaba 1.10 $prop_value{'border-top-style'} = ["INHERIT"];
2430 wakaba 1.19 $prop_value{'border-right-style'} = $prop_value{'border-top-style'};
2431     $prop_value{'border-bottom-style'} = $prop_value{'border-top-style'};
2432     $prop_value{'border-left-style'} = $prop_value{'border-right-style'};
2433     return ($t, \%prop_value);
2434 wakaba 1.7 } else {
2435     $onerror->(type => 'syntax error:keyword:'.$prop_name,
2436     level => $self->{must_level},
2437     token => $t);
2438     return ($t, undef);
2439     }
2440     $prop_value{'border-right-style'} = $prop_value{'border-top-style'};
2441     $prop_value{'border-bottom-style'} = $prop_value{'border-top-style'};
2442     $prop_value{'border-left-style'} = $prop_value{'border-right-style'};
2443     } else {
2444     $onerror->(type => 'syntax error:keyword:'.$prop_name,
2445     level => $self->{must_level},
2446     token => $t);
2447     return ($t, undef);
2448     }
2449    
2450     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
2451     if ($t->{type} == IDENT_TOKEN) {
2452     my $prop_value = lc $t->{value}; ## TODO: case folding
2453     $t = $tt->get_next_token;
2454 wakaba 1.19 if ($border_style_keyword->{$prop_value} and
2455 wakaba 1.7 $self->{prop_value}->{'border-right-style'}->{$prop_value}) {
2456     $prop_value{'border-right-style'} = ["KEYWORD", $prop_value];
2457     } else {
2458     $onerror->(type => 'syntax error:keyword:'.$prop_name,
2459     level => $self->{must_level},
2460     token => $t);
2461     return ($t, undef);
2462     }
2463     $prop_value{'border-left-style'} = $prop_value{'border-right-style'};
2464    
2465     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
2466     if ($t->{type} == IDENT_TOKEN) {
2467     my $prop_value = lc $t->{value}; ## TODO: case folding
2468     $t = $tt->get_next_token;
2469     if ($border_style_keyword->{$prop_value} and
2470     $self->{prop_value}->{'border-bottom-style'}->{$prop_value}) {
2471     $prop_value{'border-bottom-style'} = ["KEYWORD", $prop_value];
2472     } else {
2473     $onerror->(type => 'syntax error:keyword:'.$prop_name,
2474     level => $self->{must_level},
2475     token => $t);
2476     return ($t, undef);
2477     }
2478    
2479     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
2480     if ($t->{type} == IDENT_TOKEN) {
2481     my $prop_value = lc $t->{value}; ## TODO: case folding
2482     $t = $tt->get_next_token;
2483     if ($border_style_keyword->{$prop_value} and
2484     $self->{prop_value}->{'border-left-style'}->{$prop_value}) {
2485     $prop_value{'border-left-style'} = ["KEYWORD", $prop_value];
2486     } else {
2487     $onerror->(type => 'syntax error:keyword:'.$prop_name,
2488     level => $self->{must_level},
2489     token => $t);
2490     return ($t, undef);
2491     }
2492     }
2493     }
2494     }
2495    
2496     return ($t, \%prop_value);
2497     },
2498     serialize => sub {
2499     my ($self, $prop_name, $value) = @_;
2500    
2501     local $Error::Depth = $Error::Depth + 1;
2502     my @v;
2503     push @v, $self->border_top_style;
2504     return undef unless defined $v[-1];
2505     push @v, $self->border_right_style;
2506     return undef unless defined $v[-1];
2507     push @v, $self->border_bottom_style;
2508     return undef unless defined $v[-1];
2509 wakaba 1.19 push @v, $self->border_left_style;
2510 wakaba 1.7 return undef unless defined $v[-1];
2511    
2512     pop @v if $v[1] eq $v[3];
2513     pop @v if $v[0] eq $v[2];
2514     pop @v if $v[0] eq $v[1];
2515     return join ' ', @v;
2516     },
2517     };
2518     $Attr->{border_style} = $Prop->{'border-style'};
2519    
2520 wakaba 1.19 $Prop->{margin} = {
2521     css => 'margin',
2522     dom => 'margin',
2523     parse => sub {
2524     my ($self, $prop_name, $tt, $t, $onerror) = @_;
2525    
2526     my %prop_value;
2527    
2528     my $sign = 1;
2529     if ($t->{type} == MINUS_TOKEN) {
2530     $t = $tt->get_next_token;
2531     $sign = -1;
2532     }
2533    
2534     if ($t->{type} == DIMENSION_TOKEN) {
2535     my $value = $t->{number} * $sign;
2536     my $unit = lc $t->{value}; ## TODO: case
2537     $t = $tt->get_next_token;
2538     if ($length_unit->{$unit}) {
2539     $prop_value{'margin-top'} = ['DIMENSION', $value, $unit];
2540     } else {
2541     $onerror->(type => 'syntax error:'.$prop_name,
2542     level => $self->{must_level},
2543     token => $t);
2544     return ($t, undef);
2545     }
2546     } elsif ($t->{type} == PERCENTAGE_TOKEN) {
2547     my $value = $t->{number} * $sign;
2548     $t = $tt->get_next_token;
2549     $prop_value{'margin-top'} = ['PERCENTAGE', $value];
2550 wakaba 1.20 } elsif ($t->{type} == NUMBER_TOKEN and
2551 wakaba 1.19 ($self->{unitless_px} or $t->{number} == 0)) {
2552     my $value = $t->{number} * $sign;
2553     $t = $tt->get_next_token;
2554     $prop_value{'margin-top'} = ['DIMENSION', $value, 'px'];
2555     } elsif ($sign > 0 and $t->{type} == IDENT_TOKEN) {
2556     my $prop_value = lc $t->{value}; ## TODO: case folding
2557     $t = $tt->get_next_token;
2558     if ($prop_value eq 'auto') {
2559     $prop_value{'margin-top'} = ['KEYWORD', $prop_value];
2560     } elsif ($prop_value eq 'inherit') {
2561     $prop_value{'margin-top'} = ['INHERIT'];
2562     $prop_value{'margin-right'} = $prop_value{'margin-top'};
2563     $prop_value{'margin-bottom'} = $prop_value{'margin-top'};
2564     $prop_value{'margin-left'} = $prop_value{'margin-right'};
2565     return ($t, \%prop_value);
2566     } else {
2567     $onerror->(type => 'syntax error:'.$prop_name,
2568     level => $self->{must_level},
2569     token => $t);
2570     return ($t, undef);
2571     }
2572     } else {
2573     $onerror->(type => 'syntax error:'.$prop_name,
2574     level => $self->{must_level},
2575     token => $t);
2576     return ($t, undef);
2577     }
2578     $prop_value{'margin-right'} = $prop_value{'margin-top'};
2579     $prop_value{'margin-bottom'} = $prop_value{'margin-top'};
2580     $prop_value{'margin-left'} = $prop_value{'margin-right'};
2581    
2582     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
2583     $sign = 1;
2584     if ($t->{type} == MINUS_TOKEN) {
2585     $t = $tt->get_next_token;
2586     $sign = -1;
2587     }
2588    
2589     if ($t->{type} == DIMENSION_TOKEN) {
2590     my $value = $t->{number} * $sign;
2591     my $unit = lc $t->{value}; ## TODO: case
2592     $t = $tt->get_next_token;
2593     if ($length_unit->{$unit}) {
2594     $prop_value{'margin-right'} = ['DIMENSION', $value, $unit];
2595     } else {
2596     $onerror->(type => 'syntax error:'.$prop_name,
2597     level => $self->{must_level},
2598     token => $t);
2599     return ($t, undef);
2600     }
2601     } elsif ($t->{type} == PERCENTAGE_TOKEN) {
2602     my $value = $t->{number} * $sign;
2603     $t = $tt->get_next_token;
2604     $prop_value{'margin-right'} = ['PERCENTAGE', $value];
2605 wakaba 1.20 } elsif ($t->{type} == NUMBER_TOKEN and
2606 wakaba 1.19 ($self->{unitless_px} or $t->{number} == 0)) {
2607     my $value = $t->{number} * $sign;
2608     $t = $tt->get_next_token;
2609     $prop_value{'margin-right'} = ['DIMENSION', $value, 'px'];
2610     } elsif ($sign > 0 and $t->{type} == IDENT_TOKEN) {
2611     my $prop_value = lc $t->{value}; ## TODO: case folding
2612     $t = $tt->get_next_token;
2613     if ($prop_value eq 'auto') {
2614     $prop_value{'margin-right'} = ['KEYWORD', $prop_value];
2615     } else {
2616     $onerror->(type => 'syntax error:'.$prop_name,
2617     level => $self->{must_level},
2618     token => $t);
2619     return ($t, undef);
2620     }
2621     } else {
2622     return ($t, \%prop_value);
2623     }
2624     $prop_value{'margin-left'} = $prop_value{'margin-right'};
2625    
2626     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
2627     $sign = 1;
2628     if ($t->{type} == MINUS_TOKEN) {
2629     $t = $tt->get_next_token;
2630     $sign = -1;
2631     }
2632    
2633     if ($t->{type} == DIMENSION_TOKEN) {
2634     my $value = $t->{number} * $sign;
2635     my $unit = lc $t->{value}; ## TODO: case
2636     $t = $tt->get_next_token;
2637     if ($length_unit->{$unit}) {
2638     $prop_value{'margin-bottom'} = ['DIMENSION', $value, $unit];
2639     } else {
2640     $onerror->(type => 'syntax error:'.$prop_name,
2641     level => $self->{must_level},
2642     token => $t);
2643     return ($t, undef);
2644     }
2645     } elsif ($t->{type} == PERCENTAGE_TOKEN) {
2646     my $value = $t->{number} * $sign;
2647     $t = $tt->get_next_token;
2648     $prop_value{'margin-bottom'} = ['PERCENTAGE', $value];
2649 wakaba 1.20 } elsif ($t->{type} == NUMBER_TOKEN and
2650 wakaba 1.19 ($self->{unitless_px} or $t->{number} == 0)) {
2651     my $value = $t->{number} * $sign;
2652     $t = $tt->get_next_token;
2653     $prop_value{'margin-bottom'} = ['DIMENSION', $value, 'px'];
2654     } elsif ($sign > 0 and $t->{type} == IDENT_TOKEN) {
2655     my $prop_value = lc $t->{value}; ## TODO: case folding
2656     $t = $tt->get_next_token;
2657     if ($prop_value eq 'auto') {
2658     $prop_value{'margin-bottom'} = ['KEYWORD', $prop_value];
2659     } else {
2660     $onerror->(type => 'syntax error:'.$prop_name,
2661     level => $self->{must_level},
2662     token => $t);
2663     return ($t, undef);
2664     }
2665     } else {
2666     return ($t, \%prop_value);
2667     }
2668    
2669     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
2670     $sign = 1;
2671     if ($t->{type} == MINUS_TOKEN) {
2672     $t = $tt->get_next_token;
2673     $sign = -1;
2674     }
2675    
2676     if ($t->{type} == DIMENSION_TOKEN) {
2677     my $value = $t->{number} * $sign;
2678     my $unit = lc $t->{value}; ## TODO: case
2679     $t = $tt->get_next_token;
2680     if ($length_unit->{$unit}) {
2681     $prop_value{'margin-left'} = ['DIMENSION', $value, $unit];
2682     } else {
2683     $onerror->(type => 'syntax error:'.$prop_name,
2684     level => $self->{must_level},
2685     token => $t);
2686     return ($t, undef);
2687     }
2688     } elsif ($t->{type} == PERCENTAGE_TOKEN) {
2689     my $value = $t->{number} * $sign;
2690     $t = $tt->get_next_token;
2691     $prop_value{'margin-left'} = ['PERCENTAGE', $value];
2692 wakaba 1.20 } elsif ($t->{type} == NUMBER_TOKEN and
2693 wakaba 1.19 ($self->{unitless_px} or $t->{number} == 0)) {
2694     my $value = $t->{number} * $sign;
2695     $t = $tt->get_next_token;
2696     $prop_value{'margin-left'} = ['DIMENSION', $value, 'px'];
2697     } elsif ($sign > 0 and $t->{type} == IDENT_TOKEN) {
2698     my $prop_value = lc $t->{value}; ## TODO: case folding
2699     $t = $tt->get_next_token;
2700     if ($prop_value eq 'auto') {
2701     $prop_value{'margin-left'} = ['KEYWORD', $prop_value];
2702     } else {
2703     $onerror->(type => 'syntax error:'.$prop_name,
2704     level => $self->{must_level},
2705     token => $t);
2706     return ($t, undef);
2707     }
2708     } else {
2709     return ($t, \%prop_value);
2710     }
2711    
2712     return ($t, \%prop_value);
2713     },
2714     serialize => sub {
2715     my ($self, $prop_name, $value) = @_;
2716    
2717     local $Error::Depth = $Error::Depth + 1;
2718     my @v;
2719     push @v, $self->margin_top;
2720     return undef unless defined $v[-1];
2721     push @v, $self->margin_right;
2722     return undef unless defined $v[-1];
2723     push @v, $self->margin_bottom;
2724     return undef unless defined $v[-1];
2725     push @v, $self->margin_left;
2726     return undef unless defined $v[-1];
2727    
2728     pop @v if $v[1] eq $v[3];
2729     pop @v if $v[0] eq $v[2];
2730     pop @v if $v[0] eq $v[1];
2731     return join ' ', @v;
2732     },
2733     };
2734     $Attr->{margin} = $Prop->{margin};
2735    
2736     $Prop->{padding} = {
2737     css => 'padding',
2738     dom => 'padding',
2739     parse => sub {
2740     my ($self, $prop_name, $tt, $t, $onerror) = @_;
2741    
2742     my %prop_value;
2743    
2744     my $sign = 1;
2745     if ($t->{type} == MINUS_TOKEN) {
2746     $t = $tt->get_next_token;
2747     $sign = -1;
2748     }
2749    
2750     if ($t->{type} == DIMENSION_TOKEN) {
2751     my $value = $t->{number} * $sign;
2752     my $unit = lc $t->{value}; ## TODO: case
2753     $t = $tt->get_next_token;
2754     if ($length_unit->{$unit} and $value >= 0) {
2755     $prop_value{'padding-top'} = ['DIMENSION', $value, $unit];
2756     } else {
2757     $onerror->(type => 'syntax error:'.$prop_name,
2758     level => $self->{must_level},
2759     token => $t);
2760     return ($t, undef);
2761     }
2762     } elsif ($t->{type} == PERCENTAGE_TOKEN) {
2763     my $value = $t->{number} * $sign;
2764     $t = $tt->get_next_token;
2765     $prop_value{'padding-top'} = ['PERCENTAGE', $value];
2766     unless ($value >= 0) {
2767     $onerror->(type => 'syntax error:'.$prop_name,
2768     level => $self->{must_level},
2769     token => $t);
2770     return ($t, undef);
2771     }
2772 wakaba 1.20 } elsif ($t->{type} == NUMBER_TOKEN and
2773 wakaba 1.19 ($self->{unitless_px} or $t->{number} == 0)) {
2774     my $value = $t->{number} * $sign;
2775     $t = $tt->get_next_token;
2776     $prop_value{'padding-top'} = ['DIMENSION', $value, 'px'];
2777     unless ($value >= 0) {
2778     $onerror->(type => 'syntax error:'.$prop_name,
2779     level => $self->{must_level},
2780     token => $t);
2781     return ($t, undef);
2782     }
2783     } elsif ($sign > 0 and $t->{type} == IDENT_TOKEN) {
2784     my $prop_value = lc $t->{value}; ## TODO: case folding
2785     $t = $tt->get_next_token;
2786     if ($prop_value eq 'inherit') {
2787     $prop_value{'padding-top'} = ['INHERIT'];
2788     $prop_value{'padding-right'} = $prop_value{'padding-top'};
2789     $prop_value{'padding-bottom'} = $prop_value{'padding-top'};
2790     $prop_value{'padding-left'} = $prop_value{'padding-right'};
2791     return ($t, \%prop_value);
2792     } else {
2793     $onerror->(type => 'syntax error:'.$prop_name,
2794     level => $self->{must_level},
2795     token => $t);
2796     return ($t, undef);
2797     }
2798     } else {
2799     $onerror->(type => 'syntax error:'.$prop_name,
2800     level => $self->{must_level},
2801     token => $t);
2802     return ($t, undef);
2803     }
2804     $prop_value{'padding-right'} = $prop_value{'padding-top'};
2805     $prop_value{'padding-bottom'} = $prop_value{'padding-top'};
2806     $prop_value{'padding-left'} = $prop_value{'padding-right'};
2807    
2808     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
2809     $sign = 1;
2810     if ($t->{type} == MINUS_TOKEN) {
2811     $t = $tt->get_next_token;
2812     $sign = -1;
2813     }
2814    
2815     if ($t->{type} == DIMENSION_TOKEN) {
2816     my $value = $t->{number} * $sign;
2817     my $unit = lc $t->{value}; ## TODO: case
2818     $t = $tt->get_next_token;
2819     if ($length_unit->{$unit} and $value >= 0) {
2820     $prop_value{'padding-right'} = ['DIMENSION', $value, $unit];
2821     } else {
2822     $onerror->(type => 'syntax error:'.$prop_name,
2823     level => $self->{must_level},
2824     token => $t);
2825     return ($t, undef);
2826     }
2827     } elsif ($t->{type} == PERCENTAGE_TOKEN) {
2828     my $value = $t->{number} * $sign;
2829     $t = $tt->get_next_token;
2830     $prop_value{'padding-right'} = ['PERCENTAGE', $value];
2831     unless ($value >= 0) {
2832     $onerror->(type => 'syntax error:'.$prop_name,
2833     level => $self->{must_level},
2834     token => $t);
2835     return ($t, undef);
2836     }
2837 wakaba 1.20 } elsif ($t->{type} == NUMBER_TOKEN and
2838 wakaba 1.19 ($self->{unitless_px} or $t->{number} == 0)) {
2839     my $value = $t->{number} * $sign;
2840     $t = $tt->get_next_token;
2841     $prop_value{'padding-right'} = ['DIMENSION', $value, 'px'];
2842     unless ($value >= 0) {
2843     $onerror->(type => 'syntax error:'.$prop_name,
2844     level => $self->{must_level},
2845     token => $t);
2846     return ($t, undef);
2847     }
2848     } else {
2849     return ($t, \%prop_value);
2850     }
2851     $prop_value{'padding-left'} = $prop_value{'padding-right'};
2852    
2853     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
2854     $sign = 1;
2855     if ($t->{type} == MINUS_TOKEN) {
2856     $t = $tt->get_next_token;
2857     $sign = -1;
2858     }
2859    
2860     if ($t->{type} == DIMENSION_TOKEN) {
2861     my $value = $t->{number} * $sign;
2862     my $unit = lc $t->{value}; ## TODO: case
2863     $t = $tt->get_next_token;
2864     if ($length_unit->{$unit} and $value >= 0) {
2865     $prop_value{'padding-bottom'} = ['DIMENSION', $value, $unit];
2866     } else {
2867     $onerror->(type => 'syntax error:'.$prop_name,
2868     level => $self->{must_level},
2869     token => $t);
2870     return ($t, undef);
2871     }
2872     } elsif ($t->{type} == PERCENTAGE_TOKEN) {
2873     my $value = $t->{number} * $sign;
2874     $t = $tt->get_next_token;
2875     $prop_value{'padding-bottom'} = ['PERCENTAGE', $value];
2876     unless ($value >= 0) {
2877     $onerror->(type => 'syntax error:'.$prop_name,
2878     level => $self->{must_level},
2879     token => $t);
2880     return ($t, undef);
2881     }
2882 wakaba 1.20 } elsif ($t->{type} == NUMBER_TOKEN and
2883 wakaba 1.19 ($self->{unitless_px} or $t->{number} == 0)) {
2884     my $value = $t->{number} * $sign;
2885     $t = $tt->get_next_token;
2886     $prop_value{'padding-bottom'} = ['DIMENSION', $value, 'px'];
2887     unless ($value >= 0) {
2888     $onerror->(type => 'syntax error:'.$prop_name,
2889     level => $self->{must_level},
2890     token => $t);
2891     return ($t, undef);
2892     }
2893     } else {
2894     return ($t, \%prop_value);
2895     }
2896    
2897     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
2898     $sign = 1;
2899     if ($t->{type} == MINUS_TOKEN) {
2900     $t = $tt->get_next_token;
2901     $sign = -1;
2902     }
2903    
2904     if ($t->{type} == DIMENSION_TOKEN) {
2905     my $value = $t->{number} * $sign;
2906     my $unit = lc $t->{value}; ## TODO: case
2907     $t = $tt->get_next_token;
2908     if ($length_unit->{$unit} and $value >= 0) {
2909     $prop_value{'padding-left'} = ['DIMENSION', $value, $unit];
2910     } else {
2911     $onerror->(type => 'syntax error:'.$prop_name,
2912     level => $self->{must_level},
2913     token => $t);
2914     return ($t, undef);
2915     }
2916     } elsif ($t->{type} == PERCENTAGE_TOKEN) {
2917     my $value = $t->{number} * $sign;
2918     $t = $tt->get_next_token;
2919     $prop_value{'padding-left'} = ['PERCENTAGE', $value];
2920     unless ($value >= 0) {
2921     $onerror->(type => 'syntax error:'.$prop_name,
2922     level => $self->{must_level},
2923     token => $t);
2924     return ($t, undef);
2925     }
2926 wakaba 1.20 } elsif ($t->{type} == NUMBER_TOKEN and
2927 wakaba 1.19 ($self->{unitless_px} or $t->{number} == 0)) {
2928     my $value = $t->{number} * $sign;
2929     $t = $tt->get_next_token;
2930     $prop_value{'padding-left'} = ['DIMENSION', $value, 'px'];
2931     unless ($value >= 0) {
2932     $onerror->(type => 'syntax error:'.$prop_name,
2933     level => $self->{must_level},
2934     token => $t);
2935     return ($t, undef);
2936     }
2937     } else {
2938     return ($t, \%prop_value);
2939     }
2940    
2941     return ($t, \%prop_value);
2942     },
2943     serialize => sub {
2944     my ($self, $prop_name, $value) = @_;
2945    
2946     local $Error::Depth = $Error::Depth + 1;
2947     my @v;
2948     push @v, $self->padding_top;
2949     return undef unless defined $v[-1];
2950     push @v, $self->padding_right;
2951     return undef unless defined $v[-1];
2952     push @v, $self->padding_bottom;
2953     return undef unless defined $v[-1];
2954     push @v, $self->padding_left;
2955     return undef unless defined $v[-1];
2956    
2957     pop @v if $v[1] eq $v[3];
2958     pop @v if $v[0] eq $v[2];
2959     pop @v if $v[0] eq $v[1];
2960     return join ' ', @v;
2961     },
2962     };
2963     $Attr->{padding} = $Prop->{padding};
2964    
2965 wakaba 1.20 $Prop->{'border-width'} = {
2966     css => 'border-width',
2967     dom => 'border_width',
2968     parse => sub {
2969     my ($self, $prop_name, $tt, $t, $onerror) = @_;
2970    
2971     my %prop_value;
2972    
2973     my $sign = 1;
2974     if ($t->{type} == MINUS_TOKEN) {
2975     $t = $tt->get_next_token;
2976     $sign = -1;
2977     }
2978    
2979     if ($t->{type} == DIMENSION_TOKEN) {
2980     my $value = $t->{number} * $sign;
2981     my $unit = lc $t->{value}; ## TODO: case
2982     $t = $tt->get_next_token;
2983     if ($length_unit->{$unit} and $value >= 0) {
2984     $prop_value{'border-top-width'} = ['DIMENSION', $value, $unit];
2985     } else {
2986     $onerror->(type => 'syntax error:'.$prop_name,
2987     level => $self->{must_level},
2988     token => $t);
2989     return ($t, undef);
2990     }
2991     } elsif ($t->{type} == NUMBER_TOKEN and
2992     ($self->{unitless_px} or $t->{number} == 0)) {
2993     my $value = $t->{number} * $sign;
2994     $t = $tt->get_next_token;
2995     $prop_value{'border-top-width'} = ['DIMENSION', $value, 'px'];
2996     unless ($value >= 0) {
2997     $onerror->(type => 'syntax error:'.$prop_name,
2998     level => $self->{must_level},
2999     token => $t);
3000     return ($t, undef);
3001     }
3002     } elsif ($sign > 0 and $t->{type} == IDENT_TOKEN) {
3003     my $prop_value = lc $t->{value}; ## TODO: case folding
3004     $t = $tt->get_next_token;
3005     if ({thin => 1, medium => 1, thick => 1}->{$prop_value}) {
3006     $prop_value{'border-top-width'} = ['KEYWORD', $prop_value];
3007     } elsif ($prop_value eq 'inherit') {
3008     $prop_value{'border-top-width'} = ['INHERIT'];
3009     $prop_value{'border-right-width'} = $prop_value{'border-top-width'};
3010     $prop_value{'border-bottom-width'} = $prop_value{'border-top-width'};
3011     $prop_value{'border-left-width'} = $prop_value{'border-right-width'};
3012     return ($t, \%prop_value);
3013     } else {
3014     $onerror->(type => 'syntax error:'.$prop_name,
3015     level => $self->{must_level},
3016     token => $t);
3017     return ($t, undef);
3018     }
3019     } else {
3020     $onerror->(type => 'syntax error:'.$prop_name,
3021     level => $self->{must_level},
3022     token => $t);
3023     return ($t, undef);
3024     }
3025     $prop_value{'border-right-width'} = $prop_value{'border-top-width'};
3026     $prop_value{'border-bottom-width'} = $prop_value{'border-top-width'};
3027     $prop_value{'border-left-width'} = $prop_value{'border-right-width'};
3028    
3029     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
3030     $sign = 1;
3031     if ($t->{type} == MINUS_TOKEN) {
3032     $t = $tt->get_next_token;
3033     $sign = -1;
3034     }
3035    
3036     if ($t->{type} == DIMENSION_TOKEN) {
3037     my $value = $t->{number} * $sign;
3038     my $unit = lc $t->{value}; ## TODO: case
3039     $t = $tt->get_next_token;
3040     if ($length_unit->{$unit} and $value >= 0) {
3041     $prop_value{'border-right-width'} = ['DIMENSION', $value, $unit];
3042     } else {
3043     $onerror->(type => 'syntax error:'.$prop_name,
3044     level => $self->{must_level},
3045     token => $t);
3046     return ($t, undef);
3047     }
3048     } elsif ($t->{type} == NUMBER_TOKEN and
3049     ($self->{unitless_px} or $t->{number} == 0)) {
3050     my $value = $t->{number} * $sign;
3051     $t = $tt->get_next_token;
3052     $prop_value{'border-right-width'} = ['DIMENSION', $value, 'px'];
3053     unless ($value >= 0) {
3054     $onerror->(type => 'syntax error:'.$prop_name,
3055     level => $self->{must_level},
3056     token => $t);
3057     return ($t, undef);
3058     }
3059     } elsif ($sign > 0 and $t->{type} == IDENT_TOKEN) {
3060     my $prop_value = lc $t->{value}; ## TODO: case
3061     if ({thin => 1, medium => 1, thick => 1}->{$prop_value}) {
3062     $prop_value{'border-right-width'} = ['KEYWORD', $prop_value];
3063     $t = $tt->get_next_token;
3064     }
3065     } else {
3066     return ($t, \%prop_value);
3067     }
3068     $prop_value{'border-left-width'} = $prop_value{'border-right-width'};
3069    
3070     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
3071     $sign = 1;
3072     if ($t->{type} == MINUS_TOKEN) {
3073     $t = $tt->get_next_token;
3074     $sign = -1;
3075     }
3076    
3077     if ($t->{type} == DIMENSION_TOKEN) {
3078     my $value = $t->{number} * $sign;
3079     my $unit = lc $t->{value}; ## TODO: case
3080     $t = $tt->get_next_token;
3081     if ($length_unit->{$unit} and $value >= 0) {
3082     $prop_value{'border-bottom-width'} = ['DIMENSION', $value, $unit];
3083     } else {
3084     $onerror->(type => 'syntax error:'.$prop_name,
3085     level => $self->{must_level},
3086     token => $t);
3087     return ($t, undef);
3088     }
3089     } elsif ($t->{type} == NUMBER_TOKEN and
3090     ($self->{unitless_px} or $t->{number} == 0)) {
3091     my $value = $t->{number} * $sign;
3092     $t = $tt->get_next_token;
3093     $prop_value{'border-bottom-width'} = ['DIMENSION', $value, 'px'];
3094     unless ($value >= 0) {
3095     $onerror->(type => 'syntax error:'.$prop_name,
3096     level => $self->{must_level},
3097     token => $t);
3098     return ($t, undef);
3099     }
3100     } elsif ($sign > 0 and $t->{type} == IDENT_TOKEN) {
3101     my $prop_value = lc $t->{value}; ## TODO: case
3102     if ({thin => 1, medium => 1, thick => 1}->{$prop_value}) {
3103     $prop_value{'border-bottom-width'} = ['KEYWORD', $prop_value];
3104     $t = $tt->get_next_token;
3105     }
3106     } else {
3107     return ($t, \%prop_value);
3108     }
3109    
3110     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
3111     $sign = 1;
3112     if ($t->{type} == MINUS_TOKEN) {
3113     $t = $tt->get_next_token;
3114     $sign = -1;
3115     }
3116    
3117     if ($t->{type} == DIMENSION_TOKEN) {
3118     my $value = $t->{number} * $sign;
3119     my $unit = lc $t->{value}; ## TODO: case
3120     $t = $tt->get_next_token;
3121     if ($length_unit->{$unit} and $value >= 0) {
3122     $prop_value{'border-left-width'} = ['DIMENSION', $value, $unit];
3123     } else {
3124     $onerror->(type => 'syntax error:'.$prop_name,
3125     level => $self->{must_level},
3126     token => $t);
3127     return ($t, undef);
3128     }
3129     } elsif ($t->{type} == NUMBER_TOKEN and
3130     ($self->{unitless_px} or $t->{number} == 0)) {
3131     my $value = $t->{number} * $sign;
3132     $t = $tt->get_next_token;
3133     $prop_value{'border-left-width'} = ['DIMENSION', $value, 'px'];
3134     unless ($value >= 0) {
3135     $onerror->(type => 'syntax error:'.$prop_name,
3136     level => $self->{must_level},
3137     token => $t);
3138     return ($t, undef);
3139     }
3140     } elsif ($sign > 0 and $t->{type} == IDENT_TOKEN) {
3141     my $prop_value = lc $t->{value}; ## TODO: case
3142     if ({thin => 1, medium => 1, thick => 1}->{$prop_value}) {
3143     $prop_value{'border-left-width'} = ['KEYWORD', $prop_value];
3144     $t = $tt->get_next_token;
3145     }
3146     } else {
3147     return ($t, \%prop_value);
3148     }
3149    
3150     return ($t, \%prop_value);
3151     },
3152     serialize => sub {
3153     my ($self, $prop_name, $value) = @_;
3154    
3155     local $Error::Depth = $Error::Depth + 1;
3156     my @v;
3157     push @v, $self->padding_top;
3158     return undef unless defined $v[-1];
3159     push @v, $self->padding_right;
3160     return undef unless defined $v[-1];
3161     push @v, $self->padding_bottom;
3162     return undef unless defined $v[-1];
3163     push @v, $self->padding_left;
3164     return undef unless defined $v[-1];
3165    
3166     pop @v if $v[1] eq $v[3];
3167     pop @v if $v[0] eq $v[2];
3168     pop @v if $v[0] eq $v[1];
3169     return join ' ', @v;
3170     },
3171     };
3172     $Attr->{padding} = $Prop->{padding};
3173    
3174 wakaba 1.12 $Prop->{'list-style'} = {
3175     css => 'list-style',
3176     dom => 'list_style',
3177     parse => sub {
3178     my ($self, $prop_name, $tt, $t, $onerror) = @_;
3179    
3180     my %prop_value;
3181     my $none = 0;
3182    
3183     F: for my $f (1..3) {
3184     if ($t->{type} == IDENT_TOKEN) {
3185     my $prop_value = lc $t->{value}; ## TODO: case folding
3186     $t = $tt->get_next_token;
3187    
3188     if ($prop_value eq 'none') {
3189     $none++;
3190     } elsif ($Prop->{'list-style-type'}->{keyword}->{$prop_value}) {
3191     if (exists $prop_value{'list-style-type'}) {
3192     $onerror->(type => q[syntax error:duplicate:'list-style-type':].
3193     $prop_name,
3194     level => $self->{must_level},
3195     token => $t);
3196     return ($t, undef);
3197     } else {
3198     $prop_value{'list-style-type'} = ['KEYWORD', $prop_value];
3199     }
3200     } elsif ($Prop->{'list-style-position'}->{keyword}->{$prop_value}) {
3201     if (exists $prop_value{'list-style-position'}) {
3202     $onerror->(type => q[syntax error:duplicate:'list-style-position':].
3203     $prop_name,
3204     level => $self->{must_level},
3205     token => $t);
3206     return ($t, undef);
3207     }
3208    
3209     $prop_value{'list-style-position'} = ['KEYWORD', $prop_value];
3210     } elsif ($f == 1 and $prop_value eq 'inherit') {
3211     $prop_value{'list-style-type'} = ["INHERIT"];
3212     $prop_value{'list-style-position'} = ["INHERIT"];
3213     $prop_value{'list-style-image'} = ["INHERIT"];
3214     last F;
3215     } else {
3216     if ($f == 1) {
3217     $onerror->(type => 'syntax error:'.$prop_name,
3218     level => $self->{must_level},
3219     token => $t);
3220     return ($t, undef);
3221     } else {
3222     last F;
3223     }
3224     }
3225     } elsif ($t->{type} == URI_TOKEN) {
3226     if (exists $prop_value{'list-style-image'}) {
3227     $onerror->(type => q[syntax error:duplicate:'list-style-image':].
3228     $prop_name,
3229     level => $self->{must_level},
3230     token => $t);
3231     return ($t, undef);
3232     }
3233    
3234     $prop_value{'list-style-image'}
3235 wakaba 1.13 = ['URI', $t->{value}, \($self->{base_uri})];
3236 wakaba 1.12 $t = $tt->get_next_token;
3237     } else {
3238     if ($f == 1) {
3239     $onerror->(type => 'syntax error:keyword:'.$prop_name,
3240     level => $self->{must_level},
3241     token => $t);
3242     return ($t, undef);
3243     } else {
3244     last F;
3245     }
3246     }
3247    
3248     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
3249     } # F
3250     ## NOTE: No browser support |list-style: url(xxx|{EOF}.
3251    
3252     if ($none == 1) {
3253     if (exists $prop_value{'list-style-type'}) {
3254     if (exists $prop_value{'list-style-image'}) {
3255     $onerror->(type => q[syntax error:duplicate:'list-style-image':].
3256     $prop_name,
3257     level => $self->{must_level},
3258     token => $t);
3259     return ($t, undef);
3260     } else {
3261     $prop_value{'list-style-image'} = ['KEYWORD', 'none'];
3262     }
3263     } else {
3264     $prop_value{'list-style-type'} = ['KEYWORD', 'none'];
3265     $prop_value{'list-style-image'} = ['KEYWORD', 'none']
3266     unless exists $prop_value{'list-style-image'};
3267     }
3268     } elsif ($none == 2) {
3269     if (exists $prop_value{'list-style-type'}) {
3270     $onerror->(type => q[syntax error:duplicate:'list-style-type':].
3271     $prop_name,
3272     level => $self->{must_level},
3273     token => $t);
3274     return ($t, undef);
3275     }
3276     if (exists $prop_value{'list-style-image'}) {
3277     $onerror->(type => q[syntax error:duplicate:'list-style-image':].
3278     $prop_name,
3279     level => $self->{must_level},
3280     token => $t);
3281     return ($t, undef);
3282     }
3283    
3284     $prop_value{'list-style-type'} = ['KEYWORD', 'none'];
3285     $prop_value{'list-style-image'} = ['KEYWORD', 'none'];
3286     } elsif ($none == 3) {
3287     $onerror->(type => q[syntax error:duplicate:'list-style-type':].
3288     $prop_name,
3289     level => $self->{must_level},
3290     token => $t);
3291     return ($t, undef);
3292     }
3293    
3294     for (qw/list-style-type list-style-position list-style-image/) {
3295     $prop_value{$_} = $Prop->{$_}->{initial} unless exists $prop_value{$_};
3296     }
3297    
3298     return ($t, \%prop_value);
3299     },
3300     serialize => sub {
3301     my ($self, $prop_name, $value) = @_;
3302    
3303     local $Error::Depth = $Error::Depth + 1;
3304     return $self->list_style_type . ' ' . $self->list_style_position .
3305     ' ' . $self->list_style_image;
3306     },
3307     };
3308     $Attr->{list_style} = $Prop->{'list-style'};
3309    
3310 wakaba 1.16 ## NOTE: Future version of the implementation will change the way to
3311     ## store the parsed value to support CSS 3 properties.
3312     $Prop->{'text-decoration'} = {
3313     css => 'text-decoration',
3314     dom => 'text_decoration',
3315     key => 'text_decoration',
3316     parse => sub {
3317     my ($self, $prop_name, $tt, $t, $onerror) = @_;
3318    
3319     my $value = ['DECORATION']; # , underline, overline, line-through, blink
3320    
3321     if ($t->{type} == IDENT_TOKEN) {
3322     my $v = lc $t->{value}; ## TODO: case
3323     $t = $tt->get_next_token;
3324     if ($v eq 'inherit') {
3325     return ($t, {$prop_name => ['INHERIT']});
3326     } elsif ($v eq 'none') {
3327     return ($t, {$prop_name => $value});
3328     } elsif ($v eq 'underline' and
3329     $self->{prop_value}->{$prop_name}->{$v}) {
3330     $value->[1] = 1;
3331     } elsif ($v eq 'overline' and
3332     $self->{prop_value}->{$prop_name}->{$v}) {
3333     $value->[2] = 1;
3334     } elsif ($v eq 'line-through' and
3335     $self->{prop_value}->{$prop_name}->{$v}) {
3336     $value->[3] = 1;
3337     } elsif ($v eq 'blink' and
3338     $self->{prop_value}->{$prop_name}->{$v}) {
3339     $value->[4] = 1;
3340     } else {
3341     $onerror->(type => 'syntax error:'.$prop_name,
3342     level => $self->{must_level},
3343     token => $t);
3344     return ($t, undef);
3345     }
3346     }
3347    
3348     F: {
3349     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
3350     last F unless $t->{type} == IDENT_TOKEN;
3351    
3352     my $v = lc $t->{value}; ## TODO: case
3353     $t = $tt->get_next_token;
3354     if ($v eq 'underline' and
3355     $self->{prop_value}->{$prop_name}->{$v}) {
3356     $value->[1] = 1;
3357     } elsif ($v eq 'overline' and
3358     $self->{prop_value}->{$prop_name}->{$v}) {
3359     $value->[1] = 2;
3360     } elsif ($v eq 'line-through' and
3361     $self->{prop_value}->{$prop_name}->{$v}) {
3362     $value->[1] = 3;
3363     } elsif ($v eq 'blink' and
3364     $self->{prop_value}->{$prop_name}->{$v}) {
3365     $value->[1] = 4;
3366     } else {
3367     last F;
3368     }
3369    
3370     redo F;
3371     } # F
3372    
3373     return ($t, {$prop_name => $value});
3374     },
3375     serialize => $default_serializer,
3376     initial => ["KEYWORD", "none"],
3377     #inherited => 0,
3378     compute => $compute_as_specified,
3379     };
3380     $Attr->{text_decoration} = $Prop->{'text-decoration'};
3381     $Key->{text_decoration} = $Prop->{'text-decoration'};
3382    
3383 wakaba 1.1 1;
3384 wakaba 1.23 ## $Date: 2008/01/04 05:36:36 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24