/[suikacvs]/messaging/manakai/lib/Message/DOM/CSSStyleDeclaration.pm
Suika

Diff of /messaging/manakai/lib/Message/DOM/CSSStyleDeclaration.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.9 by wakaba, Mon Jan 14 05:53:45 2008 UTC revision 1.14 by wakaba, Sat Jan 26 05:12:05 2008 UTC
# Line 1  Line 1 
1  package Message::DOM::CSSStyleDeclaration;  package Message::DOM::CSSStyleDeclaration;
2  use strict;  use strict;
3  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
4  push our @ISA, 'Message::IF::CSSStyleDeclaration';  push our @ISA, 'Message::IF::CSSStyleDeclaration',
5        'Message::IF::CSS2Properties';
6    
7  sub ____new ($) {  sub ____new ($) {
8    return bless \{}, $_[0];    return bless \{}, $_[0];
# Line 17  sub AUTOLOAD { Line 18  sub AUTOLOAD {
18    
19    if ($prop_def) {    if ($prop_def) {
20      no strict 'refs';      no strict 'refs';
21      *{ $method_name } = sub {      if ($prop_def->{serialize}) {
22        ## TODO: setter        *{ $method_name } = sub {
23            ## TODO: setter
24        my $self = $_[0];    
25        my $value = $$self->{$prop_def->{key}};          my $self = $_[0];
26        if ($value) {          my $value = $$self->{$prop_def->{key}};
27          return $prop_def->{serialize}->($self, $prop_def->{css}, $value->[0]);          if ($value) {
28        } else {            return $prop_def->{serialize}->($self, $prop_def->{css}, $value->[0]);
29          return undef;          } else {
30        }            return '';
31        ## TODO: null? ""? ... if not set?          }
32        ## ISSUE: If one of shorthand component properties is !important?        };
33      };      } elsif ($prop_def->{serialize_shorthand} or
34                 $prop_def->{serialize_multiple}) {
35          *{ $method_name } = sub {
36            ## TODO: setter
37      
38            my $self = $_[0];
39            my $v = ($prop_def->{serialize_shorthand} or
40                     $prop_def->{serialize_multiple})->($self);
41            if (defined $v->{$prop_def->{css}}) {
42              return $v->{$prop_def->{css}}->[0];
43            } else {
44              return '';
45            }
46            ## ISSUE: If one of shorthand component properties is !important?
47          };
48        } else {
49          die qq<Implementation error: Can't load serializer for "$AUTOLOAD">;
50        }
51      goto &{ $AUTOLOAD };      goto &{ $AUTOLOAD };
52    } else {    } else {
53      require Carp;      require Carp;
# Line 37  sub AUTOLOAD { Line 55  sub AUTOLOAD {
55    }    }
56  } # AUTOLOAD  } # AUTOLOAD
57    
58    use overload
59        '@{}' => sub {
60          tie my @list, ref $_[0], $_[0];
61          return \@list;
62        },
63        fallback => 1;
64    
65    sub TIEARRAY ($$) { $_[1] }
66    
67  ## |CSSStyleDeclaration| attributes  ## |CSSStyleDeclaration| attributes
68    
69  sub css_text ($;$) {  sub css_text ($;$) {
# Line 51  sub css_text ($;$) { Line 78  sub css_text ($;$) {
78    my $self = $_[0];    my $self = $_[0];
79    my $r = '';    my $r = '';
80    my %serialized;    my %serialized;
81    for (grep {$$self->{$_}} keys %$$self) {    for (sort {$a cmp $b} grep {$$self->{$_}} keys %$$self) {
82      my $prop_def = $Whatpm::CSS::Parser::Key->{$_};      my $prop_def = $Whatpm::CSS::Parser::Key->{$_};
83      next unless $prop_def;      next unless $prop_def;
84    
# Line 60  sub css_text ($;$) { Line 87  sub css_text ($;$) {
87          $serialized{$prop_def->{serialize_multiple}} = 1;          $serialized{$prop_def->{serialize_multiple}} = 1;
88          my $v = $prop_def->{serialize_multiple}->($self);          my $v = $prop_def->{serialize_multiple}->($self);
89          for my $prop_name (sort {$a cmp $b} keys %$v) {          for my $prop_name (sort {$a cmp $b} keys %$v) {
90            $r .= '  ' . $prop_name . ': ' . $v->{$prop_name} . ";\n"            $r .= '  ' . $prop_name . ': ' . $v->{$prop_name}->[0];
91              $r .= ' ! ' . $v->{$prop_name}->[1] if length $v->{$prop_name}->[1];
92              $r .= ";\n";
93          }          }
94        }        }
95      } else {      } else {
96        my $value = $$self->{$_};        my $value = $$self->{$_};
97        my $s = $prop_def->{serialize}->($self, $prop_def->{css}, $value->[0]);        my $s = $prop_def->{serialize}->($self, $prop_def->{css}, $value->[0]);
98        if (defined $s) {        if (length $s) {
99          $r .= '  ' . $prop_def->{css} . ': ' . $s;          $r .= '  ' . $prop_def->{css} . ': ' . $s;
100          $r .= ' !' . $value->[1] if defined $value->[1];          $r .= ' ! ' . $value->[1] if length $value->[1];
101          $r .= ";\n";          $r .= ";\n";
102        }        }
103      }      }
# Line 76  sub css_text ($;$) { Line 105  sub css_text ($;$) {
105    return $r;    return $r;
106  } # css_text  } # css_text
107    
108    sub length ($) {
109      require Whatpm::CSS::Parser;
110      return scalar @{[grep {$_}
111                       map { $Whatpm::CSS::Parser::Key->{$_} }
112                       keys %${$_[0]}]->[$_[1]]};
113    } # length
114    *FETCHSIZE = \&length;
115    
116    ## TODO: STORESIZE
117    
118  sub parent_rule ($) {  sub parent_rule ($) {
119    return ${$_[0]}->{parent_rule};    return ${$_[0]}->{parent_rule};
120  } # parent_rule  } # parent_rule
# Line 89  sub get_property_priority ($$) { Line 128  sub get_property_priority ($$) {
128    my $prop_def = $Whatpm::CSS::Parser::Prop->{$prop_name};    my $prop_def = $Whatpm::CSS::Parser::Prop->{$prop_name};
129    return '' unless defined $prop_def;    return '' unless defined $prop_def;
130    
131    my $v = ${$_[0]}->{$prop_def->{key}};    if ($prop_def->{serialize}) {
132        my $v = ${$_[0]}->{$prop_def->{key}};
133    return ((defined $v->[1] and $v->[1] eq 'important') ? 'important' : '');      return $v ? $v->[1] : '';
134      } elsif ($prop_def->{serialize_shorthand} or
135               $prop_def->{serialize_multiple}) {
136        my $v = ($prop_def->{serialize_shorthand} or
137                 $prop_def->{serialize_multiple})->($_[0]);
138        if (defined $v->{$prop_def->{css}}) {
139          return $v->{$prop_def->{css}}->[1];
140        } else {
141          return '';
142        }
143      } else {
144        die "Implementation error: No serializer for property '$prop_name'";
145      }
146  } # get_property_priority  } # get_property_priority
147    
148    sub item ($$) {
149      require Whatpm::CSS::Parser;
150      return '' if $_[1] < 0;
151      ## TODO: ordering (should be same as that in |css_text|.
152      my $v = [map {$_->{key}}
153               grep {$_}
154               map { $Whatpm::CSS::Parser::Key->{$_} }
155               keys %${$_[0]}]->[$_[1]];
156      return defined $v ? $v : '';
157    } # item
158    *FETCH = \&item;
159    
160    ## TODO: STORE, DELETE
161    
162    sub EXISTS ($$) {
163      return length $_[0]->item;
164    } # EXISTS
165    
166  ## TODO: Implement other methods and attributes  ## TODO: Implement other methods and attributes
167    
168  package Message::DOM::CSSComputedStyleDeclaration;  package Message::DOM::CSSComputedStyleDeclaration;
169  push our @ISA, 'Message::IF::CSSStyleDeclaration';  push our @ISA, 'Message::IF::CSSStyleDeclaration',
170        'Message::IF::CSS2Properties';
171    
172  sub ____new ($$$) {  sub ____new ($$$) {
173    my $self = bless \{}, shift;    my $self = bless \{}, shift;
# Line 116  sub AUTOLOAD { Line 186  sub AUTOLOAD {
186    
187    if ($prop_def) {    if ($prop_def) {
188      no strict 'refs';      no strict 'refs';
189      *{ $method_name } = sub {      if ($prop_def->{compute} or $prop_def->{compute_multiple}) {
190        ## TODO: setter        *{ $method_name } = sub {
191            ## TODO: setter
192        my $self = $_[0];    
193        my $value = $$self->{cascade}->get_computed_value          my $self = $_[0];
194            ($$self->{element}, $prop_def->{css});          my $value = $$self->{cascade}->get_computed_value
195        if ($value) {              ($$self->{element}, $prop_def->{css});
196          return $prop_def->{serialize}->($self, $prop_def->{css}, $value);          if ($value) {
197        } else {            return $prop_def->{serialize}->($self, $prop_def->{css}, $value);
198          return undef;          } else {
199        }            return '';
200        ## TODO: null? ""? ... if not set?          }
201      };        };
202        } elsif ($prop_def->{serialize_shorthand} or
203                 $prop_def->{serialize_multiple}) {
204          *{ $method_name } = sub {
205            ## TODO: setter
206            my $self = shift;
207    
208            my $v = ($prop_def->{serialize_shorthand} or
209                     $prop_def->{serialize_multiple})->($self);
210            if (defined $v->{$prop_def->{css}}) {
211              return $v->{$prop_def->{css}}->[0];
212            } else {
213              return '';
214            }
215          };
216        } else {
217          ## TODO: This should be an error of the implementation.
218          ## However, currently some shorthand properties does not have
219          ## serializer.
220          ## TODO: Remove {serialize} from shorthand properties, since
221          ## they have no effect.
222          warn "$0: No computed value function for $method_name";
223          #die "$0: No computed value function for $method_name";
224          *{ $method_name } = sub { };
225        }
226      goto &{ $AUTOLOAD };      goto &{ $AUTOLOAD };
227    } else {    } else {
228      require Carp;      require Carp;
# Line 136  sub AUTOLOAD { Line 230  sub AUTOLOAD {
230    }    }
231  } # AUTOLOAD  } # AUTOLOAD
232    
233    use overload
234        '@{}' => sub {
235          tie my @list, ref $_[0], $_[0];
236          return \@list;
237        },
238        fallback => 1;
239    
240    sub TIEARRAY ($$) { $_[1] }
241    
242    ## |CSSStyleDeclaration| attributes
243    
244  sub css_text ($;$) {  sub css_text ($;$) {
245    ## TODO: error if modified    ## TODO: error if modified
246    
# Line 160  sub css_text ($;$) { Line 265  sub css_text ($;$) {
265          $serialized{$prop_def->{serialize_multiple}} = 1;          $serialized{$prop_def->{serialize_multiple}} = 1;
266          my $v = $prop_def->{serialize_multiple}->($self);          my $v = $prop_def->{serialize_multiple}->($self);
267          for my $prop_name (sort {$a cmp $b} keys %$v) {          for my $prop_name (sort {$a cmp $b} keys %$v) {
268            $r .= '  ' . $prop_name . ': ' . $v->{$prop_name} . ";\n"            $r .= '  ' . $prop_name . ': ' . $v->{$prop_name}->[0];
269              $r .= ' ! ' . $v->{$prop_name}->[1] if length $v->{$prop_name}->[1];
270              $r .= ";\n";
271          }          }
272        }        }
273      } else {      } else {
274        my $prop_value = $$self->{cascade}->get_computed_value        my $prop_value = $$self->{cascade}->get_computed_value
275            ($$self->{element}, $prop_def->{css});            ($$self->{element}, $prop_def->{css});
276        my $s = $prop_def->{serialize}->($self, $prop_def->{css}, $prop_value);        my $s = $prop_def->{serialize}->($self, $prop_def->{css}, $prop_value);
277        if (defined $s) {        if (length $s) {
278          $r .= '  ' . $prop_def->{css} . ': ' . $s;          $r .= '  ' . $prop_def->{css} . ': ' . $s;
279          $r .= ";\n";          $r .= ";\n";
280        } else {        } else {
# Line 182  sub css_text ($;$) { Line 289  sub css_text ($;$) {
289    return $r;    return $r;
290  } # css_text  } # css_text
291    
292    ## TODO: What should we enumerate is unclear.
293    sub length ($) {
294      require Whatpm::CSS::Parser;
295      return scalar @{[grep {$_}
296                       values %$Whatpm::CSS::Parser::Key]};
297    } # length
298    *FETCHSIZE = \&length;
299    
300    ## TODO: STORESIZE
301    
302  ## |CSSStyleDeclaration| methods  ## |CSSStyleDeclaration| methods
303    
304  sub get_property_priority ($$) {  sub get_property_priority ($$) { '' }
305    return '';  
306  } # get_property_priority  sub item ($$) {
307      require Whatpm::CSS::Parser;
308      return '' if $_[1] < 0;
309      ## TODO: ordering (should be same as that in |css_text|.
310      my $v = [sort {$a cmp $b}
311               map {$_->{css}}
312               grep {$_}
313               values %$Whatpm::CSS::Parser::Key]->[$_[1]];
314      return defined $v ? $v : '';
315    } # item
316    *FETCH = \&item;
317    
318    ## TODO: STORE, DELETE
319    
320    sub EXISTS ($$) {
321      return length $_[0]->item;
322    } # EXISTS
323    
324  ## TODO: members  ## TODO: members
325    
326  package Message::IF::CSSStyleDeclaration;  package Message::IF::CSSStyleDeclaration;
327    package Message::IF::CSS2Properties;
328    
329  1;  1;
330  ## $Date$  ## $Date$

Legend:
Removed from v.1.9  
changed lines
  Added in v.1.14

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24