/[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.50 - (hide annotations) (download)
Sat Jan 26 14:48:09 2008 UTC (17 years, 5 months ago) by wakaba
Branch: MAIN
Changes since 1.49: +10 -3 lines
++ whatpm/t/ChangeLog	26 Jan 2008 14:46:58 -0000
	* css-font.dat: New test data for 'font-weight'
	and 'font-size' are added.

	* css-visual.dat: New test data for leading and
	trailing zeros are added.

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

++ whatpm/Whatpm/CSS/ChangeLog	26 Jan 2008 14:46:14 -0000
	* Parser.pm ('font-weight' parser): Support for '+'.

	* Tokenizer.pm: Normalize number stored in |NUMBER_TOKEN|,
	|PERCENTAGE_TOKEN|, and |DIMENSION_TOKEN|.

2008-01-26  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.40 my $self = bless {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.28 # $self->{hashless_rgb} = 1/0
13    
14 wakaba 1.40 ## Default error handler
15     $self->{onerror} = sub {
16     my %opt = @_;
17     require Carp;
18     Carp::carp
19     (sprintf 'Document <%s>: Line %d column %d (token %s): %s%s',
20     defined $opt{uri} ? ${$opt{uri}} : 'thisdocument:/',
21     $opt{token}->{line},
22     $opt{token}->{column},
23     Whatpm::CSS::Tokenizer->serialize_token ($opt{token}),
24     $opt{type},
25     defined $opt{value} ? " (value $opt{value})" : '');
26     };
27    
28 wakaba 1.28 ## Media-dependent RGB color range clipper
29     $self->{clip_color} = sub {
30     shift; #my $self = shift;
31     my $value = shift;
32     if (defined $value and $value->[0] eq 'RGBA') {
33     my ($r, $g, $b) = @$value[1, 2, 3];
34     $r = 0 if $r < 0; $r = 255 if $r > 255;
35     $g = 0 if $g < 0; $g = 255 if $g > 255;
36     $b = 0 if $b < 0; $b = 255 if $b > 255;
37     return ['RGBA', $r, $g, $b, $value->[4]];
38     }
39     return $value;
40     };
41 wakaba 1.1
42 wakaba 1.31 ## System dependent font expander
43     $self->{get_system_font} = sub {
44     #my ($self, $normalized_system_font_name, $font_properties) = @_;
45     ## Modify $font_properties hash (except for 'font-family' property).
46     return $_[2];
47     };
48    
49 wakaba 1.1 return $self;
50     } # new
51    
52     sub BEFORE_STATEMENT_STATE () { 0 }
53     sub BEFORE_DECLARATION_STATE () { 1 }
54     sub IGNORED_STATEMENT_STATE () { 2 }
55     sub IGNORED_DECLARATION_STATE () { 3 }
56    
57 wakaba 1.5 our $Prop; ## By CSS property name
58     our $Attr; ## By CSSOM attribute name
59     our $Key; ## By internal key
60    
61 wakaba 1.1 sub parse_char_string ($$) {
62     my $self = $_[0];
63    
64     my $s = $_[1];
65     pos ($s) = 0;
66 wakaba 1.2 my $line = 1;
67     my $column = 0;
68 wakaba 1.1
69     my $tt = Whatpm::CSS::Tokenizer->new;
70 wakaba 1.38 my $onerror = $tt->{onerror} = $self->{onerror};
71 wakaba 1.37 $tt->{get_char} = sub ($) {
72 wakaba 1.1 if (pos $s < length $s) {
73 wakaba 1.2 my $c = ord substr $s, pos ($s)++, 1;
74     if ($c == 0x000A) {
75     $line++;
76     $column = 0;
77     } elsif ($c == 0x000D) {
78     unless (substr ($s, pos ($s), 1) eq "\x0A") {
79     $line++;
80     $column = 0;
81     } else {
82     $column++;
83     }
84     } else {
85     $column++;
86     }
87 wakaba 1.47 $_[0]->{line} = $line;
88     $_[0]->{column} = $column;
89 wakaba 1.2 return $c;
90 wakaba 1.1 } else {
91 wakaba 1.47 $_[0]->{column} = $column + 1; ## Set the same number always.
92 wakaba 1.1 return -1;
93     }
94     }; # $tt->{get_char}
95     $tt->init;
96    
97     my $sp = Whatpm::CSS::SelectorsParser->new;
98 wakaba 1.38 $sp->{onerror} = $self->{onerror};
99 wakaba 1.1 $sp->{must_level} = $self->{must_level};
100 wakaba 1.2 $sp->{pseudo_element} = $self->{pseudo_element};
101     $sp->{pseudo_class} = $self->{pseudo_class};
102 wakaba 1.1
103 wakaba 1.33 my $nsmap = {prefix_to_uri => {}, uri_to_prefixes => {}};
104     # $nsmap->{prefix_to_uri}->{p/""} = uri/undef
105     # $nsmap->{uri_to_prefixes}->{uri} = ["p|"/"",...]/undef
106     # $nsmap->{has_namespace} = 1/0
107 wakaba 1.4 $sp->{lookup_namespace_uri} = sub {
108 wakaba 1.33 return $nsmap->{prefix_to_uri}->{$_[0]}; # $_[0] is '' (default) or prefix
109 wakaba 1.4 }; # $sp->{lookup_namespace_uri}
110 wakaba 1.1
111     require Message::DOM::CSSStyleSheet;
112     require Message::DOM::CSSRule;
113     require Message::DOM::CSSStyleDeclaration;
114    
115 wakaba 1.11 $self->{base_uri} = $self->{href} unless defined $self->{base_uri};
116 wakaba 1.38 $sp->{href} = $self->{href};
117 wakaba 1.11
118 wakaba 1.1 my $state = BEFORE_STATEMENT_STATE;
119     my $t = $tt->get_next_token;
120    
121     my $open_rules = [[]];
122     my $current_rules = $open_rules->[-1];
123     my $current_decls;
124     my $closing_tokens = [];
125 wakaba 1.3 my $charset_allowed = 1;
126 wakaba 1.4 my $namespace_allowed = 1;
127 wakaba 1.1
128     S: {
129     if ($state == BEFORE_STATEMENT_STATE) {
130     $t = $tt->get_next_token
131     while $t->{type} == S_TOKEN or
132     $t->{type} == CDO_TOKEN or
133     $t->{type} == CDC_TOKEN;
134    
135     if ($t->{type} == ATKEYWORD_TOKEN) {
136 wakaba 1.5 if (lc $t->{value} eq 'namespace') { ## TODO: case folding
137 wakaba 1.4 $t = $tt->get_next_token;
138     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
139    
140     my $prefix;
141     if ($t->{type} == IDENT_TOKEN) {
142     $prefix = lc $t->{value};
143 wakaba 1.33 ## TODO: case (Unicode lowercase)
144 wakaba 1.4
145     $t = $tt->get_next_token;
146     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
147     }
148    
149     if ($t->{type} == STRING_TOKEN or $t->{type} == URI_TOKEN) {
150     my $uri = $t->{value};
151    
152     $t = $tt->get_next_token;
153     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
154    
155     ## ISSUE: On handling of empty namespace URI, Firefox 2 and
156     ## Opera 9 work differently (See SuikaWiki:namespace).
157     ## TODO: We need to check what we do once it is specced.
158    
159     if ($t->{type} == SEMICOLON_TOKEN) {
160     if ($namespace_allowed) {
161 wakaba 1.33 my $p = $prefix;
162     $nsmap->{has_namespace} = 1;
163     if (defined $prefix) {
164     $nsmap->{prefix_to_uri}->{$prefix} = $uri;
165     $p .= '|';
166     } else {
167     $nsmap->{prefix_to_uri}->{''} = $uri;
168     $p = '';
169     }
170     for my $u (keys %{$nsmap->{uri_to_prefixes}}) {
171     next if $u eq $uri;
172     my $list = $nsmap->{uri_to_prefixes}->{$u};
173     next unless $list;
174     for (reverse 0..$#$list) {
175     splice @$list, $_, 1, () if $list->[$_] eq $p;
176     }
177     }
178     push @{$nsmap->{uri_to_prefixes}->{$uri} ||= []}, $p;
179 wakaba 1.4 push @$current_rules,
180     Message::DOM::CSSNamespaceRule->____new ($prefix, $uri);
181     undef $charset_allowed;
182     } else {
183 wakaba 1.39 $onerror->(type => 'at-rule not allowed:namespace',
184 wakaba 1.4 level => $self->{must_level},
185 wakaba 1.38 uri => \$self->{href},
186 wakaba 1.4 token => $t);
187     }
188    
189     $t = $tt->get_next_token;
190     ## Stay in the state.
191     redo S;
192     } else {
193     #
194     }
195     } else {
196     #
197     }
198    
199 wakaba 1.39 $onerror->(type => 'at-rule syntax error:namespace',
200 wakaba 1.4 level => $self->{must_level},
201 wakaba 1.38 uri => \$self->{href},
202 wakaba 1.4 token => $t);
203     #
204 wakaba 1.5 } elsif (lc $t->{value} eq 'charset') { ## TODO: case folding
205 wakaba 1.3 $t = $tt->get_next_token;
206     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
207    
208     if ($t->{type} == STRING_TOKEN) {
209     my $encoding = $t->{value};
210    
211     $t = $tt->get_next_token;
212     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
213    
214     if ($t->{type} == SEMICOLON_TOKEN) {
215     if ($charset_allowed) {
216     push @$current_rules,
217     Message::DOM::CSSCharsetRule->____new ($encoding);
218     undef $charset_allowed;
219     } else {
220 wakaba 1.39 $onerror->(type => 'at-rule not allowed:charset',
221 wakaba 1.3 level => $self->{must_level},
222 wakaba 1.38 uri => \$self->{href},
223 wakaba 1.3 token => $t);
224     }
225    
226     ## TODO: Detect the conformance errors for @charset...
227    
228     $t = $tt->get_next_token;
229     ## Stay in the state.
230     redo S;
231     } else {
232     #
233     }
234     } else {
235     #
236     }
237    
238 wakaba 1.39 $onerror->(type => 'at-rule syntax error:charset',
239 wakaba 1.3 level => $self->{must_level},
240 wakaba 1.38 uri => \$self->{href},
241 wakaba 1.3 token => $t);
242 wakaba 1.4 #
243 wakaba 1.3 ## NOTE: When adding support for new at-rule, insert code
244 wakaba 1.4 ## "undef $charset_allowed" and "undef $namespace_token" as
245     ## appropriate.
246 wakaba 1.3 } else {
247 wakaba 1.39 $onerror->(type => 'at-rule not supported',
248 wakaba 1.3 level => $self->{unsupported_level},
249 wakaba 1.38 uri => \$self->{href},
250 wakaba 1.39 token => $t,
251     value => $t->{value});
252 wakaba 1.3 }
253 wakaba 1.1
254     $t = $tt->get_next_token;
255     $state = IGNORED_STATEMENT_STATE;
256     redo S;
257     } elsif (@$open_rules > 1 and $t->{type} == RBRACE_TOKEN) {
258     pop @$open_rules;
259     ## Stay in the state.
260     $t = $tt->get_next_token;
261     redo S;
262     } elsif ($t->{type} == EOF_TOKEN) {
263     if (@$open_rules > 1) {
264 wakaba 1.39 $onerror->(type => 'block not closed',
265 wakaba 1.2 level => $self->{must_level},
266 wakaba 1.38 uri => \$self->{href},
267 wakaba 1.2 token => $t);
268 wakaba 1.1 }
269    
270     last S;
271     } else {
272 wakaba 1.3 undef $charset_allowed;
273 wakaba 1.4 undef $namespace_allowed;
274 wakaba 1.3
275 wakaba 1.1 ($t, my $selectors) = $sp->_parse_selectors_with_tokenizer
276     ($tt, LBRACE_TOKEN, $t);
277    
278     $t = $tt->get_next_token
279     while $t->{type} != LBRACE_TOKEN and $t->{type} != EOF_TOKEN;
280    
281     if ($t->{type} == LBRACE_TOKEN) {
282     $current_decls = Message::DOM::CSSStyleDeclaration->____new;
283     my $rs = Message::DOM::CSSStyleRule->____new
284     ($selectors, $current_decls);
285     push @{$current_rules}, $rs if defined $selectors;
286    
287     $state = BEFORE_DECLARATION_STATE;
288     $t = $tt->get_next_token;
289     redo S;
290     } else {
291 wakaba 1.39 $onerror->(type => 'no declaration block',
292 wakaba 1.2 level => $self->{must_level},
293 wakaba 1.38 uri => \$self->{href},
294 wakaba 1.2 token => $t);
295 wakaba 1.1
296     ## Stay in the state.
297     $t = $tt->get_next_token;
298     redo S;
299     }
300     }
301     } elsif ($state == BEFORE_DECLARATION_STATE) {
302     ## NOTE: DELIM? in declaration will be removed:
303     ## <http://csswg.inkedblade.net/spec/css2.1?s=declaration%20delim#issue-2>.
304    
305 wakaba 1.5 my $prop_def;
306     my $prop_value;
307 wakaba 1.35 my $prop_flag = '';
308 wakaba 1.1 $t = $tt->get_next_token while $t->{type} == S_TOKEN;
309     if ($t->{type} == IDENT_TOKEN) { # property
310 wakaba 1.5 my $prop_name = lc $t->{value}; ## TODO: case folding
311     $t = $tt->get_next_token;
312 wakaba 1.29 $t = $tt->get_next_token while $t->{type} == S_TOKEN;
313 wakaba 1.5 if ($t->{type} == COLON_TOKEN) {
314     $t = $tt->get_next_token;
315     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
316    
317     $prop_def = $Prop->{$prop_name};
318 wakaba 1.6 if ($prop_def and $self->{prop}->{$prop_name}) {
319 wakaba 1.5 ($t, $prop_value)
320     = $prop_def->{parse}->($self, $prop_name, $tt, $t, $onerror);
321     if ($prop_value) {
322     ## NOTE: {parse} don't have to consume trailing spaces.
323     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
324    
325     if ($t->{type} == EXCLAMATION_TOKEN) {
326     $t = $tt->get_next_token;
327     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
328     if ($t->{type} == IDENT_TOKEN and
329     lc $t->{value} eq 'important') { ## TODO: case folding
330     $prop_flag = 'important';
331    
332     $t = $tt->get_next_token;
333     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
334    
335     #
336     } else {
337 wakaba 1.39 $onerror->(type => 'priority syntax error',
338 wakaba 1.5 level => $self->{must_level},
339 wakaba 1.38 uri => \$self->{href},
340 wakaba 1.5 token => $t);
341    
342     ## Reprocess.
343     $state = IGNORED_DECLARATION_STATE;
344     redo S;
345     }
346     }
347    
348     #
349     } else {
350     ## Syntax error.
351    
352     ## Reprocess.
353     $state = IGNORED_DECLARATION_STATE;
354     redo S;
355     }
356     } else {
357 wakaba 1.39 $onerror->(type => 'property not supported',
358 wakaba 1.5 level => $self->{unsupported_level},
359 wakaba 1.38 token => $t, value => $prop_name,
360     uri => \$self->{href});
361 wakaba 1.5
362     #
363     $state = IGNORED_DECLARATION_STATE;
364     redo S;
365     }
366     } else {
367 wakaba 1.39 $onerror->(type => 'no property colon',
368 wakaba 1.5 level => $self->{must_level},
369 wakaba 1.38 uri => \$self->{href},
370 wakaba 1.5 token => $t);
371 wakaba 1.1
372 wakaba 1.5 #
373     $state = IGNORED_DECLARATION_STATE;
374     redo S;
375     }
376     }
377    
378     if ($t->{type} == RBRACE_TOKEN) {
379 wakaba 1.1 $t = $tt->get_next_token;
380 wakaba 1.5 $state = BEFORE_STATEMENT_STATE;
381     #redo S;
382     } elsif ($t->{type} == SEMICOLON_TOKEN) {
383 wakaba 1.1 $t = $tt->get_next_token;
384 wakaba 1.5 ## Stay in the state.
385     #redo S;
386 wakaba 1.1 } elsif ($t->{type} == EOF_TOKEN) {
387 wakaba 1.39 $onerror->(type => 'block not closed',
388 wakaba 1.2 level => $self->{must_level},
389 wakaba 1.38 uri => \$self->{href},
390 wakaba 1.2 token => $t);
391 wakaba 1.1 ## Reprocess.
392     $state = BEFORE_STATEMENT_STATE;
393 wakaba 1.5 #redo S;
394     } else {
395     if ($prop_value) {
396 wakaba 1.39 $onerror->(type => 'no property semicolon',
397 wakaba 1.5 level => $self->{must_level},
398 wakaba 1.38 uri => \$self->{href},
399 wakaba 1.5 token => $t);
400     } else {
401 wakaba 1.39 $onerror->(type => 'no property name',
402 wakaba 1.5 level => $self->{must_level},
403 wakaba 1.38 uri => \$self->{href},
404 wakaba 1.5 token => $t);
405     }
406    
407     #
408     $state = IGNORED_DECLARATION_STATE;
409 wakaba 1.1 redo S;
410     }
411    
412 wakaba 1.35 my $important = ($prop_flag eq 'important');
413 wakaba 1.7 for my $set_prop_name (keys %{$prop_value or {}}) {
414     my $set_prop_def = $Prop->{$set_prop_name};
415     $$current_decls->{$set_prop_def->{key}}
416     = [$prop_value->{$set_prop_name}, $prop_flag]
417     if $important or
418     not $$current_decls->{$set_prop_def->{key}} or
419 wakaba 1.42 $$current_decls->{$set_prop_def->{key}}->[1] ne 'important';
420 wakaba 1.5 }
421 wakaba 1.1 redo S;
422     } elsif ($state == IGNORED_STATEMENT_STATE or
423     $state == IGNORED_DECLARATION_STATE) {
424     if (@$closing_tokens) { ## Something is yet in opening state.
425     if ($t->{type} == EOF_TOKEN) {
426     @$closing_tokens = ();
427     ## Reprocess.
428     $state = $state == IGNORED_STATEMENT_STATE
429     ? BEFORE_STATEMENT_STATE : BEFORE_DECLARATION_STATE;
430     redo S;
431     } elsif ($t->{type} == $closing_tokens->[-1]) {
432     pop @$closing_tokens;
433     if (@$closing_tokens == 0 and
434     $t->{type} == RBRACE_TOKEN and
435     $state == IGNORED_STATEMENT_STATE) {
436     $t = $tt->get_next_token;
437     $state = BEFORE_STATEMENT_STATE;
438     redo S;
439     } else {
440     $t = $tt->get_next_token;
441     ## Stay in the state.
442     redo S;
443     }
444 wakaba 1.28 } elsif ({
445     RBRACE_TOKEN, 1,
446     #RBRACKET_TOKEN, 1,
447     #RPAREN_TOKEN, 1,
448     SEMICOLON_TOKEN, 1,
449     }->{$t->{type}}) {
450     $t = $tt->get_next_token;
451     ## Stay in the state.
452     #
453 wakaba 1.1 } else {
454     #
455     }
456     } else {
457     if ($t->{type} == SEMICOLON_TOKEN) {
458     $t = $tt->get_next_token;
459     $state = $state == IGNORED_STATEMENT_STATE
460     ? BEFORE_STATEMENT_STATE : BEFORE_DECLARATION_STATE;
461     redo S;
462 wakaba 1.28 } elsif ($t->{type} == RBRACE_TOKEN) {
463     if ($state == IGNORED_DECLARATION_STATE) {
464     $t = $tt->get_next_token;
465     $state = BEFORE_STATEMENT_STATE;
466     redo S;
467     } else {
468     ## NOTE: Maybe this state cannot be reached.
469     $t = $tt->get_next_token;
470     ## Stay in the state.
471     redo S;
472     }
473 wakaba 1.1 } elsif ($t->{type} == EOF_TOKEN) {
474     ## Reprocess.
475     $state = $state == IGNORED_STATEMENT_STATE
476     ? BEFORE_STATEMENT_STATE : BEFORE_DECLARATION_STATE;
477     redo S;
478 wakaba 1.28 #} elsif ($t->{type} == RBRACKET_TOKEN or $t->{type} == RPAREN_TOKEN) {
479     # $t = $tt->get_next_token;
480     # ## Stay in the state.
481     # #
482 wakaba 1.1 } else {
483     #
484     }
485     }
486    
487     while (not {
488     EOF_TOKEN, 1,
489     RBRACE_TOKEN, 1,
490 wakaba 1.28 ## NOTE: ']' and ')' are disabled for browser compatibility.
491     #RBRACKET_TOKEN, 1,
492     #RPAREN_TOKEN, 1,
493 wakaba 1.1 SEMICOLON_TOKEN, 1,
494     }->{$t->{type}}) {
495     if ($t->{type} == LBRACE_TOKEN) {
496     push @$closing_tokens, RBRACE_TOKEN;
497 wakaba 1.28 #} elsif ($t->{type} == LBRACKET_TOKEN) {
498     # push @$closing_tokens, RBRACKET_TOKEN;
499     #} elsif ($t->{type} == LPAREN_TOKEN or $t->{type} == FUNCTION_TOKEN) {
500     # push @$closing_tokens, RPAREN_TOKEN;
501 wakaba 1.1 }
502    
503     $t = $tt->get_next_token;
504     }
505    
506     #
507     ## Stay in the state.
508     redo S;
509     } else {
510     die "$0: parse_char_string: Unknown state: $state";
511     }
512     } # S
513    
514     my $ss = Message::DOM::CSSStyleSheet->____new
515 wakaba 1.11 (manakai_base_uri => $self->{base_uri},
516     css_rules => $open_rules->[0],
517 wakaba 1.1 ## TODO: href
518     ## TODO: owner_node
519     ## TODO: media
520     type => 'text/css', ## TODO: OK?
521 wakaba 1.33 _parser => $self, _nsmap => $nsmap);
522 wakaba 1.1 return $ss;
523     } # parse_char_string
524    
525 wakaba 1.9 my $compute_as_specified = sub ($$$$) {
526     #my ($self, $element, $prop_name, $specified_value) = @_;
527     return $_[3];
528     }; # $compute_as_specified
529    
530 wakaba 1.11 my $default_serializer = sub {
531     my ($self, $prop_name, $value) = @_;
532 wakaba 1.15 if ($value->[0] eq 'NUMBER' or $value->[0] eq 'WEIGHT') {
533     ## TODO: What we currently do for 'font-weight' is different from
534     ## any browser for lighter/bolder cases. We need to fix this, but
535     ## how?
536 wakaba 1.11 return $value->[1]; ## TODO: big or small number cases?
537 wakaba 1.18 } elsif ($value->[0] eq 'DIMENSION') {
538     return $value->[1] . $value->[2]; ## NOTE: This is what browsers do.
539 wakaba 1.22 } elsif ($value->[0] eq 'PERCENTAGE') {
540     return $value->[1] . '%';
541 wakaba 1.11 } elsif ($value->[0] eq 'KEYWORD') {
542     return $value->[1];
543     } elsif ($value->[0] eq 'URI') {
544     ## NOTE: This is what browsers do.
545     return 'url('.$value->[1].')';
546 wakaba 1.28 } elsif ($value->[0] eq 'RGBA') {
547     if ($value->[4] == 1) {
548     return 'rgb('.$value->[1].', '.$value->[2].', '.$value->[3].')';
549     } elsif ($value->[4] == 0) {
550     ## TODO: check what browsers do...
551     return 'transparent';
552     } else {
553     return 'rgba('.$value->[1].', '.$value->[2].', '.$value->[3].', '
554     .$value->[4].')';
555     }
556 wakaba 1.11 } elsif ($value->[0] eq 'INHERIT') {
557     return 'inherit';
558 wakaba 1.16 } elsif ($value->[0] eq 'DECORATION') {
559     my @v = ();
560     push @v, 'underline' if $value->[1];
561     push @v, 'overline' if $value->[2];
562     push @v, 'line-through' if $value->[3];
563     push @v, 'blink' if $value->[4];
564     return 'none' unless @v;
565     return join ' ', @v;
566 wakaba 1.11 } else {
567 wakaba 1.34 return '';
568 wakaba 1.11 }
569     }; # $default_serializer
570    
571 wakaba 1.28 my $x11_colors = {
572     'aliceblue' => [0xf0, 0xf8, 0xff],
573     'antiquewhite' => [0xfa, 0xeb, 0xd7],
574     'aqua' => [0x00, 0xff, 0xff],
575     'aquamarine' => [0x7f, 0xff, 0xd4],
576     'azure' => [0xf0, 0xff, 0xff],
577     'beige' => [0xf5, 0xf5, 0xdc],
578     'bisque' => [0xff, 0xe4, 0xc4],
579     'black' => [0x00, 0x00, 0x00],
580     'blanchedalmond' => [0xff, 0xeb, 0xcd],
581     'blue' => [0x00, 0x00, 0xff],
582     'blueviolet' => [0x8a, 0x2b, 0xe2],
583     'brown' => [0xa5, 0x2a, 0x2a],
584     'burlywood' => [0xde, 0xb8, 0x87],
585     'cadetblue' => [0x5f, 0x9e, 0xa0],
586     'chartreuse' => [0x7f, 0xff, 0x00],
587     'chocolate' => [0xd2, 0x69, 0x1e],
588     'coral' => [0xff, 0x7f, 0x50],
589     'cornflowerblue' => [0x64, 0x95, 0xed],
590     'cornsilk' => [0xff, 0xf8, 0xdc],
591     'crimson' => [0xdc, 0x14, 0x3c],
592     'cyan' => [0x00, 0xff, 0xff],
593     'darkblue' => [0x00, 0x00, 0x8b],
594     'darkcyan' => [0x00, 0x8b, 0x8b],
595     'darkgoldenrod' => [0xb8, 0x86, 0x0b],
596     'darkgray' => [0xa9, 0xa9, 0xa9],
597     'darkgreen' => [0x00, 0x64, 0x00],
598     'darkgrey' => [0xa9, 0xa9, 0xa9],
599     'darkkhaki' => [0xbd, 0xb7, 0x6b],
600     'darkmagenta' => [0x8b, 0x00, 0x8b],
601     'darkolivegreen' => [0x55, 0x6b, 0x2f],
602     'darkorange' => [0xff, 0x8c, 0x00],
603     'darkorchid' => [0x99, 0x32, 0xcc],
604     'darkred' => [0x8b, 0x00, 0x00],
605     'darksalmon' => [0xe9, 0x96, 0x7a],
606     'darkseagreen' => [0x8f, 0xbc, 0x8f],
607     'darkslateblue' => [0x48, 0x3d, 0x8b],
608     'darkslategray' => [0x2f, 0x4f, 0x4f],
609     'darkslategrey' => [0x2f, 0x4f, 0x4f],
610     'darkturquoise' => [0x00, 0xce, 0xd1],
611     'darkviolet' => [0x94, 0x00, 0xd3],
612     'deeppink' => [0xff, 0x14, 0x93],
613     'deepskyblue' => [0x00, 0xbf, 0xff],
614     'dimgray' => [0x69, 0x69, 0x69],
615     'dimgrey' => [0x69, 0x69, 0x69],
616     'dodgerblue' => [0x1e, 0x90, 0xff],
617     'firebrick' => [0xb2, 0x22, 0x22],
618     'floralwhite' => [0xff, 0xfa, 0xf0],
619     'forestgreen' => [0x22, 0x8b, 0x22],
620     'fuchsia' => [0xff, 0x00, 0xff],
621     'gainsboro' => [0xdc, 0xdc, 0xdc],
622     'ghostwhite' => [0xf8, 0xf8, 0xff],
623     'gold' => [0xff, 0xd7, 0x00],
624     'goldenrod' => [0xda, 0xa5, 0x20],
625     'gray' => [0x80, 0x80, 0x80],
626     'green' => [0x00, 0x80, 0x00],
627     'greenyellow' => [0xad, 0xff, 0x2f],
628     'grey' => [0x80, 0x80, 0x80],
629     'honeydew' => [0xf0, 0xff, 0xf0],
630     'hotpink' => [0xff, 0x69, 0xb4],
631     'indianred' => [0xcd, 0x5c, 0x5c],
632     'indigo' => [0x4b, 0x00, 0x82],
633     'ivory' => [0xff, 0xff, 0xf0],
634     'khaki' => [0xf0, 0xe6, 0x8c],
635     'lavender' => [0xe6, 0xe6, 0xfa],
636     'lavenderblush' => [0xff, 0xf0, 0xf5],
637     'lawngreen' => [0x7c, 0xfc, 0x00],
638     'lemonchiffon' => [0xff, 0xfa, 0xcd],
639     'lightblue' => [0xad, 0xd8, 0xe6],
640     'lightcoral' => [0xf0, 0x80, 0x80],
641     'lightcyan' => [0xe0, 0xff, 0xff],
642     'lightgoldenrodyellow' => [0xfa, 0xfa, 0xd2],
643     'lightgray' => [0xd3, 0xd3, 0xd3],
644     'lightgreen' => [0x90, 0xee, 0x90],
645     'lightgrey' => [0xd3, 0xd3, 0xd3],
646     'lightpink' => [0xff, 0xb6, 0xc1],
647     'lightsalmon' => [0xff, 0xa0, 0x7a],
648     'lightseagreen' => [0x20, 0xb2, 0xaa],
649     'lightskyblue' => [0x87, 0xce, 0xfa],
650     'lightslategray' => [0x77, 0x88, 0x99],
651     'lightslategrey' => [0x77, 0x88, 0x99],
652     'lightsteelblue' => [0xb0, 0xc4, 0xde],
653     'lightyellow' => [0xff, 0xff, 0xe0],
654     'lime' => [0x00, 0xff, 0x00],
655     'limegreen' => [0x32, 0xcd, 0x32],
656     'linen' => [0xfa, 0xf0, 0xe6],
657     'magenta' => [0xff, 0x00, 0xff],
658     'maroon' => [0x80, 0x00, 0x00],
659     'mediumaquamarine' => [0x66, 0xcd, 0xaa],
660     'mediumblue' => [0x00, 0x00, 0xcd],
661     'mediumorchid' => [0xba, 0x55, 0xd3],
662     'mediumpurple' => [0x93, 0x70, 0xdb],
663     'mediumseagreen' => [0x3c, 0xb3, 0x71],
664     'mediumslateblue' => [0x7b, 0x68, 0xee],
665     'mediumspringgreen' => [0x00, 0xfa, 0x9a],
666     'mediumturquoise' => [0x48, 0xd1, 0xcc],
667     'mediumvioletred' => [0xc7, 0x15, 0x85],
668     'midnightblue' => [0x19, 0x19, 0x70],
669     'mintcream' => [0xf5, 0xff, 0xfa],
670     'mistyrose' => [0xff, 0xe4, 0xe1],
671     'moccasin' => [0xff, 0xe4, 0xb5],
672     'navajowhite' => [0xff, 0xde, 0xad],
673     'navy' => [0x00, 0x00, 0x80],
674     'oldlace' => [0xfd, 0xf5, 0xe6],
675     'olive' => [0x80, 0x80, 0x00],
676     'olivedrab' => [0x6b, 0x8e, 0x23],
677     'orange' => [0xff, 0xa5, 0x00],
678     'orangered' => [0xff, 0x45, 0x00],
679     'orchid' => [0xda, 0x70, 0xd6],
680     'palegoldenrod' => [0xee, 0xe8, 0xaa],
681     'palegreen' => [0x98, 0xfb, 0x98],
682     'paleturquoise' => [0xaf, 0xee, 0xee],
683     'palevioletred' => [0xdb, 0x70, 0x93],
684     'papayawhip' => [0xff, 0xef, 0xd5],
685     'peachpuff' => [0xff, 0xda, 0xb9],
686     'peru' => [0xcd, 0x85, 0x3f],
687     'pink' => [0xff, 0xc0, 0xcb],
688     'plum' => [0xdd, 0xa0, 0xdd],
689     'powderblue' => [0xb0, 0xe0, 0xe6],
690     'purple' => [0x80, 0x00, 0x80],
691     'red' => [0xff, 0x00, 0x00],
692     'rosybrown' => [0xbc, 0x8f, 0x8f],
693     'royalblue' => [0x41, 0x69, 0xe1],
694     'saddlebrown' => [0x8b, 0x45, 0x13],
695     'salmon' => [0xfa, 0x80, 0x72],
696     'sandybrown' => [0xf4, 0xa4, 0x60],
697     'seagreen' => [0x2e, 0x8b, 0x57],
698     'seashell' => [0xff, 0xf5, 0xee],
699     'sienna' => [0xa0, 0x52, 0x2d],
700     'silver' => [0xc0, 0xc0, 0xc0],
701     'skyblue' => [0x87, 0xce, 0xeb],
702     'slateblue' => [0x6a, 0x5a, 0xcd],
703     'slategray' => [0x70, 0x80, 0x90],
704     'slategrey' => [0x70, 0x80, 0x90],
705     'snow' => [0xff, 0xfa, 0xfa],
706     'springgreen' => [0x00, 0xff, 0x7f],
707     'steelblue' => [0x46, 0x82, 0xb4],
708     'tan' => [0xd2, 0xb4, 0x8c],
709     'teal' => [0x00, 0x80, 0x80],
710     'thistle' => [0xd8, 0xbf, 0xd8],
711     'tomato' => [0xff, 0x63, 0x47],
712     'turquoise' => [0x40, 0xe0, 0xd0],
713     'violet' => [0xee, 0x82, 0xee],
714     'wheat' => [0xf5, 0xde, 0xb3],
715     'white' => [0xff, 0xff, 0xff],
716     'whitesmoke' => [0xf5, 0xf5, 0xf5],
717     'yellow' => [0xff, 0xff, 0x00],
718     'yellowgreen' => [0x9a, 0xcd, 0x32],
719     }; # $x11_colors
720    
721     my $system_colors = {
722     activeborder => 1, activecaption => 1, appworkspace => 1, background => 1,
723     buttonface => 1, buttonhighlight => 1, buttonshadow => 1, buttontext => 1,
724     captiontext => 1, graytext => 1, highlight => 1, highlighttext => 1,
725     inactiveborder => 1, inactivecaption => 1, inactivecaptiontext => 1,
726     infobackground => 1, infotext => 1, menu => 1, menutext => 1,
727     scrollbar => 1, threeddarkshadow => 1, threedface => 1, threedhighlight => 1,
728     threedlightshadow => 1, threedshadow => 1, window => 1, windowframe => 1,
729     windowtext => 1,
730     }; # $system_colors
731    
732     my $parse_color = sub {
733     my ($self, $prop_name, $tt, $t, $onerror) = @_;
734    
735     ## See
736     ## <http://suika.fam.cx/gate/2005/sw/%3Ccolor%3E>,
737     ## <http://suika.fam.cx/gate/2005/sw/rgb>,
738     ## <http://suika.fam.cx/gate/2005/sw/-moz-rgba>,
739     ## <http://suika.fam.cx/gate/2005/sw/hsl>,
740     ## <http://suika.fam.cx/gate/2005/sw/-moz-hsla>, and
741     ## <http://suika.fam.cx/gate/2005/sw/color>
742     ## for browser compatibility issue.
743    
744     ## NOTE: Implementing CSS3 Color CR (2003), except for attr(),
745     ## rgba(), and hsla().
746     ## NOTE: rgb(...{EOF} is not supported (only Opera does).
747    
748     if ($t->{type} == IDENT_TOKEN) {
749     my $value = lc $t->{value}; ## TODO: case
750     if ($x11_colors->{$value} or
751     $system_colors->{$value}) {
752 wakaba 1.31 ## NOTE: "For systems that do not have a corresponding value, the
753     ## specified value should be mapped to the nearest system value, or to
754     ## a default color." [CSS 2.1].
755 wakaba 1.28 $t = $tt->get_next_token;
756     return ($t, {$prop_name => ['KEYWORD', $value]});
757     } elsif ({
758     transparent => 1, ## For 'background-color' in CSS2.1, everywhre in CSS3.
759     flavor => 1, ## CSS3.
760     invert => 1, ## For 'outline-color' in CSS2.1.
761     '-moz-use-text-color' => 1, ## For <border-color> in Gecko.
762     '-manakai-default' => 1, ## CSS2.1 initial for 'color'
763     '-manakai-invert-or-currentcolor' => 1, ## CSS2.1 initial4'outline-color'
764     }->{$value} and $self->{prop_value}->{$prop_name}->{$value}) {
765     $t = $tt->get_next_token;
766     return ($t, {$prop_name => ['KEYWORD', $value]});
767     } elsif ($value eq 'currentcolor' or $value eq '-moz-use-text-color') {
768     $t = $tt->get_next_token;
769     if ($prop_name eq 'color') {
770     return ($t, {$prop_name => ['INHERIT']});
771     } else {
772     return ($t, {$prop_name => ['KEYWORD', $value]});
773     }
774     } elsif ($value eq 'inherit') {
775     $t = $tt->get_next_token;
776     return ($t, {$prop_name => ['INHERIT']});
777     }
778     }
779    
780     if ($t->{type} == HASH_TOKEN or
781 wakaba 1.29 ($self->{hashless_rgb} and {
782     IDENT_TOKEN, 1,
783     NUMBER_TOKEN, 1,
784     DIMENSION_TOKEN, 1,
785     }->{$t->{type}})) {
786     my $v = lc (defined $t->{number} ? $t->{number} : '' . $t->{value}); ## TODO: case
787 wakaba 1.28 if ($v =~ /\A([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})\z/) {
788     $t = $tt->get_next_token;
789     return ($t, {$prop_name => ['RGBA', hex $1, hex $2, hex $3, 1]});
790     } elsif ($v =~ /\A([0-9a-f])([0-9a-f])([0-9a-f])\z/) {
791     $t = $tt->get_next_token;
792     return ($t, {$prop_name => ['RGBA', hex $1.$1, hex $2.$2,
793     hex $3.$3, 1]});
794     }
795     }
796    
797     if ($t->{type} == FUNCTION_TOKEN) {
798     my $func = lc $t->{value}; ## TODO: case
799     if ($func eq 'rgb') {
800     $t = $tt->get_next_token;
801     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
802     my $sign = 1;
803     if ($t->{type} == MINUS_TOKEN) {
804     $sign = -1;
805     $t = $tt->get_next_token;
806     }
807     if ($t->{type} == NUMBER_TOKEN) {
808     my $r = $t->{number} * $sign;
809     $t = $tt->get_next_token;
810     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
811     if ($t->{type} == COMMA_TOKEN) {
812     $t = $tt->get_next_token;
813     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
814     $sign = 1;
815     if ($t->{type} == MINUS_TOKEN) {
816     $sign = -1;
817     $t = $tt->get_next_token;
818     }
819     if ($t->{type} == NUMBER_TOKEN) {
820     my $g = $t->{number} * $sign;
821     $t = $tt->get_next_token;
822     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
823     if ($t->{type} == COMMA_TOKEN) {
824     $t = $tt->get_next_token;
825     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
826     $sign = 1;
827     if ($t->{type} == MINUS_TOKEN) {
828     $sign = -1;
829     $t = $tt->get_next_token;
830     }
831     if ($t->{type} == NUMBER_TOKEN) {
832     my $b = $t->{number} * $sign;
833     $t = $tt->get_next_token;
834     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
835     if ($t->{type} == RPAREN_TOKEN) {
836     $t = $tt->get_next_token;
837     return ($t,
838     {$prop_name =>
839     $self->{clip_color}->($self,
840     ['RGBA', $r, $g, $b, 1])});
841     }
842     }
843     }
844     }
845     }
846     } elsif ($t->{type} == PERCENTAGE_TOKEN) {
847     my $r = $t->{number} * 255 / 100 * $sign;
848     $t = $tt->get_next_token;
849     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
850     if ($t->{type} == COMMA_TOKEN) {
851     $t = $tt->get_next_token;
852     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
853     $sign = 1;
854     if ($t->{type} == MINUS_TOKEN) {
855     $sign = -1;
856     $t = $tt->get_next_token;
857     }
858     if ($t->{type} == PERCENTAGE_TOKEN) {
859     my $g = $t->{number} * 255 / 100 * $sign;
860     $t = $tt->get_next_token;
861     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
862     if ($t->{type} == COMMA_TOKEN) {
863     $t = $tt->get_next_token;
864     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
865     $sign = 1;
866     if ($t->{type} == MINUS_TOKEN) {
867     $sign = -1;
868     $t = $tt->get_next_token;
869     }
870     if ($t->{type} == PERCENTAGE_TOKEN) {
871     my $b = $t->{number} * 255 / 100 * $sign;
872     $t = $tt->get_next_token;
873     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
874     if ($t->{type} == RPAREN_TOKEN) {
875     $t = $tt->get_next_token;
876     return ($t,
877     {$prop_name =>
878     $self->{clip_color}->($self,
879     ['RGBA', $r, $g, $b, 1])});
880     }
881     }
882     }
883     }
884     }
885     }
886     } elsif ($func eq 'hsl') {
887     $t = $tt->get_next_token;
888     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
889     my $sign = 1;
890     if ($t->{type} == MINUS_TOKEN) {
891     $sign = -1;
892     $t = $tt->get_next_token;
893     }
894     if ($t->{type} == NUMBER_TOKEN) {
895     my $h = (((($t->{number} * $sign) % 360) + 360) % 360) / 360;
896     $t = $tt->get_next_token;
897     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
898     if ($t->{type} == COMMA_TOKEN) {
899     $t = $tt->get_next_token;
900     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
901     $sign = 1;
902     if ($t->{type} == MINUS_TOKEN) {
903     $sign = -1;
904     $t = $tt->get_next_token;
905     }
906     if ($t->{type} == PERCENTAGE_TOKEN) {
907     my $s = $t->{number} * $sign / 100;
908     $s = 0 if $s < 0;
909     $s = 1 if $s > 1;
910     $t = $tt->get_next_token;
911     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
912     if ($t->{type} == COMMA_TOKEN) {
913     $t = $tt->get_next_token;
914     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
915     $sign = 1;
916     if ($t->{type} == MINUS_TOKEN) {
917     $sign = -1;
918     $t = $tt->get_next_token;
919     }
920     if ($t->{type} == PERCENTAGE_TOKEN) {
921     my $l = $t->{number} * $sign / 100;
922     $l = 0 if $l < 0;
923     $l = 1 if $l > 1;
924     $t = $tt->get_next_token;
925     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
926     if ($t->{type} == RPAREN_TOKEN) {
927     my $m2 = $l <= 0.5 ? $l * ($s + 1) : $l + $s - $l * $s;
928     my $m1 = $l * 2 - $m2;
929     my $hue2rgb = sub ($$$) {
930     my ($m1, $m2, $h) = @_;
931     $h++ if $h < 0;
932     $h-- if $h > 1;
933     return $m1 + ($m2 - $m1) * $h * 6 if $h * 6 < 1;
934     return $m2 if $h * 2 < 1;
935     return $m1 + ($m2 - $m1) * (2/3 - $h) * 6 if $h * 3 < 2;
936     return $m1;
937     };
938     $t = $tt->get_next_token;
939     return ($t,
940     {$prop_name =>
941     $self->{clip_color}
942     ->($self,
943     ['RGBA',
944     $hue2rgb->($m1, $m2, $h + 1/3),
945     $hue2rgb->($m1, $m2, $h),
946     $hue2rgb->($m1, $m2, $h - 1/3), 1])});
947     }
948     }
949     }
950     }
951     }
952     }
953     }
954     }
955    
956 wakaba 1.39 $onerror->(type => 'syntax error:color',
957 wakaba 1.28 level => $self->{must_level},
958 wakaba 1.38 uri => \$self->{href},
959 wakaba 1.28 token => $t);
960    
961     return ($t, undef);
962     }; # $parse_color
963    
964 wakaba 1.5 $Prop->{color} = {
965     css => 'color',
966     dom => 'color',
967     key => 'color',
968 wakaba 1.28 parse => $parse_color,
969     serialize => $default_serializer,
970     initial => ['KEYWORD', '-manakai-default'],
971     inherited => 1,
972     compute => sub ($$$$) {
973     my ($self, $element, $prop_name, $specified_value) = @_;
974 wakaba 1.5
975 wakaba 1.28 if (defined $specified_value) {
976     if ($specified_value->[0] eq 'KEYWORD') {
977     if ($x11_colors->{$specified_value->[1]}) {
978     return ['RGBA', @{$x11_colors->{$specified_value->[1]}}, 1];
979     } elsif ($specified_value->[1] eq 'transparent') {
980     return ['RGBA', 0, 0, 0, 0];
981     } elsif ($specified_value->[1] eq 'currentcolor' or
982     $specified_value->[1] eq '-moz-use-text-color' or
983     ($specified_value->[1] eq '-manakai-invert-or-currentcolor'and
984     not $self->{has_invert})) {
985     unless ($prop_name eq 'color') {
986     return $self->get_computed_value ($element, 'color');
987     } else {
988     ## NOTE: This is an error, since it should have been
989     ## converted to 'inherit' at parse time.
990     return ['KEYWORD', '-manakai-default'];
991     }
992     } elsif ($specified_value->[1] eq '-manakai-invert-or-currentcolor') {
993     return ['KEYWORD', 'invert'];
994     }
995 wakaba 1.5 }
996     }
997    
998 wakaba 1.28 return $specified_value;
999 wakaba 1.5 },
1000     };
1001     $Attr->{color} = $Prop->{color};
1002     $Key->{color} = $Prop->{color};
1003    
1004 wakaba 1.28 $Prop->{'background-color'} = {
1005     css => 'background-color',
1006     dom => 'background_color',
1007     key => 'background_color',
1008     parse => $parse_color,
1009     serialize => $default_serializer,
1010 wakaba 1.30 serialize_multiple => sub {
1011     my $self = shift;
1012    
1013     my $r = {};
1014     my $has_all;
1015    
1016     my $x = $self->background_position_x;
1017     my $y = $self->background_position_y;
1018     my $xi = $self->get_property_priority ('background-position-x');
1019     my $yi = $self->get_property_priority ('background-position-y');
1020 wakaba 1.34 if (length $x) {
1021     if (length $y) {
1022 wakaba 1.30 if ($xi eq $yi) {
1023 wakaba 1.48 if ($x eq 'inherit') {
1024     if ($y eq 'inherit') {
1025     $r->{'background-position'} = ['inherit', $xi];
1026     $has_all = 1;
1027     } else {
1028     $r->{'background-position-x'} = [$x, $xi];
1029     $r->{'background-position-y'} = [$y, $yi];
1030     }
1031     } elsif ($y eq 'inherit') {
1032     $r->{'background-position-x'} = [$x, $xi];
1033     $r->{'background-position-y'} = [$y, $yi];
1034     } else {
1035     $r->{'background-position'} = [$x . ' ' . $y, $xi];
1036     $has_all = 1;
1037     }
1038 wakaba 1.30 } else {
1039 wakaba 1.43 $r->{'background-position-x'} = [$x, $xi];
1040     $r->{'background-position-y'} = [$y, $yi];
1041 wakaba 1.30 }
1042     } else {
1043 wakaba 1.43 $r->{'background-position-x'} = [$x, $xi];
1044 wakaba 1.30 }
1045     } else {
1046 wakaba 1.34 if (length $y) {
1047 wakaba 1.43 $r->{'background-position-y'} = [$y, $yi];
1048 wakaba 1.30 } else {
1049     #
1050     }
1051     }
1052    
1053     for my $prop (qw/color image repeat attachment/) {
1054     my $prop_name = 'background_'.$prop;
1055     my $value = $self->$prop_name;
1056 wakaba 1.34 if (length $value) {
1057 wakaba 1.30 my $i = $self->get_property_priority ('background-'.$prop);
1058 wakaba 1.48 undef $has_all unless $xi eq $i;
1059 wakaba 1.45 $r->{'background-'.$prop} = [$value, $i];
1060 wakaba 1.30 } else {
1061     undef $has_all;
1062     }
1063     }
1064    
1065     if ($has_all) {
1066     my @v;
1067     push @v, $r->{'background-color'}
1068 wakaba 1.43 unless $r->{'background-color'}->[0] eq 'transparent';
1069 wakaba 1.30 push @v, $r->{'background-image'}
1070 wakaba 1.43 unless $r->{'background-image'}->[0] eq 'none';
1071 wakaba 1.30 push @v, $r->{'background-repeat'}
1072 wakaba 1.43 unless $r->{'background-repeat'}->[0] eq 'repeat';
1073 wakaba 1.30 push @v, $r->{'background-attachment'}
1074 wakaba 1.43 unless $r->{'background-attachment'}->[0] eq 'scroll';
1075 wakaba 1.30 push @v, $r->{'background-position'}
1076 wakaba 1.43 unless $r->{'background-position'}->[0] eq '0% 0%';
1077 wakaba 1.30 if (@v) {
1078 wakaba 1.48 my $inherit = 0;
1079     for (@v) {
1080     $inherit++ if $_->[0] eq 'inherit';
1081     }
1082     if ($inherit == 5) {
1083     return {background => ['inherit', $xi]};
1084     } elsif ($inherit) {
1085     return $r;
1086     } else {
1087     return {background => [(join ' ', map {$_->[0]} @v), $xi]};
1088     }
1089 wakaba 1.30 } else {
1090 wakaba 1.48 return {background => ['transparent none repeat scroll 0% 0%', $xi]};
1091 wakaba 1.30 }
1092     } else {
1093     return $r;
1094     }
1095     },
1096 wakaba 1.28 initial => ['KEYWORD', 'transparent'],
1097     #inherited => 0,
1098     compute => $Prop->{color}->{compute},
1099     };
1100     $Attr->{background_color} = $Prop->{'background-color'};
1101     $Key->{background_color} = $Prop->{'background-color'};
1102    
1103     $Prop->{'border-top-color'} = {
1104     css => 'border-top-color',
1105     dom => 'border_top_color',
1106     key => 'border_top_color',
1107     parse => $parse_color,
1108     serialize => $default_serializer,
1109 wakaba 1.29 serialize_multiple => sub {
1110     my $self = shift;
1111     ## NOTE: This algorithm returns the same result as that of Firefox 2
1112     ## in many case, but not always.
1113     my $r = {
1114 wakaba 1.46 'border-top-color' => [$self->border_top_color,
1115     $self->get_property_priority
1116     ('border-top-color')],
1117     'border-top-style' => [$self->border_top_style,
1118     $self->get_property_priority
1119     ('border-top-style')],
1120     'border-top-width' => [$self->border_top_width,
1121     $self->get_property_priority
1122     ('border-top-width')],
1123     'border-right-color' => [$self->border_right_color,
1124     $self->get_property_priority
1125     ('border-right-color')],
1126     'border-right-style' => [$self->border_right_style,
1127     $self->get_property_priority
1128     ('border-right-style')],
1129     'border-right-width' => [$self->border_right_width,
1130     $self->get_property_priority
1131     ('border-right-width')],
1132     'border-bottom-color' => [$self->border_bottom_color,
1133     $self->get_property_priority
1134     ('border-bottom-color')],
1135     'border-bottom-style' => [$self->border_bottom_style,
1136     $self->get_property_priority
1137     ('border-bottom-style')],
1138     'border-bottom-width' => [$self->border_bottom_width,
1139     $self->get_property_priority
1140     ('border-bottom-width')],
1141     'border-left-color' => [$self->border_left_color,
1142     $self->get_property_priority
1143     ('border-leftcolor')],
1144     'border-left-style' => [$self->border_left_style,
1145     $self->get_property_priority
1146     ('border-left-style')],
1147     'border-left-width' => [$self->border_left_width,
1148     $self->get_property_priority
1149     ('border-left-width')],
1150 wakaba 1.29 };
1151     my $i = 0;
1152     for my $prop (qw/border-top border-right border-bottom border-left/) {
1153 wakaba 1.43 if (length $r->{$prop.'-color'}->[0] and
1154     length $r->{$prop.'-style'}->[0] and
1155 wakaba 1.46 length $r->{$prop.'-width'}->[0] and
1156     $r->{$prop.'-color'}->[1] eq $r->{$prop.'-style'}->[1] and
1157     $r->{$prop.'-color'}->[1] eq $r->{$prop.'-width'}->[1]) {
1158     my $inherit = 0;
1159     $inherit++ if $r->{$prop.'-color'}->[0] eq 'inherit';
1160     $inherit++ if $r->{$prop.'-style'}->[0] eq 'inherit';
1161     $inherit++ if $r->{$prop.'-width'}->[0] eq 'inherit';
1162     if ($inherit == 3) {
1163     $r->{$prop} = $r->{$prop.'-color'};
1164     } elsif ($inherit) {
1165     next;
1166     } else {
1167     $r->{$prop} = [$r->{$prop.'-width'}->[0] . ' ' .
1168     $r->{$prop.'-style'}->[0] . ' ' .
1169     $r->{$prop.'-color'}->[0],
1170     $r->{$prop.'-color'}->[1]];
1171     }
1172 wakaba 1.29 delete $r->{$prop.'-width'};
1173     delete $r->{$prop.'-style'};
1174     delete $r->{$prop.'-color'};
1175     $i++;
1176     }
1177     }
1178 wakaba 1.46 if ($i == 4 and
1179     $r->{'border-top'}->[0] eq $r->{'border-right'}->[0] and
1180 wakaba 1.43 $r->{'border-right'}->[0] eq $r->{'border-bottom'}->[0] and
1181 wakaba 1.46 $r->{'border-bottom'}->[0] eq $r->{'border-left'}->[0] and
1182     $r->{'border-top'}->[1] eq $r->{'border-right'}->[1] and
1183     $r->{'border-right'}->[1] eq $r->{'border-bottom'}->[1] and
1184     $r->{'border-bottom'}->[1] eq $r->{'border-left'}->[1]) {
1185 wakaba 1.29 return {border => $r->{'border-top'}};
1186     }
1187    
1188     unless ($i) {
1189     for my $prop (qw/color style width/) {
1190     if (defined $r->{'border-top-'.$prop} and
1191     defined $r->{'border-bottom-'.$prop} and
1192     defined $r->{'border-right-'.$prop} and
1193 wakaba 1.46 defined $r->{'border-left-'.$prop} and
1194     length $r->{'border-top-'.$prop}->[0] and
1195     length $r->{'border-bottom-'.$prop}->[0] and
1196     length $r->{'border-right-'.$prop}->[0] and
1197     length $r->{'border-left-'.$prop}->[0] and
1198     $r->{'border-top-'.$prop}->[1]
1199     eq $r->{'border-bottom-'.$prop}->[1] and
1200     $r->{'border-top-'.$prop}->[1]
1201     eq $r->{'border-right-'.$prop}->[1] and
1202     $r->{'border-top-'.$prop}->[1]
1203     eq $r->{'border-left-'.$prop}->[1]) {
1204 wakaba 1.29 my @v = ($r->{'border-top-'.$prop},
1205     $r->{'border-right-'.$prop},
1206     $r->{'border-bottom-'.$prop},
1207     $r->{'border-left-'.$prop});
1208 wakaba 1.46 my $inherit = 0;
1209     for (@v) {
1210     $inherit++ if $_->[0] eq 'inherit';
1211     }
1212     if ($inherit == 4) {
1213     $r->{'border-'.$prop}
1214     = ['inherit', $r->{'border-top-'.$prop}->[1]];
1215     } elsif ($inherit) {
1216     next;
1217     } else {
1218     pop @v
1219     if $r->{'border-right-'.$prop}->[0]
1220     eq $r->{'border-left-'.$prop}->[0];
1221     pop @v
1222     if $r->{'border-bottom-'.$prop}->[0]
1223     eq $r->{'border-top-'.$prop}->[0];
1224     pop @v
1225     if $r->{'border-right-'.$prop}->[0]
1226     eq $r->{'border-top-'.$prop}->[0];
1227     $r->{'border-'.$prop} = [(join ' ', map {$_->[0]} @v),
1228     $r->{'border-top-'.$prop}->[1]];
1229     }
1230 wakaba 1.29 delete $r->{'border-top-'.$prop};
1231     delete $r->{'border-bottom-'.$prop};
1232     delete $r->{'border-right-'.$prop};
1233     delete $r->{'border-left-'.$prop};
1234     }
1235     }
1236     }
1237    
1238 wakaba 1.43 delete $r->{$_} for grep {not length $r->{$_}->[0]} keys %$r;
1239 wakaba 1.29 return $r;
1240     },
1241 wakaba 1.28 initial => ['KEYWORD', 'currentcolor'],
1242     #inherited => 0,
1243     compute => $Prop->{color}->{compute},
1244     };
1245     $Attr->{border_top_color} = $Prop->{'border-top-color'};
1246     $Key->{border_top_color} = $Prop->{'border-top-color'};
1247    
1248     $Prop->{'border-right-color'} = {
1249     css => 'border-right-color',
1250     dom => 'border_right_color',
1251     key => 'border_right_color',
1252     parse => $parse_color,
1253     serialize => $default_serializer,
1254 wakaba 1.29 serialize_multiple => $Prop->{'border-top-color'}->{serialize_multiple},
1255 wakaba 1.28 initial => ['KEYWORD', 'currentcolor'],
1256     #inherited => 0,
1257     compute => $Prop->{color}->{compute},
1258     };
1259 wakaba 1.29 $Attr->{border_right_color} = $Prop->{'border-right-color'};
1260     $Key->{border_right_color} = $Prop->{'border-right-color'};
1261 wakaba 1.28
1262     $Prop->{'border-bottom-color'} = {
1263     css => 'border-bottom-color',
1264     dom => 'border_bottom_color',
1265     key => 'border_bottom_color',
1266     parse => $parse_color,
1267     serialize => $default_serializer,
1268 wakaba 1.29 serialize_multiple => $Prop->{'border-top-color'}->{serialize_multiple},
1269 wakaba 1.28 initial => ['KEYWORD', 'currentcolor'],
1270     #inherited => 0,
1271     compute => $Prop->{color}->{compute},
1272     };
1273     $Attr->{border_bottom_color} = $Prop->{'border-bottom-color'};
1274     $Key->{border_bottom_color} = $Prop->{'border-bottom-color'};
1275    
1276     $Prop->{'border-left-color'} = {
1277     css => 'border-left-color',
1278     dom => 'border_left_color',
1279     key => 'border_left_color',
1280     parse => $parse_color,
1281     serialize => $default_serializer,
1282 wakaba 1.29 serialize_multiple => $Prop->{'border-top-color'}->{serialize_multiple},
1283 wakaba 1.28 initial => ['KEYWORD', 'currentcolor'],
1284     #inherited => 0,
1285     compute => $Prop->{color}->{compute},
1286     };
1287     $Attr->{border_left_color} = $Prop->{'border-left-color'};
1288     $Key->{border_left_color} = $Prop->{'border-left-color'};
1289    
1290     $Prop->{'outline-color'} = {
1291     css => 'outline-color',
1292     dom => 'outline_color',
1293     key => 'outline_color',
1294     parse => $parse_color,
1295     serialize => $default_serializer,
1296 wakaba 1.29 serialize_multiple => sub {
1297     my $self = shift;
1298     my $oc = $self->outline_color;
1299     my $os = $self->outline_style;
1300     my $ow = $self->outline_width;
1301     my $r = {};
1302 wakaba 1.34 if (length $oc and length $os and length $ow) {
1303 wakaba 1.43 $r->{outline} = [$ow . ' ' . $os . ' ' . $oc];
1304 wakaba 1.29 } else {
1305 wakaba 1.43 $r->{'outline-color'} = [$oc] if length $oc;
1306     $r->{'outline-style'} = [$os] if length $os;
1307     $r->{'outline-width'} = [$ow] if length $ow;
1308 wakaba 1.29 }
1309     return $r;
1310     },
1311 wakaba 1.28 initial => ['KEYWORD', '-manakai-invert-or-currentcolor'],
1312     #inherited => 0,
1313     compute => $Prop->{color}->{compute},
1314     };
1315     $Attr->{outline_color} = $Prop->{'outline-color'};
1316     $Key->{outline_color} = $Prop->{'outline-color'};
1317    
1318 wakaba 1.6 my $one_keyword_parser = sub {
1319     my ($self, $prop_name, $tt, $t, $onerror) = @_;
1320    
1321     if ($t->{type} == IDENT_TOKEN) {
1322     my $prop_value = lc $t->{value}; ## TODO: case folding
1323     $t = $tt->get_next_token;
1324     if ($Prop->{$prop_name}->{keyword}->{$prop_value} and
1325     $self->{prop_value}->{$prop_name}->{$prop_value}) {
1326 wakaba 1.7 return ($t, {$prop_name => ["KEYWORD", $prop_value]});
1327 wakaba 1.6 } elsif ($prop_value eq 'inherit') {
1328 wakaba 1.10 return ($t, {$prop_name => ['INHERIT']});
1329 wakaba 1.6 }
1330     }
1331    
1332 wakaba 1.39 $onerror->(type => "syntax error:'".$prop_name."'",
1333 wakaba 1.6 level => $self->{must_level},
1334 wakaba 1.38 uri => \$self->{href},
1335 wakaba 1.6 token => $t);
1336     return ($t, undef);
1337     };
1338    
1339     $Prop->{display} = {
1340     css => 'display',
1341     dom => 'display',
1342     key => 'display',
1343     parse => $one_keyword_parser,
1344 wakaba 1.11 serialize => $default_serializer,
1345 wakaba 1.6 keyword => {
1346     block => 1, inline => 1, 'inline-block' => 1, 'inline-table' => 1,
1347     'list-item' => 1, none => 1,
1348     table => 1, 'table-caption' => 1, 'table-cell' => 1, 'table-column' => 1,
1349     'table-column-group' => 1, 'table-header-group' => 1,
1350     'table-footer-group' => 1, 'table-row' => 1, 'table-row-group' => 1,
1351     },
1352 wakaba 1.9 initial => ["KEYWORD", "inline"],
1353     #inherited => 0,
1354     compute => sub {
1355     my ($self, $element, $prop_name, $specified_value) = @_;
1356     ## NOTE: CSS 2.1 Section 9.7.
1357    
1358     ## WARNING: |compute| for 'float' property invoke this CODE
1359     ## in some case. Careless modification might cause a infinite loop.
1360    
1361 wakaba 1.17 if (defined $specified_value and $specified_value->[0] eq 'KEYWORD') {
1362 wakaba 1.9 if ($specified_value->[1] eq 'none') {
1363     ## Case 1 [CSS 2.1]
1364     return $specified_value;
1365     } else {
1366     my $position = $self->get_computed_value ($element, 'position');
1367     if ($position->[0] eq 'KEYWORD' and
1368     ($position->[1] eq 'absolute' or
1369     $position->[1] eq 'fixed')) {
1370     ## Case 2 [CSS 2.1]
1371     #
1372     } else {
1373     my $float = $self->get_computed_value ($element, 'float');
1374     if ($float->[0] eq 'KEYWORD' and $float->[1] ne 'none') {
1375     ## Caes 3 [CSS 2.1]
1376     #
1377     } elsif (not defined $element->manakai_parent_element) {
1378     ## Case 4 [CSS 2.1]
1379     #
1380     } else {
1381     ## Case 5 [CSS 2.1]
1382     return $specified_value;
1383     }
1384     }
1385    
1386     return ["KEYWORD",
1387     {
1388     'inline-table' => 'table',
1389     inline => 'block',
1390     'run-in' => 'block',
1391     'table-row-group' => 'block',
1392     'table-column' => 'block',
1393     'table-column-group' => 'block',
1394     'table-header-group' => 'block',
1395     'table-footer-group' => 'block',
1396     'table-row' => 'block',
1397     'table-cell' => 'block',
1398     'table-caption' => 'block',
1399     'inline-block' => 'block',
1400     }->{$specified_value->[1]} || $specified_value->[1]];
1401     }
1402     } else {
1403     return $specified_value; ## Maybe an error of the implementation.
1404     }
1405     },
1406 wakaba 1.6 };
1407     $Attr->{display} = $Prop->{display};
1408     $Key->{display} = $Prop->{display};
1409    
1410     $Prop->{position} = {
1411     css => 'position',
1412     dom => 'position',
1413     key => 'position',
1414     parse => $one_keyword_parser,
1415 wakaba 1.11 serialize => $default_serializer,
1416 wakaba 1.6 keyword => {
1417     static => 1, relative => 1, absolute => 1, fixed => 1,
1418     },
1419 wakaba 1.10 initial => ["KEYWORD", "static"],
1420 wakaba 1.9 #inherited => 0,
1421     compute => $compute_as_specified,
1422 wakaba 1.6 };
1423     $Attr->{position} = $Prop->{position};
1424     $Key->{position} = $Prop->{position};
1425    
1426     $Prop->{float} = {
1427     css => 'float',
1428     dom => 'css_float',
1429     key => 'float',
1430     parse => $one_keyword_parser,
1431 wakaba 1.11 serialize => $default_serializer,
1432 wakaba 1.6 keyword => {
1433     left => 1, right => 1, none => 1,
1434     },
1435 wakaba 1.9 initial => ["KEYWORD", "none"],
1436     #inherited => 0,
1437     compute => sub {
1438     my ($self, $element, $prop_name, $specified_value) = @_;
1439     ## NOTE: CSS 2.1 Section 9.7.
1440    
1441     ## WARNING: |compute| for 'display' property invoke this CODE
1442     ## in some case. Careless modification might cause a infinite loop.
1443    
1444 wakaba 1.17 if (defined $specified_value and $specified_value->[0] eq 'KEYWORD') {
1445 wakaba 1.9 if ($specified_value->[1] eq 'none') {
1446     ## Case 1 [CSS 2.1]
1447     return $specified_value;
1448     } else {
1449     my $position = $self->get_computed_value ($element, 'position');
1450     if ($position->[0] eq 'KEYWORD' and
1451     ($position->[1] eq 'absolute' or
1452     $position->[1] eq 'fixed')) {
1453     ## Case 2 [CSS 2.1]
1454     return ["KEYWORD", "none"];
1455     }
1456     }
1457     }
1458    
1459     ## ISSUE: CSS 2.1 section 9.7 and 9.5.1 ('float' definition) disagree
1460     ## on computed value of 'float' property.
1461    
1462     ## Case 3, 4, and 5 [CSS 2.1]
1463     return $specified_value;
1464     },
1465 wakaba 1.6 };
1466     $Attr->{css_float} = $Prop->{float};
1467     $Attr->{style_float} = $Prop->{float}; ## NOTE: IEism
1468     $Key->{float} = $Prop->{float};
1469    
1470     $Prop->{clear} = {
1471     css => 'clear',
1472     dom => 'clear',
1473     key => 'clear',
1474     parse => $one_keyword_parser,
1475 wakaba 1.11 serialize => $default_serializer,
1476 wakaba 1.6 keyword => {
1477     left => 1, right => 1, none => 1, both => 1,
1478     },
1479 wakaba 1.9 initial => ["KEYWORD", "none"],
1480     #inherited => 0,
1481     compute => $compute_as_specified,
1482 wakaba 1.6 };
1483     $Attr->{clear} = $Prop->{clear};
1484     $Key->{clear} = $Prop->{clear};
1485    
1486     $Prop->{direction} = {
1487     css => 'direction',
1488     dom => 'direction',
1489     key => 'direction',
1490     parse => $one_keyword_parser,
1491 wakaba 1.11 serialize => $default_serializer,
1492 wakaba 1.6 keyword => {
1493     ltr => 1, rtl => 1,
1494     },
1495 wakaba 1.9 initial => ["KEYWORD", "ltr"],
1496     inherited => 1,
1497     compute => $compute_as_specified,
1498 wakaba 1.6 };
1499     $Attr->{direction} = $Prop->{direction};
1500     $Key->{direction} = $Prop->{direction};
1501    
1502     $Prop->{'unicode-bidi'} = {
1503     css => 'unicode-bidi',
1504     dom => 'unicode_bidi',
1505     key => 'unicode_bidi',
1506     parse => $one_keyword_parser,
1507 wakaba 1.11 serialize => $default_serializer,
1508 wakaba 1.6 keyword => {
1509     normal => 1, embed => 1, 'bidi-override' => 1,
1510     },
1511 wakaba 1.9 initial => ["KEYWORD", "normal"],
1512     #inherited => 0,
1513     compute => $compute_as_specified,
1514 wakaba 1.6 };
1515     $Attr->{unicode_bidi} = $Prop->{'unicode-bidi'};
1516     $Key->{unicode_bidi} = $Prop->{'unicode-bidi'};
1517    
1518 wakaba 1.11 $Prop->{overflow} = {
1519     css => 'overflow',
1520     dom => 'overflow',
1521     key => 'overflow',
1522     parse => $one_keyword_parser,
1523     serialize => $default_serializer,
1524     keyword => {
1525     visible => 1, hidden => 1, scroll => 1, auto => 1,
1526     },
1527     initial => ["KEYWORD", "visible"],
1528     #inherited => 0,
1529     compute => $compute_as_specified,
1530     };
1531     $Attr->{overflow} = $Prop->{overflow};
1532     $Key->{overflow} = $Prop->{overflow};
1533    
1534     $Prop->{visibility} = {
1535     css => 'visibility',
1536     dom => 'visibility',
1537     key => 'visibility',
1538     parse => $one_keyword_parser,
1539     serialize => $default_serializer,
1540     keyword => {
1541     visible => 1, hidden => 1, collapse => 1,
1542     },
1543     initial => ["KEYWORD", "visible"],
1544     #inherited => 0,
1545     compute => $compute_as_specified,
1546     };
1547     $Attr->{visibility} = $Prop->{visibility};
1548     $Key->{visibility} = $Prop->{visibility};
1549    
1550     $Prop->{'list-style-type'} = {
1551     css => 'list-style-type',
1552     dom => 'list_style_type',
1553     key => 'list_style_type',
1554     parse => $one_keyword_parser,
1555     serialize => $default_serializer,
1556     keyword => {
1557     qw/
1558     disc 1 circle 1 square 1 decimal 1 decimal-leading-zero 1
1559     lower-roman 1 upper-roman 1 lower-greek 1 lower-latin 1
1560     upper-latin 1 armenian 1 georgian 1 lower-alpha 1 upper-alpha 1
1561     none 1
1562     /,
1563     },
1564     initial => ["KEYWORD", 'disc'],
1565     inherited => 1,
1566     compute => $compute_as_specified,
1567     };
1568     $Attr->{list_style_type} = $Prop->{'list-style-type'};
1569     $Key->{list_style_type} = $Prop->{'list-style-type'};
1570    
1571     $Prop->{'list-style-position'} = {
1572     css => 'list-style-position',
1573     dom => 'list_style_position',
1574     key => 'list_style_position',
1575     parse => $one_keyword_parser,
1576     serialize => $default_serializer,
1577     keyword => {
1578     inside => 1, outside => 1,
1579     },
1580     initial => ["KEYWORD", 'outside'],
1581     inherited => 1,
1582     compute => $compute_as_specified,
1583     };
1584     $Attr->{list_style_position} = $Prop->{'list-style-position'};
1585     $Key->{list_style_position} = $Prop->{'list-style-position'};
1586    
1587 wakaba 1.12 $Prop->{'page-break-before'} = {
1588     css => 'page-break-before',
1589     dom => 'page_break_before',
1590     key => 'page_break_before',
1591     parse => $one_keyword_parser,
1592     serialize => $default_serializer,
1593     keyword => {
1594     auto => 1, always => 1, avoid => 1, left => 1, right => 1,
1595     },
1596     initial => ["KEYWORD", 'auto'],
1597 wakaba 1.15 #inherited => 0,
1598 wakaba 1.12 compute => $compute_as_specified,
1599     };
1600     $Attr->{page_break_before} = $Prop->{'page-break-before'};
1601     $Key->{page_break_before} = $Prop->{'page-break-before'};
1602    
1603     $Prop->{'page-break-after'} = {
1604     css => 'page-break-after',
1605     dom => 'page_break_after',
1606     key => 'page_break_after',
1607     parse => $one_keyword_parser,
1608     serialize => $default_serializer,
1609     keyword => {
1610     auto => 1, always => 1, avoid => 1, left => 1, right => 1,
1611     },
1612     initial => ["KEYWORD", 'auto'],
1613 wakaba 1.15 #inherited => 0,
1614 wakaba 1.12 compute => $compute_as_specified,
1615     };
1616     $Attr->{page_break_after} = $Prop->{'page-break-after'};
1617     $Key->{page_break_after} = $Prop->{'page-break-after'};
1618    
1619     $Prop->{'page-break-inside'} = {
1620     css => 'page-break-inside',
1621     dom => 'page_break_inside',
1622     key => 'page_break_inside',
1623     parse => $one_keyword_parser,
1624     serialize => $default_serializer,
1625     keyword => {
1626     auto => 1, avoid => 1,
1627     },
1628     initial => ["KEYWORD", 'auto'],
1629     inherited => 1,
1630     compute => $compute_as_specified,
1631     };
1632     $Attr->{page_break_inside} = $Prop->{'page-break-inside'};
1633     $Key->{page_break_inside} = $Prop->{'page-break-inside'};
1634    
1635 wakaba 1.15 $Prop->{'background-repeat'} = {
1636     css => 'background-repeat',
1637     dom => 'background_repeat',
1638     key => 'background_repeat',
1639     parse => $one_keyword_parser,
1640     serialize => $default_serializer,
1641 wakaba 1.30 serialize_multiple => $Prop->{'background-color'}->{serialize_multiple},
1642 wakaba 1.15 keyword => {
1643     repeat => 1, 'repeat-x' => 1, 'repeat-y' => 1, 'no-repeat' => 1,
1644     },
1645     initial => ["KEYWORD", 'repeat'],
1646     #inherited => 0,
1647     compute => $compute_as_specified,
1648     };
1649     $Attr->{background_repeat} = $Prop->{'background-repeat'};
1650     $Key->{backgroud_repeat} = $Prop->{'background-repeat'};
1651    
1652     $Prop->{'background-attachment'} = {
1653     css => 'background-attachment',
1654     dom => 'background_attachment',
1655     key => 'background_attachment',
1656     parse => $one_keyword_parser,
1657     serialize => $default_serializer,
1658 wakaba 1.30 serialize_multiple => $Prop->{'background-color'}->{serialize_multiple},
1659 wakaba 1.15 keyword => {
1660     scroll => 1, fixed => 1,
1661     },
1662     initial => ["KEYWORD", 'scroll'],
1663     #inherited => 0,
1664     compute => $compute_as_specified,
1665     };
1666     $Attr->{background_attachment} = $Prop->{'background-attachment'};
1667     $Key->{backgroud_attachment} = $Prop->{'background-attachment'};
1668    
1669     $Prop->{'font-style'} = {
1670     css => 'font-style',
1671 wakaba 1.18 dom => 'font_style',
1672     key => 'font_style',
1673 wakaba 1.15 parse => $one_keyword_parser,
1674     serialize => $default_serializer,
1675     keyword => {
1676     normal => 1, italic => 1, oblique => 1,
1677     },
1678     initial => ["KEYWORD", 'normal'],
1679     inherited => 1,
1680     compute => $compute_as_specified,
1681     };
1682     $Attr->{font_style} = $Prop->{'font-style'};
1683     $Key->{font_style} = $Prop->{'font-style'};
1684    
1685     $Prop->{'font-variant'} = {
1686     css => 'font-variant',
1687     dom => 'font_variant',
1688     key => 'font_variant',
1689     parse => $one_keyword_parser,
1690     serialize => $default_serializer,
1691     keyword => {
1692     normal => 1, 'small-caps' => 1,
1693     },
1694     initial => ["KEYWORD", 'normal'],
1695     inherited => 1,
1696     compute => $compute_as_specified,
1697     };
1698     $Attr->{font_variant} = $Prop->{'font-variant'};
1699     $Key->{font_variant} = $Prop->{'font-variant'};
1700    
1701 wakaba 1.16 $Prop->{'text-align'} = {
1702     css => 'text-align',
1703     dom => 'text_align',
1704     key => 'text_align',
1705     parse => $one_keyword_parser,
1706     serialize => $default_serializer,
1707     keyword => {
1708     left => 1, right => 1, center => 1, justify => 1, ## CSS 2
1709     begin => 1, end => 1, ## CSS 3
1710     },
1711     initial => ["KEYWORD", 'begin'],
1712     inherited => 1,
1713     compute => $compute_as_specified,
1714     };
1715     $Attr->{text_align} = $Prop->{'text-align'};
1716     $Key->{text_align} = $Prop->{'text-align'};
1717    
1718     $Prop->{'text-transform'} = {
1719     css => 'text-transform',
1720     dom => 'text_transform',
1721     key => 'text_transform',
1722     parse => $one_keyword_parser,
1723     serialize => $default_serializer,
1724     keyword => {
1725     capitalize => 1, uppercase => 1, lowercase => 1, none => 1,
1726     },
1727     initial => ["KEYWORD", 'none'],
1728     inherited => 1,
1729     compute => $compute_as_specified,
1730     };
1731     $Attr->{text_transform} = $Prop->{'text-transform'};
1732     $Key->{text_transform} = $Prop->{'text-transform'};
1733    
1734     $Prop->{'white-space'} = {
1735     css => 'white-space',
1736     dom => 'white_space',
1737     key => 'white_space',
1738     parse => $one_keyword_parser,
1739     serialize => $default_serializer,
1740     keyword => {
1741     normal => 1, pre => 1, nowrap => 1, 'pre-wrap' => 1, 'pre-line' => 1,
1742     },
1743     initial => ["KEYWORD", 'normal'],
1744     inherited => 1,
1745     compute => $compute_as_specified,
1746     };
1747     $Attr->{white_space} = $Prop->{'white-space'};
1748     $Key->{white_space} = $Prop->{'white-space'};
1749    
1750     $Prop->{'caption-side'} = {
1751     css => 'caption-side',
1752     dom => 'caption_side',
1753     key => 'caption_side',
1754     parse => $one_keyword_parser,
1755     serialize => $default_serializer,
1756     keyword => {
1757     top => 1, bottom => 1,
1758     },
1759     initial => ['KEYWORD', 'top'],
1760     inherited => 1,
1761     compute => $compute_as_specified,
1762     };
1763     $Attr->{caption_side} = $Prop->{'caption-side'};
1764     $Key->{caption_side} = $Prop->{'caption-side'};
1765    
1766     $Prop->{'table-layout'} = {
1767     css => 'table-layout',
1768     dom => 'table_layout',
1769     key => 'table_layout',
1770     parse => $one_keyword_parser,
1771     serialize => $default_serializer,
1772     keyword => {
1773     auto => 1, fixed => 1,
1774     },
1775     initial => ['KEYWORD', 'auto'],
1776     #inherited => 0,
1777     compute => $compute_as_specified,
1778     };
1779     $Attr->{table_layout} = $Prop->{'table-layout'};
1780     $Key->{table_layout} = $Prop->{'table-layout'};
1781    
1782     $Prop->{'border-collapse'} = {
1783     css => 'border-collapse',
1784     dom => 'border_collapse',
1785     key => 'border_collapse',
1786     parse => $one_keyword_parser,
1787     serialize => $default_serializer,
1788     keyword => {
1789     collapse => 1, separate => 1,
1790     },
1791     initial => ['KEYWORD', 'separate'],
1792     inherited => 1,
1793     compute => $compute_as_specified,
1794     };
1795     $Attr->{border_collapse} = $Prop->{'border-collapse'};
1796     $Key->{border_collapse} = $Prop->{'border-collapse'};
1797    
1798     $Prop->{'empty-cells'} = {
1799     css => 'empty-cells',
1800     dom => 'empty_cells',
1801     key => 'empty_cells',
1802     parse => $one_keyword_parser,
1803     serialize => $default_serializer,
1804     keyword => {
1805     show => 1, hide => 1,
1806     },
1807     initial => ['KEYWORD', 'show'],
1808     inherited => 1,
1809     compute => $compute_as_specified,
1810     };
1811     $Attr->{empty_cells} = $Prop->{'empty-cells'};
1812     $Key->{empty_cells} = $Prop->{'empty-cells'};
1813    
1814 wakaba 1.11 $Prop->{'z-index'} = {
1815     css => 'z-index',
1816     dom => 'z_index',
1817     key => 'z_index',
1818     parse => sub {
1819     my ($self, $prop_name, $tt, $t, $onerror) = @_;
1820    
1821 wakaba 1.12 my $sign = 1;
1822     if ($t->{type} == MINUS_TOKEN) {
1823     $sign = -1;
1824     $t = $tt->get_next_token;
1825     }
1826    
1827 wakaba 1.11 if ($t->{type} == NUMBER_TOKEN) {
1828     ## ISSUE: See <http://suika.fam.cx/gate/2005/sw/z-index> for
1829     ## browser compatibility issue.
1830     my $value = $t->{number};
1831     $t = $tt->get_next_token;
1832 wakaba 1.12 return ($t, {$prop_name => ["NUMBER", $sign * int ($value / 1)]});
1833     } elsif ($sign > 0 and $t->{type} == IDENT_TOKEN) {
1834 wakaba 1.11 my $value = lc $t->{value}; ## TODO: case
1835     $t = $tt->get_next_token;
1836     if ($value eq 'auto') {
1837     ## NOTE: |z-index| is the default value and therefore it must be
1838     ## supported anyway.
1839     return ($t, {$prop_name => ["KEYWORD", 'auto']});
1840     } elsif ($value eq 'inherit') {
1841     return ($t, {$prop_name => ['INHERIT']});
1842     }
1843     }
1844    
1845 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
1846 wakaba 1.11 level => $self->{must_level},
1847 wakaba 1.38 uri => \$self->{href},
1848 wakaba 1.11 token => $t);
1849     return ($t, undef);
1850     },
1851     serialize => $default_serializer,
1852     initial => ['KEYWORD', 'auto'],
1853     #inherited => 0,
1854     compute => $compute_as_specified,
1855     };
1856     $Attr->{z_index} = $Prop->{'z-index'};
1857     $Key->{z_index} = $Prop->{'z-index'};
1858    
1859 wakaba 1.12 $Prop->{orphans} = {
1860     css => 'orphans',
1861     dom => 'orphans',
1862     key => 'orphans',
1863     parse => sub {
1864     my ($self, $prop_name, $tt, $t, $onerror) = @_;
1865    
1866     my $sign = 1;
1867     if ($t->{type} == MINUS_TOKEN) {
1868     $t = $tt->get_next_token;
1869     $sign = -1;
1870     }
1871    
1872     if ($t->{type} == NUMBER_TOKEN) {
1873     ## ISSUE: See <http://suika.fam.cx/gate/2005/sw/orphans> and
1874     ## <http://suika.fam.cx/gate/2005/sw/widows> for
1875     ## browser compatibility issue.
1876     my $value = $t->{number};
1877     $t = $tt->get_next_token;
1878     return ($t, {$prop_name => ["NUMBER", $sign * int ($value / 1)]});
1879     } elsif ($sign > 0 and $t->{type} == IDENT_TOKEN) {
1880     my $value = lc $t->{value}; ## TODO: case
1881     $t = $tt->get_next_token;
1882     if ($value eq 'inherit') {
1883     return ($t, {$prop_name => ['INHERIT']});
1884     }
1885     }
1886    
1887 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
1888 wakaba 1.12 level => $self->{must_level},
1889 wakaba 1.38 uri => \$self->{href},
1890 wakaba 1.12 token => $t);
1891     return ($t, undef);
1892     },
1893     serialize => $default_serializer,
1894     initial => ['NUMBER', 2],
1895     inherited => 1,
1896     compute => $compute_as_specified,
1897     };
1898     $Attr->{orphans} = $Prop->{orphans};
1899     $Key->{orphans} = $Prop->{orphans};
1900    
1901     $Prop->{widows} = {
1902     css => 'widows',
1903     dom => 'widows',
1904     key => 'widows',
1905     parse => $Prop->{orphans}->{parse},
1906     serialize => $default_serializer,
1907     initial => ['NUMBER', 2],
1908     inherited => 1,
1909     compute => $compute_as_specified,
1910     };
1911     $Attr->{widows} = $Prop->{widows};
1912     $Key->{widows} = $Prop->{widows};
1913    
1914 wakaba 1.32 $Prop->{opacity} = {
1915     css => 'opacity',
1916     dom => 'opacity',
1917     key => 'opacity',
1918     parse => sub {
1919     my ($self, $prop_name, $tt, $t, $onerror) = @_;
1920    
1921     my $sign = 1;
1922     if ($t->{type} == MINUS_TOKEN) {
1923     $t = $tt->get_next_token;
1924     $sign = -1;
1925     }
1926    
1927     if ($t->{type} == NUMBER_TOKEN) {
1928     ## ISSUE: See <http://suika.fam.cx/gate/2005/sw/opacity> for
1929     ## browser compatibility issue.
1930     my $value = $t->{number};
1931     $t = $tt->get_next_token;
1932     return ($t, {$prop_name => ["NUMBER", $sign * $value]});
1933     } elsif ($sign > 0 and $t->{type} == IDENT_TOKEN) {
1934     my $value = lc $t->{value}; ## TODO: case
1935     $t = $tt->get_next_token;
1936     if ($value eq 'inherit') {
1937     return ($t, {$prop_name => ['INHERIT']});
1938     }
1939     }
1940    
1941 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
1942 wakaba 1.32 level => $self->{must_level},
1943 wakaba 1.38 uri => \$self->{href},
1944 wakaba 1.32 token => $t);
1945     return ($t, undef);
1946     },
1947     serialize => $default_serializer,
1948     initial => ['NUMBER', 2],
1949     inherited => 1,
1950     compute => sub {
1951     my ($self, $element, $prop_name, $specified_value) = @_;
1952    
1953     if (defined $specified_value) {
1954     if ($specified_value->[0] eq 'NUMBER') {
1955     if ($specified_value->[1] < 0) {
1956     return ['NUMBER', 0];
1957     } elsif ($specified_value->[1] > 1) {
1958     return ['NUMBER', 1];
1959     }
1960     }
1961     }
1962    
1963     return $specified_value;
1964     },
1965     serialize_multiple => sub {
1966     ## NOTE: This CODE is necessary to avoid two 'opacity' properties
1967     ## are outputed in |cssText| (for 'opacity' and for '-moz-opacity').
1968 wakaba 1.43 return {opacity => [shift->opacity]},
1969 wakaba 1.32 },
1970     };
1971     $Attr->{opacity} = $Prop->{opacity};
1972     $Key->{opacity} = $Prop->{opacity};
1973    
1974     $Prop->{'-moz-opacity'} = $Prop->{opacity};
1975 wakaba 1.36 $Attr->{_moz_opacity} = $Attr->{opacity};
1976 wakaba 1.32
1977 wakaba 1.19 my $length_unit = {
1978     em => 1, ex => 1, px => 1,
1979     in => 1, cm => 1, mm => 1, pt => 1, pc => 1,
1980     };
1981    
1982 wakaba 1.18 $Prop->{'font-size'} = {
1983     css => 'font-size',
1984     dom => 'font_size',
1985     key => 'font_size',
1986     parse => sub {
1987     my ($self, $prop_name, $tt, $t, $onerror) = @_;
1988    
1989 wakaba 1.49 my $has_sign;
1990 wakaba 1.18 my $sign = 1;
1991     if ($t->{type} == MINUS_TOKEN) {
1992     $t = $tt->get_next_token;
1993     $sign = -1;
1994 wakaba 1.49 $has_sign = 1;
1995     } elsif ($t->{type} == PLUS_TOKEN) {
1996     $t = $tt->get_next_token;
1997     $has_sign = 1;
1998 wakaba 1.18 }
1999    
2000     if ($t->{type} == DIMENSION_TOKEN) {
2001     my $value = $t->{number} * $sign;
2002     my $unit = lc $t->{value}; ## TODO: case
2003 wakaba 1.19 if ($length_unit->{$unit} and $value >= 0) {
2004 wakaba 1.49 $t = $tt->get_next_token;
2005 wakaba 1.18 return ($t, {$prop_name => ['DIMENSION', $value, $unit]});
2006     }
2007     } elsif ($t->{type} == PERCENTAGE_TOKEN) {
2008     my $value = $t->{number} * $sign;
2009 wakaba 1.49 if ($value >= 0) {
2010     $t = $tt->get_next_token;
2011     return ($t, {$prop_name => ['PERCENTAGE', $value]});
2012     }
2013 wakaba 1.20 } elsif ($t->{type} == NUMBER_TOKEN and
2014 wakaba 1.19 ($self->{unitless_px} or $t->{number} == 0)) {
2015 wakaba 1.18 my $value = $t->{number} * $sign;
2016 wakaba 1.49 if ($value >= 0) {
2017     $t = $tt->get_next_token;
2018     return ($t, {$prop_name => ['DIMENSION', $value, 'px']});
2019     }
2020     } elsif (not $has_sign and $t->{type} == IDENT_TOKEN) {
2021 wakaba 1.18 my $value = lc $t->{value}; ## TODO: case
2022     if ({
2023     'xx-small' => 1, 'x-small' => 1, small => 1, medium => 1,
2024     large => 1, 'x-large' => 1, 'xx-large' => 1,
2025 wakaba 1.49 '-manakai-xxx-large' => 1, '-webkit-xxx-large' => 1,
2026 wakaba 1.18 larger => 1, smaller => 1,
2027     }->{$value}) {
2028 wakaba 1.49 $t = $tt->get_next_token;
2029 wakaba 1.18 return ($t, {$prop_name => ['KEYWORD', $value]});
2030     } elsif ($value eq 'inherit') {
2031 wakaba 1.49 $t = $tt->get_next_token;
2032 wakaba 1.18 return ($t, {$prop_name => ['INHERIT']});
2033     }
2034     }
2035    
2036 wakaba 1.49 $onerror->(type => "syntax error:'$prop_name'",
2037 wakaba 1.18 level => $self->{must_level},
2038 wakaba 1.38 uri => \$self->{href},
2039 wakaba 1.18 token => $t);
2040     return ($t, undef);
2041     },
2042     serialize => $default_serializer,
2043     initial => ['KEYWORD', 'medium'],
2044     inherited => 1,
2045     compute => sub {
2046     my ($self, $element, $prop_name, $specified_value) = @_;
2047    
2048     if (defined $specified_value) {
2049     if ($specified_value->[0] eq 'DIMENSION') {
2050     my $unit = $specified_value->[2];
2051     my $value = $specified_value->[1];
2052    
2053     if ($unit eq 'em' or $unit eq 'ex') {
2054     $value *= 0.5 if $unit eq 'ex';
2055     ## TODO: Preferred way to determine the |ex| size is defined
2056     ## in CSS 2.1.
2057    
2058     my $parent_element = $element->manakai_parent_element;
2059     if (defined $parent_element) {
2060     $value *= $self->get_computed_value ($parent_element, $prop_name)
2061     ->[1];
2062     } else {
2063     $value *= $self->{font_size}->[3]; # medium
2064     }
2065     $unit = 'px';
2066     } elsif ({in => 1, cm => 1, mm => 1, pt => 1, pc => 1}->{$unit}) {
2067     ($value *= 12, $unit = 'pc') if $unit eq 'pc';
2068     ($value /= 72, $unit = 'in') if $unit eq 'pt';
2069     ($value *= 2.54, $unit = 'cm') if $unit eq 'in';
2070     ($value *= 10, $unit = 'mm') if $unit eq 'cm';
2071     ($value /= 0.26, $unit = 'px') if $unit eq 'mm';
2072     }
2073 wakaba 1.19 ## else: consistency error
2074 wakaba 1.18
2075     return ['DIMENSION', $value, $unit];
2076     } elsif ($specified_value->[0] eq 'PERCENTAGE') {
2077     my $parent_element = $element->manakai_parent_element;
2078     my $parent_cv;
2079     if (defined $parent_element) {
2080     $parent_cv = $self->get_computed_value
2081     ($parent_element, $prop_name);
2082     } else {
2083     $parent_cv = [undef, $self->{font_size}->[3]];
2084     }
2085     return ['DIMENSION', $parent_cv->[1] * $specified_value->[1] / 100,
2086     'px'];
2087     } elsif ($specified_value->[0] eq 'KEYWORD') {
2088     if ($specified_value->[1] eq 'larger') {
2089     my $parent_element = $element->manakai_parent_element;
2090     if (defined $parent_element) {
2091     my $parent_cv = $self->get_computed_value
2092     ($parent_element, $prop_name);
2093     return ['DIMENSION',
2094     $self->{get_larger_font_size}->($self, $parent_cv->[1]),
2095     'px'];
2096     } else { ## 'larger' relative to 'medium', initial of 'font-size'
2097     return ['DIMENSION', $self->{font_size}->[4], 'px'];
2098     }
2099     } elsif ($specified_value->[1] eq 'smaller') {
2100     my $parent_element = $element->manakai_parent_element;
2101     if (defined $parent_element) {
2102     my $parent_cv = $self->get_computed_value
2103     ($parent_element, $prop_name);
2104     return ['DIMENSION',
2105     $self->{get_smaller_font_size}->($self, $parent_cv->[1]),
2106     'px'];
2107     } else { ## 'smaller' relative to 'medium', initial of 'font-size'
2108     return ['DIMENSION', $self->{font_size}->[2], 'px'];
2109     }
2110     } else {
2111 wakaba 1.49 ## TODO: different computation in quirks mode?
2112 wakaba 1.18 return ['DIMENSION', $self->{font_size}->[{
2113     'xx-small' => 0,
2114     'x-small' => 1,
2115     small => 2,
2116     medium => 3,
2117     large => 4,
2118     'x-large' => 5,
2119     'xx-large' => 6,
2120     '-manakai-xxx-large' => 7,
2121 wakaba 1.49 '-webkit-xxx-large' => 7,
2122 wakaba 1.18 }->{$specified_value->[1]}], 'px'];
2123     }
2124     }
2125     }
2126    
2127     return $specified_value;
2128     },
2129     };
2130     $Attr->{font_size} = $Prop->{'font-size'};
2131     $Key->{font_size} = $Prop->{'font-size'};
2132    
2133 wakaba 1.19 my $compute_length = sub {
2134     my ($self, $element, $prop_name, $specified_value) = @_;
2135    
2136     if (defined $specified_value) {
2137     if ($specified_value->[0] eq 'DIMENSION') {
2138     my $unit = $specified_value->[2];
2139     my $value = $specified_value->[1];
2140    
2141     if ($unit eq 'em' or $unit eq 'ex') {
2142     $value *= 0.5 if $unit eq 'ex';
2143     ## TODO: Preferred way to determine the |ex| size is defined
2144     ## in CSS 2.1.
2145    
2146     $value *= $self->get_computed_value ($element, 'font-size')->[1];
2147     $unit = 'px';
2148     } elsif ({in => 1, cm => 1, mm => 1, pt => 1, pc => 1}->{$unit}) {
2149     ($value *= 12, $unit = 'pc') if $unit eq 'pc';
2150     ($value /= 72, $unit = 'in') if $unit eq 'pt';
2151     ($value *= 2.54, $unit = 'cm') if $unit eq 'in';
2152     ($value *= 10, $unit = 'mm') if $unit eq 'cm';
2153     ($value /= 0.26, $unit = 'px') if $unit eq 'mm';
2154     }
2155    
2156     return ['DIMENSION', $value, $unit];
2157     }
2158     }
2159    
2160     return $specified_value;
2161     }; # $compute_length
2162    
2163 wakaba 1.23 $Prop->{'letter-spacing'} = {
2164     css => 'letter-spacing',
2165     dom => 'letter_spacing',
2166     key => 'letter_spacing',
2167     parse => sub {
2168     my ($self, $prop_name, $tt, $t, $onerror) = @_;
2169    
2170 wakaba 1.24 ## NOTE: Used also for 'word-spacing', '-manakai-border-spacing-x',
2171     ## and '-manakai-border-spacing-y'.
2172 wakaba 1.23
2173     my $sign = 1;
2174     if ($t->{type} == MINUS_TOKEN) {
2175     $t = $tt->get_next_token;
2176     $sign = -1;
2177     }
2178     my $allow_negative = $Prop->{$prop_name}->{allow_negative};
2179    
2180     if ($t->{type} == DIMENSION_TOKEN) {
2181     my $value = $t->{number} * $sign;
2182     my $unit = lc $t->{value}; ## TODO: case
2183     $t = $tt->get_next_token;
2184 wakaba 1.24 if ($length_unit->{$unit} and ($allow_negative or $value >= 0)) {
2185 wakaba 1.23 return ($t, {$prop_name => ['DIMENSION', $value, $unit]});
2186     }
2187     } elsif ($t->{type} == NUMBER_TOKEN and
2188     ($self->{unitless_px} or $t->{number} == 0)) {
2189     my $value = $t->{number} * $sign;
2190     $t = $tt->get_next_token;
2191 wakaba 1.24 return ($t, {$prop_name => ['DIMENSION', $value, 'px']})
2192     if $allow_negative or $value >= 0;
2193 wakaba 1.23 } elsif ($sign > 0 and $t->{type} == IDENT_TOKEN) {
2194     my $value = lc $t->{value}; ## TODO: case
2195     $t = $tt->get_next_token;
2196 wakaba 1.24 if ($Prop->{$prop_name}->{keyword}->{$value}) {
2197 wakaba 1.23 return ($t, {$prop_name => ['KEYWORD', $value]});
2198     } elsif ($value eq 'inherit') {
2199     return ($t, {$prop_name => ['INHERIT']});
2200     }
2201     }
2202    
2203 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
2204 wakaba 1.23 level => $self->{must_level},
2205 wakaba 1.38 uri => \$self->{href},
2206 wakaba 1.23 token => $t);
2207     return ($t, undef);
2208     },
2209 wakaba 1.24 allow_negative => 1,
2210     keyword => {normal => 1},
2211 wakaba 1.23 serialize => $default_serializer,
2212     initial => ['KEYWORD', 'normal'],
2213     inherited => 1,
2214     compute => $compute_length,
2215     };
2216     $Attr->{letter_spacing} = $Prop->{'letter-spacing'};
2217     $Key->{letter_spacing} = $Prop->{'letter-spacing'};
2218    
2219     $Prop->{'word-spacing'} = {
2220     css => 'word-spacing',
2221     dom => 'word_spacing',
2222     key => 'word_spacing',
2223     parse => $Prop->{'letter-spacing'}->{parse},
2224 wakaba 1.24 allow_negative => 1,
2225     keyword => {normal => 1},
2226 wakaba 1.23 serialize => $default_serializer,
2227     initial => ['KEYWORD', 'normal'],
2228     inherited => 1,
2229     compute => $compute_length,
2230     };
2231     $Attr->{word_spacing} = $Prop->{'word-spacing'};
2232     $Key->{word_spacing} = $Prop->{'word-spacing'};
2233    
2234 wakaba 1.24 $Prop->{'-manakai-border-spacing-x'} = {
2235     css => '-manakai-border-spacing-x',
2236     dom => '_manakai_border_spacing_x',
2237     key => 'border_spacing_x',
2238     parse => $Prop->{'letter-spacing'}->{parse},
2239     #allow_negative => 0,
2240     #keyword => {},
2241     serialize => $default_serializer,
2242 wakaba 1.25 serialize_multiple => sub {
2243     my $self = shift;
2244    
2245     local $Error::Depth = $Error::Depth + 1;
2246     my $x = $self->_manakai_border_spacing_x;
2247     my $y = $self->_manakai_border_spacing_y;
2248     my $xi = $self->get_property_priority ('-manakai-border-spacing-x');
2249     my $yi = $self->get_property_priority ('-manakai-border-spacing-y');
2250 wakaba 1.34 if (length $x) {
2251     if (length $y) {
2252 wakaba 1.26 if ($xi eq $yi) {
2253 wakaba 1.25 if ($x eq $y) {
2254 wakaba 1.43 return {'border-spacing' => [$x, $xi]};
2255 wakaba 1.25 } else {
2256 wakaba 1.43 return {'border-spacing' => [$x . ' ' . $y, $xi]};
2257 wakaba 1.25 }
2258     } else {
2259 wakaba 1.43 return {'-manakai-border-spacing-x' => [$x, $xi],
2260     '-manakai-border-spacing-y' => [$y, $yi]};
2261 wakaba 1.25 }
2262     } else {
2263 wakaba 1.43 return {'-manakai-border-spacing-x' => [$x, $xi]};
2264 wakaba 1.25 }
2265     } else {
2266 wakaba 1.34 if (length $y) {
2267 wakaba 1.43 return {'-manakai-border-spacing-y' => [$y, $yi]};
2268 wakaba 1.25 } else {
2269     return {};
2270     }
2271     }
2272     },
2273 wakaba 1.24 initial => ['DIMENSION', 0, 'px'],
2274     inherited => 1,
2275     compute => $compute_length,
2276     };
2277     $Attr->{_manakai_border_spacing_x} = $Prop->{'-manakai-border-spacing-x'};
2278     $Key->{border_spacing_x} = $Prop->{'-manakai-border-spacing-x'};
2279    
2280     $Prop->{'-manakai-border-spacing-y'} = {
2281     css => '-manakai-border-spacing-y',
2282     dom => '_manakai_border_spacing_y',
2283     key => 'border_spacing_y',
2284     parse => $Prop->{'letter-spacing'}->{parse},
2285     #allow_negative => 0,
2286     #keyword => {},
2287     serialize => $default_serializer,
2288 wakaba 1.25 serialize_multiple => $Prop->{'-manakai-border-spacing-x'}
2289     ->{serialize_multiple},
2290 wakaba 1.24 initial => ['DIMENSION', 0, 'px'],
2291     inherited => 1,
2292     compute => $compute_length,
2293     };
2294     $Attr->{_manakai_border_spacing_y} = $Prop->{'-manakai-border-spacing-y'};
2295     $Key->{border_spacing_y} = $Prop->{'-manakai-border-spacing-y'};
2296    
2297 wakaba 1.19 $Prop->{'margin-top'} = {
2298     css => 'margin-top',
2299     dom => 'margin_top',
2300     key => 'margin_top',
2301     parse => sub {
2302     my ($self, $prop_name, $tt, $t, $onerror) = @_;
2303    
2304 wakaba 1.21 ## NOTE: Used for 'margin-top', 'margin-right', 'margin-bottom',
2305 wakaba 1.22 ## 'margin-left', 'top', 'right', 'bottom', 'left', 'padding-top',
2306     ## 'padding-right', 'padding-bottom', 'padding-left',
2307     ## 'border-top-width', 'border-right-width', 'border-bottom-width',
2308 wakaba 1.27 ## 'border-left-width', 'text-indent', 'background-position-x',
2309     ## and 'background-position-y'.
2310 wakaba 1.21
2311 wakaba 1.19 my $sign = 1;
2312 wakaba 1.41 my $has_sign;
2313 wakaba 1.19 if ($t->{type} == MINUS_TOKEN) {
2314     $t = $tt->get_next_token;
2315 wakaba 1.41 $has_sign = 1;
2316 wakaba 1.19 $sign = -1;
2317 wakaba 1.41 } elsif ($t->{type} == PLUS_TOKEN) {
2318     $t = $tt->get_next_token;
2319     $has_sign = 1;
2320 wakaba 1.19 }
2321 wakaba 1.22 my $allow_negative = $Prop->{$prop_name}->{allow_negative};
2322 wakaba 1.19
2323     if ($t->{type} == DIMENSION_TOKEN) {
2324     my $value = $t->{number} * $sign;
2325     my $unit = lc $t->{value}; ## TODO: case
2326     $t = $tt->get_next_token;
2327 wakaba 1.22 if ($length_unit->{$unit} and ($allow_negative or $value >= 0)) {
2328 wakaba 1.19 return ($t, {$prop_name => ['DIMENSION', $value, $unit]});
2329     }
2330     } elsif ($t->{type} == PERCENTAGE_TOKEN) {
2331     my $value = $t->{number} * $sign;
2332     $t = $tt->get_next_token;
2333 wakaba 1.22 return ($t, {$prop_name => ['PERCENTAGE', $value]})
2334     if $allow_negative or $value >= 0;
2335 wakaba 1.20 } elsif ($t->{type} == NUMBER_TOKEN and
2336 wakaba 1.19 ($self->{unitless_px} or $t->{number} == 0)) {
2337     my $value = $t->{number} * $sign;
2338     $t = $tt->get_next_token;
2339 wakaba 1.22 return ($t, {$prop_name => ['DIMENSION', $value, 'px']})
2340     if $allow_negative or $value >= 0;
2341 wakaba 1.41 } elsif (not $has_sign and $t->{type} == IDENT_TOKEN) {
2342 wakaba 1.19 my $value = lc $t->{value}; ## TODO: case
2343 wakaba 1.22 if ($Prop->{$prop_name}->{keyword}->{$value}) {
2344 wakaba 1.29 $t = $tt->get_next_token;
2345 wakaba 1.19 return ($t, {$prop_name => ['KEYWORD', $value]});
2346     } elsif ($value eq 'inherit') {
2347 wakaba 1.29 $t = $tt->get_next_token;
2348 wakaba 1.19 return ($t, {$prop_name => ['INHERIT']});
2349     }
2350 wakaba 1.29 ## NOTE: In the "else" case, don't procede the |$t| pointer
2351     ## for the support of 'border-top' property (and similar ones).
2352 wakaba 1.19 }
2353    
2354 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
2355 wakaba 1.19 level => $self->{must_level},
2356 wakaba 1.38 uri => \$self->{href},
2357 wakaba 1.19 token => $t);
2358     return ($t, undef);
2359     },
2360 wakaba 1.22 allow_negative => 1,
2361     keyword => {auto => 1},
2362 wakaba 1.19 serialize => $default_serializer,
2363 wakaba 1.42 serialize_multiple => sub {
2364     my $self = shift;
2365    
2366 wakaba 1.43 ## NOTE: Same as |serialize_multiple| of 'padding-top'.
2367    
2368 wakaba 1.42 my $use_shorthand = 1;
2369     my $t = $self->margin_top;
2370     undef $use_shorthand unless length $t;
2371     my $t_i = $self->get_property_priority ('margin-top');
2372     my $r = $self->margin_right;
2373     undef $use_shorthand
2374     if not length $r or
2375     ($r eq 'inherit' and $t ne 'inherit') or
2376     ($t eq 'inherit' and $r ne 'inherit');
2377     my $r_i = $self->get_property_priority ('margin-right');
2378     undef $use_shorthand unless $r_i eq $t_i;
2379     my $b = $self->margin_bottom;
2380     undef $use_shorthand
2381     if not length $b or
2382     ($b eq 'inherit' and $t ne 'inherit') or
2383     ($t eq 'inherit' and $b ne 'inherit');
2384     my $b_i = $self->get_property_priority ('margin-bottom');
2385     undef $use_shorthand unless $b_i eq $t_i;
2386     my $l = $self->margin_left;
2387     undef $use_shorthand
2388     if not length $l or
2389     ($l eq 'inherit' and $t ne 'inherit') or
2390     ($t eq 'inherit' and $l ne 'inherit');
2391     my $l_i = $self->get_property_priority ('margin-left');
2392     undef $use_shorthand unless $l_i eq $t_i;
2393    
2394     if ($use_shorthand) {
2395     $b .= ' ' . $l if $r ne $l;
2396     $r .= ' ' . $b if $t ne $b;
2397     $t .= ' ' . $r if $t ne $r;
2398 wakaba 1.45 return {margin => [$t, $t_i]};
2399 wakaba 1.42 } else {
2400     my $v = {};
2401     if (length $t) {
2402 wakaba 1.45 $v->{'margin-top'} = [$t, $t_i];
2403 wakaba 1.42 }
2404     if (length $r) {
2405 wakaba 1.45 $v->{'margin-right'} = [$r, $r_i];
2406 wakaba 1.42 }
2407     if (length $b) {
2408 wakaba 1.45 $v->{'margin-bottom'} = [$b, $b_i];
2409 wakaba 1.42 }
2410     if (length $l) {
2411 wakaba 1.45 $v->{'margin-left'} = [$l, $l_i];
2412 wakaba 1.42 }
2413     return $v;
2414     }
2415     },
2416 wakaba 1.19 initial => ['DIMENSION', 0, 'px'],
2417     #inherited => 0,
2418     compute => $compute_length,
2419     };
2420     $Attr->{margin_top} = $Prop->{'margin-top'};
2421     $Key->{margin_top} = $Prop->{'margin-top'};
2422    
2423     $Prop->{'margin-bottom'} = {
2424     css => 'margin-bottom',
2425     dom => 'margin_bottom',
2426     key => 'margin_bottom',
2427     parse => $Prop->{'margin-top'}->{parse},
2428 wakaba 1.22 allow_negative => 1,
2429     keyword => {auto => 1},
2430 wakaba 1.19 serialize => $default_serializer,
2431 wakaba 1.42 serialize_multiple => $Prop->{'margin-top'}->{serialize_multiple},
2432 wakaba 1.19 initial => ['DIMENSION', 0, 'px'],
2433     #inherited => 0,
2434     compute => $compute_length,
2435     };
2436     $Attr->{margin_bottom} = $Prop->{'margin-bottom'};
2437     $Key->{margin_bottom} = $Prop->{'margin-bottom'};
2438    
2439     $Prop->{'margin-right'} = {
2440     css => 'margin-right',
2441     dom => 'margin_right',
2442     key => 'margin_right',
2443     parse => $Prop->{'margin-top'}->{parse},
2444 wakaba 1.22 allow_negative => 1,
2445     keyword => {auto => 1},
2446 wakaba 1.19 serialize => $default_serializer,
2447 wakaba 1.42 serialize_multiple => $Prop->{'margin-top'}->{serialize_multiple},
2448 wakaba 1.19 initial => ['DIMENSION', 0, 'px'],
2449     #inherited => 0,
2450     compute => $compute_length,
2451     };
2452     $Attr->{margin_right} = $Prop->{'margin-right'};
2453     $Key->{margin_right} = $Prop->{'margin-right'};
2454    
2455     $Prop->{'margin-left'} = {
2456     css => 'margin-left',
2457     dom => 'margin_left',
2458     key => 'margin_left',
2459     parse => $Prop->{'margin-top'}->{parse},
2460 wakaba 1.22 allow_negative => 1,
2461     keyword => {auto => 1},
2462 wakaba 1.19 serialize => $default_serializer,
2463 wakaba 1.42 serialize_multiple => $Prop->{'margin-top'}->{serialize_multiple},
2464 wakaba 1.19 initial => ['DIMENSION', 0, 'px'],
2465     #inherited => 0,
2466     compute => $compute_length,
2467     };
2468     $Attr->{margin_left} = $Prop->{'margin-left'};
2469     $Key->{margin_left} = $Prop->{'margin-left'};
2470    
2471 wakaba 1.21 $Prop->{top} = {
2472     css => 'top',
2473     dom => 'top',
2474     key => 'top',
2475     parse => $Prop->{'margin-top'}->{parse},
2476 wakaba 1.22 allow_negative => 1,
2477     keyword => {auto => 1},
2478 wakaba 1.21 serialize => $default_serializer,
2479     initial => ['KEYWORD', 'auto'],
2480     #inherited => 0,
2481     compute_multiple => sub {
2482     my ($self, $element, $eid, $prop_name) = @_;
2483    
2484     my $pos_value = $self->get_computed_value ($element, 'position');
2485     if (defined $pos_value and $pos_value->[0] eq 'KEYWORD') {
2486     if ($pos_value->[1] eq 'static') {
2487     $self->{computed_value}->{$eid}->{top} = ['KEYWORD', 'auto'];
2488     $self->{computed_value}->{$eid}->{bottom} = ['KEYWORD', 'auto'];
2489     return;
2490     } elsif ($pos_value->[1] eq 'relative') {
2491     my $top_specified = $self->get_specified_value_no_inherit
2492     ($element, 'top');
2493     if (defined $top_specified and
2494     ($top_specified->[0] eq 'DIMENSION' or
2495     $top_specified->[0] eq 'PERCENTAGE')) {
2496     my $tv = $self->{computed_value}->{$eid}->{top}
2497     = $compute_length->($self, $element, 'top', $top_specified);
2498     $self->{computed_value}->{$eid}->{bottom}
2499     = [$tv->[0], -$tv->[1], $tv->[2]];
2500     } else { # top: auto
2501     my $bottom_specified = $self->get_specified_value_no_inherit
2502     ($element, 'bottom');
2503     if (defined $bottom_specified and
2504     ($bottom_specified->[0] eq 'DIMENSION' or
2505     $bottom_specified->[0] eq 'PERCENTAGE')) {
2506     my $tv = $self->{computed_value}->{$eid}->{bottom}
2507     = $compute_length->($self, $element, 'bottom',
2508     $bottom_specified);
2509     $self->{computed_value}->{$eid}->{top}
2510     = [$tv->[0], -$tv->[1], $tv->[2]];
2511     } else { # bottom: auto
2512     $self->{computed_value}->{$eid}->{top} = ['DIMENSION', 0, 'px'];
2513     $self->{computed_value}->{$eid}->{bottom} = ['DIMENSION', 0, 'px'];
2514     }
2515     }
2516     return;
2517     }
2518     }
2519    
2520     my $top_specified = $self->get_specified_value_no_inherit
2521     ($element, 'top');
2522     $self->{computed_value}->{$eid}->{top}
2523     = $compute_length->($self, $element, 'top', $top_specified);
2524     my $bottom_specified = $self->get_specified_value_no_inherit
2525     ($element, 'bottom');
2526     $self->{computed_value}->{$eid}->{bottom}
2527     = $compute_length->($self, $element, 'bottom', $bottom_specified);
2528     },
2529     };
2530     $Attr->{top} = $Prop->{top};
2531     $Key->{top} = $Prop->{top};
2532    
2533     $Prop->{bottom} = {
2534     css => 'bottom',
2535     dom => 'bottom',
2536     key => 'bottom',
2537     parse => $Prop->{'margin-top'}->{parse},
2538 wakaba 1.22 allow_negative => 1,
2539     keyword => {auto => 1},
2540 wakaba 1.21 serialize => $default_serializer,
2541     initial => ['KEYWORD', 'auto'],
2542     #inherited => 0,
2543     compute_multiple => $Prop->{top}->{compute_multiple},
2544     };
2545     $Attr->{bottom} = $Prop->{bottom};
2546     $Key->{bottom} = $Prop->{bottom};
2547    
2548     $Prop->{left} = {
2549     css => 'left',
2550     dom => 'left',
2551     key => 'left',
2552     parse => $Prop->{'margin-top'}->{parse},
2553 wakaba 1.22 allow_negative => 1,
2554     keyword => {auto => 1},
2555 wakaba 1.21 serialize => $default_serializer,
2556     initial => ['KEYWORD', 'auto'],
2557     #inherited => 0,
2558     compute_multiple => sub {
2559     my ($self, $element, $eid, $prop_name) = @_;
2560    
2561     my $pos_value = $self->get_computed_value ($element, 'position');
2562     if (defined $pos_value and $pos_value->[0] eq 'KEYWORD') {
2563     if ($pos_value->[1] eq 'static') {
2564     $self->{computed_value}->{$eid}->{left} = ['KEYWORD', 'auto'];
2565     $self->{computed_value}->{$eid}->{right} = ['KEYWORD', 'auto'];
2566     return;
2567     } elsif ($pos_value->[1] eq 'relative') {
2568     my $left_specified = $self->get_specified_value_no_inherit
2569     ($element, 'left');
2570     if (defined $left_specified and
2571     ($left_specified->[0] eq 'DIMENSION' or
2572     $left_specified->[0] eq 'PERCENTAGE')) {
2573     my $right_specified = $self->get_specified_value_no_inherit
2574     ($element, 'right');
2575     if (defined $right_specified and
2576     ($right_specified->[0] eq 'DIMENSION' or
2577     $right_specified->[0] eq 'PERCENTAGE')) {
2578     my $direction = $self->get_computed_value ($element, 'direction');
2579     if (defined $direction and $direction->[0] eq 'KEYWORD' and
2580     $direction->[0] eq 'ltr') {
2581     my $tv = $self->{computed_value}->{$eid}->{left}
2582     = $compute_length->($self, $element, 'left',
2583     $left_specified);
2584     $self->{computed_value}->{$eid}->{right}
2585     = [$tv->[0], -$tv->[1], $tv->[2]];
2586     } else {
2587     my $tv = $self->{computed_value}->{$eid}->{right}
2588     = $compute_length->($self, $element, 'right',
2589     $right_specified);
2590     $self->{computed_value}->{$eid}->{left}
2591     = [$tv->[0], -$tv->[1], $tv->[2]];
2592     }
2593     } else {
2594     my $tv = $self->{computed_value}->{$eid}->{left}
2595     = $compute_length->($self, $element, 'left', $left_specified);
2596     $self->{computed_value}->{$eid}->{right}
2597     = [$tv->[0], -$tv->[1], $tv->[2]];
2598     }
2599     } else { # left: auto
2600     my $right_specified = $self->get_specified_value_no_inherit
2601     ($element, 'right');
2602     if (defined $right_specified and
2603     ($right_specified->[0] eq 'DIMENSION' or
2604     $right_specified->[0] eq 'PERCENTAGE')) {
2605     my $tv = $self->{computed_value}->{$eid}->{right}
2606     = $compute_length->($self, $element, 'right',
2607     $right_specified);
2608     $self->{computed_value}->{$eid}->{left}
2609     = [$tv->[0], -$tv->[1], $tv->[2]];
2610     } else { # right: auto
2611     $self->{computed_value}->{$eid}->{left} = ['DIMENSION', 0, 'px'];
2612     $self->{computed_value}->{$eid}->{right} = ['DIMENSION', 0, 'px'];
2613     }
2614     }
2615     return;
2616     }
2617     }
2618    
2619     my $left_specified = $self->get_specified_value_no_inherit
2620     ($element, 'left');
2621     $self->{computed_value}->{$eid}->{left}
2622     = $compute_length->($self, $element, 'left', $left_specified);
2623     my $right_specified = $self->get_specified_value_no_inherit
2624     ($element, 'right');
2625     $self->{computed_value}->{$eid}->{right}
2626     = $compute_length->($self, $element, 'right', $right_specified);
2627     },
2628     };
2629     $Attr->{left} = $Prop->{left};
2630     $Key->{left} = $Prop->{left};
2631    
2632     $Prop->{right} = {
2633     css => 'right',
2634     dom => 'right',
2635     key => 'right',
2636     parse => $Prop->{'margin-top'}->{parse},
2637     serialize => $default_serializer,
2638     initial => ['KEYWORD', 'auto'],
2639     #inherited => 0,
2640     compute_multiple => $Prop->{left}->{compute_multiple},
2641     };
2642     $Attr->{right} = $Prop->{right};
2643     $Key->{right} = $Prop->{right};
2644    
2645 wakaba 1.22 $Prop->{width} = {
2646     css => 'width',
2647     dom => 'width',
2648     key => 'width',
2649     parse => $Prop->{'margin-top'}->{parse},
2650     #allow_negative => 0,
2651     keyword => {auto => 1},
2652     serialize => $default_serializer,
2653     initial => ['KEYWORD', 'auto'],
2654     #inherited => 0,
2655     compute => $compute_length,
2656     ## NOTE: See <http://suika.fam.cx/gate/2005/sw/width> for
2657     ## browser compatibility issues.
2658     };
2659     $Attr->{width} = $Prop->{width};
2660     $Key->{width} = $Prop->{width};
2661    
2662     $Prop->{'min-width'} = {
2663     css => 'min-width',
2664     dom => 'min_width',
2665     key => 'min_width',
2666     parse => $Prop->{'margin-top'}->{parse},
2667     #allow_negative => 0,
2668     #keyword => {},
2669     serialize => $default_serializer,
2670     initial => ['DIMENSION', 0, 'px'],
2671     #inherited => 0,
2672     compute => $compute_length,
2673     };
2674     $Attr->{min_width} = $Prop->{'min-width'};
2675     $Key->{min_width} = $Prop->{'min-width'};
2676    
2677     $Prop->{'max-width'} = {
2678     css => 'max-width',
2679     dom => 'max_width',
2680     key => 'max_width',
2681     parse => $Prop->{'margin-top'}->{parse},
2682     #allow_negative => 0,
2683     keyword => {none => 1},
2684     serialize => $default_serializer,
2685     initial => ['KEYWORD', 'none'],
2686     #inherited => 0,
2687     compute => $compute_length,
2688     };
2689     $Attr->{max_width} = $Prop->{'max-width'};
2690     $Key->{max_width} = $Prop->{'max-width'};
2691    
2692     $Prop->{height} = {
2693     css => 'height',
2694     dom => 'height',
2695     key => 'height',
2696     parse => $Prop->{'margin-top'}->{parse},
2697     #allow_negative => 0,
2698     keyword => {auto => 1},
2699     serialize => $default_serializer,
2700     initial => ['KEYWORD', 'auto'],
2701     #inherited => 0,
2702     compute => $compute_length,
2703     ## NOTE: See <http://suika.fam.cx/gate/2005/sw/height> for
2704     ## browser compatibility issues.
2705     };
2706     $Attr->{height} = $Prop->{height};
2707     $Key->{height} = $Prop->{height};
2708    
2709     $Prop->{'min-height'} = {
2710     css => 'min-height',
2711     dom => 'min_height',
2712     key => 'min_height',
2713     parse => $Prop->{'margin-top'}->{parse},
2714     #allow_negative => 0,
2715     #keyword => {},
2716     serialize => $default_serializer,
2717     initial => ['DIMENSION', 0, 'px'],
2718     #inherited => 0,
2719     compute => $compute_length,
2720     };
2721     $Attr->{min_height} = $Prop->{'min-height'};
2722     $Key->{min_height} = $Prop->{'min-height'};
2723    
2724     $Prop->{'max-height'} = {
2725     css => 'max-height',
2726     dom => 'max_height',
2727     key => 'max_height',
2728     parse => $Prop->{'margin-top'}->{parse},
2729     #allow_negative => 0,
2730     keyword => {none => 1},
2731     serialize => $default_serializer,
2732     initial => ['KEYWORD', 'none'],
2733     #inherited => 0,
2734     compute => $compute_length,
2735     };
2736     $Attr->{max_height} = $Prop->{'max-height'};
2737     $Key->{max_height} = $Prop->{'max-height'};
2738    
2739     $Prop->{'line-height'} = {
2740     css => 'line-height',
2741     dom => 'line_height',
2742     key => 'line_height',
2743 wakaba 1.19 parse => sub {
2744     my ($self, $prop_name, $tt, $t, $onerror) = @_;
2745    
2746 wakaba 1.22 ## NOTE: Similar to 'margin-top', but different handling
2747     ## for unitless numbers.
2748    
2749 wakaba 1.19 my $sign = 1;
2750     if ($t->{type} == MINUS_TOKEN) {
2751     $t = $tt->get_next_token;
2752     $sign = -1;
2753     }
2754 wakaba 1.22 my $allow_negative = $Prop->{$prop_name}->{allow_negative};
2755 wakaba 1.19
2756     if ($t->{type} == DIMENSION_TOKEN) {
2757     my $value = $t->{number} * $sign;
2758     my $unit = lc $t->{value}; ## TODO: case
2759     $t = $tt->get_next_token;
2760     if ($length_unit->{$unit} and $value >= 0) {
2761     return ($t, {$prop_name => ['DIMENSION', $value, $unit]});
2762     }
2763     } elsif ($t->{type} == PERCENTAGE_TOKEN) {
2764     my $value = $t->{number} * $sign;
2765     $t = $tt->get_next_token;
2766 wakaba 1.22 return ($t, {$prop_name => ['PERCENTAGE', $value]})
2767     if $value >= 0;
2768     } elsif ($t->{type} == NUMBER_TOKEN) {
2769 wakaba 1.19 my $value = $t->{number} * $sign;
2770     $t = $tt->get_next_token;
2771 wakaba 1.22 return ($t, {$prop_name => ['NUMBER', $value]}) if $value >= 0;
2772 wakaba 1.19 } elsif ($sign > 0 and $t->{type} == IDENT_TOKEN) {
2773     my $value = lc $t->{value}; ## TODO: case
2774     $t = $tt->get_next_token;
2775 wakaba 1.22 if ($value eq 'normal') {
2776     return ($t, {$prop_name => ['KEYWORD', $value]});
2777     } elsif ($value eq 'inherit') {
2778 wakaba 1.19 return ($t, {$prop_name => ['INHERIT']});
2779     }
2780     }
2781    
2782 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
2783 wakaba 1.19 level => $self->{must_level},
2784 wakaba 1.38 uri => \$self->{href},
2785 wakaba 1.19 token => $t);
2786     return ($t, undef);
2787     },
2788     serialize => $default_serializer,
2789 wakaba 1.22 initial => ['KEYWORD', 'normal'],
2790     inherited => 1,
2791     compute => $compute_length,
2792     };
2793     $Attr->{line_height} = $Prop->{'line-height'};
2794     $Key->{line_height} = $Prop->{'line-height'};
2795    
2796     $Prop->{'vertical-align'} = {
2797     css => 'vertical-align',
2798     dom => 'vertical_align',
2799     key => 'vertical_align',
2800     parse => $Prop->{'margin-top'}->{parse},
2801     allow_negative => 1,
2802     keyword => {
2803     baseline => 1, sub => 1, super => 1, top => 1, 'text-top' => 1,
2804     middle => 1, bottom => 1, 'text-bottom' => 1,
2805     },
2806     ## NOTE: Currently, we don't support option to select subset of keywords
2807     ## supported by application (i.e.
2808     ## $parser->{prop_value}->{'line-height'->{$keyword}). Should we support
2809     ## it?
2810     serialize => $default_serializer,
2811     initial => ['KEYWORD', 'baseline'],
2812     #inherited => 0,
2813     compute => $compute_length,
2814     ## NOTE: See <http://suika.fam.cx/gate/2005/sw/vertical-align> for
2815     ## browser compatibility issues.
2816     };
2817     $Attr->{vertical_align} = $Prop->{'vertical-align'};
2818     $Key->{vertical_align} = $Prop->{'vertical-align'};
2819    
2820 wakaba 1.23 $Prop->{'text-indent'} = {
2821     css => 'text-indent',
2822     dom => 'text_indent',
2823     key => 'text_indent',
2824     parse => $Prop->{'margin-top'}->{parse},
2825     allow_negative => 1,
2826     keyword => {},
2827     serialize => $default_serializer,
2828     initial => ['DIMENSION', 0, 'px'],
2829     inherited => 1,
2830     compute => $compute_length,
2831     };
2832     $Attr->{text_indent} = $Prop->{'text-indent'};
2833     $Key->{text_indent} = $Prop->{'text-indent'};
2834    
2835 wakaba 1.27 $Prop->{'background-position-x'} = {
2836     css => 'background-position-x',
2837     dom => 'background_position_x',
2838     key => 'background_position_x',
2839     parse => $Prop->{'margin-top'}->{parse},
2840     allow_negative => 1,
2841     keyword => {left => 1, center => 1, right => 1},
2842     serialize => $default_serializer,
2843     initial => ['PERCENTAGE', 0],
2844     #inherited => 0,
2845     compute => sub {
2846     my ($self, $element, $prop_name, $specified_value) = @_;
2847    
2848     if (defined $specified_value and $specified_value->[0] eq 'KEYWORD') {
2849     my $v = {
2850     left => 0, center => 50, right => 100, top => 0, bottom => 100,
2851     }->{$specified_value->[1]};
2852     if (defined $v) {
2853     return ['PERCENTAGE', $v];
2854     } else {
2855     return $specified_value;
2856     }
2857     } else {
2858     return $compute_length->(@_);
2859     }
2860     },
2861 wakaba 1.30 serialize_multiple => $Prop->{'background-color'}->{serialize_multiple},
2862 wakaba 1.27 };
2863     $Attr->{background_position_x} = $Prop->{'background-position-x'};
2864     $Key->{background_position_x} = $Prop->{'background-position-x'};
2865    
2866     $Prop->{'background-position-y'} = {
2867     css => 'background-position-y',
2868     dom => 'background_position_y',
2869     key => 'background_position_y',
2870     parse => $Prop->{'margin-top'}->{parse},
2871     allow_negative => 1,
2872     keyword => {top => 1, center => 1, bottom => 1},
2873     serialize => $default_serializer,
2874 wakaba 1.30 serialize_multiple => $Prop->{'background-color'}->{serialize_multiple},
2875 wakaba 1.27 initial => ['PERCENTAGE', 0],
2876     #inherited => 0,
2877     compute => $Prop->{'background-position-x'}->{compute},
2878     };
2879     $Attr->{background_position_y} = $Prop->{'background-position-y'};
2880     $Key->{background_position_y} = $Prop->{'background-position-y'};
2881    
2882 wakaba 1.22 $Prop->{'padding-top'} = {
2883     css => 'padding-top',
2884     dom => 'padding_top',
2885     key => 'padding_top',
2886     parse => $Prop->{'margin-top'}->{parse},
2887     #allow_negative => 0,
2888     #keyword => {},
2889     serialize => $default_serializer,
2890 wakaba 1.43 serialize_multiple => sub {
2891     my $self = shift;
2892    
2893     ## NOTE: Same as |serialize_multiple| of 'margin-top'.
2894    
2895     my $use_shorthand = 1;
2896     my $t = $self->padding_top;
2897     undef $use_shorthand unless length $t;
2898     my $t_i = $self->get_property_priority ('padding-top');
2899     my $r = $self->padding_right;
2900     undef $use_shorthand
2901     if not length $r or
2902     ($r eq 'inherit' and $t ne 'inherit') or
2903     ($t eq 'inherit' and $r ne 'inherit');
2904     my $r_i = $self->get_property_priority ('padding-right');
2905     undef $use_shorthand unless $r_i eq $t_i;
2906     my $b = $self->padding_bottom;
2907     undef $use_shorthand
2908     if not length $b or
2909     ($b eq 'inherit' and $t ne 'inherit') or
2910     ($t eq 'inherit' and $b ne 'inherit');
2911     my $b_i = $self->get_property_priority ('padding-bottom');
2912     undef $use_shorthand unless $b_i eq $t_i;
2913     my $l = $self->padding_left;
2914     undef $use_shorthand
2915     if not length $l or
2916     ($l eq 'inherit' and $t ne 'inherit') or
2917     ($t eq 'inherit' and $l ne 'inherit');
2918     my $l_i = $self->get_property_priority ('padding-left');
2919     undef $use_shorthand unless $l_i eq $t_i;
2920    
2921     if ($use_shorthand) {
2922     $b .= ' ' . $l if $r ne $l;
2923     $r .= ' ' . $b if $t ne $b;
2924     $t .= ' ' . $r if $t ne $r;
2925 wakaba 1.45 return {padding => [$t, $t_i]};
2926 wakaba 1.43 } else {
2927     my $v = {};
2928     if (length $t) {
2929 wakaba 1.45 $v->{'padding-top'} = [$t, $t_i];
2930 wakaba 1.43 }
2931     if (length $r) {
2932 wakaba 1.45 $v->{'padding-right'} = [$r, $r_i];
2933 wakaba 1.43 }
2934     if (length $b) {
2935 wakaba 1.45 $v->{'padding-bottom'} = [$b, $b_i];
2936 wakaba 1.43 }
2937     if (length $l) {
2938 wakaba 1.45 $v->{'padding-left'} = [$l, $l_i];
2939 wakaba 1.43 }
2940     return $v;
2941     }
2942     },
2943 wakaba 1.19 initial => ['DIMENSION', 0, 'px'],
2944     #inherited => 0,
2945     compute => $compute_length,
2946     };
2947     $Attr->{padding_top} = $Prop->{'padding-top'};
2948     $Key->{padding_top} = $Prop->{'padding-top'};
2949    
2950     $Prop->{'padding-bottom'} = {
2951     css => 'padding-bottom',
2952     dom => 'padding_bottom',
2953     key => 'padding_bottom',
2954     parse => $Prop->{'padding-top'}->{parse},
2955 wakaba 1.22 #allow_negative => 0,
2956     #keyword => {},
2957 wakaba 1.19 serialize => $default_serializer,
2958 wakaba 1.43 serialize_multiple => $Prop->{'padding-top'}->{serialize_multiple},
2959 wakaba 1.19 initial => ['DIMENSION', 0, 'px'],
2960     #inherited => 0,
2961     compute => $compute_length,
2962     };
2963     $Attr->{padding_bottom} = $Prop->{'padding-bottom'};
2964     $Key->{padding_bottom} = $Prop->{'padding-bottom'};
2965    
2966     $Prop->{'padding-right'} = {
2967     css => 'padding-right',
2968     dom => 'padding_right',
2969     key => 'padding_right',
2970     parse => $Prop->{'padding-top'}->{parse},
2971 wakaba 1.22 #allow_negative => 0,
2972     #keyword => {},
2973 wakaba 1.19 serialize => $default_serializer,
2974 wakaba 1.43 serialize_multiple => $Prop->{'padding-top'}->{serialize_multiple},
2975 wakaba 1.19 initial => ['DIMENSION', 0, 'px'],
2976     #inherited => 0,
2977     compute => $compute_length,
2978     };
2979     $Attr->{padding_right} = $Prop->{'padding-right'};
2980     $Key->{padding_right} = $Prop->{'padding-right'};
2981    
2982     $Prop->{'padding-left'} = {
2983     css => 'padding-left',
2984     dom => 'padding_left',
2985     key => 'padding_left',
2986     parse => $Prop->{'padding-top'}->{parse},
2987 wakaba 1.22 #allow_negative => 0,
2988     #keyword => {},
2989 wakaba 1.19 serialize => $default_serializer,
2990 wakaba 1.43 serialize_multiple => $Prop->{'padding-top'}->{serialize_multiple},
2991 wakaba 1.19 initial => ['DIMENSION', 0, 'px'],
2992     #inherited => 0,
2993     compute => $compute_length,
2994     };
2995     $Attr->{padding_left} = $Prop->{'padding-left'};
2996     $Key->{padding_left} = $Prop->{'padding-left'};
2997    
2998 wakaba 1.20 $Prop->{'border-top-width'} = {
2999     css => 'border-top-width',
3000     dom => 'border_top_width',
3001     key => 'border_top_width',
3002 wakaba 1.22 parse => $Prop->{'margin-top'}->{parse},
3003     #allow_negative => 0,
3004     keyword => {thin => 1, medium => 1, thick => 1},
3005 wakaba 1.20 serialize => $default_serializer,
3006 wakaba 1.29 serialize_multiple => $Prop->{'border-top-color'}->{serialize_multiple},
3007 wakaba 1.20 initial => ['KEYWORD', 'medium'],
3008     #inherited => 0,
3009     compute => sub {
3010     my ($self, $element, $prop_name, $specified_value) = @_;
3011    
3012 wakaba 1.23 ## NOTE: Used for 'border-top-width', 'border-right-width',
3013     ## 'border-bottom-width', 'border-right-width', and
3014     ## 'outline-width'.
3015    
3016 wakaba 1.20 my $style_prop = $prop_name;
3017     $style_prop =~ s/width/style/;
3018     my $style = $self->get_computed_value ($element, $style_prop);
3019     if (defined $style and $style->[0] eq 'KEYWORD' and
3020     ($style->[1] eq 'none' or $style->[1] eq 'hidden')) {
3021     return ['DIMENSION', 0, 'px'];
3022     }
3023    
3024     my $value = $compute_length->(@_);
3025     if (defined $value and $value->[0] eq 'KEYWORD') {
3026     if ($value->[1] eq 'thin') {
3027     return ['DIMENSION', 1, 'px']; ## Firefox/Opera
3028     } elsif ($value->[1] eq 'medium') {
3029     return ['DIMENSION', 3, 'px']; ## Firefox/Opera
3030     } elsif ($value->[1] eq 'thick') {
3031     return ['DIMENSION', 5, 'px']; ## Firefox
3032     }
3033     }
3034     return $value;
3035     },
3036 wakaba 1.23 ## NOTE: CSS3 will allow <percentage> as an option in <border-width>.
3037     ## Opera 9 has already implemented it.
3038 wakaba 1.20 };
3039     $Attr->{border_top_width} = $Prop->{'border-top-width'};
3040     $Key->{border_top_width} = $Prop->{'border-top-width'};
3041    
3042     $Prop->{'border-right-width'} = {
3043     css => 'border-right-width',
3044     dom => 'border_right_width',
3045     key => 'border_right_width',
3046     parse => $Prop->{'border-top-width'}->{parse},
3047 wakaba 1.22 #allow_negative => 0,
3048     keyword => {thin => 1, medium => 1, thick => 1},
3049 wakaba 1.20 serialize => $default_serializer,
3050 wakaba 1.29 serialize_multiple => $Prop->{'border-top-color'}->{serialize_multiple},
3051 wakaba 1.20 initial => ['KEYWORD', 'medium'],
3052     #inherited => 0,
3053     compute => $Prop->{'border-top-width'}->{compute},
3054     };
3055     $Attr->{border_right_width} = $Prop->{'border-right-width'};
3056     $Key->{border_right_width} = $Prop->{'border-right-width'};
3057    
3058     $Prop->{'border-bottom-width'} = {
3059     css => 'border-bottom-width',
3060     dom => 'border_bottom_width',
3061     key => 'border_bottom_width',
3062     parse => $Prop->{'border-top-width'}->{parse},
3063 wakaba 1.22 #allow_negative => 0,
3064     keyword => {thin => 1, medium => 1, thick => 1},
3065 wakaba 1.20 serialize => $default_serializer,
3066 wakaba 1.29 serialize_multiple => $Prop->{'border-top-color'}->{serialize_multiple},
3067 wakaba 1.20 initial => ['KEYWORD', 'medium'],
3068     #inherited => 0,
3069     compute => $Prop->{'border-top-width'}->{compute},
3070     };
3071     $Attr->{border_bottom_width} = $Prop->{'border-bottom-width'};
3072     $Key->{border_bottom_width} = $Prop->{'border-bottom-width'};
3073    
3074     $Prop->{'border-left-width'} = {
3075     css => 'border-left-width',
3076     dom => 'border_left_width',
3077     key => 'border_left_width',
3078     parse => $Prop->{'border-top-width'}->{parse},
3079 wakaba 1.22 #allow_negative => 0,
3080     keyword => {thin => 1, medium => 1, thick => 1},
3081 wakaba 1.20 serialize => $default_serializer,
3082 wakaba 1.29 serialize_multiple => $Prop->{'border-top-color'}->{serialize_multiple},
3083 wakaba 1.20 initial => ['KEYWORD', 'medium'],
3084     #inherited => 0,
3085     compute => $Prop->{'border-top-width'}->{compute},
3086     };
3087     $Attr->{border_left_width} = $Prop->{'border-left-width'};
3088     $Key->{border_left_width} = $Prop->{'border-left-width'};
3089    
3090 wakaba 1.23 $Prop->{'outline-width'} = {
3091     css => 'outline-width',
3092     dom => 'outline_width',
3093     key => 'outline_width',
3094     parse => $Prop->{'border-top-width'}->{parse},
3095     #allow_negative => 0,
3096     keyword => {thin => 1, medium => 1, thick => 1},
3097     serialize => $default_serializer,
3098 wakaba 1.29 serialize_multiple => $Prop->{'outline-color'}->{serialize_multiple},
3099 wakaba 1.23 initial => ['KEYWORD', 'medium'],
3100     #inherited => 0,
3101     compute => $Prop->{'border-top-width'}->{compute},
3102     };
3103     $Attr->{outline_width} = $Prop->{'outline-width'};
3104     $Key->{outline_width} = $Prop->{'outline-width'};
3105    
3106 wakaba 1.15 $Prop->{'font-weight'} = {
3107     css => 'font-weight',
3108     dom => 'font_weight',
3109     key => 'font_weight',
3110     parse => sub {
3111     my ($self, $prop_name, $tt, $t, $onerror) = @_;
3112    
3113 wakaba 1.50 my $has_sign;
3114     if ($t->{type} == PLUS_TOKEN) {
3115     $has_sign = 1;
3116     $t = $tt->get_next_token;
3117     }
3118    
3119 wakaba 1.15 if ($t->{type} == NUMBER_TOKEN) {
3120     ## ISSUE: See <http://suika.fam.cx/gate/2005/sw/font-weight> for
3121     ## browser compatibility issue.
3122     my $value = $t->{number};
3123     $t = $tt->get_next_token;
3124     if ($value % 100 == 0 and 100 <= $value and $value <= 900) {
3125     return ($t, {$prop_name => ['WEIGHT', $value, 0]});
3126     }
3127 wakaba 1.50 } elsif (not $has_sign and $t->{type} == IDENT_TOKEN) {
3128 wakaba 1.15 my $value = lc $t->{value}; ## TODO: case
3129     if ({
3130     normal => 1, bold => 1, bolder => 1, lighter => 1,
3131     }->{$value}) {
3132 wakaba 1.50 $t = $tt->get_next_token;
3133 wakaba 1.15 return ($t, {$prop_name => ['KEYWORD', $value]});
3134     } elsif ($value eq 'inherit') {
3135 wakaba 1.50 $t = $tt->get_next_token;
3136 wakaba 1.15 return ($t, {$prop_name => ['INHERIT']});
3137     }
3138     }
3139    
3140 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
3141 wakaba 1.15 level => $self->{must_level},
3142 wakaba 1.38 uri => \$self->{href},
3143 wakaba 1.15 token => $t);
3144     return ($t, undef);
3145     },
3146     serialize => $default_serializer,
3147     initial => ['KEYWORD', 'normal'],
3148     inherited => 1,
3149     compute => sub {
3150     my ($self, $element, $prop_name, $specified_value) = @_;
3151    
3152 wakaba 1.17 if (defined $specified_value and $specified_value->[0] eq 'KEYWORD') {
3153 wakaba 1.15 if ($specified_value->[1] eq 'normal') {
3154     return ['WEIGHT', 400, 0];
3155     } elsif ($specified_value->[1] eq 'bold') {
3156     return ['WEIGHT', 700, 0];
3157     } elsif ($specified_value->[1] eq 'bolder') {
3158     my $parent_element = $element->manakai_parent_element;
3159     if (defined $parent_element) {
3160     my $parent_value = $self->get_cascaded_value
3161     ($parent_element, $prop_name); ## NOTE: What Firefox does.
3162     return ['WEIGHT', $parent_value->[1], $parent_value->[2] + 1];
3163     } else {
3164     return ['WEIGHT', 400, 1];
3165     }
3166     } elsif ($specified_value->[1] eq 'lighter') {
3167     my $parent_element = $element->manakai_parent_element;
3168     if (defined $parent_element) {
3169     my $parent_value = $self->get_cascaded_value
3170     ($parent_element, $prop_name); ## NOTE: What Firefox does.
3171     return ['WEIGHT', $parent_value->[1], $parent_value->[2] - 1];
3172     } else {
3173     return ['WEIGHT', 400, 1];
3174     }
3175     }
3176 wakaba 1.17 #} elsif (defined $specified_value and $specified_value->[0] eq 'WEIGHT') {
3177 wakaba 1.15 #
3178     }
3179    
3180     return $specified_value;
3181     },
3182     };
3183     $Attr->{font_weight} = $Prop->{'font-weight'};
3184     $Key->{font_weight} = $Prop->{'font-weight'};
3185    
3186 wakaba 1.13 my $uri_or_none_parser = sub {
3187 wakaba 1.11 my ($self, $prop_name, $tt, $t, $onerror) = @_;
3188    
3189 wakaba 1.13 if ($t->{type} == URI_TOKEN) {
3190 wakaba 1.11 my $value = $t->{value};
3191     $t = $tt->get_next_token;
3192     return ($t, {$prop_name => ['URI', $value, \($self->{base_uri})]});
3193     } elsif ($t->{type} == IDENT_TOKEN) {
3194     my $value = lc $t->{value}; ## TODO: case
3195     $t = $tt->get_next_token;
3196     if ($value eq 'none') {
3197     ## NOTE: |none| is the default value and therefore it must be
3198     ## supported anyway.
3199     return ($t, {$prop_name => ["KEYWORD", 'none']});
3200     } elsif ($value eq 'inherit') {
3201     return ($t, {$prop_name => ['INHERIT']});
3202     }
3203     ## NOTE: None of Firefox2, WinIE6, and Opera9 support this case.
3204     #} elsif ($t->{type} == URI_INVALID_TOKEN) {
3205     # my $value = $t->{value};
3206     # $t = $tt->get_next_token;
3207     # if ($t->{type} == EOF_TOKEN) {
3208 wakaba 1.39 # $onerror->(type => 'uri not closed',
3209 wakaba 1.11 # level => $self->{must_level},
3210 wakaba 1.38 # uri => \$self->{href},
3211 wakaba 1.11 # token => $t);
3212     #
3213     # return ($t, {$prop_name => ['URI', $value, \($self->{base_uri})]});
3214     # }
3215     }
3216    
3217 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
3218 wakaba 1.11 level => $self->{must_level},
3219 wakaba 1.38 uri => \$self->{href},
3220 wakaba 1.11 token => $t);
3221     return ($t, undef);
3222 wakaba 1.13 }; # $uri_or_none_parser
3223    
3224 wakaba 1.14 my $compute_uri_or_none = sub {
3225 wakaba 1.11 my ($self, $element, $prop_name, $specified_value) = @_;
3226    
3227     if (defined $specified_value and
3228     $specified_value->[0] eq 'URI' and
3229     defined $specified_value->[2]) {
3230     require Message::DOM::DOMImplementation;
3231     return ['URI',
3232     Message::DOM::DOMImplementation->create_uri_reference
3233     ($specified_value->[1])
3234     ->get_absolute_reference (${$specified_value->[2]})
3235     ->get_uri_reference,
3236     $specified_value->[2]];
3237     }
3238    
3239     return $specified_value;
3240 wakaba 1.14 }; # $compute_uri_or_none
3241    
3242     $Prop->{'list-style-image'} = {
3243     css => 'list-style-image',
3244     dom => 'list_style_image',
3245     key => 'list_style_image',
3246     parse => $uri_or_none_parser,
3247     serialize => $default_serializer,
3248     initial => ['KEYWORD', 'none'],
3249     inherited => 1,
3250     compute => $compute_uri_or_none,
3251 wakaba 1.11 };
3252     $Attr->{list_style_image} = $Prop->{'list-style-image'};
3253     $Key->{list_style_image} = $Prop->{'list-style-image'};
3254    
3255 wakaba 1.15 $Prop->{'background-image'} = {
3256     css => 'background-image',
3257     dom => 'background_image',
3258     key => 'background_image',
3259     parse => $uri_or_none_parser,
3260     serialize => $default_serializer,
3261 wakaba 1.30 serialize_multiple => $Prop->{'background-color'}->{serialize_multiple},
3262 wakaba 1.15 initial => ['KEYWORD', 'none'],
3263     #inherited => 0,
3264     compute => $compute_uri_or_none,
3265     };
3266     $Attr->{background_image} = $Prop->{'background-image'};
3267     $Key->{background_image} = $Prop->{'background-image'};
3268    
3269 wakaba 1.7 my $border_style_keyword = {
3270     none => 1, hidden => 1, dotted => 1, dashed => 1, solid => 1,
3271     double => 1, groove => 1, ridge => 1, inset => 1, outset => 1,
3272     };
3273    
3274     $Prop->{'border-top-style'} = {
3275     css => 'border-top-style',
3276     dom => 'border_top_style',
3277     key => 'border_top_style',
3278     parse => $one_keyword_parser,
3279 wakaba 1.11 serialize => $default_serializer,
3280 wakaba 1.29 serialize_multiple => $Prop->{'border-top-color'}->{serialize_multiple},
3281 wakaba 1.7 keyword => $border_style_keyword,
3282 wakaba 1.9 initial => ["KEYWORD", "none"],
3283     #inherited => 0,
3284     compute => $compute_as_specified,
3285 wakaba 1.7 };
3286     $Attr->{border_top_style} = $Prop->{'border-top-style'};
3287     $Key->{border_top_style} = $Prop->{'border-top-style'};
3288    
3289     $Prop->{'border-right-style'} = {
3290     css => 'border-right-style',
3291     dom => 'border_right_style',
3292     key => 'border_right_style',
3293     parse => $one_keyword_parser,
3294 wakaba 1.11 serialize => $default_serializer,
3295 wakaba 1.29 serialize_multiple => $Prop->{'border-top-color'}->{serialize_multiple},
3296 wakaba 1.7 keyword => $border_style_keyword,
3297 wakaba 1.9 initial => ["KEYWORD", "none"],
3298     #inherited => 0,
3299     compute => $compute_as_specified,
3300 wakaba 1.7 };
3301     $Attr->{border_right_style} = $Prop->{'border-right-style'};
3302     $Key->{border_right_style} = $Prop->{'border-right-style'};
3303    
3304     $Prop->{'border-bottom-style'} = {
3305     css => 'border-bottom-style',
3306     dom => 'border_bottom_style',
3307     key => 'border_bottom_style',
3308     parse => $one_keyword_parser,
3309 wakaba 1.11 serialize => $default_serializer,
3310 wakaba 1.29 serialize_multiple => $Prop->{'border-top-color'}->{serialize_multiple},
3311 wakaba 1.7 keyword => $border_style_keyword,
3312 wakaba 1.9 initial => ["KEYWORD", "none"],
3313     #inherited => 0,
3314     compute => $compute_as_specified,
3315 wakaba 1.7 };
3316     $Attr->{border_bottom_style} = $Prop->{'border-bottom-style'};
3317     $Key->{border_bottom_style} = $Prop->{'border-bottom-style'};
3318    
3319     $Prop->{'border-left-style'} = {
3320     css => 'border-left-style',
3321     dom => 'border_left_style',
3322     key => 'border_left_style',
3323     parse => $one_keyword_parser,
3324 wakaba 1.11 serialize => $default_serializer,
3325 wakaba 1.29 serialize_multiple => $Prop->{'border-top-color'}->{serialize_multiple},
3326 wakaba 1.7 keyword => $border_style_keyword,
3327 wakaba 1.9 initial => ["KEYWORD", "none"],
3328     #inherited => 0,
3329     compute => $compute_as_specified,
3330 wakaba 1.7 };
3331     $Attr->{border_left_style} = $Prop->{'border-left-style'};
3332     $Key->{border_left_style} = $Prop->{'border-left-style'};
3333    
3334 wakaba 1.16 $Prop->{'outline-style'} = {
3335     css => 'outline-style',
3336     dom => 'outline_style',
3337     key => 'outline_style',
3338     parse => $one_keyword_parser,
3339     serialize => $default_serializer,
3340 wakaba 1.29 serialize_multiple => $Prop->{'outline-color'}->{serialize_multiple},
3341 wakaba 1.23 keyword => {%$border_style_keyword},
3342 wakaba 1.16 initial => ['KEYWORD', 'none'],
3343     #inherited => 0,
3344     compute => $compute_as_specified,
3345     };
3346     $Attr->{outline_style} = $Prop->{'outline-style'};
3347     $Key->{outline_style} = $Prop->{'outline-style'};
3348 wakaba 1.23 delete $Prop->{'outline-style'}->{keyword}->{hidden};
3349 wakaba 1.16
3350 wakaba 1.44 my $generic_font_keywords = {
3351 wakaba 1.31 serif => 1, 'sans-serif' => 1, cursive => 1,
3352     fantasy => 1, monospace => 1, '-manakai-default' => 1,
3353     '-manakai-caption' => 1, '-manakai-icon' => 1,
3354     '-manakai-menu' => 1, '-manakai-message-box' => 1,
3355     '-manakai-small-caption' => 1, '-manakai-status-bar' => 1,
3356     };
3357     ## NOTE: "All five generic font families are defined to exist in all CSS
3358     ## implementations (they need not necessarily map to five distinct actual
3359     ## fonts)." [CSS 2.1].
3360     ## NOTE: "If no font with the indicated characteristics exists on a given
3361     ## platform, the user agent should either intelligently substitute (e.g., a
3362     ## smaller version of the 'caption' font might be used for the 'small-caption'
3363     ## font), or substitute a user agent default font." [CSS 2.1].
3364    
3365 wakaba 1.15 $Prop->{'font-family'} = {
3366     css => 'font-family',
3367     dom => 'font_family',
3368     key => 'font_family',
3369     parse => sub {
3370     my ($self, $prop_name, $tt, $t, $onerror) = @_;
3371    
3372     ## NOTE: See <http://suika.fam.cx/gate/2005/sw/font-family> for
3373     ## how chaotic browsers are!
3374    
3375     my @prop_value;
3376    
3377     my $font_name = '';
3378     my $may_be_generic = 1;
3379 wakaba 1.31 my $may_be_inherit = ($prop_name ne 'font');
3380 wakaba 1.15 my $has_s = 0;
3381     F: {
3382     if ($t->{type} == IDENT_TOKEN) {
3383     undef $may_be_inherit if $has_s or length $font_name;
3384     undef $may_be_generic if $has_s or length $font_name;
3385     $font_name .= ' ' if $has_s;
3386     $font_name .= $t->{value};
3387     undef $has_s;
3388     $t = $tt->get_next_token;
3389     } elsif ($t->{type} == STRING_TOKEN) {
3390     $font_name .= ' ' if $has_s;
3391     $font_name .= $t->{value};
3392     undef $may_be_inherit;
3393     undef $may_be_generic;
3394     undef $has_s;
3395     $t = $tt->get_next_token;
3396 wakaba 1.31 } elsif ($t->{type} == COMMA_TOKEN) { ## TODO: case
3397     if ($may_be_generic and $generic_font_keywords->{lc $font_name}) {
3398 wakaba 1.15 push @prop_value, ['KEYWORD', $font_name];
3399     } elsif (not $may_be_generic or length $font_name) {
3400     push @prop_value, ["STRING", $font_name];
3401     }
3402     undef $may_be_inherit;
3403     $may_be_generic = 1;
3404     undef $has_s;
3405     $font_name = '';
3406     $t = $tt->get_next_token;
3407     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
3408     } elsif ($t->{type} == S_TOKEN) {
3409     $has_s = 1;
3410     $t = $tt->get_next_token;
3411     } else {
3412 wakaba 1.31 if ($may_be_generic and $generic_font_keywords->{lc $font_name}) {
3413     push @prop_value, ['KEYWORD', $font_name]; ## TODO: case
3414 wakaba 1.15 } elsif (not $may_be_generic or length $font_name) {
3415     push @prop_value, ['STRING', $font_name];
3416     } else {
3417 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
3418 wakaba 1.15 level => $self->{must_level},
3419 wakaba 1.38 uri => \$self->{href},
3420 wakaba 1.15 token => $t);
3421     return ($t, undef);
3422     }
3423     last F;
3424     }
3425     redo F;
3426     } # F
3427    
3428     if ($may_be_inherit and
3429     @prop_value == 1 and
3430     $prop_value[0]->[0] eq 'STRING' and
3431     lc $prop_value[0]->[1] eq 'inherit') { ## TODO: case
3432     return ($t, {$prop_name => ['INHERIT']});
3433     } else {
3434     unshift @prop_value, 'FONT';
3435     return ($t, {$prop_name => \@prop_value});
3436     }
3437     },
3438     serialize => sub {
3439     my ($self, $prop_name, $value) = @_;
3440    
3441     if ($value->[0] eq 'FONT') {
3442     return join ', ', map {
3443     if ($_->[0] eq 'STRING') {
3444     '"'.$_->[1].'"'; ## NOTE: This is what Firefox does.
3445     } elsif ($_->[0] eq 'KEYWORD') {
3446     $_->[1]; ## NOTE: This is what Firefox does.
3447     } else {
3448     ## NOTE: This should be an error.
3449     '""';
3450     }
3451     } @$value[1..$#$value];
3452     } elsif ($value->[0] eq 'INHERIT') {
3453     return 'inherit';
3454     } else {
3455 wakaba 1.34 return '';
3456 wakaba 1.15 }
3457     },
3458     initial => ['FONT', ['KEYWORD', '-manakai-default']],
3459     inherited => 1,
3460     compute => $compute_as_specified,
3461     };
3462     $Attr->{font_family} = $Prop->{'font-family'};
3463     $Key->{font_family} = $Prop->{'font-family'};
3464    
3465 wakaba 1.17 $Prop->{cursor} = {
3466     css => 'cursor',
3467     dom => 'cursor',
3468     key => 'cursor',
3469     parse => sub {
3470     my ($self, $prop_name, $tt, $t, $onerror) = @_;
3471    
3472     ## NOTE: See <http://suika.fam.cx/gate/2005/sw/cursor> for browser
3473     ## compatibility issues.
3474    
3475     my @prop_value = ('CURSOR');
3476    
3477     F: {
3478     if ($t->{type} == IDENT_TOKEN) {
3479     my $v = lc $t->{value}; ## TODO: case
3480     $t = $tt->get_next_token;
3481     if ($Prop->{$prop_name}->{keyword}->{$v}) {
3482     push @prop_value, ['KEYWORD', $v];
3483     last F;
3484     } elsif ($v eq 'inherit' and @prop_value == 1) {
3485     return ($t, {$prop_name => ['INHERIT']});
3486     } else {
3487 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
3488 wakaba 1.17 level => $self->{must_level},
3489 wakaba 1.38 uri => \$self->{href},
3490 wakaba 1.17 token => $t);
3491     return ($t, undef);
3492     }
3493     } elsif ($t->{type} == URI_TOKEN) {
3494     push @prop_value, ['URI', $t->{value}, \($self->{base_uri})];
3495     $t = $tt->get_next_token;
3496     } else {
3497 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
3498 wakaba 1.17 level => $self->{must_level},
3499 wakaba 1.38 uri => \$self->{href},
3500 wakaba 1.17 token => $t);
3501     return ($t, undef);
3502     }
3503    
3504     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
3505     if ($t->{type} == COMMA_TOKEN) {
3506     $t = $tt->get_next_token;
3507     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
3508     redo F;
3509     }
3510     } # F
3511    
3512     return ($t, {$prop_name => \@prop_value});
3513     },
3514     serialize => sub {
3515     my ($self, $prop_name, $value) = @_;
3516    
3517     if ($value->[0] eq 'CURSOR') {
3518     return join ', ', map {
3519     if ($_->[0] eq 'URI') {
3520     'url('.$_->[1].')'; ## NOTE: This is what Firefox does.
3521     } elsif ($_->[0] eq 'KEYWORD') {
3522     $_->[1];
3523     } else {
3524     ## NOTE: This should be an error.
3525     '""';
3526     }
3527     } @$value[1..$#$value];
3528     } elsif ($value->[0] eq 'INHERIT') {
3529     return 'inherit';
3530     } else {
3531 wakaba 1.34 return '';
3532 wakaba 1.17 }
3533     },
3534     keyword => {
3535     auto => 1, crosshair => 1, default => 1, pointer => 1, move => 1,
3536     'e-resize' => 1, 'ne-resize' => 1, 'nw-resize' => 1, 'n-resize' => 1,
3537     'n-resize' => 1, 'se-resize' => 1, 'sw-resize' => 1, 's-resize' => 1,
3538     'w-resize' => 1, text => 1, wait => 1, help => 1, progress => 1,
3539     },
3540     initial => ['CURSOR', ['KEYWORD', 'auto']],
3541     inherited => 1,
3542     compute => sub {
3543     my ($self, $element, $prop_name, $specified_value) = @_;
3544    
3545     if (defined $specified_value and $specified_value->[0] eq 'CURSOR') {
3546     my @new_value = ('CURSOR');
3547     for my $value (@$specified_value[1..$#$specified_value]) {
3548     if ($value->[0] eq 'URI') {
3549     if (defined $value->[2]) {
3550     require Message::DOM::DOMImplementation;
3551     push @new_value, ['URI',
3552     Message::DOM::DOMImplementation
3553     ->create_uri_reference ($value->[1])
3554     ->get_absolute_reference (${$value->[2]})
3555     ->get_uri_reference,
3556     $value->[2]];
3557     } else {
3558     push @new_value, $value;
3559     }
3560     } else {
3561     push @new_value, $value;
3562     }
3563     }
3564     return \@new_value;
3565     }
3566    
3567     return $specified_value;
3568     },
3569     };
3570     $Attr->{cursor} = $Prop->{cursor};
3571     $Key->{cursor} = $Prop->{cursor};
3572    
3573 wakaba 1.7 $Prop->{'border-style'} = {
3574     css => 'border-style',
3575     dom => 'border_style',
3576     parse => sub {
3577     my ($self, $prop_name, $tt, $t, $onerror) = @_;
3578    
3579     my %prop_value;
3580     if ($t->{type} == IDENT_TOKEN) {
3581     my $prop_value = lc $t->{value}; ## TODO: case folding
3582     $t = $tt->get_next_token;
3583     if ($border_style_keyword->{$prop_value} and
3584     $self->{prop_value}->{'border-top-style'}->{$prop_value}) {
3585     $prop_value{'border-top-style'} = ["KEYWORD", $prop_value];
3586     } elsif ($prop_value eq 'inherit') {
3587 wakaba 1.10 $prop_value{'border-top-style'} = ["INHERIT"];
3588 wakaba 1.19 $prop_value{'border-right-style'} = $prop_value{'border-top-style'};
3589     $prop_value{'border-bottom-style'} = $prop_value{'border-top-style'};
3590     $prop_value{'border-left-style'} = $prop_value{'border-right-style'};
3591     return ($t, \%prop_value);
3592 wakaba 1.7 } else {
3593 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
3594 wakaba 1.7 level => $self->{must_level},
3595 wakaba 1.38 uri => \$self->{href},
3596 wakaba 1.7 token => $t);
3597     return ($t, undef);
3598     }
3599     $prop_value{'border-right-style'} = $prop_value{'border-top-style'};
3600     $prop_value{'border-bottom-style'} = $prop_value{'border-top-style'};
3601     $prop_value{'border-left-style'} = $prop_value{'border-right-style'};
3602     } else {
3603 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
3604 wakaba 1.7 level => $self->{must_level},
3605 wakaba 1.38 uri => \$self->{href},
3606 wakaba 1.7 token => $t);
3607     return ($t, undef);
3608     }
3609    
3610     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
3611     if ($t->{type} == IDENT_TOKEN) {
3612     my $prop_value = lc $t->{value}; ## TODO: case folding
3613     $t = $tt->get_next_token;
3614 wakaba 1.19 if ($border_style_keyword->{$prop_value} and
3615 wakaba 1.7 $self->{prop_value}->{'border-right-style'}->{$prop_value}) {
3616     $prop_value{'border-right-style'} = ["KEYWORD", $prop_value];
3617     } else {
3618 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
3619 wakaba 1.7 level => $self->{must_level},
3620 wakaba 1.38 uri => \$self->{href},
3621 wakaba 1.7 token => $t);
3622     return ($t, undef);
3623     }
3624     $prop_value{'border-left-style'} = $prop_value{'border-right-style'};
3625    
3626     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
3627     if ($t->{type} == IDENT_TOKEN) {
3628     my $prop_value = lc $t->{value}; ## TODO: case folding
3629     $t = $tt->get_next_token;
3630     if ($border_style_keyword->{$prop_value} and
3631     $self->{prop_value}->{'border-bottom-style'}->{$prop_value}) {
3632     $prop_value{'border-bottom-style'} = ["KEYWORD", $prop_value];
3633     } else {
3634 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
3635 wakaba 1.7 level => $self->{must_level},
3636 wakaba 1.38 uri => \$self->{href},
3637 wakaba 1.7 token => $t);
3638     return ($t, undef);
3639     }
3640    
3641     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
3642     if ($t->{type} == IDENT_TOKEN) {
3643     my $prop_value = lc $t->{value}; ## TODO: case folding
3644     $t = $tt->get_next_token;
3645     if ($border_style_keyword->{$prop_value} and
3646     $self->{prop_value}->{'border-left-style'}->{$prop_value}) {
3647     $prop_value{'border-left-style'} = ["KEYWORD", $prop_value];
3648     } else {
3649 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
3650 wakaba 1.7 level => $self->{must_level},
3651 wakaba 1.38 uri => \$self->{href},
3652 wakaba 1.7 token => $t);
3653     return ($t, undef);
3654     }
3655     }
3656     }
3657     }
3658    
3659     return ($t, \%prop_value);
3660     },
3661 wakaba 1.43 serialize_shorthand => sub {
3662     my $self = shift;
3663    
3664 wakaba 1.7 my @v;
3665     push @v, $self->border_top_style;
3666 wakaba 1.43 my $i = $self->get_property_priority ('border-top-style');
3667 wakaba 1.45 return {} unless length $v[-1];
3668 wakaba 1.7 push @v, $self->border_right_style;
3669 wakaba 1.45 return {} unless length $v[-1];
3670     return {} unless $i eq $self->get_property_priority ('border-right-style');
3671 wakaba 1.7 push @v, $self->border_bottom_style;
3672 wakaba 1.45 return {} unless length $v[-1];
3673     return {} unless $i eq $self->get_property_priority ('border-bottom-style');
3674 wakaba 1.19 push @v, $self->border_left_style;
3675 wakaba 1.45 return {} unless length $v[-1];
3676     return {} unless $i eq $self->get_property_priority ('border-left-style');
3677 wakaba 1.43
3678     my $v = 0;
3679     for (0..3) {
3680     $v++ if $v[$_] eq 'inherit';
3681     }
3682     if ($v == 4) {
3683 wakaba 1.45 return {'border-style' => ['inherit', $i]};
3684 wakaba 1.43 } elsif ($v) {
3685     return {};
3686     }
3687 wakaba 1.7
3688     pop @v if $v[1] eq $v[3];
3689     pop @v if $v[0] eq $v[2];
3690     pop @v if $v[0] eq $v[1];
3691 wakaba 1.45 return {'border-style' => [(join ' ', @v), $i]};
3692 wakaba 1.7 },
3693 wakaba 1.29 serialize_multiple => $Prop->{'border-top-color'}->{serialize_multiple},
3694 wakaba 1.7 };
3695     $Attr->{border_style} = $Prop->{'border-style'};
3696    
3697 wakaba 1.29 $Prop->{'border-color'} = {
3698     css => 'border-color',
3699     dom => 'border_color',
3700     parse => sub {
3701     my ($self, $prop_name, $tt, $t, $onerror) = @_;
3702    
3703     my %prop_value;
3704     ($t, my $pv) = $parse_color->($self, 'border-color', $tt, $t, $onerror);
3705     if (not defined $pv) {
3706     return ($t, undef);
3707     }
3708     $prop_value{'border-top-color'} = $pv->{'border-color'};
3709     $prop_value{'border-bottom-color'} = $prop_value{'border-top-color'};
3710     $prop_value{'border-right-color'} = $prop_value{'border-top-color'};
3711     $prop_value{'border-left-color'}= $prop_value{'border-right-color'};
3712     if ($prop_value{'border-top-color'}->[0] eq 'INHERIT') {
3713     return ($t, \%prop_value);
3714     }
3715    
3716     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
3717     if ({
3718     IDENT_TOKEN, 1,
3719     HASH_TOKEN, 1, NUMBER_TOKEN, 1, DIMENSION_TOKEN, 1,
3720     FUNCTION_TOKEN, 1,
3721     }->{$t->{type}}) {
3722     ($t, $pv) = $parse_color->($self, 'border-color', $tt, $t, $onerror);
3723     if (not defined $pv) {
3724     return ($t, undef);
3725     } elsif ($pv->{'border-color'}->[0] eq 'INHERIT') {
3726 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
3727 wakaba 1.29 level => $self->{must_level},
3728 wakaba 1.38 uri => \$self->{href},
3729 wakaba 1.29 token => $t);
3730     return ($t, undef);
3731     }
3732     $prop_value{'border-right-color'} = $pv->{'border-color'};
3733     $prop_value{'border-left-color'}= $prop_value{'border-right-color'};
3734    
3735     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
3736     if ({
3737     IDENT_TOKEN, 1,
3738     HASH_TOKEN, 1, NUMBER_TOKEN, 1, DIMENSION_TOKEN, 1,
3739     FUNCTION_TOKEN, 1,
3740     }->{$t->{type}}) {
3741     ($t, $pv) = $parse_color->($self, 'border-color', $tt, $t, $onerror);
3742     if (not defined $pv) {
3743     return ($t, undef);
3744     } elsif ($pv->{'border-color'}->[0] eq 'INHERIT') {
3745 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
3746 wakaba 1.29 level => $self->{must_level},
3747 wakaba 1.38 uri => \$self->{href},
3748 wakaba 1.29 token => $t);
3749     return ($t, undef);
3750     }
3751     $prop_value{'border-bottom-color'} = $pv->{'border-color'};
3752    
3753     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
3754     if ({
3755     IDENT_TOKEN, 1,
3756     HASH_TOKEN, 1, NUMBER_TOKEN, 1, DIMENSION_TOKEN, 1,
3757     FUNCTION_TOKEN, 1,
3758     }->{$t->{type}}) {
3759     ($t, $pv) = $parse_color->($self, 'border-color', $tt, $t, $onerror);
3760     if (not defined $pv) {
3761     return ($t, undef);
3762     } elsif ($pv->{'border-color'}->[0] eq 'INHERIT') {
3763 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
3764 wakaba 1.29 level => $self->{must_level},
3765 wakaba 1.38 uri => \$self->{href},
3766 wakaba 1.29 token => $t);
3767     return ($t, undef);
3768     }
3769     $prop_value{'border-left-color'} = $pv->{'border-color'};
3770     }
3771     }
3772     }
3773    
3774     return ($t, \%prop_value);
3775     },
3776 wakaba 1.43 serialize_shorthand => sub {
3777     my $self = shift;
3778    
3779 wakaba 1.29 my @v;
3780     push @v, $self->border_top_color;
3781 wakaba 1.43 my $i = $self->get_property_priority ('border-top-color');
3782 wakaba 1.45 return {} unless length $v[-1];
3783 wakaba 1.29 push @v, $self->border_right_color;
3784 wakaba 1.45 return {} unless length $v[-1];
3785     return {} unless $i eq $self->get_property_priority ('border-right-color');
3786 wakaba 1.29 push @v, $self->border_bottom_color;
3787 wakaba 1.45 return {} unless length $v[-1];
3788     return {} unless $i eq $self->get_property_priority ('border-bottom-color');
3789 wakaba 1.29 push @v, $self->border_left_color;
3790 wakaba 1.45 return {} unless length $v[-1];
3791     return {} unless $i eq $self->get_property_priority ('border-left-color');
3792 wakaba 1.43
3793     my $v = 0;
3794     for (0..3) {
3795     $v++ if $v[$_] eq 'inherit';
3796     }
3797     if ($v == 4) {
3798 wakaba 1.45 return {'border-color' => ['inherit', $i]};
3799 wakaba 1.43 } elsif ($v) {
3800     return {};
3801     }
3802 wakaba 1.29
3803     pop @v if $v[1] eq $v[3];
3804     pop @v if $v[0] eq $v[2];
3805     pop @v if $v[0] eq $v[1];
3806 wakaba 1.45 return {'border-color' => [(join ' ', @v), $i]};
3807 wakaba 1.29 },
3808     serialize_multiple => $Prop->{'border-top-color'}->{serialize_multiple},
3809     };
3810     $Attr->{border_color} = $Prop->{'border-color'};
3811    
3812     $Prop->{'border-top'} = {
3813     css => 'border-top',
3814     dom => 'border_top',
3815     parse => sub {
3816     my ($self, $prop_name, $tt, $t, $onerror) = @_;
3817    
3818     my %prop_value;
3819     my $pv;
3820     ## NOTE: Since $onerror is disabled for three invocations below,
3821     ## some informative warning messages (if they are added someday) will not
3822     ## be reported.
3823     ($t, $pv) = $parse_color->($self, $prop_name.'-color', $tt, $t, sub {});
3824     if (defined $pv) {
3825     if ($pv->{$prop_name.'-color'}->[0] eq 'INHERIT') {
3826     return ($t, {$prop_name.'-color' => ['INHERIT'],
3827     $prop_name.'-style' => ['INHERIT'],
3828     $prop_name.'-width' => ['INHERIT']});
3829     } else {
3830     $prop_value{$prop_name.'-color'} = $pv->{$prop_name.'-color'};
3831     }
3832     } else {
3833     ($t, $pv) = $Prop->{'border-top-width'}->{parse}
3834     ->($self, $prop_name.'-width', $tt, $t, sub {});
3835     if (defined $pv) {
3836     $prop_value{$prop_name.'-width'} = $pv->{$prop_name.'-width'};
3837     } else {
3838     ($t, $pv) = $Prop->{'border-top-style'}->{parse}
3839     ->($self, $prop_name.'-style', $tt, $t, sub {});
3840     if (defined $pv) {
3841     $prop_value{$prop_name.'-style'} = $pv->{$prop_name.'-style'};
3842     } else {
3843 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
3844 wakaba 1.29 level => $self->{must_level},
3845 wakaba 1.38 uri => \$self->{href},
3846 wakaba 1.29 token => $t);
3847     return ($t, undef);
3848     }
3849     }
3850     }
3851    
3852     for (1..2) {
3853     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
3854     if ($t->{type} == IDENT_TOKEN) {
3855     my $prop_value = lc $t->{value}; ## TODO: case
3856     if ($border_style_keyword->{$prop_value} and
3857     $self->{prop_value}->{'border-top-style'}->{$prop_value} and
3858     not defined $prop_value{$prop_name.'-style'}) {
3859     $prop_value{$prop_name.'-style'} = ['KEYWORD', $prop_value];
3860     $t = $tt->get_next_token;
3861     next;
3862     } elsif ({thin => 1, medium => 1, thick => 1}->{$prop_value} and
3863     not defined $prop_value{$prop_name.'-width'}) {
3864     $prop_value{$prop_name.'-width'} = ['KEYWORD', $prop_value];
3865     $t = $tt->get_next_token;
3866     next;
3867     }
3868     }
3869    
3870     undef $pv;
3871     ($t, $pv) = $parse_color->($self, $prop_name.'-color', $tt, $t, $onerror)
3872     if not defined $prop_value{$prop_name.'-color'} and
3873     {
3874     IDENT_TOKEN, 1,
3875     HASH_TOKEN, 1, NUMBER_TOKEN, 1, DIMENSION_TOKEN, 1,
3876     FUNCTION_TOKEN, 1,
3877     }->{$t->{type}};
3878     if (defined $pv) {
3879     if ($pv->{$prop_name.'-color'}->[0] eq 'INHERIT') {
3880 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
3881 wakaba 1.29 level => $self->{must_level},
3882 wakaba 1.38 uri => \$self->{href},
3883 wakaba 1.29 token => $t);
3884     } else {
3885     $prop_value{$prop_name.'-color'} = $pv->{$prop_name.'-color'};
3886     }
3887     } else {
3888     undef $pv;
3889     ($t, $pv) = $Prop->{'border-top-width'}->{parse}
3890     ->($self, $prop_name.'-width',
3891     $tt, $t, $onerror)
3892     if not defined $prop_value{$prop_name.'-width'} and
3893     {
3894     DIMENSION_TOKEN, 1,
3895     NUMBER_TOKEN, 1,
3896     IDENT_TOKEN, 1,
3897     MINUS_TOKEN, 1,
3898     }->{$t->{type}};
3899     if (defined $pv) {
3900     if ($pv->{$prop_name.'-width'}->[0] eq 'INHERIT') {
3901 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
3902 wakaba 1.29 level => $self->{must_level},
3903 wakaba 1.38 uri => \$self->{href},
3904 wakaba 1.29 token => $t);
3905     } else {
3906     $prop_value{$prop_name.'-width'} = $pv->{$prop_name.'-width'};
3907     }
3908     } else {
3909     last;
3910     }
3911     }
3912     }
3913    
3914     $prop_value{$prop_name.'-color'}
3915     ||= $Prop->{$prop_name.'-color'}->{initial};
3916     $prop_value{$prop_name.'-width'}
3917     ||= $Prop->{$prop_name.'-width'}->{initial};
3918     $prop_value{$prop_name.'-style'}
3919     ||= $Prop->{$prop_name.'-style'}->{initial};
3920    
3921     return ($t, \%prop_value);
3922     },
3923 wakaba 1.43 serialize_shorthand => sub {
3924     my $self = shift;
3925    
3926     my $w = $self->border_top_width;
3927     return {} unless length $w;
3928     my $i = $self->get_property_priority ('border-top-width');
3929     my $s = $self->border_top_style;
3930     return {} unless length $s;
3931     return {} unless $i eq $self->get_property_priority ('border-top-style');
3932     my $c = $self->border_top_color;
3933     return {} unless length $c;
3934     return {} unless $i eq $self->get_property_priority ('border-top-color');
3935    
3936     my $v = 0;
3937     $v++ if $w eq 'inherit';
3938     $v++ if $s eq 'inherit';
3939     $v++ if $c eq 'inherit';
3940     if ($v == 3) {
3941     return {'border-top' => ['inherit', $i]};
3942     } elsif ($v) {
3943     return {};
3944     }
3945 wakaba 1.29
3946 wakaba 1.43 return {'border-top' => [$w . ' ' . $s . ' ' . $c, $i]};
3947 wakaba 1.29 },
3948     serialize_multiple => $Prop->{'border-top-color'}->{serialize_multiple},
3949     };
3950     $Attr->{border_top} = $Prop->{'border-top'};
3951    
3952     $Prop->{'border-right'} = {
3953     css => 'border-right',
3954     dom => 'border_right',
3955     parse => $Prop->{'border-top'}->{parse},
3956 wakaba 1.43 serialize_shorthand => sub {
3957     my $self = shift;
3958    
3959     my $w = $self->border_right_width;
3960     return {} unless length $w;
3961     my $i = $self->get_property_priority ('border-right-width');
3962     my $s = $self->border_right_style;
3963     return {} unless length $s;
3964     return {} unless $i eq $self->get_property_priority ('border-right-style');
3965     my $c = $self->border_right_color;
3966     return {} unless length $c;
3967     return {} unless $i eq $self->get_property_priority ('border-right-color');
3968    
3969     my $v = 0;
3970     $v++ if $w eq 'inherit';
3971     $v++ if $s eq 'inherit';
3972     $v++ if $c eq 'inherit';
3973     if ($v == 3) {
3974     return {'border-right' => ['inherit', $i]};
3975     } elsif ($v) {
3976     return {};
3977     }
3978    
3979     return {'border-right' => [$w . ' ' . $s . ' ' . $c, $i]};
3980     },
3981 wakaba 1.29 serialize_multiple => $Prop->{'border-top-color'}->{serialize_multiple},
3982     };
3983     $Attr->{border_right} = $Prop->{'border-right'};
3984    
3985     $Prop->{'border-bottom'} = {
3986     css => 'border-bottom',
3987     dom => 'border_bottom',
3988     parse => $Prop->{'border-top'}->{parse},
3989 wakaba 1.43 serialize_shorthand => sub {
3990     my $self = shift;
3991    
3992     my $w = $self->border_bottom_width;
3993     return {} unless length $w;
3994     my $i = $self->get_property_priority ('border-bottom-width');
3995     my $s = $self->border_bottom_style;
3996     return {} unless length $s;
3997     return {} unless $i eq $self->get_property_priority ('border-bottom-style');
3998     my $c = $self->border_bottom_color;
3999     return {} unless length $c;
4000     return {} unless $i eq $self->get_property_priority ('border-bottom-color');
4001    
4002     my $v = 0;
4003     $v++ if $w eq 'inherit';
4004     $v++ if $s eq 'inherit';
4005     $v++ if $c eq 'inherit';
4006     if ($v == 3) {
4007     return {'border-bottom' => ['inherit', $i]};
4008     } elsif ($v) {
4009     return {};
4010     }
4011    
4012     return {'border-bottom' => [$w . ' ' . $s . ' ' . $c, $i]};
4013     },
4014 wakaba 1.29 serialize_multiple => $Prop->{'border-top-color'}->{serialize_multiple},
4015     };
4016     $Attr->{border_bottom} = $Prop->{'border-bottom'};
4017    
4018     $Prop->{'border-left'} = {
4019     css => 'border-left',
4020     dom => 'border_left',
4021     parse => $Prop->{'border-top'}->{parse},
4022 wakaba 1.43 serialize_shorthand => sub {
4023     my $self = shift;
4024    
4025     my $w = $self->border_left_width;
4026     return {} unless length $w;
4027     my $i = $self->get_property_priority ('border-left-width');
4028     my $s = $self->border_left_style;
4029     return {} unless length $s;
4030     return {} unless $i eq $self->get_property_priority ('border-left-style');
4031     my $c = $self->border_left_color;
4032     return {} unless length $c;
4033     return {} unless $i eq $self->get_property_priority ('border-left-color');
4034    
4035     my $v = 0;
4036     $v++ if $w eq 'inherit';
4037     $v++ if $s eq 'inherit';
4038     $v++ if $c eq 'inherit';
4039     if ($v == 3) {
4040     return {'border-left' => ['inherit', $i]};
4041     } elsif ($v) {
4042     return {};
4043     }
4044    
4045     return {'border-left' => [$w . ' ' . $s . ' ' . $c, $i]};
4046     },
4047 wakaba 1.29 serialize_multiple => $Prop->{'border-top-color'}->{serialize_multiple},
4048     };
4049     $Attr->{border_left} = $Prop->{'border-left'};
4050    
4051     $Prop->{outline} = {
4052     css => 'outline',
4053     dom => 'outline',
4054     parse => $Prop->{'border-top'}->{parse},
4055     serialize_multiple => $Prop->{'outline-color'}->{serialize_multiple},
4056     };
4057     $Attr->{outline} = $Prop->{outline};
4058    
4059     $Prop->{border} = {
4060     css => 'border',
4061     dom => 'border',
4062     parse => sub {
4063     my ($self, $prop_name, $tt, $t, $onerror) = @_;
4064     my $prop_value;
4065     ($t, $prop_value) = $Prop->{'border-top'}->{parse}
4066     ->($self, 'border-top', $tt, $t, $onerror);
4067     return ($t, undef) unless defined $prop_value;
4068    
4069     for (qw/border-right border-bottom border-left/) {
4070     $prop_value->{$_.'-color'} = $prop_value->{'border-top-color'}
4071     if defined $prop_value->{'border-top-color'};
4072     $prop_value->{$_.'-style'} = $prop_value->{'border-top-style'}
4073     if defined $prop_value->{'border-top-style'};
4074     $prop_value->{$_.'-width'} = $prop_value->{'border-top-width'}
4075     if defined $prop_value->{'border-top-width'};
4076     }
4077     return ($t, $prop_value);
4078     },
4079     serialize_multiple => $Prop->{'border-top-color'}->{serialize_multiple},
4080     };
4081     $Attr->{border} = $Prop->{border};
4082    
4083 wakaba 1.19 $Prop->{margin} = {
4084     css => 'margin',
4085     dom => 'margin',
4086     parse => sub {
4087     my ($self, $prop_name, $tt, $t, $onerror) = @_;
4088    
4089     my %prop_value;
4090    
4091     my $sign = 1;
4092 wakaba 1.42 my $has_sign;
4093 wakaba 1.19 if ($t->{type} == MINUS_TOKEN) {
4094     $t = $tt->get_next_token;
4095 wakaba 1.42 $has_sign = 1;
4096 wakaba 1.19 $sign = -1;
4097 wakaba 1.42 } elsif ($t->{type} == PLUS_TOKEN) {
4098     $t = $tt->get_next_token;
4099     $has_sign = 1;
4100 wakaba 1.19 }
4101    
4102     if ($t->{type} == DIMENSION_TOKEN) {
4103     my $value = $t->{number} * $sign;
4104     my $unit = lc $t->{value}; ## TODO: case
4105     $t = $tt->get_next_token;
4106     if ($length_unit->{$unit}) {
4107     $prop_value{'margin-top'} = ['DIMENSION', $value, $unit];
4108     } else {
4109 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4110 wakaba 1.19 level => $self->{must_level},
4111 wakaba 1.38 uri => \$self->{href},
4112 wakaba 1.19 token => $t);
4113     return ($t, undef);
4114     }
4115     } elsif ($t->{type} == PERCENTAGE_TOKEN) {
4116     my $value = $t->{number} * $sign;
4117     $t = $tt->get_next_token;
4118     $prop_value{'margin-top'} = ['PERCENTAGE', $value];
4119 wakaba 1.20 } elsif ($t->{type} == NUMBER_TOKEN and
4120 wakaba 1.19 ($self->{unitless_px} or $t->{number} == 0)) {
4121     my $value = $t->{number} * $sign;
4122     $t = $tt->get_next_token;
4123     $prop_value{'margin-top'} = ['DIMENSION', $value, 'px'];
4124 wakaba 1.42 } elsif (not $has_sign and $t->{type} == IDENT_TOKEN) {
4125 wakaba 1.19 my $prop_value = lc $t->{value}; ## TODO: case folding
4126     $t = $tt->get_next_token;
4127     if ($prop_value eq 'auto') {
4128     $prop_value{'margin-top'} = ['KEYWORD', $prop_value];
4129     } elsif ($prop_value eq 'inherit') {
4130     $prop_value{'margin-top'} = ['INHERIT'];
4131     $prop_value{'margin-right'} = $prop_value{'margin-top'};
4132     $prop_value{'margin-bottom'} = $prop_value{'margin-top'};
4133     $prop_value{'margin-left'} = $prop_value{'margin-right'};
4134     return ($t, \%prop_value);
4135     } else {
4136 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4137 wakaba 1.19 level => $self->{must_level},
4138 wakaba 1.38 uri => \$self->{href},
4139 wakaba 1.19 token => $t);
4140     return ($t, undef);
4141     }
4142     } else {
4143 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4144 wakaba 1.19 level => $self->{must_level},
4145 wakaba 1.38 uri => \$self->{href},
4146 wakaba 1.19 token => $t);
4147     return ($t, undef);
4148     }
4149     $prop_value{'margin-right'} = $prop_value{'margin-top'};
4150     $prop_value{'margin-bottom'} = $prop_value{'margin-top'};
4151     $prop_value{'margin-left'} = $prop_value{'margin-right'};
4152    
4153     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
4154 wakaba 1.42 undef $has_sign;
4155 wakaba 1.19 $sign = 1;
4156     if ($t->{type} == MINUS_TOKEN) {
4157     $t = $tt->get_next_token;
4158 wakaba 1.42 $has_sign = 1;
4159 wakaba 1.19 $sign = -1;
4160 wakaba 1.42 } elsif ($t->{type} == PLUS_TOKEN) {
4161     $t = $tt->get_next_token;
4162     $has_sign = 1;
4163 wakaba 1.19 }
4164    
4165     if ($t->{type} == DIMENSION_TOKEN) {
4166     my $value = $t->{number} * $sign;
4167     my $unit = lc $t->{value}; ## TODO: case
4168     $t = $tt->get_next_token;
4169     if ($length_unit->{$unit}) {
4170     $prop_value{'margin-right'} = ['DIMENSION', $value, $unit];
4171     } else {
4172 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4173 wakaba 1.19 level => $self->{must_level},
4174 wakaba 1.38 uri => \$self->{href},
4175 wakaba 1.19 token => $t);
4176     return ($t, undef);
4177     }
4178     } elsif ($t->{type} == PERCENTAGE_TOKEN) {
4179     my $value = $t->{number} * $sign;
4180     $t = $tt->get_next_token;
4181     $prop_value{'margin-right'} = ['PERCENTAGE', $value];
4182 wakaba 1.20 } elsif ($t->{type} == NUMBER_TOKEN and
4183 wakaba 1.19 ($self->{unitless_px} or $t->{number} == 0)) {
4184     my $value = $t->{number} * $sign;
4185     $t = $tt->get_next_token;
4186     $prop_value{'margin-right'} = ['DIMENSION', $value, 'px'];
4187 wakaba 1.42 } elsif (not $has_sign and $t->{type} == IDENT_TOKEN) {
4188 wakaba 1.19 my $prop_value = lc $t->{value}; ## TODO: case folding
4189     $t = $tt->get_next_token;
4190     if ($prop_value eq 'auto') {
4191     $prop_value{'margin-right'} = ['KEYWORD', $prop_value];
4192     } else {
4193 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4194 wakaba 1.19 level => $self->{must_level},
4195 wakaba 1.38 uri => \$self->{href},
4196 wakaba 1.19 token => $t);
4197     return ($t, undef);
4198     }
4199     } else {
4200 wakaba 1.42 if ($has_sign) {
4201 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4202 wakaba 1.24 level => $self->{must_level},
4203 wakaba 1.38 uri => \$self->{href},
4204 wakaba 1.24 token => $t);
4205     return ($t, undef);
4206     }
4207 wakaba 1.19 return ($t, \%prop_value);
4208     }
4209     $prop_value{'margin-left'} = $prop_value{'margin-right'};
4210    
4211     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
4212 wakaba 1.42 undef $has_sign;
4213 wakaba 1.19 $sign = 1;
4214     if ($t->{type} == MINUS_TOKEN) {
4215     $t = $tt->get_next_token;
4216 wakaba 1.42 $has_sign = 1;
4217 wakaba 1.19 $sign = -1;
4218 wakaba 1.42 } elsif ($t->{type} == PLUS_TOKEN) {
4219     $t = $tt->get_next_token;
4220     $has_sign = 1;
4221 wakaba 1.19 }
4222    
4223     if ($t->{type} == DIMENSION_TOKEN) {
4224     my $value = $t->{number} * $sign;
4225     my $unit = lc $t->{value}; ## TODO: case
4226     $t = $tt->get_next_token;
4227     if ($length_unit->{$unit}) {
4228     $prop_value{'margin-bottom'} = ['DIMENSION', $value, $unit];
4229     } else {
4230 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4231 wakaba 1.19 level => $self->{must_level},
4232 wakaba 1.38 uri => \$self->{href},
4233 wakaba 1.19 token => $t);
4234     return ($t, undef);
4235     }
4236     } elsif ($t->{type} == PERCENTAGE_TOKEN) {
4237     my $value = $t->{number} * $sign;
4238     $t = $tt->get_next_token;
4239     $prop_value{'margin-bottom'} = ['PERCENTAGE', $value];
4240 wakaba 1.20 } elsif ($t->{type} == NUMBER_TOKEN and
4241 wakaba 1.19 ($self->{unitless_px} or $t->{number} == 0)) {
4242     my $value = $t->{number} * $sign;
4243     $t = $tt->get_next_token;
4244     $prop_value{'margin-bottom'} = ['DIMENSION', $value, 'px'];
4245 wakaba 1.42 } elsif (not $has_sign and $t->{type} == IDENT_TOKEN) {
4246 wakaba 1.19 my $prop_value = lc $t->{value}; ## TODO: case folding
4247     $t = $tt->get_next_token;
4248     if ($prop_value eq 'auto') {
4249     $prop_value{'margin-bottom'} = ['KEYWORD', $prop_value];
4250     } else {
4251 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4252 wakaba 1.19 level => $self->{must_level},
4253 wakaba 1.38 uri => \$self->{href},
4254 wakaba 1.19 token => $t);
4255     return ($t, undef);
4256     }
4257     } else {
4258 wakaba 1.42 if ($has_sign) {
4259 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4260 wakaba 1.24 level => $self->{must_level},
4261 wakaba 1.38 uri => \$self->{href},
4262 wakaba 1.24 token => $t);
4263     return ($t, undef);
4264     }
4265 wakaba 1.19 return ($t, \%prop_value);
4266     }
4267    
4268     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
4269 wakaba 1.42 undef $has_sign;
4270 wakaba 1.19 $sign = 1;
4271     if ($t->{type} == MINUS_TOKEN) {
4272     $t = $tt->get_next_token;
4273 wakaba 1.42 $has_sign = 1;
4274 wakaba 1.19 $sign = -1;
4275 wakaba 1.42 } elsif ($t->{type} == PLUS_TOKEN) {
4276     $t = $tt->get_next_token;
4277     $has_sign = 1;
4278 wakaba 1.19 }
4279    
4280     if ($t->{type} == DIMENSION_TOKEN) {
4281     my $value = $t->{number} * $sign;
4282     my $unit = lc $t->{value}; ## TODO: case
4283     $t = $tt->get_next_token;
4284     if ($length_unit->{$unit}) {
4285     $prop_value{'margin-left'} = ['DIMENSION', $value, $unit];
4286     } else {
4287 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4288 wakaba 1.19 level => $self->{must_level},
4289 wakaba 1.38 uri => \$self->{href},
4290 wakaba 1.19 token => $t);
4291     return ($t, undef);
4292     }
4293     } elsif ($t->{type} == PERCENTAGE_TOKEN) {
4294     my $value = $t->{number} * $sign;
4295     $t = $tt->get_next_token;
4296     $prop_value{'margin-left'} = ['PERCENTAGE', $value];
4297 wakaba 1.20 } elsif ($t->{type} == NUMBER_TOKEN and
4298 wakaba 1.19 ($self->{unitless_px} or $t->{number} == 0)) {
4299     my $value = $t->{number} * $sign;
4300     $t = $tt->get_next_token;
4301     $prop_value{'margin-left'} = ['DIMENSION', $value, 'px'];
4302 wakaba 1.42 } elsif (not $has_sign and $t->{type} == IDENT_TOKEN) {
4303 wakaba 1.19 my $prop_value = lc $t->{value}; ## TODO: case folding
4304     $t = $tt->get_next_token;
4305     if ($prop_value eq 'auto') {
4306     $prop_value{'margin-left'} = ['KEYWORD', $prop_value];
4307     } else {
4308 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4309 wakaba 1.19 level => $self->{must_level},
4310 wakaba 1.38 uri => \$self->{href},
4311 wakaba 1.19 token => $t);
4312     return ($t, undef);
4313     }
4314     } else {
4315 wakaba 1.42 if ($has_sign) {
4316 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4317 wakaba 1.24 level => $self->{must_level},
4318 wakaba 1.38 uri => \$self->{href},
4319 wakaba 1.24 token => $t);
4320     return ($t, undef);
4321     }
4322 wakaba 1.19 return ($t, \%prop_value);
4323     }
4324    
4325     return ($t, \%prop_value);
4326     },
4327 wakaba 1.42 serialize_multiple => $Prop->{'margin-top'}->{serialize_multiple},
4328 wakaba 1.19 };
4329     $Attr->{margin} = $Prop->{margin};
4330    
4331     $Prop->{padding} = {
4332     css => 'padding',
4333     dom => 'padding',
4334     parse => sub {
4335     my ($self, $prop_name, $tt, $t, $onerror) = @_;
4336    
4337     my %prop_value;
4338    
4339     my $sign = 1;
4340     if ($t->{type} == MINUS_TOKEN) {
4341     $t = $tt->get_next_token;
4342     $sign = -1;
4343     }
4344    
4345     if ($t->{type} == DIMENSION_TOKEN) {
4346     my $value = $t->{number} * $sign;
4347     my $unit = lc $t->{value}; ## TODO: case
4348     $t = $tt->get_next_token;
4349     if ($length_unit->{$unit} and $value >= 0) {
4350     $prop_value{'padding-top'} = ['DIMENSION', $value, $unit];
4351     } else {
4352 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4353 wakaba 1.19 level => $self->{must_level},
4354 wakaba 1.38 uri => \$self->{href},
4355 wakaba 1.19 token => $t);
4356     return ($t, undef);
4357     }
4358     } elsif ($t->{type} == PERCENTAGE_TOKEN) {
4359     my $value = $t->{number} * $sign;
4360     $t = $tt->get_next_token;
4361     $prop_value{'padding-top'} = ['PERCENTAGE', $value];
4362     unless ($value >= 0) {
4363 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4364 wakaba 1.19 level => $self->{must_level},
4365 wakaba 1.38 uri => \$self->{href},
4366 wakaba 1.19 token => $t);
4367     return ($t, undef);
4368     }
4369 wakaba 1.20 } elsif ($t->{type} == NUMBER_TOKEN and
4370 wakaba 1.19 ($self->{unitless_px} or $t->{number} == 0)) {
4371     my $value = $t->{number} * $sign;
4372     $t = $tt->get_next_token;
4373     $prop_value{'padding-top'} = ['DIMENSION', $value, 'px'];
4374     unless ($value >= 0) {
4375 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4376 wakaba 1.19 level => $self->{must_level},
4377 wakaba 1.38 uri => \$self->{href},
4378 wakaba 1.19 token => $t);
4379     return ($t, undef);
4380     }
4381     } elsif ($sign > 0 and $t->{type} == IDENT_TOKEN) {
4382     my $prop_value = lc $t->{value}; ## TODO: case folding
4383     $t = $tt->get_next_token;
4384     if ($prop_value eq 'inherit') {
4385     $prop_value{'padding-top'} = ['INHERIT'];
4386     $prop_value{'padding-right'} = $prop_value{'padding-top'};
4387     $prop_value{'padding-bottom'} = $prop_value{'padding-top'};
4388     $prop_value{'padding-left'} = $prop_value{'padding-right'};
4389     return ($t, \%prop_value);
4390     } else {
4391 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4392 wakaba 1.19 level => $self->{must_level},
4393 wakaba 1.38 uri => \$self->{href},
4394 wakaba 1.19 token => $t);
4395     return ($t, undef);
4396     }
4397     } else {
4398 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4399 wakaba 1.19 level => $self->{must_level},
4400 wakaba 1.38 uri => \$self->{href},
4401 wakaba 1.19 token => $t);
4402     return ($t, undef);
4403     }
4404     $prop_value{'padding-right'} = $prop_value{'padding-top'};
4405     $prop_value{'padding-bottom'} = $prop_value{'padding-top'};
4406     $prop_value{'padding-left'} = $prop_value{'padding-right'};
4407    
4408     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
4409     $sign = 1;
4410     if ($t->{type} == MINUS_TOKEN) {
4411     $t = $tt->get_next_token;
4412     $sign = -1;
4413     }
4414    
4415     if ($t->{type} == DIMENSION_TOKEN) {
4416     my $value = $t->{number} * $sign;
4417     my $unit = lc $t->{value}; ## TODO: case
4418     $t = $tt->get_next_token;
4419     if ($length_unit->{$unit} and $value >= 0) {
4420     $prop_value{'padding-right'} = ['DIMENSION', $value, $unit];
4421     } else {
4422 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4423 wakaba 1.19 level => $self->{must_level},
4424 wakaba 1.38 uri => \$self->{href},
4425 wakaba 1.19 token => $t);
4426     return ($t, undef);
4427     }
4428     } elsif ($t->{type} == PERCENTAGE_TOKEN) {
4429     my $value = $t->{number} * $sign;
4430     $t = $tt->get_next_token;
4431     $prop_value{'padding-right'} = ['PERCENTAGE', $value];
4432     unless ($value >= 0) {
4433 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4434 wakaba 1.19 level => $self->{must_level},
4435 wakaba 1.38 uri => \$self->{href},
4436 wakaba 1.19 token => $t);
4437     return ($t, undef);
4438     }
4439 wakaba 1.20 } elsif ($t->{type} == NUMBER_TOKEN and
4440 wakaba 1.19 ($self->{unitless_px} or $t->{number} == 0)) {
4441     my $value = $t->{number} * $sign;
4442     $t = $tt->get_next_token;
4443     $prop_value{'padding-right'} = ['DIMENSION', $value, 'px'];
4444     unless ($value >= 0) {
4445 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4446 wakaba 1.19 level => $self->{must_level},
4447 wakaba 1.38 uri => \$self->{href},
4448 wakaba 1.19 token => $t);
4449     return ($t, undef);
4450     }
4451     } else {
4452 wakaba 1.24 if ($sign < 0) {
4453 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4454 wakaba 1.24 level => $self->{must_level},
4455 wakaba 1.38 uri => \$self->{href},
4456 wakaba 1.24 token => $t);
4457     return ($t, undef);
4458     }
4459 wakaba 1.19 return ($t, \%prop_value);
4460     }
4461     $prop_value{'padding-left'} = $prop_value{'padding-right'};
4462    
4463     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
4464     $sign = 1;
4465     if ($t->{type} == MINUS_TOKEN) {
4466     $t = $tt->get_next_token;
4467     $sign = -1;
4468     }
4469    
4470     if ($t->{type} == DIMENSION_TOKEN) {
4471     my $value = $t->{number} * $sign;
4472     my $unit = lc $t->{value}; ## TODO: case
4473     $t = $tt->get_next_token;
4474     if ($length_unit->{$unit} and $value >= 0) {
4475     $prop_value{'padding-bottom'} = ['DIMENSION', $value, $unit];
4476     } else {
4477 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4478 wakaba 1.19 level => $self->{must_level},
4479 wakaba 1.38 uri => \$self->{href},
4480 wakaba 1.19 token => $t);
4481     return ($t, undef);
4482     }
4483     } elsif ($t->{type} == PERCENTAGE_TOKEN) {
4484     my $value = $t->{number} * $sign;
4485     $t = $tt->get_next_token;
4486     $prop_value{'padding-bottom'} = ['PERCENTAGE', $value];
4487     unless ($value >= 0) {
4488 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4489 wakaba 1.19 level => $self->{must_level},
4490 wakaba 1.38 uri => \$self->{href},
4491 wakaba 1.19 token => $t);
4492     return ($t, undef);
4493     }
4494 wakaba 1.20 } elsif ($t->{type} == NUMBER_TOKEN and
4495 wakaba 1.19 ($self->{unitless_px} or $t->{number} == 0)) {
4496     my $value = $t->{number} * $sign;
4497     $t = $tt->get_next_token;
4498     $prop_value{'padding-bottom'} = ['DIMENSION', $value, 'px'];
4499     unless ($value >= 0) {
4500 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4501 wakaba 1.19 level => $self->{must_level},
4502 wakaba 1.38 uri => \$self->{href},
4503 wakaba 1.19 token => $t);
4504     return ($t, undef);
4505     }
4506     } else {
4507 wakaba 1.24 if ($sign < 0) {
4508 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4509 wakaba 1.24 level => $self->{must_level},
4510 wakaba 1.38 uri => \$self->{href},
4511 wakaba 1.24 token => $t);
4512     return ($t, undef);
4513     }
4514 wakaba 1.19 return ($t, \%prop_value);
4515     }
4516    
4517     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
4518     $sign = 1;
4519     if ($t->{type} == MINUS_TOKEN) {
4520     $t = $tt->get_next_token;
4521     $sign = -1;
4522     }
4523    
4524     if ($t->{type} == DIMENSION_TOKEN) {
4525     my $value = $t->{number} * $sign;
4526     my $unit = lc $t->{value}; ## TODO: case
4527     $t = $tt->get_next_token;
4528     if ($length_unit->{$unit} and $value >= 0) {
4529     $prop_value{'padding-left'} = ['DIMENSION', $value, $unit];
4530     } else {
4531 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4532 wakaba 1.19 level => $self->{must_level},
4533 wakaba 1.38 uri => \$self->{href},
4534 wakaba 1.19 token => $t);
4535     return ($t, undef);
4536     }
4537     } elsif ($t->{type} == PERCENTAGE_TOKEN) {
4538     my $value = $t->{number} * $sign;
4539     $t = $tt->get_next_token;
4540     $prop_value{'padding-left'} = ['PERCENTAGE', $value];
4541     unless ($value >= 0) {
4542 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4543 wakaba 1.19 level => $self->{must_level},
4544 wakaba 1.38 uri => \$self->{href},
4545 wakaba 1.19 token => $t);
4546     return ($t, undef);
4547     }
4548 wakaba 1.20 } elsif ($t->{type} == NUMBER_TOKEN and
4549 wakaba 1.19 ($self->{unitless_px} or $t->{number} == 0)) {
4550     my $value = $t->{number} * $sign;
4551     $t = $tt->get_next_token;
4552     $prop_value{'padding-left'} = ['DIMENSION', $value, 'px'];
4553     unless ($value >= 0) {
4554 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4555 wakaba 1.19 level => $self->{must_level},
4556 wakaba 1.38 uri => \$self->{href},
4557 wakaba 1.19 token => $t);
4558     return ($t, undef);
4559     }
4560     } else {
4561 wakaba 1.24 if ($sign < 0) {
4562 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4563 wakaba 1.24 level => $self->{must_level},
4564 wakaba 1.38 uri => \$self->{href},
4565 wakaba 1.24 token => $t);
4566     return ($t, undef);
4567     }
4568 wakaba 1.19 return ($t, \%prop_value);
4569     }
4570    
4571     return ($t, \%prop_value);
4572     },
4573 wakaba 1.43 serialize_multiple => $Prop->{'padding-top'}->{serialize_multiple},
4574 wakaba 1.19 };
4575     $Attr->{padding} = $Prop->{padding};
4576    
4577 wakaba 1.24 $Prop->{'border-spacing'} = {
4578     css => 'border-spacing',
4579     dom => 'border_spacing',
4580     parse => sub {
4581     my ($self, $prop_name, $tt, $t, $onerror) = @_;
4582    
4583     my %prop_value;
4584    
4585     my $sign = 1;
4586     if ($t->{type} == MINUS_TOKEN) {
4587     $t = $tt->get_next_token;
4588     $sign = -1;
4589     }
4590    
4591     if ($t->{type} == DIMENSION_TOKEN) {
4592     my $value = $t->{number} * $sign;
4593     my $unit = lc $t->{value}; ## TODO: case
4594     $t = $tt->get_next_token;
4595     if ($length_unit->{$unit} and $value >= 0) {
4596     $prop_value{'-manakai-border-spacing-x'} = ['DIMENSION', $value, $unit];
4597     } else {
4598 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4599 wakaba 1.24 level => $self->{must_level},
4600 wakaba 1.38 uri => \$self->{href},
4601 wakaba 1.24 token => $t);
4602     return ($t, undef);
4603     }
4604     } elsif ($t->{type} == NUMBER_TOKEN and
4605     ($self->{unitless_px} or $t->{number} == 0)) {
4606     my $value = $t->{number} * $sign;
4607     $t = $tt->get_next_token;
4608     $prop_value{'-manakai-border-spacing-x'} = ['DIMENSION', $value, 'px'];
4609     unless ($value >= 0) {
4610 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4611 wakaba 1.24 level => $self->{must_level},
4612 wakaba 1.38 uri => \$self->{href},
4613 wakaba 1.24 token => $t);
4614     return ($t, undef);
4615     }
4616     } elsif ($sign > 0 and $t->{type} == IDENT_TOKEN) {
4617     my $prop_value = lc $t->{value}; ## TODO: case folding
4618     $t = $tt->get_next_token;
4619     if ($prop_value eq 'inherit') {
4620     $prop_value{'-manakai-border-spacing-x'} = ['INHERIT'];
4621     $prop_value{'-manakai-border-spacing-y'}
4622     = $prop_value{'-manakai-border-spacing-x'};
4623     return ($t, \%prop_value);
4624     } else {
4625 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4626 wakaba 1.24 level => $self->{must_level},
4627 wakaba 1.38 uri => \$self->{href},
4628 wakaba 1.24 token => $t);
4629     return ($t, undef);
4630     }
4631     } else {
4632 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4633 wakaba 1.24 level => $self->{must_level},
4634 wakaba 1.38 uri => \$self->{href},
4635 wakaba 1.24 token => $t);
4636     return ($t, undef);
4637     }
4638     $prop_value{'-manakai-border-spacing-y'}
4639     = $prop_value{'-manakai-border-spacing-x'};
4640    
4641     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
4642     $sign = 1;
4643     if ($t->{type} == MINUS_TOKEN) {
4644     $t = $tt->get_next_token;
4645     $sign = -1;
4646     }
4647    
4648     if ($t->{type} == DIMENSION_TOKEN) {
4649     my $value = $t->{number} * $sign;
4650     my $unit = lc $t->{value}; ## TODO: case
4651     $t = $tt->get_next_token;
4652     if ($length_unit->{$unit} and $value >= 0) {
4653     $prop_value{'-manakai-border-spacing-y'} = ['DIMENSION', $value, $unit];
4654     } else {
4655 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4656 wakaba 1.24 level => $self->{must_level},
4657 wakaba 1.38 uri => \$self->{href},
4658 wakaba 1.24 token => $t);
4659     return ($t, undef);
4660     }
4661     } elsif ($t->{type} == NUMBER_TOKEN and
4662     ($self->{unitless_px} or $t->{number} == 0)) {
4663     my $value = $t->{number} * $sign;
4664     $t = $tt->get_next_token;
4665     $prop_value{'-manakai-border-spacing-y'} = ['DIMENSION', $value, 'px'];
4666     unless ($value >= 0) {
4667 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4668 wakaba 1.24 level => $self->{must_level},
4669 wakaba 1.38 uri => \$self->{href},
4670 wakaba 1.24 token => $t);
4671     return ($t, undef);
4672     }
4673     } else {
4674     if ($sign < 0) {
4675 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4676 wakaba 1.24 level => $self->{must_level},
4677 wakaba 1.38 uri => \$self->{href},
4678 wakaba 1.24 token => $t);
4679     return ($t, undef);
4680     }
4681     return ($t, \%prop_value);
4682     }
4683    
4684     return ($t, \%prop_value);
4685     },
4686     serialize => sub {
4687     my ($self, $prop_name, $value) = @_;
4688    
4689     local $Error::Depth = $Error::Depth + 1;
4690     my @v;
4691     push @v, $self->_manakai_border_spacing_x;
4692 wakaba 1.34 return '' unless length $v[-1];
4693 wakaba 1.24 push @v, $self->_manakai_border_spacing_y;
4694 wakaba 1.34 return '' unless length $v[-1];
4695 wakaba 1.24
4696     pop @v if $v[0] eq $v[1];
4697     return join ' ', @v;
4698     },
4699 wakaba 1.25 serialize_multiple => $Prop->{'-manakai-border-spacing-x'}
4700     ->{serialize_multiple},
4701 wakaba 1.24 };
4702     $Attr->{border_spacing} = $Prop->{'border-spacing'};
4703    
4704 wakaba 1.27 ## NOTE: See <http://suika.fam.cx/gate/2005/sw/background-position> for
4705     ## browser compatibility problems.
4706     $Prop->{'background-position'} = {
4707     css => 'background-position',
4708     dom => 'background_position',
4709     parse => sub {
4710     my ($self, $prop_name, $tt, $t, $onerror) = @_;
4711    
4712     my %prop_value;
4713    
4714     my $sign = 1;
4715     if ($t->{type} == MINUS_TOKEN) {
4716     $t = $tt->get_next_token;
4717     $sign = -1;
4718     }
4719    
4720     if ($t->{type} == DIMENSION_TOKEN) {
4721     my $value = $t->{number} * $sign;
4722     my $unit = lc $t->{value}; ## TODO: case
4723     $t = $tt->get_next_token;
4724     if ($length_unit->{$unit}) {
4725     $prop_value{'background-position-x'} = ['DIMENSION', $value, $unit];
4726     $prop_value{'background-position-y'} = ['PERCENTAGE', 50];
4727     } else {
4728 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4729 wakaba 1.27 level => $self->{must_level},
4730 wakaba 1.38 uri => \$self->{href},
4731 wakaba 1.27 token => $t);
4732     return ($t, undef);
4733     }
4734     } elsif ($t->{type} == PERCENTAGE_TOKEN) {
4735     my $value = $t->{number} * $sign;
4736     $t = $tt->get_next_token;
4737     $prop_value{'background-position-x'} = ['PERCENTAGE', $value];
4738     $prop_value{'background-position-y'} = ['PERCENTAGE', 50];
4739     } elsif ($t->{type} == NUMBER_TOKEN and
4740     ($self->{unitless_px} or $t->{number} == 0)) {
4741     my $value = $t->{number} * $sign;
4742     $t = $tt->get_next_token;
4743     $prop_value{'background-position-x'} = ['DIMENSION', $value, 'px'];
4744     $prop_value{'background-position-y'} = ['PERCENTAGE', 50];
4745     } elsif ($sign > 0 and $t->{type} == IDENT_TOKEN) {
4746     my $prop_value = lc $t->{value}; ## TODO: case folding
4747     $t = $tt->get_next_token;
4748     if ({left => 1, center => 1, right => 1}->{$prop_value}) {
4749     $prop_value{'background-position-x'} = ['KEYWORD', $prop_value];
4750     $prop_value{'background-position-y'} = ['KEYWORD', 'center'];
4751     } elsif ($prop_value eq 'top' or $prop_value eq 'bottom') {
4752     $prop_value{'background-position-y'} = ['KEYWORD', $prop_value];
4753    
4754     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
4755     if ($t->{type} == IDENT_TOKEN) {
4756     my $prop_value = lc $t->{value}; ## TODO: case folding
4757     if ({left => 1, center => 1, right => 1}->{$prop_value}) {
4758     $prop_value{'background-position-x'} = ['KEYWORD', $prop_value];
4759     $t = $tt->get_next_token;
4760     return ($t, \%prop_value);
4761     }
4762     }
4763     $prop_value{'background-position-x'} = ['KEYWORD', 'center'];
4764     return ($t, \%prop_value);
4765     } elsif ($prop_value eq 'inherit') {
4766     $prop_value{'background-position-x'} = ['INHERIT'];
4767     $prop_value{'background-position-y'} = ['INHERIT'];
4768     return ($t, \%prop_value);
4769     } else {
4770 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4771 wakaba 1.27 level => $self->{must_level},
4772 wakaba 1.38 uri => \$self->{href},
4773 wakaba 1.27 token => $t);
4774     return ($t, undef);
4775     }
4776     } else {
4777 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4778 wakaba 1.27 level => $self->{must_level},
4779 wakaba 1.38 uri => \$self->{href},
4780 wakaba 1.27 token => $t);
4781     return ($t, undef);
4782     }
4783    
4784     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
4785     $sign = 1;
4786     if ($t->{type} == MINUS_TOKEN) {
4787     $t = $tt->get_next_token;
4788     $sign = -1;
4789     }
4790    
4791     if ($t->{type} == DIMENSION_TOKEN) {
4792     my $value = $t->{number} * $sign;
4793     my $unit = lc $t->{value}; ## TODO: case
4794     $t = $tt->get_next_token;
4795     if ($length_unit->{$unit}) {
4796     $prop_value{'background-position-y'} = ['DIMENSION', $value, $unit];
4797     } else {
4798 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4799 wakaba 1.27 level => $self->{must_level},
4800 wakaba 1.38 uri => \$self->{href},
4801 wakaba 1.27 token => $t);
4802     return ($t, undef);
4803     }
4804     } elsif ($t->{type} == PERCENTAGE_TOKEN) {
4805     my $value = $t->{number} * $sign;
4806     $t = $tt->get_next_token;
4807     $prop_value{'background-position-y'} = ['PERCENTAGE', $value];
4808 wakaba 1.30 } elsif ($t->{type} == NUMBER_TOKEN and
4809     ($self->{unitless_px} or $t->{number} == 0)) {
4810 wakaba 1.27 my $value = $t->{number} * $sign;
4811     $t = $tt->get_next_token;
4812     $prop_value{'background-position-y'} = ['DIMENSION', $value, 'px'];
4813     } elsif ($t->{type} == IDENT_TOKEN) {
4814     my $value = lc $t->{value}; ## TODO: case
4815     if ({top => 1, center => 1, bottom => 1}->{$value}) {
4816     $prop_value{'background-position-y'} = ['KEYWORD', $value];
4817     $t = $tt->get_next_token;
4818     }
4819     } else {
4820     if ($sign < 0) {
4821 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4822 wakaba 1.27 level => $self->{must_level},
4823 wakaba 1.38 uri => \$self->{href},
4824 wakaba 1.27 token => $t);
4825     return ($t, undef);
4826     }
4827     return ($t, \%prop_value);
4828     }
4829    
4830     return ($t, \%prop_value);
4831     },
4832 wakaba 1.48 serialize_shorthand => sub {
4833     my $self = shift;
4834    
4835     my $r = {};
4836    
4837 wakaba 1.27 my $x = $self->background_position_x;
4838     my $y = $self->background_position_y;
4839 wakaba 1.48 my $xi = $self->get_property_priority ('background-position-x');
4840     my $yi = $self->get_property_priority ('background-position-y');
4841     if (length $x) {
4842     if (length $y) {
4843     if ($xi eq $yi) {
4844     if ($x eq 'inherit') {
4845     if ($y eq 'inherit') {
4846     $r->{'background-position'} = ['inherit', $xi];
4847     } else {
4848     $r->{'background-position-x'} = [$x, $xi];
4849     $r->{'background-position-y'} = [$y, $yi];
4850     }
4851     } elsif ($y eq 'inherit') {
4852     $r->{'background-position-x'} = [$x, $xi];
4853     $r->{'background-position-y'} = [$y, $yi];
4854     } else {
4855     $r->{'background-position'} = [$x . ' ' . $y, $xi];
4856     }
4857     } else {
4858     $r->{'background-position-x'} = [$x, $xi];
4859     $r->{'background-position-y'} = [$y, $yi];
4860     }
4861     } else {
4862     $r->{'background-position-x'} = [$x, $xi];
4863     }
4864     } else {
4865     if (length $y) {
4866     $r->{'background-position-y'} = [$y, $yi];
4867     } else {
4868     #
4869     }
4870     }
4871    
4872     return $r;
4873 wakaba 1.27 },
4874 wakaba 1.30 serialize_multiple => $Prop->{'background-color'}->{serialize_multiple},
4875 wakaba 1.27 };
4876     $Attr->{background_position} = $Prop->{'background-position'};
4877    
4878 wakaba 1.30 $Prop->{background} = {
4879     css => 'background',
4880     dom => 'background',
4881     parse => sub {
4882     my ($self, $prop_name, $tt, $t, $onerror) = @_;
4883     my %prop_value;
4884     B: for (1..5) {
4885 wakaba 1.48 my $has_sign;
4886 wakaba 1.30 my $sign = 1;
4887     if ($t->{type} == MINUS_TOKEN) {
4888     $sign = -1;
4889 wakaba 1.48 $has_sign = 1;
4890     $t = $tt->get_next_token;
4891     } elsif ($t->{type} == PLUS_TOKEN) {
4892     $has_sign = 1;
4893 wakaba 1.30 $t = $tt->get_next_token;
4894     }
4895    
4896 wakaba 1.48 if (not $has_sign and $t->{type} == IDENT_TOKEN) {
4897 wakaba 1.30 my $value = lc $t->{value}; ## TODO: case
4898     if ($Prop->{'background-repeat'}->{keyword}->{$value} and
4899     $self->{prop_value}->{'background-repeat'}->{$value} and
4900     not defined $prop_value{'background-repeat'}) {
4901     $prop_value{'background-repeat'} = ['KEYWORD', $value];
4902     $t = $tt->get_next_token;
4903     } elsif ($Prop->{'background-attachment'}->{keyword}->{$value} and
4904     $self->{prop_value}->{'background-attachment'}->{$value} and
4905     not defined $prop_value{'background-attachment'}) {
4906     $prop_value{'background-attachment'} = ['KEYWORD', $value];
4907     $t = $tt->get_next_token;
4908     } elsif ($value eq 'none' and
4909     not defined $prop_value{'background-image'}) {
4910     $prop_value{'background-image'} = ['KEYWORD', $value];
4911     $t = $tt->get_next_token;
4912     } elsif ({left => 1, center => 1, right => 1}->{$value} and
4913     not defined $prop_value{'background-position-x'}) {
4914     $prop_value{'background-position-x'} = ['KEYWORD', $value];
4915     $t = $tt->get_next_token;
4916     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
4917     my $sign = 1;
4918 wakaba 1.48 my $has_sign;
4919 wakaba 1.30 if ($t->{type} == MINUS_TOKEN) {
4920     $sign = -1;
4921 wakaba 1.48 $has_sign = 1;
4922     $t = $tt->get_next_token;
4923     } elsif ($t->{type} == PLUS_TOKEN) {
4924     $has_sign = 1;
4925 wakaba 1.30 $t = $tt->get_next_token;
4926     }
4927 wakaba 1.48 if (not $has_sign and $t->{type} == IDENT_TOKEN) {
4928 wakaba 1.30 my $value = lc $t->{value}; ## TODO: case
4929     if ({top => 1, bottom => 1, center => 1}->{$value}) {
4930     $prop_value{'background-position-y'} = ['KEYWORD', $value];
4931     $t = $tt->get_next_token;
4932     } elsif ($prop_value{'background-position-x'}->[1] eq 'center' and
4933     $value eq 'left' or $value eq 'right') {
4934     $prop_value{'background-position-y'} = ['KEYWORD', 'center'];
4935     $prop_value{'background-position-x'} = ['KEYWORD', $value];
4936     $t = $tt->get_next_token;
4937     } else {
4938     $prop_value{'background-position-y'} = ['KEYWORD', 'center'];
4939     }
4940     } elsif ($t->{type} == DIMENSION_TOKEN) {
4941     my $value = $t->{number} * $sign;
4942     my $unit = lc $t->{value}; ## TODO: case
4943     $t = $tt->get_next_token;
4944     if ($length_unit->{$unit}) {
4945     $prop_value{'background-position-y'}
4946     = ['DIMENSION', $value, $unit];
4947     } else {
4948 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4949 wakaba 1.30 level => $self->{must_level},
4950 wakaba 1.38 uri => \$self->{href},
4951 wakaba 1.30 token => $t);
4952 wakaba 1.48 return ($t, undef);
4953 wakaba 1.30 }
4954     } elsif ($t->{type} == PERCENTAGE_TOKEN) {
4955     my $value = $t->{number} * $sign;
4956     $t = $tt->get_next_token;
4957     $prop_value{'background-position-y'} = ['PERCENTAGE', $value];
4958     } elsif ($t->{type} == NUMBER_TOKEN and
4959     ($self->{unitless_px} or $t->{number} == 0)) {
4960     my $value = $t->{number} * $sign;
4961     $t = $tt->get_next_token;
4962     $prop_value{'background-position-y'} = ['DIMENSION', $value, 'px'];
4963 wakaba 1.48 } elsif ($has_sign) {
4964 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4965 wakaba 1.30 level => $self->{must_level},
4966 wakaba 1.38 uri => \$self->{href},
4967 wakaba 1.30 token => $t);
4968 wakaba 1.48 return ($t, undef);
4969     } else {
4970     $prop_value{'background-position-y'} = ['KEYWORD', 'center'];
4971 wakaba 1.30 }
4972     } elsif (($value eq 'top' or $value eq 'bottom') and
4973     not defined $prop_value{'background-position-y'}) {
4974     $prop_value{'background-position-y'} = ['KEYWORD', $value];
4975     $t = $tt->get_next_token;
4976     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
4977     if ($t->{type} == IDENT_TOKEN and ## TODO: case
4978 wakaba 1.48 {
4979     left => 1, center => 1, right => 1,
4980     }->{my $value = lc $t->{value}}) {
4981 wakaba 1.30 $prop_value{'background-position-x'} = ['KEYWORD', $value];
4982     $t = $tt->get_next_token;
4983     } else {
4984     $prop_value{'background-position-x'} = ['KEYWORD', 'center'];
4985     }
4986     } elsif ($value eq 'inherit' and not keys %prop_value) {
4987     $prop_value{'background-color'} =
4988     $prop_value{'background-image'} =
4989     $prop_value{'background-repeat'} =
4990     $prop_value{'background-attachment'} =
4991     $prop_value{'background-position-x'} =
4992     $prop_value{'background-position-y'} = ['INHERIT'];
4993     $t = $tt->get_next_token;
4994     return ($t, \%prop_value);
4995     } elsif (not defined $prop_value{'background-color'} or
4996     not keys %prop_value) {
4997     ($t, my $pv) = $parse_color->($self, 'background', $tt, $t,
4998     $onerror);
4999     if (defined $pv) {
5000     $prop_value{'background-color'} = $pv->{background};
5001     } else {
5002     ## NOTE: An error should already be raiased.
5003     return ($t, undef);
5004     }
5005     }
5006     } elsif (($t->{type} == DIMENSION_TOKEN or
5007     $t->{type} == PERCENTAGE_TOKEN or
5008     ($t->{type} == NUMBER_TOKEN and
5009 wakaba 1.48 ($t->{unitless_px} or $t->{number} == 0))) and
5010 wakaba 1.30 not defined $prop_value{'background-position-x'}) {
5011     if ($t->{type} == DIMENSION_TOKEN) {
5012     my $value = $t->{number} * $sign;
5013     my $unit = lc $t->{value}; ## TODO: case
5014     $t = $tt->get_next_token;
5015     if ($length_unit->{$unit}) {
5016     $prop_value{'background-position-x'}
5017     = ['DIMENSION', $value, $unit];
5018     } else {
5019 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
5020 wakaba 1.30 level => $self->{must_level},
5021 wakaba 1.38 uri => \$self->{href},
5022 wakaba 1.30 token => $t);
5023 wakaba 1.48 return ($t, undef);
5024 wakaba 1.30 }
5025     } elsif ($t->{type} == PERCENTAGE_TOKEN) {
5026     my $value = $t->{number} * $sign;
5027     $t = $tt->get_next_token;
5028     $prop_value{'background-position-x'} = ['PERCENTAGE', $value];
5029     } elsif ($t->{type} == NUMBER_TOKEN and
5030     ($self->{unitless_px} or $t->{number} == 0)) {
5031     my $value = $t->{number} * $sign;
5032     $t = $tt->get_next_token;
5033     $prop_value{'background-position-x'} = ['DIMENSION', $value, 'px'];
5034     } else {
5035     ## NOTE: Should not be happened.
5036     last B;
5037     }
5038    
5039     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
5040     if ($t->{type} == MINUS_TOKEN) {
5041     $sign = -1;
5042 wakaba 1.48 $has_sign = 1;
5043     $t = $tt->get_next_token;
5044     } elsif ($t->{type} == PLUS_TOKEN) {
5045     $has_sign = 1;
5046 wakaba 1.30 $t = $tt->get_next_token;
5047     } else {
5048 wakaba 1.48 undef $has_sign;
5049 wakaba 1.30 $sign = 1;
5050     }
5051    
5052     if ($t->{type} == DIMENSION_TOKEN) {
5053     my $value = $t->{number} * $sign;
5054     my $unit = lc $t->{value}; ## TODO: case
5055     $t = $tt->get_next_token;
5056     if ($length_unit->{$unit}) {
5057     $prop_value{'background-position-y'}
5058     = ['DIMENSION', $value, $unit];
5059     } else {
5060 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
5061 wakaba 1.30 level => $self->{must_level},
5062 wakaba 1.38 uri => \$self->{href},
5063 wakaba 1.30 token => $t);
5064 wakaba 1.48 return ($t, undef);
5065 wakaba 1.30 }
5066     } elsif ($t->{type} == PERCENTAGE_TOKEN) {
5067     my $value = $t->{number} * $sign;
5068     $t = $tt->get_next_token;
5069     $prop_value{'background-position-y'} = ['PERCENTAGE', $value];
5070     } elsif ($t->{type} == NUMBER_TOKEN and
5071     ($self->{unitless_px} or $t->{number} == 0)) {
5072     my $value = $t->{number} * $sign;
5073     $t = $tt->get_next_token;
5074     $prop_value{'background-position-y'} = ['DIMENSION', $value, 'px'];
5075     } elsif ($t->{type} == IDENT_TOKEN) {
5076     my $value = lc $t->{value}; ## TODO: case
5077     if ({top => 1, center => 1, bottom => 1}->{$value}) {
5078     $prop_value{'background-position-y'} = ['KEYWORD', $value];
5079     $t = $tt->get_next_token;
5080     } else {
5081     $prop_value{'background-position-y'} = ['PERCENTAGE', 50];
5082     }
5083     } else {
5084 wakaba 1.48 $prop_value{'background-position-y'} = ['PERCENTAGE', 50];
5085     if ($has_sign) {
5086 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
5087 wakaba 1.30 level => $self->{must_level},
5088 wakaba 1.38 uri => \$self->{href},
5089 wakaba 1.30 token => $t);
5090 wakaba 1.48 return ($t, undef);
5091 wakaba 1.30 }
5092     }
5093 wakaba 1.48 } elsif (not $has_sign and
5094     $t->{type} == URI_TOKEN and
5095 wakaba 1.30 not defined $prop_value{'background-image'}) {
5096 wakaba 1.48 $prop_value{'background-image'}
5097     = ['URI', $t->{value}, \($self->{base_uri})];
5098 wakaba 1.30 $t = $tt->get_next_token;
5099     } else {
5100 wakaba 1.48 if (keys %prop_value and not $has_sign) {
5101 wakaba 1.30 last B;
5102     } else {
5103 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
5104 wakaba 1.30 level => $self->{must_level},
5105 wakaba 1.38 uri => \$self->{href},
5106 wakaba 1.30 token => $t);
5107 wakaba 1.48 return ($t, undef);
5108 wakaba 1.30 }
5109     }
5110    
5111     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
5112     } # B
5113    
5114     $prop_value{$_} ||= $Prop->{$_}->{initial}
5115     for qw/background-image background-attachment background-repeat
5116     background-color background-position-x background-position-y/;
5117    
5118     return ($t, \%prop_value);
5119     },
5120     serialize_multiple => $Prop->{'background-color'}->{serialize_multiple},
5121     };
5122     $Attr->{background} = $Prop->{background};
5123    
5124 wakaba 1.31 $Prop->{font} = {
5125     css => 'font',
5126     dom => 'font',
5127     parse => sub {
5128     my ($self, $prop_name, $tt, $t, $onerror) = @_;
5129    
5130     my %prop_value;
5131    
5132     A: for (1..3) {
5133     if ($t->{type} == IDENT_TOKEN) {
5134     my $value = lc $t->{value}; ## TODO: case
5135     if ($value eq 'normal') {
5136     $t = $tt->get_next_token;
5137     } elsif ($Prop->{'font-style'}->{keyword}->{$value} and
5138     $self->{prop_value}->{'font-style'}->{$value} and
5139     not defined $prop_value{'font-style'}) {
5140     $prop_value{'font-style'} = ['KEYWORD', $value];
5141     $t = $tt->get_next_token;
5142     } elsif ($Prop->{'font-variant'}->{keyword}->{$value} and
5143     $self->{prop_value}->{'font-variant'}->{$value} and
5144     not defined $prop_value{'font-variant'}) {
5145     $prop_value{'font-variant'} = ['KEYWORD', $value];
5146     $t = $tt->get_next_token;
5147     } elsif ({normal => 1, bold => 1,
5148     bolder => 1, lighter => 1}->{$value} and
5149     not defined $prop_value{'font-weight'}) {
5150     $prop_value{'font-weight'} = ['KEYWORD', $value];
5151     $t = $tt->get_next_token;
5152     } elsif ($value eq 'inherit' and 0 == keys %prop_value) {
5153     $t = $tt->get_next_token;
5154     return ($t, {'font-style' => ['INHERIT'],
5155     'font-variant' => ['INHERIT'],
5156     'font-weight' => ['INHERIT'],
5157     'font-size' => ['INHERIT'],
5158     'line-height' => ['INHERIT'],
5159     'font-family' => ['INHERIT']});
5160     } elsif ({
5161     caption => 1, icon => 1, menu => 1,
5162     'message-box' => 1, 'small-caption' => 1, 'status-bar' => 1,
5163     }->{$value} and 0 == keys %prop_value) {
5164     $t = $tt->get_next_token;
5165     return ($t, $self->{get_system_font}->($self, $value, {
5166     'font-style' => $Prop->{'font-style'}->{initial},
5167     'font-variant' => $Prop->{'font-variant'}->{initial},
5168     'font-weight' => $Prop->{'font-weight'}->{initial},
5169     'font-size' => $Prop->{'font-size'}->{initial},
5170     'line-height' => $Prop->{'line-height'}->{initial},
5171     'font-family' => ['FONT', ['KEYWORD', '-manakai-'.$value]],
5172     }));
5173     } else {
5174     if (keys %prop_value) {
5175     last A;
5176     } else {
5177 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
5178 wakaba 1.31 level => $self->{must_level},
5179 wakaba 1.38 uri => \$self->{href},
5180 wakaba 1.31 token => $t);
5181     return ($t, undef);
5182     }
5183     }
5184     } elsif ($t->{type} == NUMBER_TOKEN) {
5185     if ({100 => 1, 200 => 1, 300 => 1, 400 => 1, 500 => 1,
5186 wakaba 1.49 600 => 1, 700 => 1, 800 => 1, 900 => 1}->{$t->{number}}) {
5187     $prop_value{'font-weight'} = ['WEIGHT', $t->{number}, 0];
5188 wakaba 1.31 $t = $tt->get_next_token;
5189     } else {
5190     last A;
5191     }
5192 wakaba 1.49 } elsif ($t->{type} == PLUS_TOKEN) {
5193     $t = $tt->get_next_token;
5194     if ($t->{type} == NUMBER_TOKEN) {
5195     if ({100 => 1, 200 => 1, 300 => 1, 400 => 1, 500 => 1,
5196     600 => 1, 700 => 1, 800 => 1, 900 => 1}->{$t->{number}}) {
5197     $prop_value{'font-weight'} = ['WEIGHT', $t->{number}, 0];
5198     $t = $tt->get_next_token;
5199     } else {
5200     ## NOTE: <'font-size'> or invalid
5201     last A;
5202     }
5203     } elsif ($t->{type} == DIMENSION_TOKEN or
5204     $t->{type} == PERCENTAGE_TOKEN) {
5205     ## NOTE: <'font-size'> or invalid
5206     last A;
5207     } else {
5208     $onerror->(type => "syntax error:'$prop_name'",
5209     level => $self->{must_level},
5210     uri => \$self->{href},
5211     token => $t);
5212     return ($t, undef);
5213     }
5214     } else {
5215     last A;
5216 wakaba 1.31 }
5217    
5218     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
5219     } # A
5220    
5221     for (qw/font-style font-variant font-weight/) {
5222     $prop_value{$_} = $Prop->{$_}->{initial} unless defined $prop_value{$_};
5223     }
5224    
5225     ($t, my $pv) = $Prop->{'font-size'}->{parse}
5226     ->($self, 'font', $tt, $t, $onerror);
5227     return ($t, undef) unless defined $pv;
5228     if ($pv->{font}->[0] eq 'INHERIT') {
5229 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
5230 wakaba 1.31 level => $self->{must_level},
5231 wakaba 1.38 uri => \$self->{href},
5232 wakaba 1.31 token => $t);
5233     return ($t, undef);
5234     }
5235     $prop_value{'font-size'} = $pv->{font};
5236    
5237     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
5238     if ($t->{type} == DELIM_TOKEN and $t->{value} eq '/') {
5239     $t = $tt->get_next_token;
5240     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
5241     ($t, my $pv) = $Prop->{'line-height'}->{parse}
5242     ->($self, 'font', $tt, $t, $onerror);
5243     return ($t, undef) unless defined $pv;
5244     if ($pv->{font}->[0] eq 'INHERIT') {
5245 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
5246 wakaba 1.31 level => $self->{must_level},
5247 wakaba 1.38 uri => \$self->{href},
5248 wakaba 1.31 token => $t);
5249     return ($t, undef);
5250     }
5251     $prop_value{'line-height'} = $pv->{font};
5252     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
5253     } else {
5254     $prop_value{'line-height'} = $Prop->{'line-height'}->{initial};
5255     }
5256    
5257     undef $pv;
5258     ($t, $pv) = $Prop->{'font-family'}->{parse}
5259     ->($self, 'font', $tt, $t, $onerror);
5260     return ($t, undef) unless defined $pv;
5261     $prop_value{'font-family'} = $pv->{font};
5262    
5263     return ($t, \%prop_value);
5264     },
5265 wakaba 1.44 serialize_shorthand => sub {
5266     my $self = shift;
5267 wakaba 1.31
5268     local $Error::Depth = $Error::Depth + 1;
5269     my $style = $self->font_style;
5270     my $i = $self->get_property_priority ('font-style');
5271 wakaba 1.44 return {} unless length $style;
5272 wakaba 1.31 my $variant = $self->font_variant;
5273 wakaba 1.44 return {} unless length $variant;
5274     return {} if $i ne $self->get_property_priority ('font-variant');
5275 wakaba 1.31 my $weight = $self->font_weight;
5276 wakaba 1.44 return {} unless length $weight;
5277     return {} if $i ne $self->get_property_priority ('font-weight');
5278 wakaba 1.31 my $size = $self->font_size;
5279 wakaba 1.44 return {} unless length $size;
5280     return {} if $i ne $self->get_property_priority ('font-size');
5281 wakaba 1.31 my $height = $self->line_height;
5282 wakaba 1.44 return {} unless length $height;
5283     return {} if $i ne $self->get_property_priority ('line-height');
5284 wakaba 1.31 my $family = $self->font_family;
5285 wakaba 1.44 return {} unless length $family;
5286     return {} if $i ne $self->get_property_priority ('font-family');
5287    
5288     my $v = 0;
5289     for ($style, $variant, $weight, $size, $height, $family) {
5290     $v++ if $_ eq 'inherit';
5291     }
5292     if ($v == 6) {
5293 wakaba 1.45 return {font => ['inherit', $i]};
5294 wakaba 1.44 } elsif ($v) {
5295     return {};
5296     }
5297 wakaba 1.31
5298     my @v;
5299     push @v, $style unless $style eq 'normal';
5300     push @v, $variant unless $variant eq 'normal';
5301     push @v, $weight unless $weight eq 'normal';
5302     push @v, $size.($height eq 'normal' ? '' : '/'.$height);
5303     push @v, $family;
5304 wakaba 1.45 return {font => [(join ' ', @v), $i]};
5305 wakaba 1.31 },
5306     };
5307     $Attr->{font} = $Prop->{font};
5308    
5309 wakaba 1.20 $Prop->{'border-width'} = {
5310     css => 'border-width',
5311     dom => 'border_width',
5312     parse => sub {
5313     my ($self, $prop_name, $tt, $t, $onerror) = @_;
5314    
5315     my %prop_value;
5316    
5317     my $sign = 1;
5318     if ($t->{type} == MINUS_TOKEN) {
5319     $t = $tt->get_next_token;
5320     $sign = -1;
5321     }
5322    
5323     if ($t->{type} == DIMENSION_TOKEN) {
5324     my $value = $t->{number} * $sign;
5325     my $unit = lc $t->{value}; ## TODO: case
5326     $t = $tt->get_next_token;
5327     if ($length_unit->{$unit} and $value >= 0) {
5328     $prop_value{'border-top-width'} = ['DIMENSION', $value, $unit];
5329     } else {
5330 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
5331 wakaba 1.20 level => $self->{must_level},
5332 wakaba 1.38 uri => \$self->{href},
5333 wakaba 1.20 token => $t);
5334     return ($t, undef);
5335     }
5336     } elsif ($t->{type} == NUMBER_TOKEN and
5337     ($self->{unitless_px} or $t->{number} == 0)) {
5338     my $value = $t->{number} * $sign;
5339     $t = $tt->get_next_token;
5340     $prop_value{'border-top-width'} = ['DIMENSION', $value, 'px'];
5341     unless ($value >= 0) {
5342 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
5343 wakaba 1.20 level => $self->{must_level},
5344 wakaba 1.38 uri => \$self->{href},
5345 wakaba 1.20 token => $t);
5346     return ($t, undef);
5347     }
5348     } elsif ($sign > 0 and $t->{type} == IDENT_TOKEN) {
5349     my $prop_value = lc $t->{value}; ## TODO: case folding
5350     $t = $tt->get_next_token;
5351     if ({thin => 1, medium => 1, thick => 1}->{$prop_value}) {
5352     $prop_value{'border-top-width'} = ['KEYWORD', $prop_value];
5353     } elsif ($prop_value eq 'inherit') {
5354     $prop_value{'border-top-width'} = ['INHERIT'];
5355     $prop_value{'border-right-width'} = $prop_value{'border-top-width'};
5356     $prop_value{'border-bottom-width'} = $prop_value{'border-top-width'};
5357     $prop_value{'border-left-width'} = $prop_value{'border-right-width'};
5358     return ($t, \%prop_value);
5359     } else {
5360 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
5361 wakaba 1.20 level => $self->{must_level},
5362 wakaba 1.38 uri => \$self->{href},
5363 wakaba 1.20 token => $t);
5364     return ($t, undef);
5365     }
5366     } else {
5367 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
5368 wakaba 1.20 level => $self->{must_level},
5369 wakaba 1.38 uri => \$self->{href},
5370 wakaba 1.20 token => $t);
5371     return ($t, undef);
5372     }
5373     $prop_value{'border-right-width'} = $prop_value{'border-top-width'};
5374     $prop_value{'border-bottom-width'} = $prop_value{'border-top-width'};
5375     $prop_value{'border-left-width'} = $prop_value{'border-right-width'};
5376    
5377     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
5378     $sign = 1;
5379     if ($t->{type} == MINUS_TOKEN) {
5380     $t = $tt->get_next_token;
5381     $sign = -1;
5382     }
5383    
5384     if ($t->{type} == DIMENSION_TOKEN) {
5385     my $value = $t->{number} * $sign;
5386     my $unit = lc $t->{value}; ## TODO: case
5387     $t = $tt->get_next_token;
5388     if ($length_unit->{$unit} and $value >= 0) {
5389     $prop_value{'border-right-width'} = ['DIMENSION', $value, $unit];
5390     } else {
5391 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
5392 wakaba 1.20 level => $self->{must_level},
5393 wakaba 1.38 uri => \$self->{href},
5394 wakaba 1.20 token => $t);
5395     return ($t, undef);
5396     }
5397     } elsif ($t->{type} == NUMBER_TOKEN and
5398     ($self->{unitless_px} or $t->{number} == 0)) {
5399     my $value = $t->{number} * $sign;
5400     $t = $tt->get_next_token;
5401     $prop_value{'border-right-width'} = ['DIMENSION', $value, 'px'];
5402     unless ($value >= 0) {
5403 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
5404 wakaba 1.20 level => $self->{must_level},
5405 wakaba 1.38 uri => \$self->{href},
5406 wakaba 1.20 token => $t);
5407     return ($t, undef);
5408     }
5409     } elsif ($sign > 0 and $t->{type} == IDENT_TOKEN) {
5410     my $prop_value = lc $t->{value}; ## TODO: case
5411     if ({thin => 1, medium => 1, thick => 1}->{$prop_value}) {
5412     $prop_value{'border-right-width'} = ['KEYWORD', $prop_value];
5413     $t = $tt->get_next_token;
5414     }
5415     } else {
5416 wakaba 1.24 if ($sign < 0) {
5417 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
5418 wakaba 1.24 level => $self->{must_level},
5419 wakaba 1.38 uri => \$self->{href},
5420 wakaba 1.24 token => $t);
5421     return ($t, undef);
5422     }
5423 wakaba 1.20 return ($t, \%prop_value);
5424     }
5425     $prop_value{'border-left-width'} = $prop_value{'border-right-width'};
5426    
5427     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
5428     $sign = 1;
5429     if ($t->{type} == MINUS_TOKEN) {
5430     $t = $tt->get_next_token;
5431     $sign = -1;
5432     }
5433    
5434     if ($t->{type} == DIMENSION_TOKEN) {
5435     my $value = $t->{number} * $sign;
5436     my $unit = lc $t->{value}; ## TODO: case
5437     $t = $tt->get_next_token;
5438     if ($length_unit->{$unit} and $value >= 0) {
5439     $prop_value{'border-bottom-width'} = ['DIMENSION', $value, $unit];
5440     } else {
5441 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
5442 wakaba 1.20 level => $self->{must_level},
5443 wakaba 1.38 uri => \$self->{href},
5444 wakaba 1.20 token => $t);
5445     return ($t, undef);
5446     }
5447     } elsif ($t->{type} == NUMBER_TOKEN and
5448     ($self->{unitless_px} or $t->{number} == 0)) {
5449     my $value = $t->{number} * $sign;
5450     $t = $tt->get_next_token;
5451     $prop_value{'border-bottom-width'} = ['DIMENSION', $value, 'px'];
5452     unless ($value >= 0) {
5453 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
5454 wakaba 1.20 level => $self->{must_level},
5455 wakaba 1.38 uri => \$self->{href},
5456 wakaba 1.20 token => $t);
5457     return ($t, undef);
5458     }
5459     } elsif ($sign > 0 and $t->{type} == IDENT_TOKEN) {
5460     my $prop_value = lc $t->{value}; ## TODO: case
5461     if ({thin => 1, medium => 1, thick => 1}->{$prop_value}) {
5462     $prop_value{'border-bottom-width'} = ['KEYWORD', $prop_value];
5463     $t = $tt->get_next_token;
5464     }
5465     } else {
5466 wakaba 1.24 if ($sign < 0) {
5467 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
5468 wakaba 1.24 level => $self->{must_level},
5469 wakaba 1.38 uri => \$self->{href},
5470 wakaba 1.24 token => $t);
5471     return ($t, undef);
5472     }
5473 wakaba 1.20 return ($t, \%prop_value);
5474     }
5475    
5476     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
5477     $sign = 1;
5478     if ($t->{type} == MINUS_TOKEN) {
5479     $t = $tt->get_next_token;
5480     $sign = -1;
5481     }
5482    
5483     if ($t->{type} == DIMENSION_TOKEN) {
5484     my $value = $t->{number} * $sign;
5485     my $unit = lc $t->{value}; ## TODO: case
5486     $t = $tt->get_next_token;
5487     if ($length_unit->{$unit} and $value >= 0) {
5488     $prop_value{'border-left-width'} = ['DIMENSION', $value, $unit];
5489     } else {
5490 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
5491 wakaba 1.20 level => $self->{must_level},
5492 wakaba 1.38 uri => \$self->{href},
5493 wakaba 1.20 token => $t);
5494     return ($t, undef);
5495     }
5496     } elsif ($t->{type} == NUMBER_TOKEN and
5497     ($self->{unitless_px} or $t->{number} == 0)) {
5498     my $value = $t->{number} * $sign;
5499     $t = $tt->get_next_token;
5500     $prop_value{'border-left-width'} = ['DIMENSION', $value, 'px'];
5501     unless ($value >= 0) {
5502 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
5503 wakaba 1.20 level => $self->{must_level},
5504 wakaba 1.38 uri => \$self->{href},
5505 wakaba 1.20 token => $t);
5506     return ($t, undef);
5507     }
5508     } elsif ($sign > 0 and $t->{type} == IDENT_TOKEN) {
5509     my $prop_value = lc $t->{value}; ## TODO: case
5510     if ({thin => 1, medium => 1, thick => 1}->{$prop_value}) {
5511     $prop_value{'border-left-width'} = ['KEYWORD', $prop_value];
5512     $t = $tt->get_next_token;
5513     }
5514     } else {
5515 wakaba 1.24 if ($sign < 0) {
5516 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
5517 wakaba 1.24 level => $self->{must_level},
5518 wakaba 1.38 uri => \$self->{href},
5519 wakaba 1.24 token => $t);
5520     return ($t, undef);
5521     }
5522 wakaba 1.20 return ($t, \%prop_value);
5523     }
5524    
5525     return ($t, \%prop_value);
5526     },
5527 wakaba 1.43 serialize_shorthand => sub {
5528     my $self = shift;
5529    
5530 wakaba 1.20 my @v;
5531 wakaba 1.24 push @v, $self->border_top_width;
5532 wakaba 1.43 my $i = $self->get_property_priority ('border-top-width');
5533 wakaba 1.45 return {} unless length $v[-1];
5534 wakaba 1.24 push @v, $self->border_right_width;
5535 wakaba 1.45 return {} unless length $v[-1];
5536     return {} unless $i eq $self->get_property_priority ('border-right-width');
5537 wakaba 1.24 push @v, $self->border_bottom_width;
5538 wakaba 1.45 return {} unless length $v[-1];
5539     return {} unless $i eq $self->get_property_priority ('border-bottom-width');
5540 wakaba 1.24 push @v, $self->border_left_width;
5541 wakaba 1.45 return {} unless length $v[-1];
5542     return {} unless $i eq $self->get_property_priority ('border-left-width');
5543 wakaba 1.43
5544     my $v = 0;
5545     for (0..3) {
5546     $v++ if $v[$_] eq 'inherit';
5547     }
5548     if ($v == 4) {
5549 wakaba 1.45 return {'border-width' => ['inherit', $i]};
5550 wakaba 1.43 } elsif ($v) {
5551     return {};
5552     }
5553 wakaba 1.20
5554     pop @v if $v[1] eq $v[3];
5555     pop @v if $v[0] eq $v[2];
5556     pop @v if $v[0] eq $v[1];
5557 wakaba 1.45 return {'border-width' => [(join ' ', @v), $i]};
5558 wakaba 1.20 },
5559 wakaba 1.29 serialize_multiple => $Prop->{'border-top-color'}->{serialize_multiple},
5560 wakaba 1.20 };
5561 wakaba 1.24 $Attr->{border_width} = $Prop->{'border-width'};
5562 wakaba 1.20
5563 wakaba 1.12 $Prop->{'list-style'} = {
5564     css => 'list-style',
5565     dom => 'list_style',
5566     parse => sub {
5567     my ($self, $prop_name, $tt, $t, $onerror) = @_;
5568    
5569     my %prop_value;
5570     my $none = 0;
5571    
5572     F: for my $f (1..3) {
5573     if ($t->{type} == IDENT_TOKEN) {
5574     my $prop_value = lc $t->{value}; ## TODO: case folding
5575     $t = $tt->get_next_token;
5576    
5577     if ($prop_value eq 'none') {
5578     $none++;
5579     } elsif ($Prop->{'list-style-type'}->{keyword}->{$prop_value}) {
5580     if (exists $prop_value{'list-style-type'}) {
5581 wakaba 1.39 $onerror->(type => "duplication:'list-style-type'",
5582 wakaba 1.12 level => $self->{must_level},
5583 wakaba 1.38 uri => \$self->{href},
5584 wakaba 1.12 token => $t);
5585     return ($t, undef);
5586     } else {
5587     $prop_value{'list-style-type'} = ['KEYWORD', $prop_value];
5588     }
5589     } elsif ($Prop->{'list-style-position'}->{keyword}->{$prop_value}) {
5590     if (exists $prop_value{'list-style-position'}) {
5591 wakaba 1.39 $onerror->(type => "duplication:'list-style-position'",
5592 wakaba 1.12 level => $self->{must_level},
5593 wakaba 1.38 uri => \$self->{href},
5594 wakaba 1.12 token => $t);
5595     return ($t, undef);
5596     }
5597    
5598     $prop_value{'list-style-position'} = ['KEYWORD', $prop_value];
5599     } elsif ($f == 1 and $prop_value eq 'inherit') {
5600     $prop_value{'list-style-type'} = ["INHERIT"];
5601     $prop_value{'list-style-position'} = ["INHERIT"];
5602     $prop_value{'list-style-image'} = ["INHERIT"];
5603     last F;
5604     } else {
5605     if ($f == 1) {
5606 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
5607 wakaba 1.12 level => $self->{must_level},
5608 wakaba 1.38 uri => \$self->{href},
5609 wakaba 1.12 token => $t);
5610     return ($t, undef);
5611     } else {
5612     last F;
5613     }
5614     }
5615     } elsif ($t->{type} == URI_TOKEN) {
5616     if (exists $prop_value{'list-style-image'}) {
5617 wakaba 1.39 $onerror->(type => "duplication:'list-style-image'",
5618 wakaba 1.38 uri => \$self->{href},
5619 wakaba 1.12 level => $self->{must_level},
5620     token => $t);
5621     return ($t, undef);
5622     }
5623    
5624     $prop_value{'list-style-image'}
5625 wakaba 1.13 = ['URI', $t->{value}, \($self->{base_uri})];
5626 wakaba 1.12 $t = $tt->get_next_token;
5627     } else {
5628     if ($f == 1) {
5629 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
5630 wakaba 1.12 level => $self->{must_level},
5631 wakaba 1.38 uri => \$self->{href},
5632 wakaba 1.12 token => $t);
5633     return ($t, undef);
5634     } else {
5635     last F;
5636     }
5637     }
5638    
5639     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
5640     } # F
5641     ## NOTE: No browser support |list-style: url(xxx|{EOF}.
5642    
5643     if ($none == 1) {
5644     if (exists $prop_value{'list-style-type'}) {
5645     if (exists $prop_value{'list-style-image'}) {
5646 wakaba 1.39 $onerror->(type => "duplication:'list-style-image'",
5647 wakaba 1.38 uri => \$self->{href},
5648 wakaba 1.12 level => $self->{must_level},
5649     token => $t);
5650     return ($t, undef);
5651     } else {
5652     $prop_value{'list-style-image'} = ['KEYWORD', 'none'];
5653     }
5654     } else {
5655     $prop_value{'list-style-type'} = ['KEYWORD', 'none'];
5656     $prop_value{'list-style-image'} = ['KEYWORD', 'none']
5657     unless exists $prop_value{'list-style-image'};
5658     }
5659     } elsif ($none == 2) {
5660     if (exists $prop_value{'list-style-type'}) {
5661 wakaba 1.39 $onerror->(type => "duplication:'list-style-type'",
5662 wakaba 1.38 uri => \$self->{href},
5663 wakaba 1.12 level => $self->{must_level},
5664     token => $t);
5665     return ($t, undef);
5666     }
5667     if (exists $prop_value{'list-style-image'}) {
5668 wakaba 1.39 $onerror->(type => "duplication:'list-style-image'",
5669 wakaba 1.38 uri => \$self->{href},
5670 wakaba 1.12 level => $self->{must_level},
5671     token => $t);
5672     return ($t, undef);
5673     }
5674    
5675     $prop_value{'list-style-type'} = ['KEYWORD', 'none'];
5676     $prop_value{'list-style-image'} = ['KEYWORD', 'none'];
5677     } elsif ($none == 3) {
5678 wakaba 1.39 $onerror->(type => "duplication:'list-style-type'",
5679 wakaba 1.38 uri => \$self->{href},
5680 wakaba 1.12 level => $self->{must_level},
5681     token => $t);
5682     return ($t, undef);
5683     }
5684    
5685     for (qw/list-style-type list-style-position list-style-image/) {
5686     $prop_value{$_} = $Prop->{$_}->{initial} unless exists $prop_value{$_};
5687     }
5688    
5689     return ($t, \%prop_value);
5690     },
5691 wakaba 1.43 ## NOTE: We don't merge longhands in |css_text| serialization,
5692     ## since no browser does.
5693     serialize_shorthand => sub {
5694     my $self = shift;
5695    
5696     ## NOTE: Don't omit any value even if it is the initial value,
5697     ## since WinIE is buggy.
5698 wakaba 1.12
5699 wakaba 1.43 my $type = $self->list_style_type;
5700     return {} unless length $type;
5701     my $type_i = $self->get_property_priority ('list-style-type');
5702     my $image = $self->list_style_image;
5703     return {} unless length $image;
5704     my $image_i = $self->get_property_priority ('list-style-image');
5705     return {} unless $type_i eq $image_i;
5706     my $position = $self->list_style_position;
5707     return {} unless length $position;
5708     my $position_i = $self->get_property_priority ('list-style-position');
5709     return {} unless $type_i eq $position_i;
5710    
5711 wakaba 1.45 return {'list-style' => [$type . ' ' . $image . ' ' . $position, $type_i]};
5712 wakaba 1.12 },
5713     };
5714     $Attr->{list_style} = $Prop->{'list-style'};
5715    
5716 wakaba 1.16 ## NOTE: Future version of the implementation will change the way to
5717     ## store the parsed value to support CSS 3 properties.
5718     $Prop->{'text-decoration'} = {
5719     css => 'text-decoration',
5720     dom => 'text_decoration',
5721     key => 'text_decoration',
5722     parse => sub {
5723     my ($self, $prop_name, $tt, $t, $onerror) = @_;
5724    
5725     my $value = ['DECORATION']; # , underline, overline, line-through, blink
5726    
5727     if ($t->{type} == IDENT_TOKEN) {
5728     my $v = lc $t->{value}; ## TODO: case
5729     $t = $tt->get_next_token;
5730     if ($v eq 'inherit') {
5731     return ($t, {$prop_name => ['INHERIT']});
5732     } elsif ($v eq 'none') {
5733     return ($t, {$prop_name => $value});
5734     } elsif ($v eq 'underline' and
5735     $self->{prop_value}->{$prop_name}->{$v}) {
5736     $value->[1] = 1;
5737     } elsif ($v eq 'overline' and
5738     $self->{prop_value}->{$prop_name}->{$v}) {
5739     $value->[2] = 1;
5740     } elsif ($v eq 'line-through' and
5741     $self->{prop_value}->{$prop_name}->{$v}) {
5742     $value->[3] = 1;
5743     } elsif ($v eq 'blink' and
5744     $self->{prop_value}->{$prop_name}->{$v}) {
5745     $value->[4] = 1;
5746     } else {
5747 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
5748 wakaba 1.16 level => $self->{must_level},
5749 wakaba 1.38 uri => \$self->{href},
5750 wakaba 1.16 token => $t);
5751     return ($t, undef);
5752     }
5753     }
5754    
5755     F: {
5756     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
5757     last F unless $t->{type} == IDENT_TOKEN;
5758    
5759     my $v = lc $t->{value}; ## TODO: case
5760     $t = $tt->get_next_token;
5761     if ($v eq 'underline' and
5762     $self->{prop_value}->{$prop_name}->{$v}) {
5763     $value->[1] = 1;
5764     } elsif ($v eq 'overline' and
5765     $self->{prop_value}->{$prop_name}->{$v}) {
5766     $value->[1] = 2;
5767     } elsif ($v eq 'line-through' and
5768     $self->{prop_value}->{$prop_name}->{$v}) {
5769     $value->[1] = 3;
5770     } elsif ($v eq 'blink' and
5771     $self->{prop_value}->{$prop_name}->{$v}) {
5772     $value->[1] = 4;
5773     } else {
5774     last F;
5775     }
5776    
5777     redo F;
5778     } # F
5779    
5780     return ($t, {$prop_name => $value});
5781     },
5782     serialize => $default_serializer,
5783     initial => ["KEYWORD", "none"],
5784     #inherited => 0,
5785     compute => $compute_as_specified,
5786     };
5787     $Attr->{text_decoration} = $Prop->{'text-decoration'};
5788     $Key->{text_decoration} = $Prop->{'text-decoration'};
5789    
5790 wakaba 1.1 1;
5791 wakaba 1.50 ## $Date: 2008/01/26 14:31:32 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24