/[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.48 - (hide annotations) (download)
Sat Jan 26 11:18:40 2008 UTC (17 years, 5 months ago) by wakaba
Branch: MAIN
Changes since 1.47: +105 -56 lines
++ whatpm/t/ChangeLog	26 Jan 2008 11:18:34 -0000
	* CSS-Parser-1.t: 'background-position' was missing
	from the list of default values.

	* css-visual.dat: New test data for 'background'
	and 'background-position' are added.

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

++ whatpm/Whatpm/CSS/ChangeLog	26 Jan 2008 11:17:46 -0000
	* Parser.pm ('background' serialize_multiple, 'background-position'
	serialize_shorthand): Reimplemented taking 'inherit'
	and 'important' into account.
	('background' parse): Support for '+'.  Correct initial value
	for 'background-position-y' was not set in some cases.
	Wrong value was set to 'background-position-x' in some case.
	Did not return by some syntax errors.

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     my $sign = 1;
1990     if ($t->{type} == MINUS_TOKEN) {
1991     $t = $tt->get_next_token;
1992     $sign = -1;
1993     }
1994    
1995     if ($t->{type} == DIMENSION_TOKEN) {
1996     my $value = $t->{number} * $sign;
1997     my $unit = lc $t->{value}; ## TODO: case
1998     $t = $tt->get_next_token;
1999 wakaba 1.19 if ($length_unit->{$unit} and $value >= 0) {
2000 wakaba 1.18 return ($t, {$prop_name => ['DIMENSION', $value, $unit]});
2001     }
2002     } elsif ($t->{type} == PERCENTAGE_TOKEN) {
2003     my $value = $t->{number} * $sign;
2004     $t = $tt->get_next_token;
2005     return ($t, {$prop_name => ['PERCENTAGE', $value]}) if $value >= 0;
2006 wakaba 1.20 } elsif ($t->{type} == NUMBER_TOKEN and
2007 wakaba 1.19 ($self->{unitless_px} or $t->{number} == 0)) {
2008 wakaba 1.18 my $value = $t->{number} * $sign;
2009     $t = $tt->get_next_token;
2010     return ($t, {$prop_name => ['DIMENSION', $value, 'px']}) if $value >= 0;
2011     } elsif ($sign > 0 and $t->{type} == IDENT_TOKEN) {
2012     my $value = lc $t->{value}; ## TODO: case
2013     $t = $tt->get_next_token;
2014     if ({
2015     'xx-small' => 1, 'x-small' => 1, small => 1, medium => 1,
2016     large => 1, 'x-large' => 1, 'xx-large' => 1,
2017     '-manakai-xxx-large' => 1, # -webkit-xxx-large
2018     larger => 1, smaller => 1,
2019     }->{$value}) {
2020     return ($t, {$prop_name => ['KEYWORD', $value]});
2021     } elsif ($value eq 'inherit') {
2022     return ($t, {$prop_name => ['INHERIT']});
2023     }
2024     }
2025    
2026 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name",
2027 wakaba 1.18 level => $self->{must_level},
2028 wakaba 1.38 uri => \$self->{href},
2029 wakaba 1.18 token => $t);
2030     return ($t, undef);
2031     },
2032     serialize => $default_serializer,
2033     initial => ['KEYWORD', 'medium'],
2034     inherited => 1,
2035     compute => sub {
2036     my ($self, $element, $prop_name, $specified_value) = @_;
2037    
2038     if (defined $specified_value) {
2039     if ($specified_value->[0] eq 'DIMENSION') {
2040     my $unit = $specified_value->[2];
2041     my $value = $specified_value->[1];
2042    
2043     if ($unit eq 'em' or $unit eq 'ex') {
2044     $value *= 0.5 if $unit eq 'ex';
2045     ## TODO: Preferred way to determine the |ex| size is defined
2046     ## in CSS 2.1.
2047    
2048     my $parent_element = $element->manakai_parent_element;
2049     if (defined $parent_element) {
2050     $value *= $self->get_computed_value ($parent_element, $prop_name)
2051     ->[1];
2052     } else {
2053     $value *= $self->{font_size}->[3]; # medium
2054     }
2055     $unit = 'px';
2056     } elsif ({in => 1, cm => 1, mm => 1, pt => 1, pc => 1}->{$unit}) {
2057     ($value *= 12, $unit = 'pc') if $unit eq 'pc';
2058     ($value /= 72, $unit = 'in') if $unit eq 'pt';
2059     ($value *= 2.54, $unit = 'cm') if $unit eq 'in';
2060     ($value *= 10, $unit = 'mm') if $unit eq 'cm';
2061     ($value /= 0.26, $unit = 'px') if $unit eq 'mm';
2062     }
2063 wakaba 1.19 ## else: consistency error
2064 wakaba 1.18
2065     return ['DIMENSION', $value, $unit];
2066     } elsif ($specified_value->[0] eq 'PERCENTAGE') {
2067     my $parent_element = $element->manakai_parent_element;
2068     my $parent_cv;
2069     if (defined $parent_element) {
2070     $parent_cv = $self->get_computed_value
2071     ($parent_element, $prop_name);
2072     } else {
2073     $parent_cv = [undef, $self->{font_size}->[3]];
2074     }
2075     return ['DIMENSION', $parent_cv->[1] * $specified_value->[1] / 100,
2076     'px'];
2077     } elsif ($specified_value->[0] eq 'KEYWORD') {
2078     if ($specified_value->[1] eq 'larger') {
2079     my $parent_element = $element->manakai_parent_element;
2080     if (defined $parent_element) {
2081     my $parent_cv = $self->get_computed_value
2082     ($parent_element, $prop_name);
2083     return ['DIMENSION',
2084     $self->{get_larger_font_size}->($self, $parent_cv->[1]),
2085     'px'];
2086     } else { ## 'larger' relative to 'medium', initial of 'font-size'
2087     return ['DIMENSION', $self->{font_size}->[4], 'px'];
2088     }
2089     } elsif ($specified_value->[1] eq 'smaller') {
2090     my $parent_element = $element->manakai_parent_element;
2091     if (defined $parent_element) {
2092     my $parent_cv = $self->get_computed_value
2093     ($parent_element, $prop_name);
2094     return ['DIMENSION',
2095     $self->{get_smaller_font_size}->($self, $parent_cv->[1]),
2096     'px'];
2097     } else { ## 'smaller' relative to 'medium', initial of 'font-size'
2098     return ['DIMENSION', $self->{font_size}->[2], 'px'];
2099     }
2100     } else {
2101     return ['DIMENSION', $self->{font_size}->[{
2102     'xx-small' => 0,
2103     'x-small' => 1,
2104     small => 2,
2105     medium => 3,
2106     large => 4,
2107     'x-large' => 5,
2108     'xx-large' => 6,
2109     '-manakai-xxx-large' => 7,
2110     }->{$specified_value->[1]}], 'px'];
2111     }
2112     }
2113     }
2114    
2115     return $specified_value;
2116     },
2117     };
2118     $Attr->{font_size} = $Prop->{'font-size'};
2119     $Key->{font_size} = $Prop->{'font-size'};
2120    
2121 wakaba 1.19 my $compute_length = sub {
2122     my ($self, $element, $prop_name, $specified_value) = @_;
2123    
2124     if (defined $specified_value) {
2125     if ($specified_value->[0] eq 'DIMENSION') {
2126     my $unit = $specified_value->[2];
2127     my $value = $specified_value->[1];
2128    
2129     if ($unit eq 'em' or $unit eq 'ex') {
2130     $value *= 0.5 if $unit eq 'ex';
2131     ## TODO: Preferred way to determine the |ex| size is defined
2132     ## in CSS 2.1.
2133    
2134     $value *= $self->get_computed_value ($element, 'font-size')->[1];
2135     $unit = 'px';
2136     } elsif ({in => 1, cm => 1, mm => 1, pt => 1, pc => 1}->{$unit}) {
2137     ($value *= 12, $unit = 'pc') if $unit eq 'pc';
2138     ($value /= 72, $unit = 'in') if $unit eq 'pt';
2139     ($value *= 2.54, $unit = 'cm') if $unit eq 'in';
2140     ($value *= 10, $unit = 'mm') if $unit eq 'cm';
2141     ($value /= 0.26, $unit = 'px') if $unit eq 'mm';
2142     }
2143    
2144     return ['DIMENSION', $value, $unit];
2145     }
2146     }
2147    
2148     return $specified_value;
2149     }; # $compute_length
2150    
2151 wakaba 1.23 $Prop->{'letter-spacing'} = {
2152     css => 'letter-spacing',
2153     dom => 'letter_spacing',
2154     key => 'letter_spacing',
2155     parse => sub {
2156     my ($self, $prop_name, $tt, $t, $onerror) = @_;
2157    
2158 wakaba 1.24 ## NOTE: Used also for 'word-spacing', '-manakai-border-spacing-x',
2159     ## and '-manakai-border-spacing-y'.
2160 wakaba 1.23
2161     my $sign = 1;
2162     if ($t->{type} == MINUS_TOKEN) {
2163     $t = $tt->get_next_token;
2164     $sign = -1;
2165     }
2166     my $allow_negative = $Prop->{$prop_name}->{allow_negative};
2167    
2168     if ($t->{type} == DIMENSION_TOKEN) {
2169     my $value = $t->{number} * $sign;
2170     my $unit = lc $t->{value}; ## TODO: case
2171     $t = $tt->get_next_token;
2172 wakaba 1.24 if ($length_unit->{$unit} and ($allow_negative or $value >= 0)) {
2173 wakaba 1.23 return ($t, {$prop_name => ['DIMENSION', $value, $unit]});
2174     }
2175     } elsif ($t->{type} == NUMBER_TOKEN and
2176     ($self->{unitless_px} or $t->{number} == 0)) {
2177     my $value = $t->{number} * $sign;
2178     $t = $tt->get_next_token;
2179 wakaba 1.24 return ($t, {$prop_name => ['DIMENSION', $value, 'px']})
2180     if $allow_negative or $value >= 0;
2181 wakaba 1.23 } elsif ($sign > 0 and $t->{type} == IDENT_TOKEN) {
2182     my $value = lc $t->{value}; ## TODO: case
2183     $t = $tt->get_next_token;
2184 wakaba 1.24 if ($Prop->{$prop_name}->{keyword}->{$value}) {
2185 wakaba 1.23 return ($t, {$prop_name => ['KEYWORD', $value]});
2186     } elsif ($value eq 'inherit') {
2187     return ($t, {$prop_name => ['INHERIT']});
2188     }
2189     }
2190    
2191 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
2192 wakaba 1.23 level => $self->{must_level},
2193 wakaba 1.38 uri => \$self->{href},
2194 wakaba 1.23 token => $t);
2195     return ($t, undef);
2196     },
2197 wakaba 1.24 allow_negative => 1,
2198     keyword => {normal => 1},
2199 wakaba 1.23 serialize => $default_serializer,
2200     initial => ['KEYWORD', 'normal'],
2201     inherited => 1,
2202     compute => $compute_length,
2203     };
2204     $Attr->{letter_spacing} = $Prop->{'letter-spacing'};
2205     $Key->{letter_spacing} = $Prop->{'letter-spacing'};
2206    
2207     $Prop->{'word-spacing'} = {
2208     css => 'word-spacing',
2209     dom => 'word_spacing',
2210     key => 'word_spacing',
2211     parse => $Prop->{'letter-spacing'}->{parse},
2212 wakaba 1.24 allow_negative => 1,
2213     keyword => {normal => 1},
2214 wakaba 1.23 serialize => $default_serializer,
2215     initial => ['KEYWORD', 'normal'],
2216     inherited => 1,
2217     compute => $compute_length,
2218     };
2219     $Attr->{word_spacing} = $Prop->{'word-spacing'};
2220     $Key->{word_spacing} = $Prop->{'word-spacing'};
2221    
2222 wakaba 1.24 $Prop->{'-manakai-border-spacing-x'} = {
2223     css => '-manakai-border-spacing-x',
2224     dom => '_manakai_border_spacing_x',
2225     key => 'border_spacing_x',
2226     parse => $Prop->{'letter-spacing'}->{parse},
2227     #allow_negative => 0,
2228     #keyword => {},
2229     serialize => $default_serializer,
2230 wakaba 1.25 serialize_multiple => sub {
2231     my $self = shift;
2232    
2233     local $Error::Depth = $Error::Depth + 1;
2234     my $x = $self->_manakai_border_spacing_x;
2235     my $y = $self->_manakai_border_spacing_y;
2236     my $xi = $self->get_property_priority ('-manakai-border-spacing-x');
2237     my $yi = $self->get_property_priority ('-manakai-border-spacing-y');
2238 wakaba 1.34 if (length $x) {
2239     if (length $y) {
2240 wakaba 1.26 if ($xi eq $yi) {
2241 wakaba 1.25 if ($x eq $y) {
2242 wakaba 1.43 return {'border-spacing' => [$x, $xi]};
2243 wakaba 1.25 } else {
2244 wakaba 1.43 return {'border-spacing' => [$x . ' ' . $y, $xi]};
2245 wakaba 1.25 }
2246     } else {
2247 wakaba 1.43 return {'-manakai-border-spacing-x' => [$x, $xi],
2248     '-manakai-border-spacing-y' => [$y, $yi]};
2249 wakaba 1.25 }
2250     } else {
2251 wakaba 1.43 return {'-manakai-border-spacing-x' => [$x, $xi]};
2252 wakaba 1.25 }
2253     } else {
2254 wakaba 1.34 if (length $y) {
2255 wakaba 1.43 return {'-manakai-border-spacing-y' => [$y, $yi]};
2256 wakaba 1.25 } else {
2257     return {};
2258     }
2259     }
2260     },
2261 wakaba 1.24 initial => ['DIMENSION', 0, 'px'],
2262     inherited => 1,
2263     compute => $compute_length,
2264     };
2265     $Attr->{_manakai_border_spacing_x} = $Prop->{'-manakai-border-spacing-x'};
2266     $Key->{border_spacing_x} = $Prop->{'-manakai-border-spacing-x'};
2267    
2268     $Prop->{'-manakai-border-spacing-y'} = {
2269     css => '-manakai-border-spacing-y',
2270     dom => '_manakai_border_spacing_y',
2271     key => 'border_spacing_y',
2272     parse => $Prop->{'letter-spacing'}->{parse},
2273     #allow_negative => 0,
2274     #keyword => {},
2275     serialize => $default_serializer,
2276 wakaba 1.25 serialize_multiple => $Prop->{'-manakai-border-spacing-x'}
2277     ->{serialize_multiple},
2278 wakaba 1.24 initial => ['DIMENSION', 0, 'px'],
2279     inherited => 1,
2280     compute => $compute_length,
2281     };
2282     $Attr->{_manakai_border_spacing_y} = $Prop->{'-manakai-border-spacing-y'};
2283     $Key->{border_spacing_y} = $Prop->{'-manakai-border-spacing-y'};
2284    
2285 wakaba 1.19 $Prop->{'margin-top'} = {
2286     css => 'margin-top',
2287     dom => 'margin_top',
2288     key => 'margin_top',
2289     parse => sub {
2290     my ($self, $prop_name, $tt, $t, $onerror) = @_;
2291    
2292 wakaba 1.21 ## NOTE: Used for 'margin-top', 'margin-right', 'margin-bottom',
2293 wakaba 1.22 ## 'margin-left', 'top', 'right', 'bottom', 'left', 'padding-top',
2294     ## 'padding-right', 'padding-bottom', 'padding-left',
2295     ## 'border-top-width', 'border-right-width', 'border-bottom-width',
2296 wakaba 1.27 ## 'border-left-width', 'text-indent', 'background-position-x',
2297     ## and 'background-position-y'.
2298 wakaba 1.21
2299 wakaba 1.19 my $sign = 1;
2300 wakaba 1.41 my $has_sign;
2301 wakaba 1.19 if ($t->{type} == MINUS_TOKEN) {
2302     $t = $tt->get_next_token;
2303 wakaba 1.41 $has_sign = 1;
2304 wakaba 1.19 $sign = -1;
2305 wakaba 1.41 } elsif ($t->{type} == PLUS_TOKEN) {
2306     $t = $tt->get_next_token;
2307     $has_sign = 1;
2308 wakaba 1.19 }
2309 wakaba 1.22 my $allow_negative = $Prop->{$prop_name}->{allow_negative};
2310 wakaba 1.19
2311     if ($t->{type} == DIMENSION_TOKEN) {
2312     my $value = $t->{number} * $sign;
2313     my $unit = lc $t->{value}; ## TODO: case
2314     $t = $tt->get_next_token;
2315 wakaba 1.22 if ($length_unit->{$unit} and ($allow_negative or $value >= 0)) {
2316 wakaba 1.19 return ($t, {$prop_name => ['DIMENSION', $value, $unit]});
2317     }
2318     } elsif ($t->{type} == PERCENTAGE_TOKEN) {
2319     my $value = $t->{number} * $sign;
2320     $t = $tt->get_next_token;
2321 wakaba 1.22 return ($t, {$prop_name => ['PERCENTAGE', $value]})
2322     if $allow_negative or $value >= 0;
2323 wakaba 1.20 } elsif ($t->{type} == NUMBER_TOKEN and
2324 wakaba 1.19 ($self->{unitless_px} or $t->{number} == 0)) {
2325     my $value = $t->{number} * $sign;
2326     $t = $tt->get_next_token;
2327 wakaba 1.22 return ($t, {$prop_name => ['DIMENSION', $value, 'px']})
2328     if $allow_negative or $value >= 0;
2329 wakaba 1.41 } elsif (not $has_sign and $t->{type} == IDENT_TOKEN) {
2330 wakaba 1.19 my $value = lc $t->{value}; ## TODO: case
2331 wakaba 1.22 if ($Prop->{$prop_name}->{keyword}->{$value}) {
2332 wakaba 1.29 $t = $tt->get_next_token;
2333 wakaba 1.19 return ($t, {$prop_name => ['KEYWORD', $value]});
2334     } elsif ($value eq 'inherit') {
2335 wakaba 1.29 $t = $tt->get_next_token;
2336 wakaba 1.19 return ($t, {$prop_name => ['INHERIT']});
2337     }
2338 wakaba 1.29 ## NOTE: In the "else" case, don't procede the |$t| pointer
2339     ## for the support of 'border-top' property (and similar ones).
2340 wakaba 1.19 }
2341    
2342 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
2343 wakaba 1.19 level => $self->{must_level},
2344 wakaba 1.38 uri => \$self->{href},
2345 wakaba 1.19 token => $t);
2346     return ($t, undef);
2347     },
2348 wakaba 1.22 allow_negative => 1,
2349     keyword => {auto => 1},
2350 wakaba 1.19 serialize => $default_serializer,
2351 wakaba 1.42 serialize_multiple => sub {
2352     my $self = shift;
2353    
2354 wakaba 1.43 ## NOTE: Same as |serialize_multiple| of 'padding-top'.
2355    
2356 wakaba 1.42 my $use_shorthand = 1;
2357     my $t = $self->margin_top;
2358     undef $use_shorthand unless length $t;
2359     my $t_i = $self->get_property_priority ('margin-top');
2360     my $r = $self->margin_right;
2361     undef $use_shorthand
2362     if not length $r or
2363     ($r eq 'inherit' and $t ne 'inherit') or
2364     ($t eq 'inherit' and $r ne 'inherit');
2365     my $r_i = $self->get_property_priority ('margin-right');
2366     undef $use_shorthand unless $r_i eq $t_i;
2367     my $b = $self->margin_bottom;
2368     undef $use_shorthand
2369     if not length $b or
2370     ($b eq 'inherit' and $t ne 'inherit') or
2371     ($t eq 'inherit' and $b ne 'inherit');
2372     my $b_i = $self->get_property_priority ('margin-bottom');
2373     undef $use_shorthand unless $b_i eq $t_i;
2374     my $l = $self->margin_left;
2375     undef $use_shorthand
2376     if not length $l or
2377     ($l eq 'inherit' and $t ne 'inherit') or
2378     ($t eq 'inherit' and $l ne 'inherit');
2379     my $l_i = $self->get_property_priority ('margin-left');
2380     undef $use_shorthand unless $l_i eq $t_i;
2381    
2382     if ($use_shorthand) {
2383     $b .= ' ' . $l if $r ne $l;
2384     $r .= ' ' . $b if $t ne $b;
2385     $t .= ' ' . $r if $t ne $r;
2386 wakaba 1.45 return {margin => [$t, $t_i]};
2387 wakaba 1.42 } else {
2388     my $v = {};
2389     if (length $t) {
2390 wakaba 1.45 $v->{'margin-top'} = [$t, $t_i];
2391 wakaba 1.42 }
2392     if (length $r) {
2393 wakaba 1.45 $v->{'margin-right'} = [$r, $r_i];
2394 wakaba 1.42 }
2395     if (length $b) {
2396 wakaba 1.45 $v->{'margin-bottom'} = [$b, $b_i];
2397 wakaba 1.42 }
2398     if (length $l) {
2399 wakaba 1.45 $v->{'margin-left'} = [$l, $l_i];
2400 wakaba 1.42 }
2401     return $v;
2402     }
2403     },
2404 wakaba 1.19 initial => ['DIMENSION', 0, 'px'],
2405     #inherited => 0,
2406     compute => $compute_length,
2407     };
2408     $Attr->{margin_top} = $Prop->{'margin-top'};
2409     $Key->{margin_top} = $Prop->{'margin-top'};
2410    
2411     $Prop->{'margin-bottom'} = {
2412     css => 'margin-bottom',
2413     dom => 'margin_bottom',
2414     key => 'margin_bottom',
2415     parse => $Prop->{'margin-top'}->{parse},
2416 wakaba 1.22 allow_negative => 1,
2417     keyword => {auto => 1},
2418 wakaba 1.19 serialize => $default_serializer,
2419 wakaba 1.42 serialize_multiple => $Prop->{'margin-top'}->{serialize_multiple},
2420 wakaba 1.19 initial => ['DIMENSION', 0, 'px'],
2421     #inherited => 0,
2422     compute => $compute_length,
2423     };
2424     $Attr->{margin_bottom} = $Prop->{'margin-bottom'};
2425     $Key->{margin_bottom} = $Prop->{'margin-bottom'};
2426    
2427     $Prop->{'margin-right'} = {
2428     css => 'margin-right',
2429     dom => 'margin_right',
2430     key => 'margin_right',
2431     parse => $Prop->{'margin-top'}->{parse},
2432 wakaba 1.22 allow_negative => 1,
2433     keyword => {auto => 1},
2434 wakaba 1.19 serialize => $default_serializer,
2435 wakaba 1.42 serialize_multiple => $Prop->{'margin-top'}->{serialize_multiple},
2436 wakaba 1.19 initial => ['DIMENSION', 0, 'px'],
2437     #inherited => 0,
2438     compute => $compute_length,
2439     };
2440     $Attr->{margin_right} = $Prop->{'margin-right'};
2441     $Key->{margin_right} = $Prop->{'margin-right'};
2442    
2443     $Prop->{'margin-left'} = {
2444     css => 'margin-left',
2445     dom => 'margin_left',
2446     key => 'margin_left',
2447     parse => $Prop->{'margin-top'}->{parse},
2448 wakaba 1.22 allow_negative => 1,
2449     keyword => {auto => 1},
2450 wakaba 1.19 serialize => $default_serializer,
2451 wakaba 1.42 serialize_multiple => $Prop->{'margin-top'}->{serialize_multiple},
2452 wakaba 1.19 initial => ['DIMENSION', 0, 'px'],
2453     #inherited => 0,
2454     compute => $compute_length,
2455     };
2456     $Attr->{margin_left} = $Prop->{'margin-left'};
2457     $Key->{margin_left} = $Prop->{'margin-left'};
2458    
2459 wakaba 1.21 $Prop->{top} = {
2460     css => 'top',
2461     dom => 'top',
2462     key => 'top',
2463     parse => $Prop->{'margin-top'}->{parse},
2464 wakaba 1.22 allow_negative => 1,
2465     keyword => {auto => 1},
2466 wakaba 1.21 serialize => $default_serializer,
2467     initial => ['KEYWORD', 'auto'],
2468     #inherited => 0,
2469     compute_multiple => sub {
2470     my ($self, $element, $eid, $prop_name) = @_;
2471    
2472     my $pos_value = $self->get_computed_value ($element, 'position');
2473     if (defined $pos_value and $pos_value->[0] eq 'KEYWORD') {
2474     if ($pos_value->[1] eq 'static') {
2475     $self->{computed_value}->{$eid}->{top} = ['KEYWORD', 'auto'];
2476     $self->{computed_value}->{$eid}->{bottom} = ['KEYWORD', 'auto'];
2477     return;
2478     } elsif ($pos_value->[1] eq 'relative') {
2479     my $top_specified = $self->get_specified_value_no_inherit
2480     ($element, 'top');
2481     if (defined $top_specified and
2482     ($top_specified->[0] eq 'DIMENSION' or
2483     $top_specified->[0] eq 'PERCENTAGE')) {
2484     my $tv = $self->{computed_value}->{$eid}->{top}
2485     = $compute_length->($self, $element, 'top', $top_specified);
2486     $self->{computed_value}->{$eid}->{bottom}
2487     = [$tv->[0], -$tv->[1], $tv->[2]];
2488     } else { # top: auto
2489     my $bottom_specified = $self->get_specified_value_no_inherit
2490     ($element, 'bottom');
2491     if (defined $bottom_specified and
2492     ($bottom_specified->[0] eq 'DIMENSION' or
2493     $bottom_specified->[0] eq 'PERCENTAGE')) {
2494     my $tv = $self->{computed_value}->{$eid}->{bottom}
2495     = $compute_length->($self, $element, 'bottom',
2496     $bottom_specified);
2497     $self->{computed_value}->{$eid}->{top}
2498     = [$tv->[0], -$tv->[1], $tv->[2]];
2499     } else { # bottom: auto
2500     $self->{computed_value}->{$eid}->{top} = ['DIMENSION', 0, 'px'];
2501     $self->{computed_value}->{$eid}->{bottom} = ['DIMENSION', 0, 'px'];
2502     }
2503     }
2504     return;
2505     }
2506     }
2507    
2508     my $top_specified = $self->get_specified_value_no_inherit
2509     ($element, 'top');
2510     $self->{computed_value}->{$eid}->{top}
2511     = $compute_length->($self, $element, 'top', $top_specified);
2512     my $bottom_specified = $self->get_specified_value_no_inherit
2513     ($element, 'bottom');
2514     $self->{computed_value}->{$eid}->{bottom}
2515     = $compute_length->($self, $element, 'bottom', $bottom_specified);
2516     },
2517     };
2518     $Attr->{top} = $Prop->{top};
2519     $Key->{top} = $Prop->{top};
2520    
2521     $Prop->{bottom} = {
2522     css => 'bottom',
2523     dom => 'bottom',
2524     key => 'bottom',
2525     parse => $Prop->{'margin-top'}->{parse},
2526 wakaba 1.22 allow_negative => 1,
2527     keyword => {auto => 1},
2528 wakaba 1.21 serialize => $default_serializer,
2529     initial => ['KEYWORD', 'auto'],
2530     #inherited => 0,
2531     compute_multiple => $Prop->{top}->{compute_multiple},
2532     };
2533     $Attr->{bottom} = $Prop->{bottom};
2534     $Key->{bottom} = $Prop->{bottom};
2535    
2536     $Prop->{left} = {
2537     css => 'left',
2538     dom => 'left',
2539     key => 'left',
2540     parse => $Prop->{'margin-top'}->{parse},
2541 wakaba 1.22 allow_negative => 1,
2542     keyword => {auto => 1},
2543 wakaba 1.21 serialize => $default_serializer,
2544     initial => ['KEYWORD', 'auto'],
2545     #inherited => 0,
2546     compute_multiple => sub {
2547     my ($self, $element, $eid, $prop_name) = @_;
2548    
2549     my $pos_value = $self->get_computed_value ($element, 'position');
2550     if (defined $pos_value and $pos_value->[0] eq 'KEYWORD') {
2551     if ($pos_value->[1] eq 'static') {
2552     $self->{computed_value}->{$eid}->{left} = ['KEYWORD', 'auto'];
2553     $self->{computed_value}->{$eid}->{right} = ['KEYWORD', 'auto'];
2554     return;
2555     } elsif ($pos_value->[1] eq 'relative') {
2556     my $left_specified = $self->get_specified_value_no_inherit
2557     ($element, 'left');
2558     if (defined $left_specified and
2559     ($left_specified->[0] eq 'DIMENSION' or
2560     $left_specified->[0] eq 'PERCENTAGE')) {
2561     my $right_specified = $self->get_specified_value_no_inherit
2562     ($element, 'right');
2563     if (defined $right_specified and
2564     ($right_specified->[0] eq 'DIMENSION' or
2565     $right_specified->[0] eq 'PERCENTAGE')) {
2566     my $direction = $self->get_computed_value ($element, 'direction');
2567     if (defined $direction and $direction->[0] eq 'KEYWORD' and
2568     $direction->[0] eq 'ltr') {
2569     my $tv = $self->{computed_value}->{$eid}->{left}
2570     = $compute_length->($self, $element, 'left',
2571     $left_specified);
2572     $self->{computed_value}->{$eid}->{right}
2573     = [$tv->[0], -$tv->[1], $tv->[2]];
2574     } else {
2575     my $tv = $self->{computed_value}->{$eid}->{right}
2576     = $compute_length->($self, $element, 'right',
2577     $right_specified);
2578     $self->{computed_value}->{$eid}->{left}
2579     = [$tv->[0], -$tv->[1], $tv->[2]];
2580     }
2581     } else {
2582     my $tv = $self->{computed_value}->{$eid}->{left}
2583     = $compute_length->($self, $element, 'left', $left_specified);
2584     $self->{computed_value}->{$eid}->{right}
2585     = [$tv->[0], -$tv->[1], $tv->[2]];
2586     }
2587     } else { # left: auto
2588     my $right_specified = $self->get_specified_value_no_inherit
2589     ($element, 'right');
2590     if (defined $right_specified and
2591     ($right_specified->[0] eq 'DIMENSION' or
2592     $right_specified->[0] eq 'PERCENTAGE')) {
2593     my $tv = $self->{computed_value}->{$eid}->{right}
2594     = $compute_length->($self, $element, 'right',
2595     $right_specified);
2596     $self->{computed_value}->{$eid}->{left}
2597     = [$tv->[0], -$tv->[1], $tv->[2]];
2598     } else { # right: auto
2599     $self->{computed_value}->{$eid}->{left} = ['DIMENSION', 0, 'px'];
2600     $self->{computed_value}->{$eid}->{right} = ['DIMENSION', 0, 'px'];
2601     }
2602     }
2603     return;
2604     }
2605     }
2606    
2607     my $left_specified = $self->get_specified_value_no_inherit
2608     ($element, 'left');
2609     $self->{computed_value}->{$eid}->{left}
2610     = $compute_length->($self, $element, 'left', $left_specified);
2611     my $right_specified = $self->get_specified_value_no_inherit
2612     ($element, 'right');
2613     $self->{computed_value}->{$eid}->{right}
2614     = $compute_length->($self, $element, 'right', $right_specified);
2615     },
2616     };
2617     $Attr->{left} = $Prop->{left};
2618     $Key->{left} = $Prop->{left};
2619    
2620     $Prop->{right} = {
2621     css => 'right',
2622     dom => 'right',
2623     key => 'right',
2624     parse => $Prop->{'margin-top'}->{parse},
2625     serialize => $default_serializer,
2626     initial => ['KEYWORD', 'auto'],
2627     #inherited => 0,
2628     compute_multiple => $Prop->{left}->{compute_multiple},
2629     };
2630     $Attr->{right} = $Prop->{right};
2631     $Key->{right} = $Prop->{right};
2632    
2633 wakaba 1.22 $Prop->{width} = {
2634     css => 'width',
2635     dom => 'width',
2636     key => 'width',
2637     parse => $Prop->{'margin-top'}->{parse},
2638     #allow_negative => 0,
2639     keyword => {auto => 1},
2640     serialize => $default_serializer,
2641     initial => ['KEYWORD', 'auto'],
2642     #inherited => 0,
2643     compute => $compute_length,
2644     ## NOTE: See <http://suika.fam.cx/gate/2005/sw/width> for
2645     ## browser compatibility issues.
2646     };
2647     $Attr->{width} = $Prop->{width};
2648     $Key->{width} = $Prop->{width};
2649    
2650     $Prop->{'min-width'} = {
2651     css => 'min-width',
2652     dom => 'min_width',
2653     key => 'min_width',
2654     parse => $Prop->{'margin-top'}->{parse},
2655     #allow_negative => 0,
2656     #keyword => {},
2657     serialize => $default_serializer,
2658     initial => ['DIMENSION', 0, 'px'],
2659     #inherited => 0,
2660     compute => $compute_length,
2661     };
2662     $Attr->{min_width} = $Prop->{'min-width'};
2663     $Key->{min_width} = $Prop->{'min-width'};
2664    
2665     $Prop->{'max-width'} = {
2666     css => 'max-width',
2667     dom => 'max_width',
2668     key => 'max_width',
2669     parse => $Prop->{'margin-top'}->{parse},
2670     #allow_negative => 0,
2671     keyword => {none => 1},
2672     serialize => $default_serializer,
2673     initial => ['KEYWORD', 'none'],
2674     #inherited => 0,
2675     compute => $compute_length,
2676     };
2677     $Attr->{max_width} = $Prop->{'max-width'};
2678     $Key->{max_width} = $Prop->{'max-width'};
2679    
2680     $Prop->{height} = {
2681     css => 'height',
2682     dom => 'height',
2683     key => 'height',
2684     parse => $Prop->{'margin-top'}->{parse},
2685     #allow_negative => 0,
2686     keyword => {auto => 1},
2687     serialize => $default_serializer,
2688     initial => ['KEYWORD', 'auto'],
2689     #inherited => 0,
2690     compute => $compute_length,
2691     ## NOTE: See <http://suika.fam.cx/gate/2005/sw/height> for
2692     ## browser compatibility issues.
2693     };
2694     $Attr->{height} = $Prop->{height};
2695     $Key->{height} = $Prop->{height};
2696    
2697     $Prop->{'min-height'} = {
2698     css => 'min-height',
2699     dom => 'min_height',
2700     key => 'min_height',
2701     parse => $Prop->{'margin-top'}->{parse},
2702     #allow_negative => 0,
2703     #keyword => {},
2704     serialize => $default_serializer,
2705     initial => ['DIMENSION', 0, 'px'],
2706     #inherited => 0,
2707     compute => $compute_length,
2708     };
2709     $Attr->{min_height} = $Prop->{'min-height'};
2710     $Key->{min_height} = $Prop->{'min-height'};
2711    
2712     $Prop->{'max-height'} = {
2713     css => 'max-height',
2714     dom => 'max_height',
2715     key => 'max_height',
2716     parse => $Prop->{'margin-top'}->{parse},
2717     #allow_negative => 0,
2718     keyword => {none => 1},
2719     serialize => $default_serializer,
2720     initial => ['KEYWORD', 'none'],
2721     #inherited => 0,
2722     compute => $compute_length,
2723     };
2724     $Attr->{max_height} = $Prop->{'max-height'};
2725     $Key->{max_height} = $Prop->{'max-height'};
2726    
2727     $Prop->{'line-height'} = {
2728     css => 'line-height',
2729     dom => 'line_height',
2730     key => 'line_height',
2731 wakaba 1.19 parse => sub {
2732     my ($self, $prop_name, $tt, $t, $onerror) = @_;
2733    
2734 wakaba 1.22 ## NOTE: Similar to 'margin-top', but different handling
2735     ## for unitless numbers.
2736    
2737 wakaba 1.19 my $sign = 1;
2738     if ($t->{type} == MINUS_TOKEN) {
2739     $t = $tt->get_next_token;
2740     $sign = -1;
2741     }
2742 wakaba 1.22 my $allow_negative = $Prop->{$prop_name}->{allow_negative};
2743 wakaba 1.19
2744     if ($t->{type} == DIMENSION_TOKEN) {
2745     my $value = $t->{number} * $sign;
2746     my $unit = lc $t->{value}; ## TODO: case
2747     $t = $tt->get_next_token;
2748     if ($length_unit->{$unit} and $value >= 0) {
2749     return ($t, {$prop_name => ['DIMENSION', $value, $unit]});
2750     }
2751     } elsif ($t->{type} == PERCENTAGE_TOKEN) {
2752     my $value = $t->{number} * $sign;
2753     $t = $tt->get_next_token;
2754 wakaba 1.22 return ($t, {$prop_name => ['PERCENTAGE', $value]})
2755     if $value >= 0;
2756     } elsif ($t->{type} == NUMBER_TOKEN) {
2757 wakaba 1.19 my $value = $t->{number} * $sign;
2758     $t = $tt->get_next_token;
2759 wakaba 1.22 return ($t, {$prop_name => ['NUMBER', $value]}) if $value >= 0;
2760 wakaba 1.19 } elsif ($sign > 0 and $t->{type} == IDENT_TOKEN) {
2761     my $value = lc $t->{value}; ## TODO: case
2762     $t = $tt->get_next_token;
2763 wakaba 1.22 if ($value eq 'normal') {
2764     return ($t, {$prop_name => ['KEYWORD', $value]});
2765     } elsif ($value eq 'inherit') {
2766 wakaba 1.19 return ($t, {$prop_name => ['INHERIT']});
2767     }
2768     }
2769    
2770 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
2771 wakaba 1.19 level => $self->{must_level},
2772 wakaba 1.38 uri => \$self->{href},
2773 wakaba 1.19 token => $t);
2774     return ($t, undef);
2775     },
2776     serialize => $default_serializer,
2777 wakaba 1.22 initial => ['KEYWORD', 'normal'],
2778     inherited => 1,
2779     compute => $compute_length,
2780     };
2781     $Attr->{line_height} = $Prop->{'line-height'};
2782     $Key->{line_height} = $Prop->{'line-height'};
2783    
2784     $Prop->{'vertical-align'} = {
2785     css => 'vertical-align',
2786     dom => 'vertical_align',
2787     key => 'vertical_align',
2788     parse => $Prop->{'margin-top'}->{parse},
2789     allow_negative => 1,
2790     keyword => {
2791     baseline => 1, sub => 1, super => 1, top => 1, 'text-top' => 1,
2792     middle => 1, bottom => 1, 'text-bottom' => 1,
2793     },
2794     ## NOTE: Currently, we don't support option to select subset of keywords
2795     ## supported by application (i.e.
2796     ## $parser->{prop_value}->{'line-height'->{$keyword}). Should we support
2797     ## it?
2798     serialize => $default_serializer,
2799     initial => ['KEYWORD', 'baseline'],
2800     #inherited => 0,
2801     compute => $compute_length,
2802     ## NOTE: See <http://suika.fam.cx/gate/2005/sw/vertical-align> for
2803     ## browser compatibility issues.
2804     };
2805     $Attr->{vertical_align} = $Prop->{'vertical-align'};
2806     $Key->{vertical_align} = $Prop->{'vertical-align'};
2807    
2808 wakaba 1.23 $Prop->{'text-indent'} = {
2809     css => 'text-indent',
2810     dom => 'text_indent',
2811     key => 'text_indent',
2812     parse => $Prop->{'margin-top'}->{parse},
2813     allow_negative => 1,
2814     keyword => {},
2815     serialize => $default_serializer,
2816     initial => ['DIMENSION', 0, 'px'],
2817     inherited => 1,
2818     compute => $compute_length,
2819     };
2820     $Attr->{text_indent} = $Prop->{'text-indent'};
2821     $Key->{text_indent} = $Prop->{'text-indent'};
2822    
2823 wakaba 1.27 $Prop->{'background-position-x'} = {
2824     css => 'background-position-x',
2825     dom => 'background_position_x',
2826     key => 'background_position_x',
2827     parse => $Prop->{'margin-top'}->{parse},
2828     allow_negative => 1,
2829     keyword => {left => 1, center => 1, right => 1},
2830     serialize => $default_serializer,
2831     initial => ['PERCENTAGE', 0],
2832     #inherited => 0,
2833     compute => sub {
2834     my ($self, $element, $prop_name, $specified_value) = @_;
2835    
2836     if (defined $specified_value and $specified_value->[0] eq 'KEYWORD') {
2837     my $v = {
2838     left => 0, center => 50, right => 100, top => 0, bottom => 100,
2839     }->{$specified_value->[1]};
2840     if (defined $v) {
2841     return ['PERCENTAGE', $v];
2842     } else {
2843     return $specified_value;
2844     }
2845     } else {
2846     return $compute_length->(@_);
2847     }
2848     },
2849 wakaba 1.30 serialize_multiple => $Prop->{'background-color'}->{serialize_multiple},
2850 wakaba 1.27 };
2851     $Attr->{background_position_x} = $Prop->{'background-position-x'};
2852     $Key->{background_position_x} = $Prop->{'background-position-x'};
2853    
2854     $Prop->{'background-position-y'} = {
2855     css => 'background-position-y',
2856     dom => 'background_position_y',
2857     key => 'background_position_y',
2858     parse => $Prop->{'margin-top'}->{parse},
2859     allow_negative => 1,
2860     keyword => {top => 1, center => 1, bottom => 1},
2861     serialize => $default_serializer,
2862 wakaba 1.30 serialize_multiple => $Prop->{'background-color'}->{serialize_multiple},
2863 wakaba 1.27 initial => ['PERCENTAGE', 0],
2864     #inherited => 0,
2865     compute => $Prop->{'background-position-x'}->{compute},
2866     };
2867     $Attr->{background_position_y} = $Prop->{'background-position-y'};
2868     $Key->{background_position_y} = $Prop->{'background-position-y'};
2869    
2870 wakaba 1.22 $Prop->{'padding-top'} = {
2871     css => 'padding-top',
2872     dom => 'padding_top',
2873     key => 'padding_top',
2874     parse => $Prop->{'margin-top'}->{parse},
2875     #allow_negative => 0,
2876     #keyword => {},
2877     serialize => $default_serializer,
2878 wakaba 1.43 serialize_multiple => sub {
2879     my $self = shift;
2880    
2881     ## NOTE: Same as |serialize_multiple| of 'margin-top'.
2882    
2883     my $use_shorthand = 1;
2884     my $t = $self->padding_top;
2885     undef $use_shorthand unless length $t;
2886     my $t_i = $self->get_property_priority ('padding-top');
2887     my $r = $self->padding_right;
2888     undef $use_shorthand
2889     if not length $r or
2890     ($r eq 'inherit' and $t ne 'inherit') or
2891     ($t eq 'inherit' and $r ne 'inherit');
2892     my $r_i = $self->get_property_priority ('padding-right');
2893     undef $use_shorthand unless $r_i eq $t_i;
2894     my $b = $self->padding_bottom;
2895     undef $use_shorthand
2896     if not length $b or
2897     ($b eq 'inherit' and $t ne 'inherit') or
2898     ($t eq 'inherit' and $b ne 'inherit');
2899     my $b_i = $self->get_property_priority ('padding-bottom');
2900     undef $use_shorthand unless $b_i eq $t_i;
2901     my $l = $self->padding_left;
2902     undef $use_shorthand
2903     if not length $l or
2904     ($l eq 'inherit' and $t ne 'inherit') or
2905     ($t eq 'inherit' and $l ne 'inherit');
2906     my $l_i = $self->get_property_priority ('padding-left');
2907     undef $use_shorthand unless $l_i eq $t_i;
2908    
2909     if ($use_shorthand) {
2910     $b .= ' ' . $l if $r ne $l;
2911     $r .= ' ' . $b if $t ne $b;
2912     $t .= ' ' . $r if $t ne $r;
2913 wakaba 1.45 return {padding => [$t, $t_i]};
2914 wakaba 1.43 } else {
2915     my $v = {};
2916     if (length $t) {
2917 wakaba 1.45 $v->{'padding-top'} = [$t, $t_i];
2918 wakaba 1.43 }
2919     if (length $r) {
2920 wakaba 1.45 $v->{'padding-right'} = [$r, $r_i];
2921 wakaba 1.43 }
2922     if (length $b) {
2923 wakaba 1.45 $v->{'padding-bottom'} = [$b, $b_i];
2924 wakaba 1.43 }
2925     if (length $l) {
2926 wakaba 1.45 $v->{'padding-left'} = [$l, $l_i];
2927 wakaba 1.43 }
2928     return $v;
2929     }
2930     },
2931 wakaba 1.19 initial => ['DIMENSION', 0, 'px'],
2932     #inherited => 0,
2933     compute => $compute_length,
2934     };
2935     $Attr->{padding_top} = $Prop->{'padding-top'};
2936     $Key->{padding_top} = $Prop->{'padding-top'};
2937    
2938     $Prop->{'padding-bottom'} = {
2939     css => 'padding-bottom',
2940     dom => 'padding_bottom',
2941     key => 'padding_bottom',
2942     parse => $Prop->{'padding-top'}->{parse},
2943 wakaba 1.22 #allow_negative => 0,
2944     #keyword => {},
2945 wakaba 1.19 serialize => $default_serializer,
2946 wakaba 1.43 serialize_multiple => $Prop->{'padding-top'}->{serialize_multiple},
2947 wakaba 1.19 initial => ['DIMENSION', 0, 'px'],
2948     #inherited => 0,
2949     compute => $compute_length,
2950     };
2951     $Attr->{padding_bottom} = $Prop->{'padding-bottom'};
2952     $Key->{padding_bottom} = $Prop->{'padding-bottom'};
2953    
2954     $Prop->{'padding-right'} = {
2955     css => 'padding-right',
2956     dom => 'padding_right',
2957     key => 'padding_right',
2958     parse => $Prop->{'padding-top'}->{parse},
2959 wakaba 1.22 #allow_negative => 0,
2960     #keyword => {},
2961 wakaba 1.19 serialize => $default_serializer,
2962 wakaba 1.43 serialize_multiple => $Prop->{'padding-top'}->{serialize_multiple},
2963 wakaba 1.19 initial => ['DIMENSION', 0, 'px'],
2964     #inherited => 0,
2965     compute => $compute_length,
2966     };
2967     $Attr->{padding_right} = $Prop->{'padding-right'};
2968     $Key->{padding_right} = $Prop->{'padding-right'};
2969    
2970     $Prop->{'padding-left'} = {
2971     css => 'padding-left',
2972     dom => 'padding_left',
2973     key => 'padding_left',
2974     parse => $Prop->{'padding-top'}->{parse},
2975 wakaba 1.22 #allow_negative => 0,
2976     #keyword => {},
2977 wakaba 1.19 serialize => $default_serializer,
2978 wakaba 1.43 serialize_multiple => $Prop->{'padding-top'}->{serialize_multiple},
2979 wakaba 1.19 initial => ['DIMENSION', 0, 'px'],
2980     #inherited => 0,
2981     compute => $compute_length,
2982     };
2983     $Attr->{padding_left} = $Prop->{'padding-left'};
2984     $Key->{padding_left} = $Prop->{'padding-left'};
2985    
2986 wakaba 1.20 $Prop->{'border-top-width'} = {
2987     css => 'border-top-width',
2988     dom => 'border_top_width',
2989     key => 'border_top_width',
2990 wakaba 1.22 parse => $Prop->{'margin-top'}->{parse},
2991     #allow_negative => 0,
2992     keyword => {thin => 1, medium => 1, thick => 1},
2993 wakaba 1.20 serialize => $default_serializer,
2994 wakaba 1.29 serialize_multiple => $Prop->{'border-top-color'}->{serialize_multiple},
2995 wakaba 1.20 initial => ['KEYWORD', 'medium'],
2996     #inherited => 0,
2997     compute => sub {
2998     my ($self, $element, $prop_name, $specified_value) = @_;
2999    
3000 wakaba 1.23 ## NOTE: Used for 'border-top-width', 'border-right-width',
3001     ## 'border-bottom-width', 'border-right-width', and
3002     ## 'outline-width'.
3003    
3004 wakaba 1.20 my $style_prop = $prop_name;
3005     $style_prop =~ s/width/style/;
3006     my $style = $self->get_computed_value ($element, $style_prop);
3007     if (defined $style and $style->[0] eq 'KEYWORD' and
3008     ($style->[1] eq 'none' or $style->[1] eq 'hidden')) {
3009     return ['DIMENSION', 0, 'px'];
3010     }
3011    
3012     my $value = $compute_length->(@_);
3013     if (defined $value and $value->[0] eq 'KEYWORD') {
3014     if ($value->[1] eq 'thin') {
3015     return ['DIMENSION', 1, 'px']; ## Firefox/Opera
3016     } elsif ($value->[1] eq 'medium') {
3017     return ['DIMENSION', 3, 'px']; ## Firefox/Opera
3018     } elsif ($value->[1] eq 'thick') {
3019     return ['DIMENSION', 5, 'px']; ## Firefox
3020     }
3021     }
3022     return $value;
3023     },
3024 wakaba 1.23 ## NOTE: CSS3 will allow <percentage> as an option in <border-width>.
3025     ## Opera 9 has already implemented it.
3026 wakaba 1.20 };
3027     $Attr->{border_top_width} = $Prop->{'border-top-width'};
3028     $Key->{border_top_width} = $Prop->{'border-top-width'};
3029    
3030     $Prop->{'border-right-width'} = {
3031     css => 'border-right-width',
3032     dom => 'border_right_width',
3033     key => 'border_right_width',
3034     parse => $Prop->{'border-top-width'}->{parse},
3035 wakaba 1.22 #allow_negative => 0,
3036     keyword => {thin => 1, medium => 1, thick => 1},
3037 wakaba 1.20 serialize => $default_serializer,
3038 wakaba 1.29 serialize_multiple => $Prop->{'border-top-color'}->{serialize_multiple},
3039 wakaba 1.20 initial => ['KEYWORD', 'medium'],
3040     #inherited => 0,
3041     compute => $Prop->{'border-top-width'}->{compute},
3042     };
3043     $Attr->{border_right_width} = $Prop->{'border-right-width'};
3044     $Key->{border_right_width} = $Prop->{'border-right-width'};
3045    
3046     $Prop->{'border-bottom-width'} = {
3047     css => 'border-bottom-width',
3048     dom => 'border_bottom_width',
3049     key => 'border_bottom_width',
3050     parse => $Prop->{'border-top-width'}->{parse},
3051 wakaba 1.22 #allow_negative => 0,
3052     keyword => {thin => 1, medium => 1, thick => 1},
3053 wakaba 1.20 serialize => $default_serializer,
3054 wakaba 1.29 serialize_multiple => $Prop->{'border-top-color'}->{serialize_multiple},
3055 wakaba 1.20 initial => ['KEYWORD', 'medium'],
3056     #inherited => 0,
3057     compute => $Prop->{'border-top-width'}->{compute},
3058     };
3059     $Attr->{border_bottom_width} = $Prop->{'border-bottom-width'};
3060     $Key->{border_bottom_width} = $Prop->{'border-bottom-width'};
3061    
3062     $Prop->{'border-left-width'} = {
3063     css => 'border-left-width',
3064     dom => 'border_left_width',
3065     key => 'border_left_width',
3066     parse => $Prop->{'border-top-width'}->{parse},
3067 wakaba 1.22 #allow_negative => 0,
3068     keyword => {thin => 1, medium => 1, thick => 1},
3069 wakaba 1.20 serialize => $default_serializer,
3070 wakaba 1.29 serialize_multiple => $Prop->{'border-top-color'}->{serialize_multiple},
3071 wakaba 1.20 initial => ['KEYWORD', 'medium'],
3072     #inherited => 0,
3073     compute => $Prop->{'border-top-width'}->{compute},
3074     };
3075     $Attr->{border_left_width} = $Prop->{'border-left-width'};
3076     $Key->{border_left_width} = $Prop->{'border-left-width'};
3077    
3078 wakaba 1.23 $Prop->{'outline-width'} = {
3079     css => 'outline-width',
3080     dom => 'outline_width',
3081     key => 'outline_width',
3082     parse => $Prop->{'border-top-width'}->{parse},
3083     #allow_negative => 0,
3084     keyword => {thin => 1, medium => 1, thick => 1},
3085     serialize => $default_serializer,
3086 wakaba 1.29 serialize_multiple => $Prop->{'outline-color'}->{serialize_multiple},
3087 wakaba 1.23 initial => ['KEYWORD', 'medium'],
3088     #inherited => 0,
3089     compute => $Prop->{'border-top-width'}->{compute},
3090     };
3091     $Attr->{outline_width} = $Prop->{'outline-width'};
3092     $Key->{outline_width} = $Prop->{'outline-width'};
3093    
3094 wakaba 1.15 $Prop->{'font-weight'} = {
3095     css => 'font-weight',
3096     dom => 'font_weight',
3097     key => 'font_weight',
3098     parse => sub {
3099     my ($self, $prop_name, $tt, $t, $onerror) = @_;
3100    
3101     if ($t->{type} == NUMBER_TOKEN) {
3102     ## ISSUE: See <http://suika.fam.cx/gate/2005/sw/font-weight> for
3103     ## browser compatibility issue.
3104     my $value = $t->{number};
3105     $t = $tt->get_next_token;
3106     if ($value % 100 == 0 and 100 <= $value and $value <= 900) {
3107     return ($t, {$prop_name => ['WEIGHT', $value, 0]});
3108     }
3109     } elsif ($t->{type} == IDENT_TOKEN) {
3110     my $value = lc $t->{value}; ## TODO: case
3111     $t = $tt->get_next_token;
3112     if ({
3113     normal => 1, bold => 1, bolder => 1, lighter => 1,
3114     }->{$value}) {
3115     return ($t, {$prop_name => ['KEYWORD', $value]});
3116     } elsif ($value eq 'inherit') {
3117     return ($t, {$prop_name => ['INHERIT']});
3118     }
3119     }
3120    
3121 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
3122 wakaba 1.15 level => $self->{must_level},
3123 wakaba 1.38 uri => \$self->{href},
3124 wakaba 1.15 token => $t);
3125     return ($t, undef);
3126     },
3127     serialize => $default_serializer,
3128     initial => ['KEYWORD', 'normal'],
3129     inherited => 1,
3130     compute => sub {
3131     my ($self, $element, $prop_name, $specified_value) = @_;
3132    
3133 wakaba 1.17 if (defined $specified_value and $specified_value->[0] eq 'KEYWORD') {
3134 wakaba 1.15 if ($specified_value->[1] eq 'normal') {
3135     return ['WEIGHT', 400, 0];
3136     } elsif ($specified_value->[1] eq 'bold') {
3137     return ['WEIGHT', 700, 0];
3138     } elsif ($specified_value->[1] eq 'bolder') {
3139     my $parent_element = $element->manakai_parent_element;
3140     if (defined $parent_element) {
3141     my $parent_value = $self->get_cascaded_value
3142     ($parent_element, $prop_name); ## NOTE: What Firefox does.
3143     return ['WEIGHT', $parent_value->[1], $parent_value->[2] + 1];
3144     } else {
3145     return ['WEIGHT', 400, 1];
3146     }
3147     } elsif ($specified_value->[1] eq 'lighter') {
3148     my $parent_element = $element->manakai_parent_element;
3149     if (defined $parent_element) {
3150     my $parent_value = $self->get_cascaded_value
3151     ($parent_element, $prop_name); ## NOTE: What Firefox does.
3152     return ['WEIGHT', $parent_value->[1], $parent_value->[2] - 1];
3153     } else {
3154     return ['WEIGHT', 400, 1];
3155     }
3156     }
3157 wakaba 1.17 #} elsif (defined $specified_value and $specified_value->[0] eq 'WEIGHT') {
3158 wakaba 1.15 #
3159     }
3160    
3161     return $specified_value;
3162     },
3163     };
3164     $Attr->{font_weight} = $Prop->{'font-weight'};
3165     $Key->{font_weight} = $Prop->{'font-weight'};
3166    
3167 wakaba 1.13 my $uri_or_none_parser = sub {
3168 wakaba 1.11 my ($self, $prop_name, $tt, $t, $onerror) = @_;
3169    
3170 wakaba 1.13 if ($t->{type} == URI_TOKEN) {
3171 wakaba 1.11 my $value = $t->{value};
3172     $t = $tt->get_next_token;
3173     return ($t, {$prop_name => ['URI', $value, \($self->{base_uri})]});
3174     } elsif ($t->{type} == IDENT_TOKEN) {
3175     my $value = lc $t->{value}; ## TODO: case
3176     $t = $tt->get_next_token;
3177     if ($value eq 'none') {
3178     ## NOTE: |none| is the default value and therefore it must be
3179     ## supported anyway.
3180     return ($t, {$prop_name => ["KEYWORD", 'none']});
3181     } elsif ($value eq 'inherit') {
3182     return ($t, {$prop_name => ['INHERIT']});
3183     }
3184     ## NOTE: None of Firefox2, WinIE6, and Opera9 support this case.
3185     #} elsif ($t->{type} == URI_INVALID_TOKEN) {
3186     # my $value = $t->{value};
3187     # $t = $tt->get_next_token;
3188     # if ($t->{type} == EOF_TOKEN) {
3189 wakaba 1.39 # $onerror->(type => 'uri not closed',
3190 wakaba 1.11 # level => $self->{must_level},
3191 wakaba 1.38 # uri => \$self->{href},
3192 wakaba 1.11 # token => $t);
3193     #
3194     # return ($t, {$prop_name => ['URI', $value, \($self->{base_uri})]});
3195     # }
3196     }
3197    
3198 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
3199 wakaba 1.11 level => $self->{must_level},
3200 wakaba 1.38 uri => \$self->{href},
3201 wakaba 1.11 token => $t);
3202     return ($t, undef);
3203 wakaba 1.13 }; # $uri_or_none_parser
3204    
3205 wakaba 1.14 my $compute_uri_or_none = sub {
3206 wakaba 1.11 my ($self, $element, $prop_name, $specified_value) = @_;
3207    
3208     if (defined $specified_value and
3209     $specified_value->[0] eq 'URI' and
3210     defined $specified_value->[2]) {
3211     require Message::DOM::DOMImplementation;
3212     return ['URI',
3213     Message::DOM::DOMImplementation->create_uri_reference
3214     ($specified_value->[1])
3215     ->get_absolute_reference (${$specified_value->[2]})
3216     ->get_uri_reference,
3217     $specified_value->[2]];
3218     }
3219    
3220     return $specified_value;
3221 wakaba 1.14 }; # $compute_uri_or_none
3222    
3223     $Prop->{'list-style-image'} = {
3224     css => 'list-style-image',
3225     dom => 'list_style_image',
3226     key => 'list_style_image',
3227     parse => $uri_or_none_parser,
3228     serialize => $default_serializer,
3229     initial => ['KEYWORD', 'none'],
3230     inherited => 1,
3231     compute => $compute_uri_or_none,
3232 wakaba 1.11 };
3233     $Attr->{list_style_image} = $Prop->{'list-style-image'};
3234     $Key->{list_style_image} = $Prop->{'list-style-image'};
3235    
3236 wakaba 1.15 $Prop->{'background-image'} = {
3237     css => 'background-image',
3238     dom => 'background_image',
3239     key => 'background_image',
3240     parse => $uri_or_none_parser,
3241     serialize => $default_serializer,
3242 wakaba 1.30 serialize_multiple => $Prop->{'background-color'}->{serialize_multiple},
3243 wakaba 1.15 initial => ['KEYWORD', 'none'],
3244     #inherited => 0,
3245     compute => $compute_uri_or_none,
3246     };
3247     $Attr->{background_image} = $Prop->{'background-image'};
3248     $Key->{background_image} = $Prop->{'background-image'};
3249    
3250 wakaba 1.7 my $border_style_keyword = {
3251     none => 1, hidden => 1, dotted => 1, dashed => 1, solid => 1,
3252     double => 1, groove => 1, ridge => 1, inset => 1, outset => 1,
3253     };
3254    
3255     $Prop->{'border-top-style'} = {
3256     css => 'border-top-style',
3257     dom => 'border_top_style',
3258     key => 'border_top_style',
3259     parse => $one_keyword_parser,
3260 wakaba 1.11 serialize => $default_serializer,
3261 wakaba 1.29 serialize_multiple => $Prop->{'border-top-color'}->{serialize_multiple},
3262 wakaba 1.7 keyword => $border_style_keyword,
3263 wakaba 1.9 initial => ["KEYWORD", "none"],
3264     #inherited => 0,
3265     compute => $compute_as_specified,
3266 wakaba 1.7 };
3267     $Attr->{border_top_style} = $Prop->{'border-top-style'};
3268     $Key->{border_top_style} = $Prop->{'border-top-style'};
3269    
3270     $Prop->{'border-right-style'} = {
3271     css => 'border-right-style',
3272     dom => 'border_right_style',
3273     key => 'border_right_style',
3274     parse => $one_keyword_parser,
3275 wakaba 1.11 serialize => $default_serializer,
3276 wakaba 1.29 serialize_multiple => $Prop->{'border-top-color'}->{serialize_multiple},
3277 wakaba 1.7 keyword => $border_style_keyword,
3278 wakaba 1.9 initial => ["KEYWORD", "none"],
3279     #inherited => 0,
3280     compute => $compute_as_specified,
3281 wakaba 1.7 };
3282     $Attr->{border_right_style} = $Prop->{'border-right-style'};
3283     $Key->{border_right_style} = $Prop->{'border-right-style'};
3284    
3285     $Prop->{'border-bottom-style'} = {
3286     css => 'border-bottom-style',
3287     dom => 'border_bottom_style',
3288     key => 'border_bottom_style',
3289     parse => $one_keyword_parser,
3290 wakaba 1.11 serialize => $default_serializer,
3291 wakaba 1.29 serialize_multiple => $Prop->{'border-top-color'}->{serialize_multiple},
3292 wakaba 1.7 keyword => $border_style_keyword,
3293 wakaba 1.9 initial => ["KEYWORD", "none"],
3294     #inherited => 0,
3295     compute => $compute_as_specified,
3296 wakaba 1.7 };
3297     $Attr->{border_bottom_style} = $Prop->{'border-bottom-style'};
3298     $Key->{border_bottom_style} = $Prop->{'border-bottom-style'};
3299    
3300     $Prop->{'border-left-style'} = {
3301     css => 'border-left-style',
3302     dom => 'border_left_style',
3303     key => 'border_left_style',
3304     parse => $one_keyword_parser,
3305 wakaba 1.11 serialize => $default_serializer,
3306 wakaba 1.29 serialize_multiple => $Prop->{'border-top-color'}->{serialize_multiple},
3307 wakaba 1.7 keyword => $border_style_keyword,
3308 wakaba 1.9 initial => ["KEYWORD", "none"],
3309     #inherited => 0,
3310     compute => $compute_as_specified,
3311 wakaba 1.7 };
3312     $Attr->{border_left_style} = $Prop->{'border-left-style'};
3313     $Key->{border_left_style} = $Prop->{'border-left-style'};
3314    
3315 wakaba 1.16 $Prop->{'outline-style'} = {
3316     css => 'outline-style',
3317     dom => 'outline_style',
3318     key => 'outline_style',
3319     parse => $one_keyword_parser,
3320     serialize => $default_serializer,
3321 wakaba 1.29 serialize_multiple => $Prop->{'outline-color'}->{serialize_multiple},
3322 wakaba 1.23 keyword => {%$border_style_keyword},
3323 wakaba 1.16 initial => ['KEYWORD', 'none'],
3324     #inherited => 0,
3325     compute => $compute_as_specified,
3326     };
3327     $Attr->{outline_style} = $Prop->{'outline-style'};
3328     $Key->{outline_style} = $Prop->{'outline-style'};
3329 wakaba 1.23 delete $Prop->{'outline-style'}->{keyword}->{hidden};
3330 wakaba 1.16
3331 wakaba 1.44 my $generic_font_keywords = {
3332 wakaba 1.31 serif => 1, 'sans-serif' => 1, cursive => 1,
3333     fantasy => 1, monospace => 1, '-manakai-default' => 1,
3334     '-manakai-caption' => 1, '-manakai-icon' => 1,
3335     '-manakai-menu' => 1, '-manakai-message-box' => 1,
3336     '-manakai-small-caption' => 1, '-manakai-status-bar' => 1,
3337     };
3338     ## NOTE: "All five generic font families are defined to exist in all CSS
3339     ## implementations (they need not necessarily map to five distinct actual
3340     ## fonts)." [CSS 2.1].
3341     ## NOTE: "If no font with the indicated characteristics exists on a given
3342     ## platform, the user agent should either intelligently substitute (e.g., a
3343     ## smaller version of the 'caption' font might be used for the 'small-caption'
3344     ## font), or substitute a user agent default font." [CSS 2.1].
3345    
3346 wakaba 1.15 $Prop->{'font-family'} = {
3347     css => 'font-family',
3348     dom => 'font_family',
3349     key => 'font_family',
3350     parse => sub {
3351     my ($self, $prop_name, $tt, $t, $onerror) = @_;
3352    
3353     ## NOTE: See <http://suika.fam.cx/gate/2005/sw/font-family> for
3354     ## how chaotic browsers are!
3355    
3356     my @prop_value;
3357    
3358     my $font_name = '';
3359     my $may_be_generic = 1;
3360 wakaba 1.31 my $may_be_inherit = ($prop_name ne 'font');
3361 wakaba 1.15 my $has_s = 0;
3362     F: {
3363     if ($t->{type} == IDENT_TOKEN) {
3364     undef $may_be_inherit if $has_s or length $font_name;
3365     undef $may_be_generic if $has_s or length $font_name;
3366     $font_name .= ' ' if $has_s;
3367     $font_name .= $t->{value};
3368     undef $has_s;
3369     $t = $tt->get_next_token;
3370     } elsif ($t->{type} == STRING_TOKEN) {
3371     $font_name .= ' ' if $has_s;
3372     $font_name .= $t->{value};
3373     undef $may_be_inherit;
3374     undef $may_be_generic;
3375     undef $has_s;
3376     $t = $tt->get_next_token;
3377 wakaba 1.31 } elsif ($t->{type} == COMMA_TOKEN) { ## TODO: case
3378     if ($may_be_generic and $generic_font_keywords->{lc $font_name}) {
3379 wakaba 1.15 push @prop_value, ['KEYWORD', $font_name];
3380     } elsif (not $may_be_generic or length $font_name) {
3381     push @prop_value, ["STRING", $font_name];
3382     }
3383     undef $may_be_inherit;
3384     $may_be_generic = 1;
3385     undef $has_s;
3386     $font_name = '';
3387     $t = $tt->get_next_token;
3388     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
3389     } elsif ($t->{type} == S_TOKEN) {
3390     $has_s = 1;
3391     $t = $tt->get_next_token;
3392     } else {
3393 wakaba 1.31 if ($may_be_generic and $generic_font_keywords->{lc $font_name}) {
3394     push @prop_value, ['KEYWORD', $font_name]; ## TODO: case
3395 wakaba 1.15 } elsif (not $may_be_generic or length $font_name) {
3396     push @prop_value, ['STRING', $font_name];
3397     } else {
3398 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
3399 wakaba 1.15 level => $self->{must_level},
3400 wakaba 1.38 uri => \$self->{href},
3401 wakaba 1.15 token => $t);
3402     return ($t, undef);
3403     }
3404     last F;
3405     }
3406     redo F;
3407     } # F
3408    
3409     if ($may_be_inherit and
3410     @prop_value == 1 and
3411     $prop_value[0]->[0] eq 'STRING' and
3412     lc $prop_value[0]->[1] eq 'inherit') { ## TODO: case
3413     return ($t, {$prop_name => ['INHERIT']});
3414     } else {
3415     unshift @prop_value, 'FONT';
3416     return ($t, {$prop_name => \@prop_value});
3417     }
3418     },
3419     serialize => sub {
3420     my ($self, $prop_name, $value) = @_;
3421    
3422     if ($value->[0] eq 'FONT') {
3423     return join ', ', map {
3424     if ($_->[0] eq 'STRING') {
3425     '"'.$_->[1].'"'; ## NOTE: This is what Firefox does.
3426     } elsif ($_->[0] eq 'KEYWORD') {
3427     $_->[1]; ## NOTE: This is what Firefox does.
3428     } else {
3429     ## NOTE: This should be an error.
3430     '""';
3431     }
3432     } @$value[1..$#$value];
3433     } elsif ($value->[0] eq 'INHERIT') {
3434     return 'inherit';
3435     } else {
3436 wakaba 1.34 return '';
3437 wakaba 1.15 }
3438     },
3439     initial => ['FONT', ['KEYWORD', '-manakai-default']],
3440     inherited => 1,
3441     compute => $compute_as_specified,
3442     };
3443     $Attr->{font_family} = $Prop->{'font-family'};
3444     $Key->{font_family} = $Prop->{'font-family'};
3445    
3446 wakaba 1.17 $Prop->{cursor} = {
3447     css => 'cursor',
3448     dom => 'cursor',
3449     key => 'cursor',
3450     parse => sub {
3451     my ($self, $prop_name, $tt, $t, $onerror) = @_;
3452    
3453     ## NOTE: See <http://suika.fam.cx/gate/2005/sw/cursor> for browser
3454     ## compatibility issues.
3455    
3456     my @prop_value = ('CURSOR');
3457    
3458     F: {
3459     if ($t->{type} == IDENT_TOKEN) {
3460     my $v = lc $t->{value}; ## TODO: case
3461     $t = $tt->get_next_token;
3462     if ($Prop->{$prop_name}->{keyword}->{$v}) {
3463     push @prop_value, ['KEYWORD', $v];
3464     last F;
3465     } elsif ($v eq 'inherit' and @prop_value == 1) {
3466     return ($t, {$prop_name => ['INHERIT']});
3467     } else {
3468 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
3469 wakaba 1.17 level => $self->{must_level},
3470 wakaba 1.38 uri => \$self->{href},
3471 wakaba 1.17 token => $t);
3472     return ($t, undef);
3473     }
3474     } elsif ($t->{type} == URI_TOKEN) {
3475     push @prop_value, ['URI', $t->{value}, \($self->{base_uri})];
3476     $t = $tt->get_next_token;
3477     } else {
3478 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
3479 wakaba 1.17 level => $self->{must_level},
3480 wakaba 1.38 uri => \$self->{href},
3481 wakaba 1.17 token => $t);
3482     return ($t, undef);
3483     }
3484    
3485     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
3486     if ($t->{type} == COMMA_TOKEN) {
3487     $t = $tt->get_next_token;
3488     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
3489     redo F;
3490     }
3491     } # F
3492    
3493     return ($t, {$prop_name => \@prop_value});
3494     },
3495     serialize => sub {
3496     my ($self, $prop_name, $value) = @_;
3497    
3498     if ($value->[0] eq 'CURSOR') {
3499     return join ', ', map {
3500     if ($_->[0] eq 'URI') {
3501     'url('.$_->[1].')'; ## NOTE: This is what Firefox does.
3502     } elsif ($_->[0] eq 'KEYWORD') {
3503     $_->[1];
3504     } else {
3505     ## NOTE: This should be an error.
3506     '""';
3507     }
3508     } @$value[1..$#$value];
3509     } elsif ($value->[0] eq 'INHERIT') {
3510     return 'inherit';
3511     } else {
3512 wakaba 1.34 return '';
3513 wakaba 1.17 }
3514     },
3515     keyword => {
3516     auto => 1, crosshair => 1, default => 1, pointer => 1, move => 1,
3517     'e-resize' => 1, 'ne-resize' => 1, 'nw-resize' => 1, 'n-resize' => 1,
3518     'n-resize' => 1, 'se-resize' => 1, 'sw-resize' => 1, 's-resize' => 1,
3519     'w-resize' => 1, text => 1, wait => 1, help => 1, progress => 1,
3520     },
3521     initial => ['CURSOR', ['KEYWORD', 'auto']],
3522     inherited => 1,
3523     compute => sub {
3524     my ($self, $element, $prop_name, $specified_value) = @_;
3525    
3526     if (defined $specified_value and $specified_value->[0] eq 'CURSOR') {
3527     my @new_value = ('CURSOR');
3528     for my $value (@$specified_value[1..$#$specified_value]) {
3529     if ($value->[0] eq 'URI') {
3530     if (defined $value->[2]) {
3531     require Message::DOM::DOMImplementation;
3532     push @new_value, ['URI',
3533     Message::DOM::DOMImplementation
3534     ->create_uri_reference ($value->[1])
3535     ->get_absolute_reference (${$value->[2]})
3536     ->get_uri_reference,
3537     $value->[2]];
3538     } else {
3539     push @new_value, $value;
3540     }
3541     } else {
3542     push @new_value, $value;
3543     }
3544     }
3545     return \@new_value;
3546     }
3547    
3548     return $specified_value;
3549     },
3550     };
3551     $Attr->{cursor} = $Prop->{cursor};
3552     $Key->{cursor} = $Prop->{cursor};
3553    
3554 wakaba 1.7 $Prop->{'border-style'} = {
3555     css => 'border-style',
3556     dom => 'border_style',
3557     parse => sub {
3558     my ($self, $prop_name, $tt, $t, $onerror) = @_;
3559    
3560     my %prop_value;
3561     if ($t->{type} == IDENT_TOKEN) {
3562     my $prop_value = lc $t->{value}; ## TODO: case folding
3563     $t = $tt->get_next_token;
3564     if ($border_style_keyword->{$prop_value} and
3565     $self->{prop_value}->{'border-top-style'}->{$prop_value}) {
3566     $prop_value{'border-top-style'} = ["KEYWORD", $prop_value];
3567     } elsif ($prop_value eq 'inherit') {
3568 wakaba 1.10 $prop_value{'border-top-style'} = ["INHERIT"];
3569 wakaba 1.19 $prop_value{'border-right-style'} = $prop_value{'border-top-style'};
3570     $prop_value{'border-bottom-style'} = $prop_value{'border-top-style'};
3571     $prop_value{'border-left-style'} = $prop_value{'border-right-style'};
3572     return ($t, \%prop_value);
3573 wakaba 1.7 } else {
3574 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
3575 wakaba 1.7 level => $self->{must_level},
3576 wakaba 1.38 uri => \$self->{href},
3577 wakaba 1.7 token => $t);
3578     return ($t, undef);
3579     }
3580     $prop_value{'border-right-style'} = $prop_value{'border-top-style'};
3581     $prop_value{'border-bottom-style'} = $prop_value{'border-top-style'};
3582     $prop_value{'border-left-style'} = $prop_value{'border-right-style'};
3583     } else {
3584 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
3585 wakaba 1.7 level => $self->{must_level},
3586 wakaba 1.38 uri => \$self->{href},
3587 wakaba 1.7 token => $t);
3588     return ($t, undef);
3589     }
3590    
3591     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
3592     if ($t->{type} == IDENT_TOKEN) {
3593     my $prop_value = lc $t->{value}; ## TODO: case folding
3594     $t = $tt->get_next_token;
3595 wakaba 1.19 if ($border_style_keyword->{$prop_value} and
3596 wakaba 1.7 $self->{prop_value}->{'border-right-style'}->{$prop_value}) {
3597     $prop_value{'border-right-style'} = ["KEYWORD", $prop_value];
3598     } else {
3599 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
3600 wakaba 1.7 level => $self->{must_level},
3601 wakaba 1.38 uri => \$self->{href},
3602 wakaba 1.7 token => $t);
3603     return ($t, undef);
3604     }
3605     $prop_value{'border-left-style'} = $prop_value{'border-right-style'};
3606    
3607     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
3608     if ($t->{type} == IDENT_TOKEN) {
3609     my $prop_value = lc $t->{value}; ## TODO: case folding
3610     $t = $tt->get_next_token;
3611     if ($border_style_keyword->{$prop_value} and
3612     $self->{prop_value}->{'border-bottom-style'}->{$prop_value}) {
3613     $prop_value{'border-bottom-style'} = ["KEYWORD", $prop_value];
3614     } else {
3615 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
3616 wakaba 1.7 level => $self->{must_level},
3617 wakaba 1.38 uri => \$self->{href},
3618 wakaba 1.7 token => $t);
3619     return ($t, undef);
3620     }
3621    
3622     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
3623     if ($t->{type} == IDENT_TOKEN) {
3624     my $prop_value = lc $t->{value}; ## TODO: case folding
3625     $t = $tt->get_next_token;
3626     if ($border_style_keyword->{$prop_value} and
3627     $self->{prop_value}->{'border-left-style'}->{$prop_value}) {
3628     $prop_value{'border-left-style'} = ["KEYWORD", $prop_value];
3629     } else {
3630 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
3631 wakaba 1.7 level => $self->{must_level},
3632 wakaba 1.38 uri => \$self->{href},
3633 wakaba 1.7 token => $t);
3634     return ($t, undef);
3635     }
3636     }
3637     }
3638     }
3639    
3640     return ($t, \%prop_value);
3641     },
3642 wakaba 1.43 serialize_shorthand => sub {
3643     my $self = shift;
3644    
3645 wakaba 1.7 my @v;
3646     push @v, $self->border_top_style;
3647 wakaba 1.43 my $i = $self->get_property_priority ('border-top-style');
3648 wakaba 1.45 return {} unless length $v[-1];
3649 wakaba 1.7 push @v, $self->border_right_style;
3650 wakaba 1.45 return {} unless length $v[-1];
3651     return {} unless $i eq $self->get_property_priority ('border-right-style');
3652 wakaba 1.7 push @v, $self->border_bottom_style;
3653 wakaba 1.45 return {} unless length $v[-1];
3654     return {} unless $i eq $self->get_property_priority ('border-bottom-style');
3655 wakaba 1.19 push @v, $self->border_left_style;
3656 wakaba 1.45 return {} unless length $v[-1];
3657     return {} unless $i eq $self->get_property_priority ('border-left-style');
3658 wakaba 1.43
3659     my $v = 0;
3660     for (0..3) {
3661     $v++ if $v[$_] eq 'inherit';
3662     }
3663     if ($v == 4) {
3664 wakaba 1.45 return {'border-style' => ['inherit', $i]};
3665 wakaba 1.43 } elsif ($v) {
3666     return {};
3667     }
3668 wakaba 1.7
3669     pop @v if $v[1] eq $v[3];
3670     pop @v if $v[0] eq $v[2];
3671     pop @v if $v[0] eq $v[1];
3672 wakaba 1.45 return {'border-style' => [(join ' ', @v), $i]};
3673 wakaba 1.7 },
3674 wakaba 1.29 serialize_multiple => $Prop->{'border-top-color'}->{serialize_multiple},
3675 wakaba 1.7 };
3676     $Attr->{border_style} = $Prop->{'border-style'};
3677    
3678 wakaba 1.29 $Prop->{'border-color'} = {
3679     css => 'border-color',
3680     dom => 'border_color',
3681     parse => sub {
3682     my ($self, $prop_name, $tt, $t, $onerror) = @_;
3683    
3684     my %prop_value;
3685     ($t, my $pv) = $parse_color->($self, 'border-color', $tt, $t, $onerror);
3686     if (not defined $pv) {
3687     return ($t, undef);
3688     }
3689     $prop_value{'border-top-color'} = $pv->{'border-color'};
3690     $prop_value{'border-bottom-color'} = $prop_value{'border-top-color'};
3691     $prop_value{'border-right-color'} = $prop_value{'border-top-color'};
3692     $prop_value{'border-left-color'}= $prop_value{'border-right-color'};
3693     if ($prop_value{'border-top-color'}->[0] eq 'INHERIT') {
3694     return ($t, \%prop_value);
3695     }
3696    
3697     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
3698     if ({
3699     IDENT_TOKEN, 1,
3700     HASH_TOKEN, 1, NUMBER_TOKEN, 1, DIMENSION_TOKEN, 1,
3701     FUNCTION_TOKEN, 1,
3702     }->{$t->{type}}) {
3703     ($t, $pv) = $parse_color->($self, 'border-color', $tt, $t, $onerror);
3704     if (not defined $pv) {
3705     return ($t, undef);
3706     } elsif ($pv->{'border-color'}->[0] eq 'INHERIT') {
3707 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
3708 wakaba 1.29 level => $self->{must_level},
3709 wakaba 1.38 uri => \$self->{href},
3710 wakaba 1.29 token => $t);
3711     return ($t, undef);
3712     }
3713     $prop_value{'border-right-color'} = $pv->{'border-color'};
3714     $prop_value{'border-left-color'}= $prop_value{'border-right-color'};
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-bottom-color'} = $pv->{'border-color'};
3733    
3734     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
3735     if ({
3736     IDENT_TOKEN, 1,
3737     HASH_TOKEN, 1, NUMBER_TOKEN, 1, DIMENSION_TOKEN, 1,
3738     FUNCTION_TOKEN, 1,
3739     }->{$t->{type}}) {
3740     ($t, $pv) = $parse_color->($self, 'border-color', $tt, $t, $onerror);
3741     if (not defined $pv) {
3742     return ($t, undef);
3743     } elsif ($pv->{'border-color'}->[0] eq 'INHERIT') {
3744 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
3745 wakaba 1.29 level => $self->{must_level},
3746 wakaba 1.38 uri => \$self->{href},
3747 wakaba 1.29 token => $t);
3748     return ($t, undef);
3749     }
3750     $prop_value{'border-left-color'} = $pv->{'border-color'};
3751     }
3752     }
3753     }
3754    
3755     return ($t, \%prop_value);
3756     },
3757 wakaba 1.43 serialize_shorthand => sub {
3758     my $self = shift;
3759    
3760 wakaba 1.29 my @v;
3761     push @v, $self->border_top_color;
3762 wakaba 1.43 my $i = $self->get_property_priority ('border-top-color');
3763 wakaba 1.45 return {} unless length $v[-1];
3764 wakaba 1.29 push @v, $self->border_right_color;
3765 wakaba 1.45 return {} unless length $v[-1];
3766     return {} unless $i eq $self->get_property_priority ('border-right-color');
3767 wakaba 1.29 push @v, $self->border_bottom_color;
3768 wakaba 1.45 return {} unless length $v[-1];
3769     return {} unless $i eq $self->get_property_priority ('border-bottom-color');
3770 wakaba 1.29 push @v, $self->border_left_color;
3771 wakaba 1.45 return {} unless length $v[-1];
3772     return {} unless $i eq $self->get_property_priority ('border-left-color');
3773 wakaba 1.43
3774     my $v = 0;
3775     for (0..3) {
3776     $v++ if $v[$_] eq 'inherit';
3777     }
3778     if ($v == 4) {
3779 wakaba 1.45 return {'border-color' => ['inherit', $i]};
3780 wakaba 1.43 } elsif ($v) {
3781     return {};
3782     }
3783 wakaba 1.29
3784     pop @v if $v[1] eq $v[3];
3785     pop @v if $v[0] eq $v[2];
3786     pop @v if $v[0] eq $v[1];
3787 wakaba 1.45 return {'border-color' => [(join ' ', @v), $i]};
3788 wakaba 1.29 },
3789     serialize_multiple => $Prop->{'border-top-color'}->{serialize_multiple},
3790     };
3791     $Attr->{border_color} = $Prop->{'border-color'};
3792    
3793     $Prop->{'border-top'} = {
3794     css => 'border-top',
3795     dom => 'border_top',
3796     parse => sub {
3797     my ($self, $prop_name, $tt, $t, $onerror) = @_;
3798    
3799     my %prop_value;
3800     my $pv;
3801     ## NOTE: Since $onerror is disabled for three invocations below,
3802     ## some informative warning messages (if they are added someday) will not
3803     ## be reported.
3804     ($t, $pv) = $parse_color->($self, $prop_name.'-color', $tt, $t, sub {});
3805     if (defined $pv) {
3806     if ($pv->{$prop_name.'-color'}->[0] eq 'INHERIT') {
3807     return ($t, {$prop_name.'-color' => ['INHERIT'],
3808     $prop_name.'-style' => ['INHERIT'],
3809     $prop_name.'-width' => ['INHERIT']});
3810     } else {
3811     $prop_value{$prop_name.'-color'} = $pv->{$prop_name.'-color'};
3812     }
3813     } else {
3814     ($t, $pv) = $Prop->{'border-top-width'}->{parse}
3815     ->($self, $prop_name.'-width', $tt, $t, sub {});
3816     if (defined $pv) {
3817     $prop_value{$prop_name.'-width'} = $pv->{$prop_name.'-width'};
3818     } else {
3819     ($t, $pv) = $Prop->{'border-top-style'}->{parse}
3820     ->($self, $prop_name.'-style', $tt, $t, sub {});
3821     if (defined $pv) {
3822     $prop_value{$prop_name.'-style'} = $pv->{$prop_name.'-style'};
3823     } else {
3824 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
3825 wakaba 1.29 level => $self->{must_level},
3826 wakaba 1.38 uri => \$self->{href},
3827 wakaba 1.29 token => $t);
3828     return ($t, undef);
3829     }
3830     }
3831     }
3832    
3833     for (1..2) {
3834     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
3835     if ($t->{type} == IDENT_TOKEN) {
3836     my $prop_value = lc $t->{value}; ## TODO: case
3837     if ($border_style_keyword->{$prop_value} and
3838     $self->{prop_value}->{'border-top-style'}->{$prop_value} and
3839     not defined $prop_value{$prop_name.'-style'}) {
3840     $prop_value{$prop_name.'-style'} = ['KEYWORD', $prop_value];
3841     $t = $tt->get_next_token;
3842     next;
3843     } elsif ({thin => 1, medium => 1, thick => 1}->{$prop_value} and
3844     not defined $prop_value{$prop_name.'-width'}) {
3845     $prop_value{$prop_name.'-width'} = ['KEYWORD', $prop_value];
3846     $t = $tt->get_next_token;
3847     next;
3848     }
3849     }
3850    
3851     undef $pv;
3852     ($t, $pv) = $parse_color->($self, $prop_name.'-color', $tt, $t, $onerror)
3853     if not defined $prop_value{$prop_name.'-color'} and
3854     {
3855     IDENT_TOKEN, 1,
3856     HASH_TOKEN, 1, NUMBER_TOKEN, 1, DIMENSION_TOKEN, 1,
3857     FUNCTION_TOKEN, 1,
3858     }->{$t->{type}};
3859     if (defined $pv) {
3860     if ($pv->{$prop_name.'-color'}->[0] eq 'INHERIT') {
3861 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
3862 wakaba 1.29 level => $self->{must_level},
3863 wakaba 1.38 uri => \$self->{href},
3864 wakaba 1.29 token => $t);
3865     } else {
3866     $prop_value{$prop_name.'-color'} = $pv->{$prop_name.'-color'};
3867     }
3868     } else {
3869     undef $pv;
3870     ($t, $pv) = $Prop->{'border-top-width'}->{parse}
3871     ->($self, $prop_name.'-width',
3872     $tt, $t, $onerror)
3873     if not defined $prop_value{$prop_name.'-width'} and
3874     {
3875     DIMENSION_TOKEN, 1,
3876     NUMBER_TOKEN, 1,
3877     IDENT_TOKEN, 1,
3878     MINUS_TOKEN, 1,
3879     }->{$t->{type}};
3880     if (defined $pv) {
3881     if ($pv->{$prop_name.'-width'}->[0] eq 'INHERIT') {
3882 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
3883 wakaba 1.29 level => $self->{must_level},
3884 wakaba 1.38 uri => \$self->{href},
3885 wakaba 1.29 token => $t);
3886     } else {
3887     $prop_value{$prop_name.'-width'} = $pv->{$prop_name.'-width'};
3888     }
3889     } else {
3890     last;
3891     }
3892     }
3893     }
3894    
3895     $prop_value{$prop_name.'-color'}
3896     ||= $Prop->{$prop_name.'-color'}->{initial};
3897     $prop_value{$prop_name.'-width'}
3898     ||= $Prop->{$prop_name.'-width'}->{initial};
3899     $prop_value{$prop_name.'-style'}
3900     ||= $Prop->{$prop_name.'-style'}->{initial};
3901    
3902     return ($t, \%prop_value);
3903     },
3904 wakaba 1.43 serialize_shorthand => sub {
3905     my $self = shift;
3906    
3907     my $w = $self->border_top_width;
3908     return {} unless length $w;
3909     my $i = $self->get_property_priority ('border-top-width');
3910     my $s = $self->border_top_style;
3911     return {} unless length $s;
3912     return {} unless $i eq $self->get_property_priority ('border-top-style');
3913     my $c = $self->border_top_color;
3914     return {} unless length $c;
3915     return {} unless $i eq $self->get_property_priority ('border-top-color');
3916    
3917     my $v = 0;
3918     $v++ if $w eq 'inherit';
3919     $v++ if $s eq 'inherit';
3920     $v++ if $c eq 'inherit';
3921     if ($v == 3) {
3922     return {'border-top' => ['inherit', $i]};
3923     } elsif ($v) {
3924     return {};
3925     }
3926 wakaba 1.29
3927 wakaba 1.43 return {'border-top' => [$w . ' ' . $s . ' ' . $c, $i]};
3928 wakaba 1.29 },
3929     serialize_multiple => $Prop->{'border-top-color'}->{serialize_multiple},
3930     };
3931     $Attr->{border_top} = $Prop->{'border-top'};
3932    
3933     $Prop->{'border-right'} = {
3934     css => 'border-right',
3935     dom => 'border_right',
3936     parse => $Prop->{'border-top'}->{parse},
3937 wakaba 1.43 serialize_shorthand => sub {
3938     my $self = shift;
3939    
3940     my $w = $self->border_right_width;
3941     return {} unless length $w;
3942     my $i = $self->get_property_priority ('border-right-width');
3943     my $s = $self->border_right_style;
3944     return {} unless length $s;
3945     return {} unless $i eq $self->get_property_priority ('border-right-style');
3946     my $c = $self->border_right_color;
3947     return {} unless length $c;
3948     return {} unless $i eq $self->get_property_priority ('border-right-color');
3949    
3950     my $v = 0;
3951     $v++ if $w eq 'inherit';
3952     $v++ if $s eq 'inherit';
3953     $v++ if $c eq 'inherit';
3954     if ($v == 3) {
3955     return {'border-right' => ['inherit', $i]};
3956     } elsif ($v) {
3957     return {};
3958     }
3959    
3960     return {'border-right' => [$w . ' ' . $s . ' ' . $c, $i]};
3961     },
3962 wakaba 1.29 serialize_multiple => $Prop->{'border-top-color'}->{serialize_multiple},
3963     };
3964     $Attr->{border_right} = $Prop->{'border-right'};
3965    
3966     $Prop->{'border-bottom'} = {
3967     css => 'border-bottom',
3968     dom => 'border_bottom',
3969     parse => $Prop->{'border-top'}->{parse},
3970 wakaba 1.43 serialize_shorthand => sub {
3971     my $self = shift;
3972    
3973     my $w = $self->border_bottom_width;
3974     return {} unless length $w;
3975     my $i = $self->get_property_priority ('border-bottom-width');
3976     my $s = $self->border_bottom_style;
3977     return {} unless length $s;
3978     return {} unless $i eq $self->get_property_priority ('border-bottom-style');
3979     my $c = $self->border_bottom_color;
3980     return {} unless length $c;
3981     return {} unless $i eq $self->get_property_priority ('border-bottom-color');
3982    
3983     my $v = 0;
3984     $v++ if $w eq 'inherit';
3985     $v++ if $s eq 'inherit';
3986     $v++ if $c eq 'inherit';
3987     if ($v == 3) {
3988     return {'border-bottom' => ['inherit', $i]};
3989     } elsif ($v) {
3990     return {};
3991     }
3992    
3993     return {'border-bottom' => [$w . ' ' . $s . ' ' . $c, $i]};
3994     },
3995 wakaba 1.29 serialize_multiple => $Prop->{'border-top-color'}->{serialize_multiple},
3996     };
3997     $Attr->{border_bottom} = $Prop->{'border-bottom'};
3998    
3999     $Prop->{'border-left'} = {
4000     css => 'border-left',
4001     dom => 'border_left',
4002     parse => $Prop->{'border-top'}->{parse},
4003 wakaba 1.43 serialize_shorthand => sub {
4004     my $self = shift;
4005    
4006     my $w = $self->border_left_width;
4007     return {} unless length $w;
4008     my $i = $self->get_property_priority ('border-left-width');
4009     my $s = $self->border_left_style;
4010     return {} unless length $s;
4011     return {} unless $i eq $self->get_property_priority ('border-left-style');
4012     my $c = $self->border_left_color;
4013     return {} unless length $c;
4014     return {} unless $i eq $self->get_property_priority ('border-left-color');
4015    
4016     my $v = 0;
4017     $v++ if $w eq 'inherit';
4018     $v++ if $s eq 'inherit';
4019     $v++ if $c eq 'inherit';
4020     if ($v == 3) {
4021     return {'border-left' => ['inherit', $i]};
4022     } elsif ($v) {
4023     return {};
4024     }
4025    
4026     return {'border-left' => [$w . ' ' . $s . ' ' . $c, $i]};
4027     },
4028 wakaba 1.29 serialize_multiple => $Prop->{'border-top-color'}->{serialize_multiple},
4029     };
4030     $Attr->{border_left} = $Prop->{'border-left'};
4031    
4032     $Prop->{outline} = {
4033     css => 'outline',
4034     dom => 'outline',
4035     parse => $Prop->{'border-top'}->{parse},
4036     serialize_multiple => $Prop->{'outline-color'}->{serialize_multiple},
4037     };
4038     $Attr->{outline} = $Prop->{outline};
4039    
4040     $Prop->{border} = {
4041     css => 'border',
4042     dom => 'border',
4043     parse => sub {
4044     my ($self, $prop_name, $tt, $t, $onerror) = @_;
4045     my $prop_value;
4046     ($t, $prop_value) = $Prop->{'border-top'}->{parse}
4047     ->($self, 'border-top', $tt, $t, $onerror);
4048     return ($t, undef) unless defined $prop_value;
4049    
4050     for (qw/border-right border-bottom border-left/) {
4051     $prop_value->{$_.'-color'} = $prop_value->{'border-top-color'}
4052     if defined $prop_value->{'border-top-color'};
4053     $prop_value->{$_.'-style'} = $prop_value->{'border-top-style'}
4054     if defined $prop_value->{'border-top-style'};
4055     $prop_value->{$_.'-width'} = $prop_value->{'border-top-width'}
4056     if defined $prop_value->{'border-top-width'};
4057     }
4058     return ($t, $prop_value);
4059     },
4060     serialize_multiple => $Prop->{'border-top-color'}->{serialize_multiple},
4061     };
4062     $Attr->{border} = $Prop->{border};
4063    
4064 wakaba 1.19 $Prop->{margin} = {
4065     css => 'margin',
4066     dom => 'margin',
4067     parse => sub {
4068     my ($self, $prop_name, $tt, $t, $onerror) = @_;
4069    
4070     my %prop_value;
4071    
4072     my $sign = 1;
4073 wakaba 1.42 my $has_sign;
4074 wakaba 1.19 if ($t->{type} == MINUS_TOKEN) {
4075     $t = $tt->get_next_token;
4076 wakaba 1.42 $has_sign = 1;
4077 wakaba 1.19 $sign = -1;
4078 wakaba 1.42 } elsif ($t->{type} == PLUS_TOKEN) {
4079     $t = $tt->get_next_token;
4080     $has_sign = 1;
4081 wakaba 1.19 }
4082    
4083     if ($t->{type} == DIMENSION_TOKEN) {
4084     my $value = $t->{number} * $sign;
4085     my $unit = lc $t->{value}; ## TODO: case
4086     $t = $tt->get_next_token;
4087     if ($length_unit->{$unit}) {
4088     $prop_value{'margin-top'} = ['DIMENSION', $value, $unit];
4089     } else {
4090 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4091 wakaba 1.19 level => $self->{must_level},
4092 wakaba 1.38 uri => \$self->{href},
4093 wakaba 1.19 token => $t);
4094     return ($t, undef);
4095     }
4096     } elsif ($t->{type} == PERCENTAGE_TOKEN) {
4097     my $value = $t->{number} * $sign;
4098     $t = $tt->get_next_token;
4099     $prop_value{'margin-top'} = ['PERCENTAGE', $value];
4100 wakaba 1.20 } elsif ($t->{type} == NUMBER_TOKEN and
4101 wakaba 1.19 ($self->{unitless_px} or $t->{number} == 0)) {
4102     my $value = $t->{number} * $sign;
4103     $t = $tt->get_next_token;
4104     $prop_value{'margin-top'} = ['DIMENSION', $value, 'px'];
4105 wakaba 1.42 } elsif (not $has_sign and $t->{type} == IDENT_TOKEN) {
4106 wakaba 1.19 my $prop_value = lc $t->{value}; ## TODO: case folding
4107     $t = $tt->get_next_token;
4108     if ($prop_value eq 'auto') {
4109     $prop_value{'margin-top'} = ['KEYWORD', $prop_value];
4110     } elsif ($prop_value eq 'inherit') {
4111     $prop_value{'margin-top'} = ['INHERIT'];
4112     $prop_value{'margin-right'} = $prop_value{'margin-top'};
4113     $prop_value{'margin-bottom'} = $prop_value{'margin-top'};
4114     $prop_value{'margin-left'} = $prop_value{'margin-right'};
4115     return ($t, \%prop_value);
4116     } else {
4117 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4118 wakaba 1.19 level => $self->{must_level},
4119 wakaba 1.38 uri => \$self->{href},
4120 wakaba 1.19 token => $t);
4121     return ($t, undef);
4122     }
4123     } else {
4124 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4125 wakaba 1.19 level => $self->{must_level},
4126 wakaba 1.38 uri => \$self->{href},
4127 wakaba 1.19 token => $t);
4128     return ($t, undef);
4129     }
4130     $prop_value{'margin-right'} = $prop_value{'margin-top'};
4131     $prop_value{'margin-bottom'} = $prop_value{'margin-top'};
4132     $prop_value{'margin-left'} = $prop_value{'margin-right'};
4133    
4134     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
4135 wakaba 1.42 undef $has_sign;
4136 wakaba 1.19 $sign = 1;
4137     if ($t->{type} == MINUS_TOKEN) {
4138     $t = $tt->get_next_token;
4139 wakaba 1.42 $has_sign = 1;
4140 wakaba 1.19 $sign = -1;
4141 wakaba 1.42 } elsif ($t->{type} == PLUS_TOKEN) {
4142     $t = $tt->get_next_token;
4143     $has_sign = 1;
4144 wakaba 1.19 }
4145    
4146     if ($t->{type} == DIMENSION_TOKEN) {
4147     my $value = $t->{number} * $sign;
4148     my $unit = lc $t->{value}; ## TODO: case
4149     $t = $tt->get_next_token;
4150     if ($length_unit->{$unit}) {
4151     $prop_value{'margin-right'} = ['DIMENSION', $value, $unit];
4152     } else {
4153 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4154 wakaba 1.19 level => $self->{must_level},
4155 wakaba 1.38 uri => \$self->{href},
4156 wakaba 1.19 token => $t);
4157     return ($t, undef);
4158     }
4159     } elsif ($t->{type} == PERCENTAGE_TOKEN) {
4160     my $value = $t->{number} * $sign;
4161     $t = $tt->get_next_token;
4162     $prop_value{'margin-right'} = ['PERCENTAGE', $value];
4163 wakaba 1.20 } elsif ($t->{type} == NUMBER_TOKEN and
4164 wakaba 1.19 ($self->{unitless_px} or $t->{number} == 0)) {
4165     my $value = $t->{number} * $sign;
4166     $t = $tt->get_next_token;
4167     $prop_value{'margin-right'} = ['DIMENSION', $value, 'px'];
4168 wakaba 1.42 } elsif (not $has_sign and $t->{type} == IDENT_TOKEN) {
4169 wakaba 1.19 my $prop_value = lc $t->{value}; ## TODO: case folding
4170     $t = $tt->get_next_token;
4171     if ($prop_value eq 'auto') {
4172     $prop_value{'margin-right'} = ['KEYWORD', $prop_value];
4173     } else {
4174 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4175 wakaba 1.19 level => $self->{must_level},
4176 wakaba 1.38 uri => \$self->{href},
4177 wakaba 1.19 token => $t);
4178     return ($t, undef);
4179     }
4180     } else {
4181 wakaba 1.42 if ($has_sign) {
4182 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4183 wakaba 1.24 level => $self->{must_level},
4184 wakaba 1.38 uri => \$self->{href},
4185 wakaba 1.24 token => $t);
4186     return ($t, undef);
4187     }
4188 wakaba 1.19 return ($t, \%prop_value);
4189     }
4190     $prop_value{'margin-left'} = $prop_value{'margin-right'};
4191    
4192     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
4193 wakaba 1.42 undef $has_sign;
4194 wakaba 1.19 $sign = 1;
4195     if ($t->{type} == MINUS_TOKEN) {
4196     $t = $tt->get_next_token;
4197 wakaba 1.42 $has_sign = 1;
4198 wakaba 1.19 $sign = -1;
4199 wakaba 1.42 } elsif ($t->{type} == PLUS_TOKEN) {
4200     $t = $tt->get_next_token;
4201     $has_sign = 1;
4202 wakaba 1.19 }
4203    
4204     if ($t->{type} == DIMENSION_TOKEN) {
4205     my $value = $t->{number} * $sign;
4206     my $unit = lc $t->{value}; ## TODO: case
4207     $t = $tt->get_next_token;
4208     if ($length_unit->{$unit}) {
4209     $prop_value{'margin-bottom'} = ['DIMENSION', $value, $unit];
4210     } else {
4211 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4212 wakaba 1.19 level => $self->{must_level},
4213 wakaba 1.38 uri => \$self->{href},
4214 wakaba 1.19 token => $t);
4215     return ($t, undef);
4216     }
4217     } elsif ($t->{type} == PERCENTAGE_TOKEN) {
4218     my $value = $t->{number} * $sign;
4219     $t = $tt->get_next_token;
4220     $prop_value{'margin-bottom'} = ['PERCENTAGE', $value];
4221 wakaba 1.20 } elsif ($t->{type} == NUMBER_TOKEN and
4222 wakaba 1.19 ($self->{unitless_px} or $t->{number} == 0)) {
4223     my $value = $t->{number} * $sign;
4224     $t = $tt->get_next_token;
4225     $prop_value{'margin-bottom'} = ['DIMENSION', $value, 'px'];
4226 wakaba 1.42 } elsif (not $has_sign and $t->{type} == IDENT_TOKEN) {
4227 wakaba 1.19 my $prop_value = lc $t->{value}; ## TODO: case folding
4228     $t = $tt->get_next_token;
4229     if ($prop_value eq 'auto') {
4230     $prop_value{'margin-bottom'} = ['KEYWORD', $prop_value];
4231     } else {
4232 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4233 wakaba 1.19 level => $self->{must_level},
4234 wakaba 1.38 uri => \$self->{href},
4235 wakaba 1.19 token => $t);
4236     return ($t, undef);
4237     }
4238     } else {
4239 wakaba 1.42 if ($has_sign) {
4240 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4241 wakaba 1.24 level => $self->{must_level},
4242 wakaba 1.38 uri => \$self->{href},
4243 wakaba 1.24 token => $t);
4244     return ($t, undef);
4245     }
4246 wakaba 1.19 return ($t, \%prop_value);
4247     }
4248    
4249     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
4250 wakaba 1.42 undef $has_sign;
4251 wakaba 1.19 $sign = 1;
4252     if ($t->{type} == MINUS_TOKEN) {
4253     $t = $tt->get_next_token;
4254 wakaba 1.42 $has_sign = 1;
4255 wakaba 1.19 $sign = -1;
4256 wakaba 1.42 } elsif ($t->{type} == PLUS_TOKEN) {
4257     $t = $tt->get_next_token;
4258     $has_sign = 1;
4259 wakaba 1.19 }
4260    
4261     if ($t->{type} == DIMENSION_TOKEN) {
4262     my $value = $t->{number} * $sign;
4263     my $unit = lc $t->{value}; ## TODO: case
4264     $t = $tt->get_next_token;
4265     if ($length_unit->{$unit}) {
4266     $prop_value{'margin-left'} = ['DIMENSION', $value, $unit];
4267     } else {
4268 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4269 wakaba 1.19 level => $self->{must_level},
4270 wakaba 1.38 uri => \$self->{href},
4271 wakaba 1.19 token => $t);
4272     return ($t, undef);
4273     }
4274     } elsif ($t->{type} == PERCENTAGE_TOKEN) {
4275     my $value = $t->{number} * $sign;
4276     $t = $tt->get_next_token;
4277     $prop_value{'margin-left'} = ['PERCENTAGE', $value];
4278 wakaba 1.20 } elsif ($t->{type} == NUMBER_TOKEN and
4279 wakaba 1.19 ($self->{unitless_px} or $t->{number} == 0)) {
4280     my $value = $t->{number} * $sign;
4281     $t = $tt->get_next_token;
4282     $prop_value{'margin-left'} = ['DIMENSION', $value, 'px'];
4283 wakaba 1.42 } elsif (not $has_sign and $t->{type} == IDENT_TOKEN) {
4284 wakaba 1.19 my $prop_value = lc $t->{value}; ## TODO: case folding
4285     $t = $tt->get_next_token;
4286     if ($prop_value eq 'auto') {
4287     $prop_value{'margin-left'} = ['KEYWORD', $prop_value];
4288     } else {
4289 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4290 wakaba 1.19 level => $self->{must_level},
4291 wakaba 1.38 uri => \$self->{href},
4292 wakaba 1.19 token => $t);
4293     return ($t, undef);
4294     }
4295     } else {
4296 wakaba 1.42 if ($has_sign) {
4297 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4298 wakaba 1.24 level => $self->{must_level},
4299 wakaba 1.38 uri => \$self->{href},
4300 wakaba 1.24 token => $t);
4301     return ($t, undef);
4302     }
4303 wakaba 1.19 return ($t, \%prop_value);
4304     }
4305    
4306     return ($t, \%prop_value);
4307     },
4308 wakaba 1.42 serialize_multiple => $Prop->{'margin-top'}->{serialize_multiple},
4309 wakaba 1.19 };
4310     $Attr->{margin} = $Prop->{margin};
4311    
4312     $Prop->{padding} = {
4313     css => 'padding',
4314     dom => 'padding',
4315     parse => sub {
4316     my ($self, $prop_name, $tt, $t, $onerror) = @_;
4317    
4318     my %prop_value;
4319    
4320     my $sign = 1;
4321     if ($t->{type} == MINUS_TOKEN) {
4322     $t = $tt->get_next_token;
4323     $sign = -1;
4324     }
4325    
4326     if ($t->{type} == DIMENSION_TOKEN) {
4327     my $value = $t->{number} * $sign;
4328     my $unit = lc $t->{value}; ## TODO: case
4329     $t = $tt->get_next_token;
4330     if ($length_unit->{$unit} and $value >= 0) {
4331     $prop_value{'padding-top'} = ['DIMENSION', $value, $unit];
4332     } else {
4333 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4334 wakaba 1.19 level => $self->{must_level},
4335 wakaba 1.38 uri => \$self->{href},
4336 wakaba 1.19 token => $t);
4337     return ($t, undef);
4338     }
4339     } elsif ($t->{type} == PERCENTAGE_TOKEN) {
4340     my $value = $t->{number} * $sign;
4341     $t = $tt->get_next_token;
4342     $prop_value{'padding-top'} = ['PERCENTAGE', $value];
4343     unless ($value >= 0) {
4344 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4345 wakaba 1.19 level => $self->{must_level},
4346 wakaba 1.38 uri => \$self->{href},
4347 wakaba 1.19 token => $t);
4348     return ($t, undef);
4349     }
4350 wakaba 1.20 } elsif ($t->{type} == NUMBER_TOKEN and
4351 wakaba 1.19 ($self->{unitless_px} or $t->{number} == 0)) {
4352     my $value = $t->{number} * $sign;
4353     $t = $tt->get_next_token;
4354     $prop_value{'padding-top'} = ['DIMENSION', $value, 'px'];
4355     unless ($value >= 0) {
4356 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4357 wakaba 1.19 level => $self->{must_level},
4358 wakaba 1.38 uri => \$self->{href},
4359 wakaba 1.19 token => $t);
4360     return ($t, undef);
4361     }
4362     } elsif ($sign > 0 and $t->{type} == IDENT_TOKEN) {
4363     my $prop_value = lc $t->{value}; ## TODO: case folding
4364     $t = $tt->get_next_token;
4365     if ($prop_value eq 'inherit') {
4366     $prop_value{'padding-top'} = ['INHERIT'];
4367     $prop_value{'padding-right'} = $prop_value{'padding-top'};
4368     $prop_value{'padding-bottom'} = $prop_value{'padding-top'};
4369     $prop_value{'padding-left'} = $prop_value{'padding-right'};
4370     return ($t, \%prop_value);
4371     } else {
4372 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4373 wakaba 1.19 level => $self->{must_level},
4374 wakaba 1.38 uri => \$self->{href},
4375 wakaba 1.19 token => $t);
4376     return ($t, undef);
4377     }
4378     } else {
4379 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4380 wakaba 1.19 level => $self->{must_level},
4381 wakaba 1.38 uri => \$self->{href},
4382 wakaba 1.19 token => $t);
4383     return ($t, undef);
4384     }
4385     $prop_value{'padding-right'} = $prop_value{'padding-top'};
4386     $prop_value{'padding-bottom'} = $prop_value{'padding-top'};
4387     $prop_value{'padding-left'} = $prop_value{'padding-right'};
4388    
4389     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
4390     $sign = 1;
4391     if ($t->{type} == MINUS_TOKEN) {
4392     $t = $tt->get_next_token;
4393     $sign = -1;
4394     }
4395    
4396     if ($t->{type} == DIMENSION_TOKEN) {
4397     my $value = $t->{number} * $sign;
4398     my $unit = lc $t->{value}; ## TODO: case
4399     $t = $tt->get_next_token;
4400     if ($length_unit->{$unit} and $value >= 0) {
4401     $prop_value{'padding-right'} = ['DIMENSION', $value, $unit];
4402     } else {
4403 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4404 wakaba 1.19 level => $self->{must_level},
4405 wakaba 1.38 uri => \$self->{href},
4406 wakaba 1.19 token => $t);
4407     return ($t, undef);
4408     }
4409     } elsif ($t->{type} == PERCENTAGE_TOKEN) {
4410     my $value = $t->{number} * $sign;
4411     $t = $tt->get_next_token;
4412     $prop_value{'padding-right'} = ['PERCENTAGE', $value];
4413     unless ($value >= 0) {
4414 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4415 wakaba 1.19 level => $self->{must_level},
4416 wakaba 1.38 uri => \$self->{href},
4417 wakaba 1.19 token => $t);
4418     return ($t, undef);
4419     }
4420 wakaba 1.20 } elsif ($t->{type} == NUMBER_TOKEN and
4421 wakaba 1.19 ($self->{unitless_px} or $t->{number} == 0)) {
4422     my $value = $t->{number} * $sign;
4423     $t = $tt->get_next_token;
4424     $prop_value{'padding-right'} = ['DIMENSION', $value, 'px'];
4425     unless ($value >= 0) {
4426 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4427 wakaba 1.19 level => $self->{must_level},
4428 wakaba 1.38 uri => \$self->{href},
4429 wakaba 1.19 token => $t);
4430     return ($t, undef);
4431     }
4432     } else {
4433 wakaba 1.24 if ($sign < 0) {
4434 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4435 wakaba 1.24 level => $self->{must_level},
4436 wakaba 1.38 uri => \$self->{href},
4437 wakaba 1.24 token => $t);
4438     return ($t, undef);
4439     }
4440 wakaba 1.19 return ($t, \%prop_value);
4441     }
4442     $prop_value{'padding-left'} = $prop_value{'padding-right'};
4443    
4444     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
4445     $sign = 1;
4446     if ($t->{type} == MINUS_TOKEN) {
4447     $t = $tt->get_next_token;
4448     $sign = -1;
4449     }
4450    
4451     if ($t->{type} == DIMENSION_TOKEN) {
4452     my $value = $t->{number} * $sign;
4453     my $unit = lc $t->{value}; ## TODO: case
4454     $t = $tt->get_next_token;
4455     if ($length_unit->{$unit} and $value >= 0) {
4456     $prop_value{'padding-bottom'} = ['DIMENSION', $value, $unit];
4457     } else {
4458 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4459 wakaba 1.19 level => $self->{must_level},
4460 wakaba 1.38 uri => \$self->{href},
4461 wakaba 1.19 token => $t);
4462     return ($t, undef);
4463     }
4464     } elsif ($t->{type} == PERCENTAGE_TOKEN) {
4465     my $value = $t->{number} * $sign;
4466     $t = $tt->get_next_token;
4467     $prop_value{'padding-bottom'} = ['PERCENTAGE', $value];
4468     unless ($value >= 0) {
4469 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4470 wakaba 1.19 level => $self->{must_level},
4471 wakaba 1.38 uri => \$self->{href},
4472 wakaba 1.19 token => $t);
4473     return ($t, undef);
4474     }
4475 wakaba 1.20 } elsif ($t->{type} == NUMBER_TOKEN and
4476 wakaba 1.19 ($self->{unitless_px} or $t->{number} == 0)) {
4477     my $value = $t->{number} * $sign;
4478     $t = $tt->get_next_token;
4479     $prop_value{'padding-bottom'} = ['DIMENSION', $value, 'px'];
4480     unless ($value >= 0) {
4481 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4482 wakaba 1.19 level => $self->{must_level},
4483 wakaba 1.38 uri => \$self->{href},
4484 wakaba 1.19 token => $t);
4485     return ($t, undef);
4486     }
4487     } else {
4488 wakaba 1.24 if ($sign < 0) {
4489 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4490 wakaba 1.24 level => $self->{must_level},
4491 wakaba 1.38 uri => \$self->{href},
4492 wakaba 1.24 token => $t);
4493     return ($t, undef);
4494     }
4495 wakaba 1.19 return ($t, \%prop_value);
4496     }
4497    
4498     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
4499     $sign = 1;
4500     if ($t->{type} == MINUS_TOKEN) {
4501     $t = $tt->get_next_token;
4502     $sign = -1;
4503     }
4504    
4505     if ($t->{type} == DIMENSION_TOKEN) {
4506     my $value = $t->{number} * $sign;
4507     my $unit = lc $t->{value}; ## TODO: case
4508     $t = $tt->get_next_token;
4509     if ($length_unit->{$unit} and $value >= 0) {
4510     $prop_value{'padding-left'} = ['DIMENSION', $value, $unit];
4511     } else {
4512 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4513 wakaba 1.19 level => $self->{must_level},
4514 wakaba 1.38 uri => \$self->{href},
4515 wakaba 1.19 token => $t);
4516     return ($t, undef);
4517     }
4518     } elsif ($t->{type} == PERCENTAGE_TOKEN) {
4519     my $value = $t->{number} * $sign;
4520     $t = $tt->get_next_token;
4521     $prop_value{'padding-left'} = ['PERCENTAGE', $value];
4522     unless ($value >= 0) {
4523 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4524 wakaba 1.19 level => $self->{must_level},
4525 wakaba 1.38 uri => \$self->{href},
4526 wakaba 1.19 token => $t);
4527     return ($t, undef);
4528     }
4529 wakaba 1.20 } elsif ($t->{type} == NUMBER_TOKEN and
4530 wakaba 1.19 ($self->{unitless_px} or $t->{number} == 0)) {
4531     my $value = $t->{number} * $sign;
4532     $t = $tt->get_next_token;
4533     $prop_value{'padding-left'} = ['DIMENSION', $value, 'px'];
4534     unless ($value >= 0) {
4535 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4536 wakaba 1.19 level => $self->{must_level},
4537 wakaba 1.38 uri => \$self->{href},
4538 wakaba 1.19 token => $t);
4539     return ($t, undef);
4540     }
4541     } else {
4542 wakaba 1.24 if ($sign < 0) {
4543 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4544 wakaba 1.24 level => $self->{must_level},
4545 wakaba 1.38 uri => \$self->{href},
4546 wakaba 1.24 token => $t);
4547     return ($t, undef);
4548     }
4549 wakaba 1.19 return ($t, \%prop_value);
4550     }
4551    
4552     return ($t, \%prop_value);
4553     },
4554 wakaba 1.43 serialize_multiple => $Prop->{'padding-top'}->{serialize_multiple},
4555 wakaba 1.19 };
4556     $Attr->{padding} = $Prop->{padding};
4557    
4558 wakaba 1.24 $Prop->{'border-spacing'} = {
4559     css => 'border-spacing',
4560     dom => 'border_spacing',
4561     parse => sub {
4562     my ($self, $prop_name, $tt, $t, $onerror) = @_;
4563    
4564     my %prop_value;
4565    
4566     my $sign = 1;
4567     if ($t->{type} == MINUS_TOKEN) {
4568     $t = $tt->get_next_token;
4569     $sign = -1;
4570     }
4571    
4572     if ($t->{type} == DIMENSION_TOKEN) {
4573     my $value = $t->{number} * $sign;
4574     my $unit = lc $t->{value}; ## TODO: case
4575     $t = $tt->get_next_token;
4576     if ($length_unit->{$unit} and $value >= 0) {
4577     $prop_value{'-manakai-border-spacing-x'} = ['DIMENSION', $value, $unit];
4578     } else {
4579 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4580 wakaba 1.24 level => $self->{must_level},
4581 wakaba 1.38 uri => \$self->{href},
4582 wakaba 1.24 token => $t);
4583     return ($t, undef);
4584     }
4585     } elsif ($t->{type} == NUMBER_TOKEN and
4586     ($self->{unitless_px} or $t->{number} == 0)) {
4587     my $value = $t->{number} * $sign;
4588     $t = $tt->get_next_token;
4589     $prop_value{'-manakai-border-spacing-x'} = ['DIMENSION', $value, 'px'];
4590     unless ($value >= 0) {
4591 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4592 wakaba 1.24 level => $self->{must_level},
4593 wakaba 1.38 uri => \$self->{href},
4594 wakaba 1.24 token => $t);
4595     return ($t, undef);
4596     }
4597     } elsif ($sign > 0 and $t->{type} == IDENT_TOKEN) {
4598     my $prop_value = lc $t->{value}; ## TODO: case folding
4599     $t = $tt->get_next_token;
4600     if ($prop_value eq 'inherit') {
4601     $prop_value{'-manakai-border-spacing-x'} = ['INHERIT'];
4602     $prop_value{'-manakai-border-spacing-y'}
4603     = $prop_value{'-manakai-border-spacing-x'};
4604     return ($t, \%prop_value);
4605     } else {
4606 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4607 wakaba 1.24 level => $self->{must_level},
4608 wakaba 1.38 uri => \$self->{href},
4609 wakaba 1.24 token => $t);
4610     return ($t, undef);
4611     }
4612     } else {
4613 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4614 wakaba 1.24 level => $self->{must_level},
4615 wakaba 1.38 uri => \$self->{href},
4616 wakaba 1.24 token => $t);
4617     return ($t, undef);
4618     }
4619     $prop_value{'-manakai-border-spacing-y'}
4620     = $prop_value{'-manakai-border-spacing-x'};
4621    
4622     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
4623     $sign = 1;
4624     if ($t->{type} == MINUS_TOKEN) {
4625     $t = $tt->get_next_token;
4626     $sign = -1;
4627     }
4628    
4629     if ($t->{type} == DIMENSION_TOKEN) {
4630     my $value = $t->{number} * $sign;
4631     my $unit = lc $t->{value}; ## TODO: case
4632     $t = $tt->get_next_token;
4633     if ($length_unit->{$unit} and $value >= 0) {
4634     $prop_value{'-manakai-border-spacing-y'} = ['DIMENSION', $value, $unit];
4635     } else {
4636 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4637 wakaba 1.24 level => $self->{must_level},
4638 wakaba 1.38 uri => \$self->{href},
4639 wakaba 1.24 token => $t);
4640     return ($t, undef);
4641     }
4642     } elsif ($t->{type} == NUMBER_TOKEN and
4643     ($self->{unitless_px} or $t->{number} == 0)) {
4644     my $value = $t->{number} * $sign;
4645     $t = $tt->get_next_token;
4646     $prop_value{'-manakai-border-spacing-y'} = ['DIMENSION', $value, 'px'];
4647     unless ($value >= 0) {
4648 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4649 wakaba 1.24 level => $self->{must_level},
4650 wakaba 1.38 uri => \$self->{href},
4651 wakaba 1.24 token => $t);
4652     return ($t, undef);
4653     }
4654     } else {
4655     if ($sign < 0) {
4656 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4657 wakaba 1.24 level => $self->{must_level},
4658 wakaba 1.38 uri => \$self->{href},
4659 wakaba 1.24 token => $t);
4660     return ($t, undef);
4661     }
4662     return ($t, \%prop_value);
4663     }
4664    
4665     return ($t, \%prop_value);
4666     },
4667     serialize => sub {
4668     my ($self, $prop_name, $value) = @_;
4669    
4670     local $Error::Depth = $Error::Depth + 1;
4671     my @v;
4672     push @v, $self->_manakai_border_spacing_x;
4673 wakaba 1.34 return '' unless length $v[-1];
4674 wakaba 1.24 push @v, $self->_manakai_border_spacing_y;
4675 wakaba 1.34 return '' unless length $v[-1];
4676 wakaba 1.24
4677     pop @v if $v[0] eq $v[1];
4678     return join ' ', @v;
4679     },
4680 wakaba 1.25 serialize_multiple => $Prop->{'-manakai-border-spacing-x'}
4681     ->{serialize_multiple},
4682 wakaba 1.24 };
4683     $Attr->{border_spacing} = $Prop->{'border-spacing'};
4684    
4685 wakaba 1.27 ## NOTE: See <http://suika.fam.cx/gate/2005/sw/background-position> for
4686     ## browser compatibility problems.
4687     $Prop->{'background-position'} = {
4688     css => 'background-position',
4689     dom => 'background_position',
4690     parse => sub {
4691     my ($self, $prop_name, $tt, $t, $onerror) = @_;
4692    
4693     my %prop_value;
4694    
4695     my $sign = 1;
4696     if ($t->{type} == MINUS_TOKEN) {
4697     $t = $tt->get_next_token;
4698     $sign = -1;
4699     }
4700    
4701     if ($t->{type} == DIMENSION_TOKEN) {
4702     my $value = $t->{number} * $sign;
4703     my $unit = lc $t->{value}; ## TODO: case
4704     $t = $tt->get_next_token;
4705     if ($length_unit->{$unit}) {
4706     $prop_value{'background-position-x'} = ['DIMENSION', $value, $unit];
4707     $prop_value{'background-position-y'} = ['PERCENTAGE', 50];
4708     } else {
4709 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4710 wakaba 1.27 level => $self->{must_level},
4711 wakaba 1.38 uri => \$self->{href},
4712 wakaba 1.27 token => $t);
4713     return ($t, undef);
4714     }
4715     } elsif ($t->{type} == PERCENTAGE_TOKEN) {
4716     my $value = $t->{number} * $sign;
4717     $t = $tt->get_next_token;
4718     $prop_value{'background-position-x'} = ['PERCENTAGE', $value];
4719     $prop_value{'background-position-y'} = ['PERCENTAGE', 50];
4720     } elsif ($t->{type} == NUMBER_TOKEN and
4721     ($self->{unitless_px} or $t->{number} == 0)) {
4722     my $value = $t->{number} * $sign;
4723     $t = $tt->get_next_token;
4724     $prop_value{'background-position-x'} = ['DIMENSION', $value, 'px'];
4725     $prop_value{'background-position-y'} = ['PERCENTAGE', 50];
4726     } elsif ($sign > 0 and $t->{type} == IDENT_TOKEN) {
4727     my $prop_value = lc $t->{value}; ## TODO: case folding
4728     $t = $tt->get_next_token;
4729     if ({left => 1, center => 1, right => 1}->{$prop_value}) {
4730     $prop_value{'background-position-x'} = ['KEYWORD', $prop_value];
4731     $prop_value{'background-position-y'} = ['KEYWORD', 'center'];
4732     } elsif ($prop_value eq 'top' or $prop_value eq 'bottom') {
4733     $prop_value{'background-position-y'} = ['KEYWORD', $prop_value];
4734    
4735     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
4736     if ($t->{type} == IDENT_TOKEN) {
4737     my $prop_value = lc $t->{value}; ## TODO: case folding
4738     if ({left => 1, center => 1, right => 1}->{$prop_value}) {
4739     $prop_value{'background-position-x'} = ['KEYWORD', $prop_value];
4740     $t = $tt->get_next_token;
4741     return ($t, \%prop_value);
4742     }
4743     }
4744     $prop_value{'background-position-x'} = ['KEYWORD', 'center'];
4745     return ($t, \%prop_value);
4746     } elsif ($prop_value eq 'inherit') {
4747     $prop_value{'background-position-x'} = ['INHERIT'];
4748     $prop_value{'background-position-y'} = ['INHERIT'];
4749     return ($t, \%prop_value);
4750     } else {
4751 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4752 wakaba 1.27 level => $self->{must_level},
4753 wakaba 1.38 uri => \$self->{href},
4754 wakaba 1.27 token => $t);
4755     return ($t, undef);
4756     }
4757     } else {
4758 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4759 wakaba 1.27 level => $self->{must_level},
4760 wakaba 1.38 uri => \$self->{href},
4761 wakaba 1.27 token => $t);
4762     return ($t, undef);
4763     }
4764    
4765     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
4766     $sign = 1;
4767     if ($t->{type} == MINUS_TOKEN) {
4768     $t = $tt->get_next_token;
4769     $sign = -1;
4770     }
4771    
4772     if ($t->{type} == DIMENSION_TOKEN) {
4773     my $value = $t->{number} * $sign;
4774     my $unit = lc $t->{value}; ## TODO: case
4775     $t = $tt->get_next_token;
4776     if ($length_unit->{$unit}) {
4777     $prop_value{'background-position-y'} = ['DIMENSION', $value, $unit];
4778     } else {
4779 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4780 wakaba 1.27 level => $self->{must_level},
4781 wakaba 1.38 uri => \$self->{href},
4782 wakaba 1.27 token => $t);
4783     return ($t, undef);
4784     }
4785     } elsif ($t->{type} == PERCENTAGE_TOKEN) {
4786     my $value = $t->{number} * $sign;
4787     $t = $tt->get_next_token;
4788     $prop_value{'background-position-y'} = ['PERCENTAGE', $value];
4789 wakaba 1.30 } elsif ($t->{type} == NUMBER_TOKEN and
4790     ($self->{unitless_px} or $t->{number} == 0)) {
4791 wakaba 1.27 my $value = $t->{number} * $sign;
4792     $t = $tt->get_next_token;
4793     $prop_value{'background-position-y'} = ['DIMENSION', $value, 'px'];
4794     } elsif ($t->{type} == IDENT_TOKEN) {
4795     my $value = lc $t->{value}; ## TODO: case
4796     if ({top => 1, center => 1, bottom => 1}->{$value}) {
4797     $prop_value{'background-position-y'} = ['KEYWORD', $value];
4798     $t = $tt->get_next_token;
4799     }
4800     } else {
4801     if ($sign < 0) {
4802 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4803 wakaba 1.27 level => $self->{must_level},
4804 wakaba 1.38 uri => \$self->{href},
4805 wakaba 1.27 token => $t);
4806     return ($t, undef);
4807     }
4808     return ($t, \%prop_value);
4809     }
4810    
4811     return ($t, \%prop_value);
4812     },
4813 wakaba 1.48 serialize_shorthand => sub {
4814     my $self = shift;
4815    
4816     my $r = {};
4817    
4818 wakaba 1.27 my $x = $self->background_position_x;
4819     my $y = $self->background_position_y;
4820 wakaba 1.48 my $xi = $self->get_property_priority ('background-position-x');
4821     my $yi = $self->get_property_priority ('background-position-y');
4822     if (length $x) {
4823     if (length $y) {
4824     if ($xi eq $yi) {
4825     if ($x eq 'inherit') {
4826     if ($y eq 'inherit') {
4827     $r->{'background-position'} = ['inherit', $xi];
4828     } else {
4829     $r->{'background-position-x'} = [$x, $xi];
4830     $r->{'background-position-y'} = [$y, $yi];
4831     }
4832     } elsif ($y eq 'inherit') {
4833     $r->{'background-position-x'} = [$x, $xi];
4834     $r->{'background-position-y'} = [$y, $yi];
4835     } else {
4836     $r->{'background-position'} = [$x . ' ' . $y, $xi];
4837     }
4838     } else {
4839     $r->{'background-position-x'} = [$x, $xi];
4840     $r->{'background-position-y'} = [$y, $yi];
4841     }
4842     } else {
4843     $r->{'background-position-x'} = [$x, $xi];
4844     }
4845     } else {
4846     if (length $y) {
4847     $r->{'background-position-y'} = [$y, $yi];
4848     } else {
4849     #
4850     }
4851     }
4852    
4853     return $r;
4854 wakaba 1.27 },
4855 wakaba 1.30 serialize_multiple => $Prop->{'background-color'}->{serialize_multiple},
4856 wakaba 1.27 };
4857     $Attr->{background_position} = $Prop->{'background-position'};
4858    
4859 wakaba 1.30 $Prop->{background} = {
4860     css => 'background',
4861     dom => 'background',
4862     parse => sub {
4863     my ($self, $prop_name, $tt, $t, $onerror) = @_;
4864     my %prop_value;
4865     B: for (1..5) {
4866 wakaba 1.48 my $has_sign;
4867 wakaba 1.30 my $sign = 1;
4868     if ($t->{type} == MINUS_TOKEN) {
4869     $sign = -1;
4870 wakaba 1.48 $has_sign = 1;
4871     $t = $tt->get_next_token;
4872     } elsif ($t->{type} == PLUS_TOKEN) {
4873     $has_sign = 1;
4874 wakaba 1.30 $t = $tt->get_next_token;
4875     }
4876    
4877 wakaba 1.48 if (not $has_sign and $t->{type} == IDENT_TOKEN) {
4878 wakaba 1.30 my $value = lc $t->{value}; ## TODO: case
4879     if ($Prop->{'background-repeat'}->{keyword}->{$value} and
4880     $self->{prop_value}->{'background-repeat'}->{$value} and
4881     not defined $prop_value{'background-repeat'}) {
4882     $prop_value{'background-repeat'} = ['KEYWORD', $value];
4883     $t = $tt->get_next_token;
4884     } elsif ($Prop->{'background-attachment'}->{keyword}->{$value} and
4885     $self->{prop_value}->{'background-attachment'}->{$value} and
4886     not defined $prop_value{'background-attachment'}) {
4887     $prop_value{'background-attachment'} = ['KEYWORD', $value];
4888     $t = $tt->get_next_token;
4889     } elsif ($value eq 'none' and
4890     not defined $prop_value{'background-image'}) {
4891     $prop_value{'background-image'} = ['KEYWORD', $value];
4892     $t = $tt->get_next_token;
4893     } elsif ({left => 1, center => 1, right => 1}->{$value} and
4894     not defined $prop_value{'background-position-x'}) {
4895     $prop_value{'background-position-x'} = ['KEYWORD', $value];
4896     $t = $tt->get_next_token;
4897     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
4898     my $sign = 1;
4899 wakaba 1.48 my $has_sign;
4900 wakaba 1.30 if ($t->{type} == MINUS_TOKEN) {
4901     $sign = -1;
4902 wakaba 1.48 $has_sign = 1;
4903     $t = $tt->get_next_token;
4904     } elsif ($t->{type} == PLUS_TOKEN) {
4905     $has_sign = 1;
4906 wakaba 1.30 $t = $tt->get_next_token;
4907     }
4908 wakaba 1.48 if (not $has_sign and $t->{type} == IDENT_TOKEN) {
4909 wakaba 1.30 my $value = lc $t->{value}; ## TODO: case
4910     if ({top => 1, bottom => 1, center => 1}->{$value}) {
4911     $prop_value{'background-position-y'} = ['KEYWORD', $value];
4912     $t = $tt->get_next_token;
4913     } elsif ($prop_value{'background-position-x'}->[1] eq 'center' and
4914     $value eq 'left' or $value eq 'right') {
4915     $prop_value{'background-position-y'} = ['KEYWORD', 'center'];
4916     $prop_value{'background-position-x'} = ['KEYWORD', $value];
4917     $t = $tt->get_next_token;
4918     } else {
4919     $prop_value{'background-position-y'} = ['KEYWORD', 'center'];
4920     }
4921     } elsif ($t->{type} == DIMENSION_TOKEN) {
4922     my $value = $t->{number} * $sign;
4923     my $unit = lc $t->{value}; ## TODO: case
4924     $t = $tt->get_next_token;
4925     if ($length_unit->{$unit}) {
4926     $prop_value{'background-position-y'}
4927     = ['DIMENSION', $value, $unit];
4928     } else {
4929 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4930 wakaba 1.30 level => $self->{must_level},
4931 wakaba 1.38 uri => \$self->{href},
4932 wakaba 1.30 token => $t);
4933 wakaba 1.48 return ($t, undef);
4934 wakaba 1.30 }
4935     } elsif ($t->{type} == PERCENTAGE_TOKEN) {
4936     my $value = $t->{number} * $sign;
4937     $t = $tt->get_next_token;
4938     $prop_value{'background-position-y'} = ['PERCENTAGE', $value];
4939     } elsif ($t->{type} == NUMBER_TOKEN and
4940     ($self->{unitless_px} or $t->{number} == 0)) {
4941     my $value = $t->{number} * $sign;
4942     $t = $tt->get_next_token;
4943     $prop_value{'background-position-y'} = ['DIMENSION', $value, 'px'];
4944 wakaba 1.48 } elsif ($has_sign) {
4945 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
4946 wakaba 1.30 level => $self->{must_level},
4947 wakaba 1.38 uri => \$self->{href},
4948 wakaba 1.30 token => $t);
4949 wakaba 1.48 return ($t, undef);
4950     } else {
4951     $prop_value{'background-position-y'} = ['KEYWORD', 'center'];
4952 wakaba 1.30 }
4953     } elsif (($value eq 'top' or $value eq 'bottom') and
4954     not defined $prop_value{'background-position-y'}) {
4955     $prop_value{'background-position-y'} = ['KEYWORD', $value];
4956     $t = $tt->get_next_token;
4957     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
4958     if ($t->{type} == IDENT_TOKEN and ## TODO: case
4959 wakaba 1.48 {
4960     left => 1, center => 1, right => 1,
4961     }->{my $value = lc $t->{value}}) {
4962 wakaba 1.30 $prop_value{'background-position-x'} = ['KEYWORD', $value];
4963     $t = $tt->get_next_token;
4964     } else {
4965     $prop_value{'background-position-x'} = ['KEYWORD', 'center'];
4966     }
4967     } elsif ($value eq 'inherit' and not keys %prop_value) {
4968     $prop_value{'background-color'} =
4969     $prop_value{'background-image'} =
4970     $prop_value{'background-repeat'} =
4971     $prop_value{'background-attachment'} =
4972     $prop_value{'background-position-x'} =
4973     $prop_value{'background-position-y'} = ['INHERIT'];
4974     $t = $tt->get_next_token;
4975     return ($t, \%prop_value);
4976     } elsif (not defined $prop_value{'background-color'} or
4977     not keys %prop_value) {
4978     ($t, my $pv) = $parse_color->($self, 'background', $tt, $t,
4979     $onerror);
4980     if (defined $pv) {
4981     $prop_value{'background-color'} = $pv->{background};
4982     } else {
4983     ## NOTE: An error should already be raiased.
4984     return ($t, undef);
4985     }
4986     }
4987     } elsif (($t->{type} == DIMENSION_TOKEN or
4988     $t->{type} == PERCENTAGE_TOKEN or
4989     ($t->{type} == NUMBER_TOKEN and
4990 wakaba 1.48 ($t->{unitless_px} or $t->{number} == 0))) and
4991 wakaba 1.30 not defined $prop_value{'background-position-x'}) {
4992     if ($t->{type} == DIMENSION_TOKEN) {
4993     my $value = $t->{number} * $sign;
4994     my $unit = lc $t->{value}; ## TODO: case
4995     $t = $tt->get_next_token;
4996     if ($length_unit->{$unit}) {
4997     $prop_value{'background-position-x'}
4998     = ['DIMENSION', $value, $unit];
4999     } else {
5000 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
5001 wakaba 1.30 level => $self->{must_level},
5002 wakaba 1.38 uri => \$self->{href},
5003 wakaba 1.30 token => $t);
5004 wakaba 1.48 return ($t, undef);
5005 wakaba 1.30 }
5006     } elsif ($t->{type} == PERCENTAGE_TOKEN) {
5007     my $value = $t->{number} * $sign;
5008     $t = $tt->get_next_token;
5009     $prop_value{'background-position-x'} = ['PERCENTAGE', $value];
5010     } elsif ($t->{type} == NUMBER_TOKEN and
5011     ($self->{unitless_px} or $t->{number} == 0)) {
5012     my $value = $t->{number} * $sign;
5013     $t = $tt->get_next_token;
5014     $prop_value{'background-position-x'} = ['DIMENSION', $value, 'px'];
5015     } else {
5016     ## NOTE: Should not be happened.
5017     last B;
5018     }
5019    
5020     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
5021     if ($t->{type} == MINUS_TOKEN) {
5022     $sign = -1;
5023 wakaba 1.48 $has_sign = 1;
5024     $t = $tt->get_next_token;
5025     } elsif ($t->{type} == PLUS_TOKEN) {
5026     $has_sign = 1;
5027 wakaba 1.30 $t = $tt->get_next_token;
5028     } else {
5029 wakaba 1.48 undef $has_sign;
5030 wakaba 1.30 $sign = 1;
5031     }
5032    
5033     if ($t->{type} == DIMENSION_TOKEN) {
5034     my $value = $t->{number} * $sign;
5035     my $unit = lc $t->{value}; ## TODO: case
5036     $t = $tt->get_next_token;
5037     if ($length_unit->{$unit}) {
5038     $prop_value{'background-position-y'}
5039     = ['DIMENSION', $value, $unit];
5040     } else {
5041 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
5042 wakaba 1.30 level => $self->{must_level},
5043 wakaba 1.38 uri => \$self->{href},
5044 wakaba 1.30 token => $t);
5045 wakaba 1.48 return ($t, undef);
5046 wakaba 1.30 }
5047     } elsif ($t->{type} == PERCENTAGE_TOKEN) {
5048     my $value = $t->{number} * $sign;
5049     $t = $tt->get_next_token;
5050     $prop_value{'background-position-y'} = ['PERCENTAGE', $value];
5051     } elsif ($t->{type} == NUMBER_TOKEN and
5052     ($self->{unitless_px} or $t->{number} == 0)) {
5053     my $value = $t->{number} * $sign;
5054     $t = $tt->get_next_token;
5055     $prop_value{'background-position-y'} = ['DIMENSION', $value, 'px'];
5056     } elsif ($t->{type} == IDENT_TOKEN) {
5057     my $value = lc $t->{value}; ## TODO: case
5058     if ({top => 1, center => 1, bottom => 1}->{$value}) {
5059     $prop_value{'background-position-y'} = ['KEYWORD', $value];
5060     $t = $tt->get_next_token;
5061     } else {
5062     $prop_value{'background-position-y'} = ['PERCENTAGE', 50];
5063     }
5064     } else {
5065 wakaba 1.48 $prop_value{'background-position-y'} = ['PERCENTAGE', 50];
5066     if ($has_sign) {
5067 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
5068 wakaba 1.30 level => $self->{must_level},
5069 wakaba 1.38 uri => \$self->{href},
5070 wakaba 1.30 token => $t);
5071 wakaba 1.48 return ($t, undef);
5072 wakaba 1.30 }
5073     }
5074 wakaba 1.48 } elsif (not $has_sign and
5075     $t->{type} == URI_TOKEN and
5076 wakaba 1.30 not defined $prop_value{'background-image'}) {
5077 wakaba 1.48 $prop_value{'background-image'}
5078     = ['URI', $t->{value}, \($self->{base_uri})];
5079 wakaba 1.30 $t = $tt->get_next_token;
5080     } else {
5081 wakaba 1.48 if (keys %prop_value and not $has_sign) {
5082 wakaba 1.30 last B;
5083     } else {
5084 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
5085 wakaba 1.30 level => $self->{must_level},
5086 wakaba 1.38 uri => \$self->{href},
5087 wakaba 1.30 token => $t);
5088 wakaba 1.48 return ($t, undef);
5089 wakaba 1.30 }
5090     }
5091    
5092     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
5093     } # B
5094    
5095     $prop_value{$_} ||= $Prop->{$_}->{initial}
5096     for qw/background-image background-attachment background-repeat
5097     background-color background-position-x background-position-y/;
5098    
5099     return ($t, \%prop_value);
5100     },
5101     serialize_multiple => $Prop->{'background-color'}->{serialize_multiple},
5102     };
5103     $Attr->{background} = $Prop->{background};
5104    
5105 wakaba 1.31 $Prop->{font} = {
5106     css => 'font',
5107     dom => 'font',
5108     parse => sub {
5109     my ($self, $prop_name, $tt, $t, $onerror) = @_;
5110    
5111     my %prop_value;
5112    
5113     A: for (1..3) {
5114     if ($t->{type} == IDENT_TOKEN) {
5115     my $value = lc $t->{value}; ## TODO: case
5116     if ($value eq 'normal') {
5117     $t = $tt->get_next_token;
5118     } elsif ($Prop->{'font-style'}->{keyword}->{$value} and
5119     $self->{prop_value}->{'font-style'}->{$value} and
5120     not defined $prop_value{'font-style'}) {
5121     $prop_value{'font-style'} = ['KEYWORD', $value];
5122     $t = $tt->get_next_token;
5123     } elsif ($Prop->{'font-variant'}->{keyword}->{$value} and
5124     $self->{prop_value}->{'font-variant'}->{$value} and
5125     not defined $prop_value{'font-variant'}) {
5126     $prop_value{'font-variant'} = ['KEYWORD', $value];
5127     $t = $tt->get_next_token;
5128     } elsif ({normal => 1, bold => 1,
5129     bolder => 1, lighter => 1}->{$value} and
5130     not defined $prop_value{'font-weight'}) {
5131     $prop_value{'font-weight'} = ['KEYWORD', $value];
5132     $t = $tt->get_next_token;
5133     } elsif ($value eq 'inherit' and 0 == keys %prop_value) {
5134     $t = $tt->get_next_token;
5135     return ($t, {'font-style' => ['INHERIT'],
5136     'font-variant' => ['INHERIT'],
5137     'font-weight' => ['INHERIT'],
5138     'font-size' => ['INHERIT'],
5139     'line-height' => ['INHERIT'],
5140     'font-family' => ['INHERIT']});
5141     } elsif ({
5142     caption => 1, icon => 1, menu => 1,
5143     'message-box' => 1, 'small-caption' => 1, 'status-bar' => 1,
5144     }->{$value} and 0 == keys %prop_value) {
5145     $t = $tt->get_next_token;
5146     return ($t, $self->{get_system_font}->($self, $value, {
5147     'font-style' => $Prop->{'font-style'}->{initial},
5148     'font-variant' => $Prop->{'font-variant'}->{initial},
5149     'font-weight' => $Prop->{'font-weight'}->{initial},
5150     'font-size' => $Prop->{'font-size'}->{initial},
5151     'line-height' => $Prop->{'line-height'}->{initial},
5152     'font-family' => ['FONT', ['KEYWORD', '-manakai-'.$value]],
5153     }));
5154     } else {
5155     if (keys %prop_value) {
5156     last A;
5157     } else {
5158 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
5159 wakaba 1.31 level => $self->{must_level},
5160 wakaba 1.38 uri => \$self->{href},
5161 wakaba 1.31 token => $t);
5162     return ($t, undef);
5163     }
5164     }
5165     } elsif ($t->{type} == NUMBER_TOKEN) {
5166     if ({100 => 1, 200 => 1, 300 => 1, 400 => 1, 500 => 1,
5167     600 => 1, 700 => 1, 800 => 1, 900 => 1}->{$t->{value}}) {
5168     $prop_value{'font-weight'} = ['NUMBER', $t->{value}];
5169     $t = $tt->get_next_token;
5170     } else {
5171     last A;
5172     }
5173     }
5174    
5175     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
5176     } # A
5177    
5178     for (qw/font-style font-variant font-weight/) {
5179     $prop_value{$_} = $Prop->{$_}->{initial} unless defined $prop_value{$_};
5180     }
5181    
5182     ($t, my $pv) = $Prop->{'font-size'}->{parse}
5183     ->($self, 'font', $tt, $t, $onerror);
5184     return ($t, undef) unless defined $pv;
5185     if ($pv->{font}->[0] eq 'INHERIT') {
5186 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
5187 wakaba 1.31 level => $self->{must_level},
5188 wakaba 1.38 uri => \$self->{href},
5189 wakaba 1.31 token => $t);
5190     return ($t, undef);
5191     }
5192     $prop_value{'font-size'} = $pv->{font};
5193    
5194     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
5195     if ($t->{type} == DELIM_TOKEN and $t->{value} eq '/') {
5196     $t = $tt->get_next_token;
5197     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
5198     ($t, my $pv) = $Prop->{'line-height'}->{parse}
5199     ->($self, 'font', $tt, $t, $onerror);
5200     return ($t, undef) unless defined $pv;
5201     if ($pv->{font}->[0] eq 'INHERIT') {
5202 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
5203 wakaba 1.31 level => $self->{must_level},
5204 wakaba 1.38 uri => \$self->{href},
5205 wakaba 1.31 token => $t);
5206     return ($t, undef);
5207     }
5208     $prop_value{'line-height'} = $pv->{font};
5209     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
5210     } else {
5211     $prop_value{'line-height'} = $Prop->{'line-height'}->{initial};
5212     }
5213    
5214     undef $pv;
5215     ($t, $pv) = $Prop->{'font-family'}->{parse}
5216     ->($self, 'font', $tt, $t, $onerror);
5217     return ($t, undef) unless defined $pv;
5218     $prop_value{'font-family'} = $pv->{font};
5219    
5220     return ($t, \%prop_value);
5221     },
5222 wakaba 1.44 serialize_shorthand => sub {
5223     my $self = shift;
5224 wakaba 1.31
5225     local $Error::Depth = $Error::Depth + 1;
5226     my $style = $self->font_style;
5227     my $i = $self->get_property_priority ('font-style');
5228 wakaba 1.44 return {} unless length $style;
5229 wakaba 1.31 my $variant = $self->font_variant;
5230 wakaba 1.44 return {} unless length $variant;
5231     return {} if $i ne $self->get_property_priority ('font-variant');
5232 wakaba 1.31 my $weight = $self->font_weight;
5233 wakaba 1.44 return {} unless length $weight;
5234     return {} if $i ne $self->get_property_priority ('font-weight');
5235 wakaba 1.31 my $size = $self->font_size;
5236 wakaba 1.44 return {} unless length $size;
5237     return {} if $i ne $self->get_property_priority ('font-size');
5238 wakaba 1.31 my $height = $self->line_height;
5239 wakaba 1.44 return {} unless length $height;
5240     return {} if $i ne $self->get_property_priority ('line-height');
5241 wakaba 1.31 my $family = $self->font_family;
5242 wakaba 1.44 return {} unless length $family;
5243     return {} if $i ne $self->get_property_priority ('font-family');
5244    
5245     my $v = 0;
5246     for ($style, $variant, $weight, $size, $height, $family) {
5247     $v++ if $_ eq 'inherit';
5248     }
5249     if ($v == 6) {
5250 wakaba 1.45 return {font => ['inherit', $i]};
5251 wakaba 1.44 } elsif ($v) {
5252     return {};
5253     }
5254 wakaba 1.31
5255     my @v;
5256     push @v, $style unless $style eq 'normal';
5257     push @v, $variant unless $variant eq 'normal';
5258     push @v, $weight unless $weight eq 'normal';
5259     push @v, $size.($height eq 'normal' ? '' : '/'.$height);
5260     push @v, $family;
5261 wakaba 1.45 return {font => [(join ' ', @v), $i]};
5262 wakaba 1.31 },
5263     };
5264     $Attr->{font} = $Prop->{font};
5265    
5266 wakaba 1.20 $Prop->{'border-width'} = {
5267     css => 'border-width',
5268     dom => 'border_width',
5269     parse => sub {
5270     my ($self, $prop_name, $tt, $t, $onerror) = @_;
5271    
5272     my %prop_value;
5273    
5274     my $sign = 1;
5275     if ($t->{type} == MINUS_TOKEN) {
5276     $t = $tt->get_next_token;
5277     $sign = -1;
5278     }
5279    
5280     if ($t->{type} == DIMENSION_TOKEN) {
5281     my $value = $t->{number} * $sign;
5282     my $unit = lc $t->{value}; ## TODO: case
5283     $t = $tt->get_next_token;
5284     if ($length_unit->{$unit} and $value >= 0) {
5285     $prop_value{'border-top-width'} = ['DIMENSION', $value, $unit];
5286     } else {
5287 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
5288 wakaba 1.20 level => $self->{must_level},
5289 wakaba 1.38 uri => \$self->{href},
5290 wakaba 1.20 token => $t);
5291     return ($t, undef);
5292     }
5293     } elsif ($t->{type} == NUMBER_TOKEN and
5294     ($self->{unitless_px} or $t->{number} == 0)) {
5295     my $value = $t->{number} * $sign;
5296     $t = $tt->get_next_token;
5297     $prop_value{'border-top-width'} = ['DIMENSION', $value, 'px'];
5298     unless ($value >= 0) {
5299 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
5300 wakaba 1.20 level => $self->{must_level},
5301 wakaba 1.38 uri => \$self->{href},
5302 wakaba 1.20 token => $t);
5303     return ($t, undef);
5304     }
5305     } elsif ($sign > 0 and $t->{type} == IDENT_TOKEN) {
5306     my $prop_value = lc $t->{value}; ## TODO: case folding
5307     $t = $tt->get_next_token;
5308     if ({thin => 1, medium => 1, thick => 1}->{$prop_value}) {
5309     $prop_value{'border-top-width'} = ['KEYWORD', $prop_value];
5310     } elsif ($prop_value eq 'inherit') {
5311     $prop_value{'border-top-width'} = ['INHERIT'];
5312     $prop_value{'border-right-width'} = $prop_value{'border-top-width'};
5313     $prop_value{'border-bottom-width'} = $prop_value{'border-top-width'};
5314     $prop_value{'border-left-width'} = $prop_value{'border-right-width'};
5315     return ($t, \%prop_value);
5316     } else {
5317 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
5318 wakaba 1.20 level => $self->{must_level},
5319 wakaba 1.38 uri => \$self->{href},
5320 wakaba 1.20 token => $t);
5321     return ($t, undef);
5322     }
5323     } else {
5324 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
5325 wakaba 1.20 level => $self->{must_level},
5326 wakaba 1.38 uri => \$self->{href},
5327 wakaba 1.20 token => $t);
5328     return ($t, undef);
5329     }
5330     $prop_value{'border-right-width'} = $prop_value{'border-top-width'};
5331     $prop_value{'border-bottom-width'} = $prop_value{'border-top-width'};
5332     $prop_value{'border-left-width'} = $prop_value{'border-right-width'};
5333    
5334     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
5335     $sign = 1;
5336     if ($t->{type} == MINUS_TOKEN) {
5337     $t = $tt->get_next_token;
5338     $sign = -1;
5339     }
5340    
5341     if ($t->{type} == DIMENSION_TOKEN) {
5342     my $value = $t->{number} * $sign;
5343     my $unit = lc $t->{value}; ## TODO: case
5344     $t = $tt->get_next_token;
5345     if ($length_unit->{$unit} and $value >= 0) {
5346     $prop_value{'border-right-width'} = ['DIMENSION', $value, $unit];
5347     } else {
5348 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
5349 wakaba 1.20 level => $self->{must_level},
5350 wakaba 1.38 uri => \$self->{href},
5351 wakaba 1.20 token => $t);
5352     return ($t, undef);
5353     }
5354     } elsif ($t->{type} == NUMBER_TOKEN and
5355     ($self->{unitless_px} or $t->{number} == 0)) {
5356     my $value = $t->{number} * $sign;
5357     $t = $tt->get_next_token;
5358     $prop_value{'border-right-width'} = ['DIMENSION', $value, 'px'];
5359     unless ($value >= 0) {
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     } elsif ($sign > 0 and $t->{type} == IDENT_TOKEN) {
5367     my $prop_value = lc $t->{value}; ## TODO: case
5368     if ({thin => 1, medium => 1, thick => 1}->{$prop_value}) {
5369     $prop_value{'border-right-width'} = ['KEYWORD', $prop_value];
5370     $t = $tt->get_next_token;
5371     }
5372     } else {
5373 wakaba 1.24 if ($sign < 0) {
5374 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
5375 wakaba 1.24 level => $self->{must_level},
5376 wakaba 1.38 uri => \$self->{href},
5377 wakaba 1.24 token => $t);
5378     return ($t, undef);
5379     }
5380 wakaba 1.20 return ($t, \%prop_value);
5381     }
5382     $prop_value{'border-left-width'} = $prop_value{'border-right-width'};
5383    
5384     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
5385     $sign = 1;
5386     if ($t->{type} == MINUS_TOKEN) {
5387     $t = $tt->get_next_token;
5388     $sign = -1;
5389     }
5390    
5391     if ($t->{type} == DIMENSION_TOKEN) {
5392     my $value = $t->{number} * $sign;
5393     my $unit = lc $t->{value}; ## TODO: case
5394     $t = $tt->get_next_token;
5395     if ($length_unit->{$unit} and $value >= 0) {
5396     $prop_value{'border-bottom-width'} = ['DIMENSION', $value, $unit];
5397     } else {
5398 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
5399 wakaba 1.20 level => $self->{must_level},
5400 wakaba 1.38 uri => \$self->{href},
5401 wakaba 1.20 token => $t);
5402     return ($t, undef);
5403     }
5404     } elsif ($t->{type} == NUMBER_TOKEN and
5405     ($self->{unitless_px} or $t->{number} == 0)) {
5406     my $value = $t->{number} * $sign;
5407     $t = $tt->get_next_token;
5408     $prop_value{'border-bottom-width'} = ['DIMENSION', $value, 'px'];
5409     unless ($value >= 0) {
5410 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
5411 wakaba 1.20 level => $self->{must_level},
5412 wakaba 1.38 uri => \$self->{href},
5413 wakaba 1.20 token => $t);
5414     return ($t, undef);
5415     }
5416     } elsif ($sign > 0 and $t->{type} == IDENT_TOKEN) {
5417     my $prop_value = lc $t->{value}; ## TODO: case
5418     if ({thin => 1, medium => 1, thick => 1}->{$prop_value}) {
5419     $prop_value{'border-bottom-width'} = ['KEYWORD', $prop_value];
5420     $t = $tt->get_next_token;
5421     }
5422     } else {
5423 wakaba 1.24 if ($sign < 0) {
5424 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
5425 wakaba 1.24 level => $self->{must_level},
5426 wakaba 1.38 uri => \$self->{href},
5427 wakaba 1.24 token => $t);
5428     return ($t, undef);
5429     }
5430 wakaba 1.20 return ($t, \%prop_value);
5431     }
5432    
5433     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
5434     $sign = 1;
5435     if ($t->{type} == MINUS_TOKEN) {
5436     $t = $tt->get_next_token;
5437     $sign = -1;
5438     }
5439    
5440     if ($t->{type} == DIMENSION_TOKEN) {
5441     my $value = $t->{number} * $sign;
5442     my $unit = lc $t->{value}; ## TODO: case
5443     $t = $tt->get_next_token;
5444     if ($length_unit->{$unit} and $value >= 0) {
5445     $prop_value{'border-left-width'} = ['DIMENSION', $value, $unit];
5446     } else {
5447 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
5448 wakaba 1.20 level => $self->{must_level},
5449 wakaba 1.38 uri => \$self->{href},
5450 wakaba 1.20 token => $t);
5451     return ($t, undef);
5452     }
5453     } elsif ($t->{type} == NUMBER_TOKEN and
5454     ($self->{unitless_px} or $t->{number} == 0)) {
5455     my $value = $t->{number} * $sign;
5456     $t = $tt->get_next_token;
5457     $prop_value{'border-left-width'} = ['DIMENSION', $value, 'px'];
5458     unless ($value >= 0) {
5459 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
5460 wakaba 1.20 level => $self->{must_level},
5461 wakaba 1.38 uri => \$self->{href},
5462 wakaba 1.20 token => $t);
5463     return ($t, undef);
5464     }
5465     } elsif ($sign > 0 and $t->{type} == IDENT_TOKEN) {
5466     my $prop_value = lc $t->{value}; ## TODO: case
5467     if ({thin => 1, medium => 1, thick => 1}->{$prop_value}) {
5468     $prop_value{'border-left-width'} = ['KEYWORD', $prop_value];
5469     $t = $tt->get_next_token;
5470     }
5471     } else {
5472 wakaba 1.24 if ($sign < 0) {
5473 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
5474 wakaba 1.24 level => $self->{must_level},
5475 wakaba 1.38 uri => \$self->{href},
5476 wakaba 1.24 token => $t);
5477     return ($t, undef);
5478     }
5479 wakaba 1.20 return ($t, \%prop_value);
5480     }
5481    
5482     return ($t, \%prop_value);
5483     },
5484 wakaba 1.43 serialize_shorthand => sub {
5485     my $self = shift;
5486    
5487 wakaba 1.20 my @v;
5488 wakaba 1.24 push @v, $self->border_top_width;
5489 wakaba 1.43 my $i = $self->get_property_priority ('border-top-width');
5490 wakaba 1.45 return {} unless length $v[-1];
5491 wakaba 1.24 push @v, $self->border_right_width;
5492 wakaba 1.45 return {} unless length $v[-1];
5493     return {} unless $i eq $self->get_property_priority ('border-right-width');
5494 wakaba 1.24 push @v, $self->border_bottom_width;
5495 wakaba 1.45 return {} unless length $v[-1];
5496     return {} unless $i eq $self->get_property_priority ('border-bottom-width');
5497 wakaba 1.24 push @v, $self->border_left_width;
5498 wakaba 1.45 return {} unless length $v[-1];
5499     return {} unless $i eq $self->get_property_priority ('border-left-width');
5500 wakaba 1.43
5501     my $v = 0;
5502     for (0..3) {
5503     $v++ if $v[$_] eq 'inherit';
5504     }
5505     if ($v == 4) {
5506 wakaba 1.45 return {'border-width' => ['inherit', $i]};
5507 wakaba 1.43 } elsif ($v) {
5508     return {};
5509     }
5510 wakaba 1.20
5511     pop @v if $v[1] eq $v[3];
5512     pop @v if $v[0] eq $v[2];
5513     pop @v if $v[0] eq $v[1];
5514 wakaba 1.45 return {'border-width' => [(join ' ', @v), $i]};
5515 wakaba 1.20 },
5516 wakaba 1.29 serialize_multiple => $Prop->{'border-top-color'}->{serialize_multiple},
5517 wakaba 1.20 };
5518 wakaba 1.24 $Attr->{border_width} = $Prop->{'border-width'};
5519 wakaba 1.20
5520 wakaba 1.12 $Prop->{'list-style'} = {
5521     css => 'list-style',
5522     dom => 'list_style',
5523     parse => sub {
5524     my ($self, $prop_name, $tt, $t, $onerror) = @_;
5525    
5526     my %prop_value;
5527     my $none = 0;
5528    
5529     F: for my $f (1..3) {
5530     if ($t->{type} == IDENT_TOKEN) {
5531     my $prop_value = lc $t->{value}; ## TODO: case folding
5532     $t = $tt->get_next_token;
5533    
5534     if ($prop_value eq 'none') {
5535     $none++;
5536     } elsif ($Prop->{'list-style-type'}->{keyword}->{$prop_value}) {
5537     if (exists $prop_value{'list-style-type'}) {
5538 wakaba 1.39 $onerror->(type => "duplication:'list-style-type'",
5539 wakaba 1.12 level => $self->{must_level},
5540 wakaba 1.38 uri => \$self->{href},
5541 wakaba 1.12 token => $t);
5542     return ($t, undef);
5543     } else {
5544     $prop_value{'list-style-type'} = ['KEYWORD', $prop_value];
5545     }
5546     } elsif ($Prop->{'list-style-position'}->{keyword}->{$prop_value}) {
5547     if (exists $prop_value{'list-style-position'}) {
5548 wakaba 1.39 $onerror->(type => "duplication:'list-style-position'",
5549 wakaba 1.12 level => $self->{must_level},
5550 wakaba 1.38 uri => \$self->{href},
5551 wakaba 1.12 token => $t);
5552     return ($t, undef);
5553     }
5554    
5555     $prop_value{'list-style-position'} = ['KEYWORD', $prop_value];
5556     } elsif ($f == 1 and $prop_value eq 'inherit') {
5557     $prop_value{'list-style-type'} = ["INHERIT"];
5558     $prop_value{'list-style-position'} = ["INHERIT"];
5559     $prop_value{'list-style-image'} = ["INHERIT"];
5560     last F;
5561     } else {
5562     if ($f == 1) {
5563 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
5564 wakaba 1.12 level => $self->{must_level},
5565 wakaba 1.38 uri => \$self->{href},
5566 wakaba 1.12 token => $t);
5567     return ($t, undef);
5568     } else {
5569     last F;
5570     }
5571     }
5572     } elsif ($t->{type} == URI_TOKEN) {
5573     if (exists $prop_value{'list-style-image'}) {
5574 wakaba 1.39 $onerror->(type => "duplication:'list-style-image'",
5575 wakaba 1.38 uri => \$self->{href},
5576 wakaba 1.12 level => $self->{must_level},
5577     token => $t);
5578     return ($t, undef);
5579     }
5580    
5581     $prop_value{'list-style-image'}
5582 wakaba 1.13 = ['URI', $t->{value}, \($self->{base_uri})];
5583 wakaba 1.12 $t = $tt->get_next_token;
5584     } else {
5585     if ($f == 1) {
5586 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
5587 wakaba 1.12 level => $self->{must_level},
5588 wakaba 1.38 uri => \$self->{href},
5589 wakaba 1.12 token => $t);
5590     return ($t, undef);
5591     } else {
5592     last F;
5593     }
5594     }
5595    
5596     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
5597     } # F
5598     ## NOTE: No browser support |list-style: url(xxx|{EOF}.
5599    
5600     if ($none == 1) {
5601     if (exists $prop_value{'list-style-type'}) {
5602     if (exists $prop_value{'list-style-image'}) {
5603 wakaba 1.39 $onerror->(type => "duplication:'list-style-image'",
5604 wakaba 1.38 uri => \$self->{href},
5605 wakaba 1.12 level => $self->{must_level},
5606     token => $t);
5607     return ($t, undef);
5608     } else {
5609     $prop_value{'list-style-image'} = ['KEYWORD', 'none'];
5610     }
5611     } else {
5612     $prop_value{'list-style-type'} = ['KEYWORD', 'none'];
5613     $prop_value{'list-style-image'} = ['KEYWORD', 'none']
5614     unless exists $prop_value{'list-style-image'};
5615     }
5616     } elsif ($none == 2) {
5617     if (exists $prop_value{'list-style-type'}) {
5618 wakaba 1.39 $onerror->(type => "duplication:'list-style-type'",
5619 wakaba 1.38 uri => \$self->{href},
5620 wakaba 1.12 level => $self->{must_level},
5621     token => $t);
5622     return ($t, undef);
5623     }
5624     if (exists $prop_value{'list-style-image'}) {
5625 wakaba 1.39 $onerror->(type => "duplication:'list-style-image'",
5626 wakaba 1.38 uri => \$self->{href},
5627 wakaba 1.12 level => $self->{must_level},
5628     token => $t);
5629     return ($t, undef);
5630     }
5631    
5632     $prop_value{'list-style-type'} = ['KEYWORD', 'none'];
5633     $prop_value{'list-style-image'} = ['KEYWORD', 'none'];
5634     } elsif ($none == 3) {
5635 wakaba 1.39 $onerror->(type => "duplication:'list-style-type'",
5636 wakaba 1.38 uri => \$self->{href},
5637 wakaba 1.12 level => $self->{must_level},
5638     token => $t);
5639     return ($t, undef);
5640     }
5641    
5642     for (qw/list-style-type list-style-position list-style-image/) {
5643     $prop_value{$_} = $Prop->{$_}->{initial} unless exists $prop_value{$_};
5644     }
5645    
5646     return ($t, \%prop_value);
5647     },
5648 wakaba 1.43 ## NOTE: We don't merge longhands in |css_text| serialization,
5649     ## since no browser does.
5650     serialize_shorthand => sub {
5651     my $self = shift;
5652    
5653     ## NOTE: Don't omit any value even if it is the initial value,
5654     ## since WinIE is buggy.
5655 wakaba 1.12
5656 wakaba 1.43 my $type = $self->list_style_type;
5657     return {} unless length $type;
5658     my $type_i = $self->get_property_priority ('list-style-type');
5659     my $image = $self->list_style_image;
5660     return {} unless length $image;
5661     my $image_i = $self->get_property_priority ('list-style-image');
5662     return {} unless $type_i eq $image_i;
5663     my $position = $self->list_style_position;
5664     return {} unless length $position;
5665     my $position_i = $self->get_property_priority ('list-style-position');
5666     return {} unless $type_i eq $position_i;
5667    
5668 wakaba 1.45 return {'list-style' => [$type . ' ' . $image . ' ' . $position, $type_i]};
5669 wakaba 1.12 },
5670     };
5671     $Attr->{list_style} = $Prop->{'list-style'};
5672    
5673 wakaba 1.16 ## NOTE: Future version of the implementation will change the way to
5674     ## store the parsed value to support CSS 3 properties.
5675     $Prop->{'text-decoration'} = {
5676     css => 'text-decoration',
5677     dom => 'text_decoration',
5678     key => 'text_decoration',
5679     parse => sub {
5680     my ($self, $prop_name, $tt, $t, $onerror) = @_;
5681    
5682     my $value = ['DECORATION']; # , underline, overline, line-through, blink
5683    
5684     if ($t->{type} == IDENT_TOKEN) {
5685     my $v = lc $t->{value}; ## TODO: case
5686     $t = $tt->get_next_token;
5687     if ($v eq 'inherit') {
5688     return ($t, {$prop_name => ['INHERIT']});
5689     } elsif ($v eq 'none') {
5690     return ($t, {$prop_name => $value});
5691     } elsif ($v eq 'underline' and
5692     $self->{prop_value}->{$prop_name}->{$v}) {
5693     $value->[1] = 1;
5694     } elsif ($v eq 'overline' and
5695     $self->{prop_value}->{$prop_name}->{$v}) {
5696     $value->[2] = 1;
5697     } elsif ($v eq 'line-through' and
5698     $self->{prop_value}->{$prop_name}->{$v}) {
5699     $value->[3] = 1;
5700     } elsif ($v eq 'blink' and
5701     $self->{prop_value}->{$prop_name}->{$v}) {
5702     $value->[4] = 1;
5703     } else {
5704 wakaba 1.39 $onerror->(type => "syntax error:'$prop_name'",
5705 wakaba 1.16 level => $self->{must_level},
5706 wakaba 1.38 uri => \$self->{href},
5707 wakaba 1.16 token => $t);
5708     return ($t, undef);
5709     }
5710     }
5711    
5712     F: {
5713     $t = $tt->get_next_token while $t->{type} == S_TOKEN;
5714     last F unless $t->{type} == IDENT_TOKEN;
5715    
5716     my $v = lc $t->{value}; ## TODO: case
5717     $t = $tt->get_next_token;
5718     if ($v eq 'underline' and
5719     $self->{prop_value}->{$prop_name}->{$v}) {
5720     $value->[1] = 1;
5721     } elsif ($v eq 'overline' and
5722     $self->{prop_value}->{$prop_name}->{$v}) {
5723     $value->[1] = 2;
5724     } elsif ($v eq 'line-through' and
5725     $self->{prop_value}->{$prop_name}->{$v}) {
5726     $value->[1] = 3;
5727     } elsif ($v eq 'blink' and
5728     $self->{prop_value}->{$prop_name}->{$v}) {
5729     $value->[1] = 4;
5730     } else {
5731     last F;
5732     }
5733    
5734     redo F;
5735     } # F
5736    
5737     return ($t, {$prop_name => $value});
5738     },
5739     serialize => $default_serializer,
5740     initial => ["KEYWORD", "none"],
5741     #inherited => 0,
5742     compute => $compute_as_specified,
5743     };
5744     $Attr->{text_decoration} = $Prop->{'text-decoration'};
5745     $Key->{text_decoration} = $Prop->{'text-decoration'};
5746    
5747 wakaba 1.1 1;
5748 wakaba 1.48 ## $Date: 2008/01/26 09:30:47 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24