/[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.49 - (hide annotations) (download)
Sat Jan 26 14:31:32 2008 UTC (17 years, 5 months ago) by wakaba
Branch: MAIN
Changes since 1.48: +48 -12 lines
++ whatpm/t/ChangeLog	26 Jan 2008 14:31:26 -0000
	* css-font.dat: New test data for 'font' and 'font-size'.

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

++ whatpm/Whatpm/CSS/ChangeLog	26 Jan 2008 14:31:00 -0000
	* Parser.pm ('font-size' parser): Support for '+'.  Fixed to
	report more accurate error position.  Enabled the
	support for '-webkit-xxx-large'.
	('font' parse): Support for '+' in <'font-weight'>
	and <'font-size'>.

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

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24