1 |
wakaba |
1.1 |
package Message::DOM::CSSStyleDeclaration; |
2 |
|
|
use strict; |
3 |
wakaba |
1.10 |
our $VERSION=do{my @r=(q$Revision: 1.9 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r}; |
4 |
wakaba |
1.1 |
push our @ISA, 'Message::IF::CSSStyleDeclaration'; |
5 |
|
|
|
6 |
|
|
sub ____new ($) { |
7 |
|
|
return bless \{}, $_[0]; |
8 |
|
|
} # ____new |
9 |
|
|
|
10 |
wakaba |
1.3 |
sub AUTOLOAD { |
11 |
|
|
my $method_name = our $AUTOLOAD; |
12 |
|
|
$method_name =~ s/.*:://; |
13 |
|
|
return if $method_name eq 'DESTROY'; |
14 |
|
|
|
15 |
|
|
require Whatpm::CSS::Parser; |
16 |
|
|
my $prop_def = $Whatpm::CSS::Parser::Attr->{$method_name}; |
17 |
|
|
|
18 |
|
|
if ($prop_def) { |
19 |
|
|
no strict 'refs'; |
20 |
|
|
*{ $method_name } = sub { |
21 |
|
|
## TODO: setter |
22 |
|
|
|
23 |
|
|
my $self = $_[0]; |
24 |
|
|
my $value = $$self->{$prop_def->{key}}; |
25 |
|
|
if ($value) { |
26 |
|
|
return $prop_def->{serialize}->($self, $prop_def->{css}, $value->[0]); |
27 |
|
|
} else { |
28 |
wakaba |
1.10 |
return ""; |
29 |
wakaba |
1.3 |
} |
30 |
wakaba |
1.8 |
## ISSUE: If one of shorthand component properties is !important? |
31 |
wakaba |
1.3 |
}; |
32 |
|
|
goto &{ $AUTOLOAD }; |
33 |
|
|
} else { |
34 |
|
|
require Carp; |
35 |
|
|
Carp::croak (qq<Can't locate method "$AUTOLOAD">); |
36 |
|
|
} |
37 |
|
|
} # AUTOLOAD |
38 |
|
|
|
39 |
wakaba |
1.1 |
## |CSSStyleDeclaration| attributes |
40 |
|
|
|
41 |
wakaba |
1.2 |
sub css_text ($;$) { |
42 |
wakaba |
1.3 |
## TODO: setter |
43 |
|
|
|
44 |
wakaba |
1.9 |
## NOTE: Where and how white space characters are inserted are |
45 |
|
|
## intentionally changed from those in browsers so that properties are |
46 |
|
|
## more prettily printed. |
47 |
|
|
## See <http://suika.fam.cx/gate/2005/sw/cssText> for what browsers do. |
48 |
|
|
## TODO: Ordering issue. |
49 |
wakaba |
1.3 |
require Whatpm::CSS::Parser; |
50 |
|
|
my $self = $_[0]; |
51 |
|
|
my $r = ''; |
52 |
wakaba |
1.7 |
my %serialized; |
53 |
wakaba |
1.3 |
for (grep {$$self->{$_}} keys %$$self) { |
54 |
|
|
my $prop_def = $Whatpm::CSS::Parser::Key->{$_}; |
55 |
|
|
next unless $prop_def; |
56 |
wakaba |
1.7 |
|
57 |
|
|
if ($prop_def->{serialize_multiple}) { |
58 |
|
|
unless ($serialized{$prop_def->{serialize_multiple}}) { |
59 |
|
|
$serialized{$prop_def->{serialize_multiple}} = 1; |
60 |
|
|
my $v = $prop_def->{serialize_multiple}->($self); |
61 |
|
|
for my $prop_name (sort {$a cmp $b} keys %$v) { |
62 |
|
|
$r .= ' ' . $prop_name . ': ' . $v->{$prop_name} . ";\n" |
63 |
|
|
} |
64 |
|
|
} |
65 |
|
|
} else { |
66 |
|
|
my $value = $$self->{$_}; |
67 |
|
|
my $s = $prop_def->{serialize}->($self, $prop_def->{css}, $value->[0]); |
68 |
wakaba |
1.10 |
if (length $s) { |
69 |
wakaba |
1.7 |
$r .= ' ' . $prop_def->{css} . ': ' . $s; |
70 |
wakaba |
1.10 |
$r .= ' ! ' . $value->[1] if length $value->[1]; |
71 |
wakaba |
1.7 |
$r .= ";\n"; |
72 |
|
|
} |
73 |
wakaba |
1.3 |
} |
74 |
|
|
} |
75 |
|
|
return $r; |
76 |
wakaba |
1.2 |
} # css_text |
77 |
|
|
|
78 |
wakaba |
1.1 |
sub parent_rule ($) { |
79 |
|
|
return ${$_[0]}->{parent_rule}; |
80 |
|
|
} # parent_rule |
81 |
|
|
|
82 |
wakaba |
1.7 |
## |CSSStyleDeclaration| methods |
83 |
|
|
|
84 |
|
|
sub get_property_priority ($$) { |
85 |
|
|
my $prop_name = ''.$_[1]; |
86 |
|
|
|
87 |
|
|
require Whatpm::CSS::Parser; |
88 |
|
|
my $prop_def = $Whatpm::CSS::Parser::Prop->{$prop_name}; |
89 |
|
|
return '' unless defined $prop_def; |
90 |
|
|
|
91 |
|
|
my $v = ${$_[0]}->{$prop_def->{key}}; |
92 |
wakaba |
1.10 |
return $v ? $v->[1] : ''; |
93 |
wakaba |
1.7 |
} # get_property_priority |
94 |
|
|
|
95 |
wakaba |
1.1 |
## TODO: Implement other methods and attributes |
96 |
|
|
|
97 |
wakaba |
1.4 |
package Message::DOM::CSSComputedStyleDeclaration; |
98 |
|
|
push our @ISA, 'Message::IF::CSSStyleDeclaration'; |
99 |
|
|
|
100 |
|
|
sub ____new ($$$) { |
101 |
|
|
my $self = bless \{}, shift; |
102 |
|
|
$$self->{cascade} = shift; # Whatpm::CSS::Cascade object. |
103 |
|
|
$$self->{element} = shift; ## TODO: This link should be weaken? |
104 |
|
|
return $self; |
105 |
|
|
} # ____new |
106 |
|
|
|
107 |
wakaba |
1.7 |
sub AUTOLOAD { |
108 |
|
|
my $method_name = our $AUTOLOAD; |
109 |
|
|
$method_name =~ s/.*:://; |
110 |
|
|
return if $method_name eq 'DESTROY'; |
111 |
|
|
|
112 |
|
|
require Whatpm::CSS::Parser; |
113 |
|
|
my $prop_def = $Whatpm::CSS::Parser::Attr->{$method_name}; |
114 |
|
|
|
115 |
|
|
if ($prop_def) { |
116 |
|
|
no strict 'refs'; |
117 |
|
|
*{ $method_name } = sub { |
118 |
|
|
## TODO: setter |
119 |
|
|
|
120 |
|
|
my $self = $_[0]; |
121 |
|
|
my $value = $$self->{cascade}->get_computed_value |
122 |
|
|
($$self->{element}, $prop_def->{css}); |
123 |
|
|
if ($value) { |
124 |
|
|
return $prop_def->{serialize}->($self, $prop_def->{css}, $value); |
125 |
|
|
} else { |
126 |
wakaba |
1.10 |
return ""; |
127 |
wakaba |
1.7 |
} |
128 |
|
|
}; |
129 |
|
|
goto &{ $AUTOLOAD }; |
130 |
|
|
} else { |
131 |
|
|
require Carp; |
132 |
|
|
Carp::croak (qq<Can't locate method "$AUTOLOAD">); |
133 |
|
|
} |
134 |
|
|
} # AUTOLOAD |
135 |
|
|
|
136 |
wakaba |
1.4 |
sub css_text ($;$) { |
137 |
|
|
## TODO: error if modified |
138 |
|
|
|
139 |
|
|
my $self = shift; |
140 |
|
|
require Whatpm::CSS::Parser; |
141 |
|
|
|
142 |
wakaba |
1.9 |
## NOTE: Where and how white space characters are inserted are |
143 |
|
|
## intentionally changed from those in browsers so that properties are |
144 |
|
|
## more prettily printed. |
145 |
|
|
## See <http://suika.fam.cx/gate/2005/sw/cssText> for what browsers do. |
146 |
wakaba |
1.4 |
## TODO: ordering |
147 |
|
|
## TODO: any spec? |
148 |
|
|
my $r = ''; |
149 |
wakaba |
1.7 |
my %serialized; |
150 |
wakaba |
1.6 |
for my $prop_def (sort {$a->{css} cmp $b->{css}} |
151 |
wakaba |
1.7 |
grep {$_->{compute} or |
152 |
|
|
$_->{compute_multiple} or |
153 |
|
|
$_->{serialize_multiple}} |
154 |
wakaba |
1.6 |
values %$Whatpm::CSS::Parser::Prop) { |
155 |
wakaba |
1.7 |
if ($prop_def->{serialize_multiple}) { |
156 |
|
|
unless ($serialized{$prop_def->{serialize_multiple}}) { |
157 |
|
|
$serialized{$prop_def->{serialize_multiple}} = 1; |
158 |
|
|
my $v = $prop_def->{serialize_multiple}->($self); |
159 |
|
|
for my $prop_name (sort {$a cmp $b} keys %$v) { |
160 |
|
|
$r .= ' ' . $prop_name . ': ' . $v->{$prop_name} . ";\n" |
161 |
|
|
} |
162 |
|
|
} |
163 |
wakaba |
1.5 |
} else { |
164 |
wakaba |
1.7 |
my $prop_value = $$self->{cascade}->get_computed_value |
165 |
|
|
($$self->{element}, $prop_def->{css}); |
166 |
|
|
my $s = $prop_def->{serialize}->($self, $prop_def->{css}, $prop_value); |
167 |
wakaba |
1.10 |
if (length $s) { |
168 |
wakaba |
1.7 |
$r .= ' ' . $prop_def->{css} . ': ' . $s; |
169 |
|
|
$r .= ";\n"; |
170 |
|
|
} else { |
171 |
|
|
## NOTE: This should be an error of the implementation. |
172 |
|
|
$r .= " /* $prop_def->{css}: ???; */\n"; |
173 |
|
|
} |
174 |
wakaba |
1.4 |
} |
175 |
|
|
} |
176 |
wakaba |
1.6 |
|
177 |
|
|
## ISSUE: Should we include CSS properties that are not supported? |
178 |
|
|
|
179 |
wakaba |
1.4 |
return $r; |
180 |
|
|
} # css_text |
181 |
|
|
|
182 |
wakaba |
1.7 |
## |CSSStyleDeclaration| methods |
183 |
|
|
|
184 |
wakaba |
1.10 |
sub get_property_priority ($$) { '' } |
185 |
wakaba |
1.7 |
|
186 |
wakaba |
1.4 |
## TODO: members |
187 |
|
|
|
188 |
wakaba |
1.1 |
package Message::IF::CSSStyleDeclaration; |
189 |
|
|
|
190 |
|
|
1; |
191 |
wakaba |
1.10 |
## $Date: 2008/01/14 05:53:45 $ |