1 |
package Message::DOM::CSSStyleDeclaration; |
2 |
use strict; |
3 |
our $VERSION=do{my @r=(q$Revision: 1.9 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r}; |
4 |
push our @ISA, 'Message::IF::CSSStyleDeclaration'; |
5 |
|
6 |
sub ____new ($) { |
7 |
return bless \{}, $_[0]; |
8 |
} # ____new |
9 |
|
10 |
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 |
return ""; |
29 |
} |
30 |
## ISSUE: If one of shorthand component properties is !important? |
31 |
}; |
32 |
goto &{ $AUTOLOAD }; |
33 |
} else { |
34 |
require Carp; |
35 |
Carp::croak (qq<Can't locate method "$AUTOLOAD">); |
36 |
} |
37 |
} # AUTOLOAD |
38 |
|
39 |
## |CSSStyleDeclaration| attributes |
40 |
|
41 |
sub css_text ($;$) { |
42 |
## TODO: setter |
43 |
|
44 |
## 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 |
require Whatpm::CSS::Parser; |
50 |
my $self = $_[0]; |
51 |
my $r = ''; |
52 |
my %serialized; |
53 |
for (grep {$$self->{$_}} keys %$$self) { |
54 |
my $prop_def = $Whatpm::CSS::Parser::Key->{$_}; |
55 |
next unless $prop_def; |
56 |
|
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 |
if (length $s) { |
69 |
$r .= ' ' . $prop_def->{css} . ': ' . $s; |
70 |
$r .= ' ! ' . $value->[1] if length $value->[1]; |
71 |
$r .= ";\n"; |
72 |
} |
73 |
} |
74 |
} |
75 |
return $r; |
76 |
} # css_text |
77 |
|
78 |
sub parent_rule ($) { |
79 |
return ${$_[0]}->{parent_rule}; |
80 |
} # parent_rule |
81 |
|
82 |
## |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 |
return $v ? $v->[1] : ''; |
93 |
} # get_property_priority |
94 |
|
95 |
## TODO: Implement other methods and attributes |
96 |
|
97 |
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 |
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 |
return ""; |
127 |
} |
128 |
}; |
129 |
goto &{ $AUTOLOAD }; |
130 |
} else { |
131 |
require Carp; |
132 |
Carp::croak (qq<Can't locate method "$AUTOLOAD">); |
133 |
} |
134 |
} # AUTOLOAD |
135 |
|
136 |
sub css_text ($;$) { |
137 |
## TODO: error if modified |
138 |
|
139 |
my $self = shift; |
140 |
require Whatpm::CSS::Parser; |
141 |
|
142 |
## 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 |
## TODO: ordering |
147 |
## TODO: any spec? |
148 |
my $r = ''; |
149 |
my %serialized; |
150 |
for my $prop_def (sort {$a->{css} cmp $b->{css}} |
151 |
grep {$_->{compute} or |
152 |
$_->{compute_multiple} or |
153 |
$_->{serialize_multiple}} |
154 |
values %$Whatpm::CSS::Parser::Prop) { |
155 |
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 |
} else { |
164 |
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 |
if (length $s) { |
168 |
$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 |
} |
175 |
} |
176 |
|
177 |
## ISSUE: Should we include CSS properties that are not supported? |
178 |
|
179 |
return $r; |
180 |
} # css_text |
181 |
|
182 |
## |CSSStyleDeclaration| methods |
183 |
|
184 |
sub get_property_priority ($$) { '' } |
185 |
|
186 |
## TODO: members |
187 |
|
188 |
package Message::IF::CSSStyleDeclaration; |
189 |
|
190 |
1; |
191 |
## $Date: 2008/01/14 05:53:45 $ |