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