/[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.42 - (hide annotations) (download)
Thu Jan 24 13:09:00 2008 UTC (16 years, 9 months ago) by wakaba
Branch: MAIN
Changes since 1.41: +89 -9 lines
++ whatpm/t/ChangeLog	24 Jan 2008 13:08:49 -0000
	* CSS-Parser-1.t: Default values are updated.

	* css-1.t: Tests for duplicate declarations are added.

	* css-visual.t: Some tests were incorrect.  Tests for 'margin'
	serialization and '+' in 'margin' are added.

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

++ whatpm/Whatpm/CSS/ChangeLog	24 Jan 2008 13:07:19 -0000
	* Parser.pm (parse_char_string): Treatement for non-important
	duplicate declarations was incorrect.
	(margin): Use 'margin' shorthand property for serializing
	margin-related properties if possible.  Support for the |+|
	sign in <'margin'> is added.

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24