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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.7 - (hide annotations) (download)
Sun Jan 6 04:32:56 2008 UTC (16 years, 10 months ago) by wakaba
Branch: MAIN
Changes since 1.6: +92 -17 lines
++ manakai/lib/Message/DOM/ChangeLog	6 Jan 2008 04:32:40 -0000
2008-01-06  Wakaba  <wakaba@suika.fam.cx>

	* CSSStyleDeclaration.pm (css_text): Allow to shorten a complete
	set of longhand properties.
	(get_property_priority): Implemented.
	(CSSComputedStyleDeclaration AUTOLOAD): Implemented for reflecting
	attributes.

1 wakaba 1.1 package Message::DOM::CSSStyleDeclaration;
2     use strict;
3 wakaba 1.7 our $VERSION=do{my @r=(q$Revision: 1.6 $=~/\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     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 wakaba 1.1 ## |CSSStyleDeclaration| attributes
40    
41 wakaba 1.2 sub css_text ($;$) {
42 wakaba 1.3 ## TODO: setter
43    
44     require Whatpm::CSS::Parser;
45     my $self = $_[0];
46     my $r = '';
47 wakaba 1.7 my %serialized;
48 wakaba 1.3 for (grep {$$self->{$_}} keys %$$self) {
49     my $prop_def = $Whatpm::CSS::Parser::Key->{$_};
50     next unless $prop_def;
51 wakaba 1.7
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 wakaba 1.3 }
69     }
70     ## TODO: shorthands
71     return $r;
72 wakaba 1.2 } # css_text
73    
74 wakaba 1.1 sub parent_rule ($) {
75     return ${$_[0]}->{parent_rule};
76     } # parent_rule
77    
78 wakaba 1.7 ## |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 wakaba 1.1 ## TODO: Implement other methods and attributes
93    
94 wakaba 1.4 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 wakaba 1.7 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 wakaba 1.4 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 wakaba 1.7 my %serialized;
144 wakaba 1.6 for my $prop_def (sort {$a->{css} cmp $b->{css}}
145 wakaba 1.7 grep {$_->{compute} or
146     $_->{compute_multiple} or
147     $_->{serialize_multiple}}
148 wakaba 1.6 values %$Whatpm::CSS::Parser::Prop) {
149 wakaba 1.7 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 wakaba 1.5 } else {
158 wakaba 1.7 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 wakaba 1.4 }
169     }
170 wakaba 1.6
171     ## ISSUE: Should we include CSS properties that are not supported?
172    
173 wakaba 1.4 return $r;
174     } # css_text
175    
176 wakaba 1.7 ## |CSSStyleDeclaration| methods
177    
178     sub get_property_priority ($$) {
179     return '';
180     } # get_property_priority
181    
182 wakaba 1.4 ## TODO: members
183    
184 wakaba 1.1 package Message::IF::CSSStyleDeclaration;
185    
186     1;
187 wakaba 1.7 ## $Date: 2008/01/04 14:45:29 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24