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