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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.9 - (hide annotations) (download)
Mon Feb 11 00:32:53 2008 UTC (16 years, 9 months ago) by wakaba
Branch: MAIN
CVS Tags: HEAD
Changes since 1.8: +21 -8 lines
++ manakai/lib/Message/DOM/ChangeLog	11 Feb 2008 00:32:39 -0000
2008-02-11  Wakaba  <wakaba@suika.fam.cx>

	* CSSRule.pm (@import css_text): Implemented.

1 wakaba 1.1 package Message::DOM::CSSRule;
2     use strict;
3 wakaba 1.9 our $VERSION=do{my @r=(q$Revision: 1.8 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
4 wakaba 1.1 push our @ISA, 'Message::IF::CSSRule';
5 wakaba 1.2 require Scalar::Util;
6 wakaba 1.1
7     ## |CSSRule| constants
8    
9     sub STYLE_RULE () { 1 }
10     sub CHARSET_RULE () { 2 }
11     sub IMPORT_RULE () { 3 }
12     sub MEDIA_RULE () { 4 }
13     sub FONT_FACE_RULE () { 5 }
14     sub PAGE_RULE () { 6 }
15     sub NAMESPACE_RULE () { 7 }
16    
17     ## |CSSRule| attributes
18    
19     sub css_text ($) {
20 wakaba 1.3 die "$0: ".(ref $_[0])."->css_text: Not implemented";
21 wakaba 1.1 } # css_text
22    
23 wakaba 1.2 sub parent_rule ($) {
24     return ${$_[0]}->{parent_rule};
25     } # parent_rule
26    
27     sub parent_style_sheet ($) {
28     if (${$_[0]}->{parent_style_sheet}) {
29     return ${$_[0]}->{parent_style_sheet};
30     } elsif (${$_[0]}->{parent_rule}) {
31     local $Error::Depth = $Error::Depth + 1;
32     return ${$_[0]}->{parent_rule}->parent_style_sheet;
33     } else {
34     ## NOTE: Not in the CSSOM ED: If the |CSSRule| object is not
35     ## yet associated to any CSS style sheet. Such object should not be
36     ## returned to applications - that is, the intention is that only
37     ## modules belonging to manakai may get |undef| from the
38     ## |parent_style_sheet| attribute during the construction of CSSOM.
39     ## Therefore, this is not counted as a manakai extension to CSSOM spec.
40     return undef;
41     }
42     } # parent_style_sheet
43 wakaba 1.1
44     sub type ($) {
45 wakaba 1.3 die "$0: ".(ref $_[0])."->type: Not implemented";
46 wakaba 1.1 } # type
47    
48     package Message::DOM::CSSStyleRule;
49     push our @ISA, 'Message::DOM::CSSRule', 'Message::IF::CSSStyleRule';
50    
51     sub ____new ($$$) {
52 wakaba 1.3 my $self = bless \{_selectors => $_[1], style => $_[2]}, $_[0];
53 wakaba 1.2 ${$_[2]}->{parent_rule} = $self;
54     Scalar::Util::weaken (${$_[2]}->{parent_rule});
55 wakaba 1.1 return $self;
56     } # ____new
57    
58     ## |CSSRule| attributes
59    
60 wakaba 1.3 sub css_text ($;$) {
61     ## TODO: setter
62    
63 wakaba 1.7 ## NOTE: Where and how white space characters are inserted are
64     ## intentionally changed from those in browsers so that properties are
65     ## more prettily printed.
66     ## See <http://suika.fam.cx/gate/2005/sw/cssText> for what browsers do.
67 wakaba 1.3 local $Error::Depth = $Error::Depth + 1;
68 wakaba 1.7 return $_[0]->selector_text . " {\n" . $_[0]->style->css_text . '}';
69 wakaba 1.3 } # css_text
70 wakaba 1.1
71     sub type ($) { Message::DOM::CSSRule::STYLE_RULE }
72    
73     ## |CSSStyleRule| attributes
74    
75 wakaba 1.3 sub selector_text ($;$) {
76     ## TODO: setter
77    
78     ## TODO: Browser-compatible serializer
79 wakaba 1.5 ## TODO: This code does not work for cases where default namespace
80     ## has no namespace prefix declared.
81     my $self = $_[0];
82 wakaba 1.3 require Whatpm::CSS::SelectorsSerializer;
83 wakaba 1.6 return Whatpm::CSS::SelectorsSerializer->serialize_selector_text
84 wakaba 1.7 ($$self->{_selectors}, ${$self->parent_style_sheet}->{_nsmap});
85 wakaba 1.3 } # selector_text
86 wakaba 1.1
87     sub style ($) {
88     return ${$_[0]}->{style};
89     } # style
90    
91     package Message::DOM::CSSCharsetRule;
92     push our @ISA, 'Message::DOM::CSSRule', 'Message::IF::CSSCharsetRule';
93    
94     sub ____new ($$) {
95     return bless \{encoding => $_[1]}, $_[0];
96     } # ____new
97    
98     ## |CSSRule| attributes
99    
100 wakaba 1.4 sub css_text ($;$) {
101     ## TODO: setter
102    
103     ## NOTE: It will be broken if |encoding| contains |"| or |\|, but this
104     ## is what browsers do.
105     return '@charset "'.${$_[0]}->{encoding}.'";';
106     } # css_text
107 wakaba 1.1
108     sub type ($) { Message::DOM::CSSRule::CHARSET_RULE }
109    
110     ## |CSSCharsetRule| attribute
111    
112     sub encoding ($) {
113     return ${$_[0]}->{encoding};
114     } # encoding
115    
116     package Message::DOM::CSSImportRule;
117     push our @ISA, 'Message::DOM::CSSRule', 'Message::IF::CSSImportRule';
118    
119 wakaba 1.9 sub ____new ($$$$$) {
120     my $self = bless \{href => $_[1],
121     base_uri => $_[2], ## Ref to base URI
122     media => \$_[3],
123     style_sheet => $_[4]}, $_[0];
124 wakaba 1.8 require Message::DOM::MediaList;
125     bless $$self->{media}, 'Message::DOM::MediaList';
126 wakaba 1.9 ${$_[4]}->{owner_rule} = $self;
127     Scalar::Util::weaken (${$_[4]}->{owner_rule});
128 wakaba 1.1 return $self;
129     } # ____new
130    
131     ## |CSSRule| attributes
132    
133 wakaba 1.9 sub css_text ($;$) {
134     ## TODO: setter
135    
136     my $self = shift;
137     my $r = '@import url(' . $self->href . ')';
138     my $media = ''.$self->media;
139     if ($media) {
140     $r .= ' ' . $media;
141     }
142     $r .= ';';
143     return $r;
144     } # css_text
145 wakaba 1.1
146     sub type ($) { Message::DOM::CSSRule::IMPORT_RULE }
147    
148     ## |CSSImportRule| attributes
149    
150     sub href ($) {
151     return ${$_[0]}->{href};
152     } # href
153    
154     sub media ($) {
155     return ${$_[0]}->{media};
156     } # media
157    
158     sub style_sheet ($) {
159     return ${$_[0]}->{style_sheet};
160     } # style_sheet
161    
162     package Message::DOM::CSSMediaRule;
163     push our @ISA, 'Message::DOM::CSSRule', 'Message::IF::CSSMediaRule';
164    
165     sub ____new ($$$) {
166 wakaba 1.8 my $self = bless \{media => \$_[1], css_rules => $_[2]}, $_[0];
167     require Message::DOM::MediaList;
168     bless $$self->{media}, 'Message::DOM::MediaList';
169 wakaba 1.1 for (@{$_[2]}) {
170 wakaba 1.2 ${$_}->{parent_rule} = $self;
171     Scalar::Util::weaken (${$_}->{parent_rule});
172 wakaba 1.1 }
173     return $self;
174     } # ____new
175    
176     ## |CSSRule| attributes
177    
178 wakaba 1.8 sub css_text ($;$) {
179     ## TODO: setter
180     my $v = '@media ' . $_[0]->media . " {\n";
181     for (@{${$_[0]}->{css_rules}}) {
182     $v .= $_->css_text . "\n";
183     ## BUG: Browsers do indent.
184     }
185     $v .= "}";
186     return $v;
187     } # css_text
188 wakaba 1.1
189     sub type ($) { Message::DOM::CSSRule::MEDIA_RULE }
190    
191     ## |CSSMediaRule| attributes
192    
193     sub css_rules ($) {
194     require Message::DOM::CSSRuleList;
195     return bless \\($_[0]), 'Message::DOM::CSSRuleList';
196     } # css_rules
197    
198     sub media ($) {
199     return ${$_[0]}->{media};
200     } # media
201    
202     package Message::DOM::CSSFontFaceRule;
203     push our @ISA, 'Message::DOM::CSSRule', 'Message::IF::CSSFontFaceRule';
204    
205     sub ____new ($$) {
206     my $self = bless \{style => $_[1]}, $_[0];
207 wakaba 1.2 ${$_[2]}->{parent_rule} = $self;
208     Scalar::Util::weaken (${$_[2]}->{parent_rule});
209     return $self;
210 wakaba 1.1 } # ____new
211    
212     ## |CSSRule| attributes
213    
214     ## TODO: |css_text|
215    
216     sub type ($) { Message::DOM::CSSRule::FONT_FACE_RULE }
217    
218     ## |CSSFontFaceRule| attribute
219    
220     sub style ($) {
221     return ${$_[0]}->{style};
222     } # style
223    
224     package Message::DOM::CSSPageRule;
225     push our @ISA, 'Message::DOM::CSSRule', 'Message::IF::CSSPageRule';
226    
227     sub ____new ($$$) {
228 wakaba 1.3 my $self = bless \{_selectors => $_[1], style => $_[2]}, $_[0];
229 wakaba 1.2 ${$_[2]}->{parent_rule} = $self;
230     Scalar::Util::weaken (${$_[2]}->{parent_rule});
231 wakaba 1.1 return $self;
232     } # ____new
233    
234     ## |CSSRule| attributes
235    
236     ## TODO: |css_text|
237    
238     sub type ($) { Message::DOM::CSSRule::PAGE_RULE }
239    
240     ## |CSSPageRule| attributes
241    
242 wakaba 1.3 sub selector_text ($;$) {
243     ## TODO: setter
244    
245     ## TODO: Browser-compatible serializer
246     require Whatpm::CSS::SelectorsSerializer;
247     return Whatpm::CSS::SelectorsSerializer->serialize_test (${$_[0]}->{_selectors});
248     } # selector_text
249 wakaba 1.1
250     sub style ($) {
251     return ${$_[0]}->{style};
252     } # style
253    
254     package Message::DOM::CSSNamespaceRule;
255     push our @ISA, 'Message::DOM::CSSRule', 'Message::IF::CSSNamespaceRule';
256    
257     sub ____new ($$$) {
258     return bless \{namespace_uri => $_[2], prefix => $_[1]}, $_[0];
259     } # ___new
260    
261     ## |CSSRule| attributes
262    
263 wakaba 1.5 sub css_text ($;$) {
264     ## TODO: Setter
265    
266     ## NOTE: Sometimes ugly, but this is what Firefox does.
267     my $prefix = ${$_[0]}->{prefix};
268     return '@namespace '.($prefix ne '' ? $prefix.' ' : '').
269     'url('.${$_[0]}->{namespace_uri}.');';
270     } # css_text
271 wakaba 1.1
272     sub type ($) { Message::DOM::CSSRule::NAMESPACE_RULE }
273    
274     ## |CSSNamespaceRule| attributes
275    
276     sub namespace_uri ($) {
277     return ${$_[0]}->{namespace_uri};
278     } # namespace_uri
279    
280     sub prefix ($) {
281     return ${$_[0]}->{prefix};
282     } # prefix
283    
284     package Message::IF::CSSRule;
285     package Message::IF::CSSStyleRule;
286     package Message::IF::CSSCharsetRule;
287     package Message::IF::CSSImportRule;
288     package Message::IF::CSSMediaRule;
289     package Message::IF::CSSFontFaceRule;
290     package Message::IF::CSSPageRule;
291    
292     1;
293 wakaba 1.9 ## $Date: 2008/02/08 15:08:04 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24