DOM.pm

filter: Statementstmt 81.4% 105 / 129 Branchbran 62.9% 34 / 54 Conditioncond 71.4% 10 / 14 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 100% 32 / 32 Podpod 100% 19 / 19 totaltotal 78.9% 202 / 256 CC 34 SCAR 26.6
CC
34
Cov
76%
compute_style
54.9
media
4.2
cssRules
4.2
CRAP 14.4
« DOM/PropertyParser.pm ↑ Summary DOM/Style.pm »
1 package CSS::DOM;
2  
3 23 ▶use 5.008002;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 23 $VERSION = '0.16';
6  
7 use # to keep CPANTS happy :-)
8 23 ▶ strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 use # same here
10 23 ▶ warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 use CSS::DOM::Exception
13 23 ▶ 'SYNTAX_ERR' ,'HIERARCHY_REQUEST_ERR', 'INDEX_SIZE_ERR';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 23 ▶use CSS::DOM::Constants 'STYLE_RULE';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 23 ▶use Scalar::Util 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 23 require CSS::DOM::RuleList;
18  
19 23 use constant 1.03 our $_constants = {
20 ruls => 0,
21 ownr => 1, # owner rule
22 node => 2, # owner node
23 dsbl => 3,
24 hrfe => 4,
25 medi => 5,
26 fetc => 6, # url fetcher
27 prsh => 7, # parent sheet
28 prpp => 8, # property parser
29 23 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
30 23 ▶{ no strict; delete @CSS::DOM::{_constants => keys %{our $_constants}} }
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32  
33 # NON-DOM METHODS
34  
35 # classy method
36 sub new {
37 101 ▶ my $self = bless[],shift;
Subroutine new: called CC=2 SCAR=6.9
Pod documented
38 101 my %args = @_;
39 101 ▶ if(defined(my $arg = delete $args{url_fetcher})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $arg = delete $args{"url_fetcher"})) 9 92 101
40 9 $self->[fetc] = $arg;
41 }
42 101 $self->[prpp] = delete $args{property_parser};
43 101 $self;
44 }
45  
46 # objectionable methods
47 sub url_fetcher {
48 30 ▶ my $old = (my$ self = shift)->[fetc];
Subroutine url_fetcher: called CC=2 SCAR=6.9
Pod documented
49 30 ▶ $ self -> [ fetc ] = shift if @ _ ;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 9 21 30
50 30 $old
51 }
52 87 ▶sub property_parser { shift->[prpp] }
Subroutine property_parser: called CC=1 SCAR=0.0
Pod documented
53  
54  
55 # FUNCTIONS
56  
57 sub parse {
58 65 ▶ require CSS::DOM::Parser;
Subroutine parse: called CC=1 SCAR=0.0
Pod documented
59 65 goto &CSS::DOM::Parser::parse;
60 }
61  
62 sub compute_style {
63 1 ▶ my %args = @_;
Subroutine compute_style: called CC=15 SCAR=40.0
Pod documented
64 # ~~~ for now we just ignore medium/height/width/ppi. We need to
65 # support those, too.
66  
67 1 require CSS::DOM::Style;
68 1 my $style = new CSS::DOM::Style;
69  
70 1 my $elem = delete $args{element};
71 1 my $pseudo = delete $args{pseudo};
72 1 ▶ $pseudo && $pseudo =~ s/^::?//;
Branch: 1 branch
BranchTrueFalseTotal
$pseudo and $pseudo =~ s/^::?// 0 1 1
73
74 # The specificity returned by the style rule is a three-character
75 # string representing the number of id, attr, and elem selector
76 # components (e.g., li.red.level gives "\0\2\1"). We prefix that
77 # with two more chars, to make:
78 # XXXXX
79 # ||||`-- element
80 # |||`-- attribute
81 # ||`-- id
82 # |`-- style attribute
83 # `-- style sheet
84  
85 # ‘Style attribute’ is \1 or \0, indicating whether the CSS proper-
86 # ties originate from a style attribute. ‘Style sheet’ is
87 # as follows:
88 # "\0") user agent normal declarations
89 # "\1") user normal declarations
90 # "\2") author normal "
91 # "\3") user agent !important declarations
92 # "\4") author !important "
93 # "\5") user " "
94  
95 # The individual properties are sorted according to this scheme.
96  
97  
98 # ~~~ This isn’t the most efficient algorithm. Perhaps we can cache
99 # some of this.
100  
101 1 my %specificity; # per property
102  
103 my @normal_spec;
104 1 my @important_spec;
105 1 my @sheets;
106 1 ▶ if(defined $args{ua_sheet}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"ua_sheet"}) 0 1 1
107 0 push @normal_spec, chr 0;
108 0 push @important_spec, chr 3;
109 0 push @sheets, delete $args{ua_sheet};
110 }
111 1 ▶ if(defined $args{user_sheet}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"user_sheet"}) 0 1 1
112 0 push @normal_spec, chr 1;
113 0 push @important_spec, chr 5;
114 0 push @sheets, delete $args{user_sheet};
115 }
116 1 ▶ if(defined $args{author_sheets}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"author_sheets"}) 0 1 1
117 0 my $s = delete $args{author_sheets};
118 0 push @normal_spec, (chr 2) x @$s;
119 0 push @important_spec, (chr 4) x @$s;
120 0 push @sheets, @$s;
121 }
122 1 ▶ while(@sheets) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sheets) 0 1 1
123 0 my $n = shift @normal_spec;
124 0 my $i = shift @important_spec;
125 0 my $s = shift @sheets;
126 0 my @rules = $s->cssRules;
127 0 ▶ while(@rules) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rules) 0 0 0
128 0 my $r = shift @rules;
129 0 my $type = $r->type;
130 0 ▶ if($type == STYLE_RULE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 1) 0 0 0
131 next unless
132 0 ▶ my $specificity = $r->_selector_matches(
Branch: 1 branch
BranchTrueFalseTotal
unless my $specificity = $r->_selector_matches($elem, $pseudo) 0 0 0
133 $elem, $pseudo
134 );
135 0 my $sty = $r->style;
136 0 for(0..$sty->length-1) {
137 0 my $p = $sty->item($_);
138 0 ▶ my $spec = (
Branch: 1 branch
BranchTrueFalseTotal
$sty->getPropertyPriority($p) =~ /^important\z/i ? : 0 0 0
139 $sty->getPropertyPriority($p)
140 =~
141 /^important\z/i
142 ? $i : $n
143 ) . "\0$specificity";
144 23 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
145 $spec ge $specificity{$p} and
146 $style->setProperty(
147 $p, $sty->getPropertyValue($p)
148 ),
149 0 ▶ $specificity{$p} = $spec;
Branch: 1 branch
BranchTrueFalseTotal
$spec ge $specificity{$p} and $style->setProperty($p, $sty->getPropertyValue($p)), $specificity{$p} = $spec 0 0 0
150 }
151 }
152 }
153 }
154
155 1 my $sty = $elem->style;
156 1 for(0..$sty->length-1) {
157 1 my $p = $sty->item($_);
158 1 ▶ my $spec = (
Branch: 1 branch
BranchTrueFalseTotal
$sty->getPropertyPriority($p) =~ /^important\z/i ? : 0 1 1
159 $sty->getPropertyPriority($p)
160 =~
161 /^important\z/i
162 ? "\4" : "\3"
163 ) . "\1\0\0\0";
164 23 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
165 $spec ge $specificity{$p} and
166 $style->setProperty(
167 $p, $sty->getPropertyValue($p)
168 ),
169 1 ▶ $specificity{$p} = $spec;
Branch: 1 branch
BranchTrueFalseTotal
$spec ge $specificity{$p} and $style->setProperty($p, $sty->getPropertyValue($p)), $specificity{$p} = $spec 1 0 1
170 }
171  
172 1 return $style;
173 }
174  
175  
176 # DOM STUFF:
177  
178 # StyleSheet interface:
179  
180 1 ▶sub type { 'text/css' }
Subroutine type: called CC=1 SCAR=0.0
Pod documented
181 sub disabled {
182 4 ▶ my $old = (my $self = shift) ->[dsbl];
Subroutine disabled: called CC=2 SCAR=6.9
Pod documented
183 4 ▶ @_ and $self->[dsbl] = shift;
Branch: 1 branch
BranchTrueFalseTotal
@_ and $self->[3] = shift() 2 2 4
184 4 $old
185 };
186 5 ▶sub ownerNode { defined $_[0][node]?$_[0][node]:() }
Subroutine ownerNode: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0][2] ? : 2 3 5
187 3 ▶sub set_ownerNode { weaken($_[0]->[node] = $_[1]) }
Subroutine set_ownerNode: called CC=1 SCAR=0.0
Pod documented
188 2 ▶sub parentStyleSheet { shift->[prsh]||() }
Subroutine parentStyleSheet: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->[7] or () 1 1 2
189 9 ▶sub _set_parentStyleSheet { weaken($_[0]->[prsh] = $_[1]) }
Subroutine _set_parentStyleSheet: called CC=1 SCAR=0.0
190 1 ▶sub href { shift->[hrfe] }
Subroutine href: called CC=1 SCAR=0.0
Pod documented
191 1 ▶sub set_href { $_[0]->[hrfe] = $_[1] }
Subroutine set_href: called CC=1 SCAR=0.0
Pod documented
192 23 ▶sub title { no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
193 1 ▶ ''.(shift->ownerNode || return)->attr('title') }
Subroutine title: called CC=2 SCAR=7.6
Pod documented
Condition50%1/2
(shift())->ownerNode || (return)
l!l
10
MC/DC0%0/1
(shift())->ownerNode || (return)
(shift())->ownerNode
Truth table50%1/2
A
A= (shift())->ownerNode
Aresult
11
00
194  
195 # If you find a bug in here, Media.pm’s method probably also needs fixing.
196 sub media {
197 3 ▶ wantarray ? @{$_[0]->[medi]||return} :
Subroutine media: called CC=4 SCAR=14.3
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 1 2 3
$_[0][5] or return 0 1 1
Condition66%2/3
$_[0][5] ||= (require CSS::DOM::MediaList, "CSS::DOM::MediaList"->new)
l!l&&r!l&&!r
110
MC/DC0%0/2
$_[0][5] ||= (require CSS::DOM::MediaList, "CSS::DOM::MediaList"->new)
$_[0][5] (require CSS::DOM::MediaList, "CSS::DOM::MediaList"->new)
Truth table66%2/3
A ||= B
A= $_[0][5]B= (require CSS::DOM::MediaList, "CSS::DOM::MediaList"->new)
ABresult
1X1
011
000
198 ($_[0]->[medi] ||= (
199 require CSS::DOM::MediaList,
200 CSS::DOM::MediaList->new
201 ))
202 }
203  
204  
205 # CSSStyleSheet interface:
206  
207 sub ownerRule {
208 3 ▶ shift->[ownr] || ()
Subroutine ownerRule: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->[1] or () 1 2 3
209 }
210 sub _set_ownerRule {
211 10 ▶ weaken($_[0]->[ownr] = $_[1]);
Subroutine _set_ownerRule: called CC=1 SCAR=0.0
212 }
213  
214 # If you find a bug in the following three methods, Media.pm’s methods
215 # probably also need fixing.
216 sub cssRules {
217 wantarray
218 405 ▶ ? @{shift->[ruls]||return}
Subroutine cssRules: called CC=4 SCAR=14.3
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 10 395 405
+(shift())->[0] or return 0 10 10
Condition66%2/3
+(shift())->[0] ||= "CSS::DOM::RuleList"->new
l!l&&r!l&&!r
298970
MC/DC0%0/2
+(shift())->[0] ||= "CSS::DOM::RuleList"->new
+(shift())->[0] "CSS::DOM::RuleList"->new
Truth table66%2/3
A ||= B
A= +(shift())->[0]B= "CSS::DOM::RuleList"->new
ABresult
1X1
011
000
219 : (shift->[ruls]||=new CSS::DOM::RuleList);
220 }
221  
222 sub insertRule { # This is supposed to raise an HIERARCHY_REQUEST_ERR if
223 # the rule cannot be inserted at the specified index;
224 # e.g., if an @import rule is inserted after a stan-
225 # dard rule. But we don’t do that, in order to maintain
226 # future compatibility.
227 136 ▶ my ($self, $rule_string, $index) = @_;
Subroutine insertRule: called CC=4 SCAR=13.9
Pod documented
228
229 136 require CSS::DOM::Parser;
230 136 my ($at,$rule);
231 {
232 136 local *@;
233 136 $rule = CSS::DOM::Parser::parse_statement(
234 $rule_string,$self
235 );
236 136 $at = $@
237 }
238 136 ▶ $at and die new CSS::DOM::Exception SYNTAX_ERR, $at;
Branch: 1 branch
BranchTrueFalseTotal
$at and die "CSS::DOM::Exception"->new(12, $at) 6 130 136
239  
240 # $rule->_set_parentStyleSheet($self);
241  
242 130 my $list = $self->cssRules; # cssRules takes care of ||=
243 130 splice @$list, $index, 0, $rule;
244  
245 130 ▶ $index < 0 ? $#$list + $index :
Branch: 2 branches
BranchTrueFalseTotal
$index < 0 ? : 1 129 130
$index <= $list->$#* ? : 128 1 129
246 $index <= $#$list ? $index :
247 $#$list
248 }
249  
250 sub deleteRule {
251 2 ▶ my ($self,$index) = @_;
Subroutine deleteRule: called CC=2 SCAR=6.9
Pod documented
252 2 my $list = $self->[ruls];
253 2 ▶ $index > $#$list and die CSS::DOM::Exception->new(
Branch: 1 branch
BranchTrueFalseTotal
$index > $list->$#* and die "CSS::DOM::Exception"->new(1, "The index passed to deleteRule ($index) is too large") 1 1 2
254 INDEX_SIZE_ERR,
255 "The index passed to deleteRule ($index) is too large"
256 );
257 1 splice @$list, $index, 1;
258 return # nothing;
259 1 }
260  
261  
262  
263 23 my %features = (
264 stylesheets => { '2.0' => 1 },
265 # css => { '2.0' => 1 },
266 css2 => { '2.0' => 1 },
267 );
268  
269 sub hasFeature {
270 24 ▶ my($feature,$v) = (lc $_[1], $_[2]);
Subroutine hasFeature: called CC=3 SCAR=11.2
Pod documented
271 exists $features{$feature} and
272 24 ▶ !defined $v || exists $features{$feature}{$v};
Branch: 1 branch
BranchTrueFalseTotal
exists $features{$feature} and !defined($v) || exists $features{$feature}{$v} 24 0 24
Condition83%5/6
exists $features{$feature} && (!defined($v) || exists $features{$feature}{$v})
!ll&&!rl&&r
0816
not defined $v or exists $features{$feature}{$v}
l!l&&r!l&&!r
888
MC/DC66%2/3
exists $features{$feature} && (!defined($v) || exists $features{$feature}{$v})
exists $features{$feature} not defined $v exists $features{$feature}{$v}
Truth table75%3/4
A && not(B) or C
A= exists $features{$feature}B= not defined $vC= exists $features{$feature}{$v}
ABCresult
0XX0
1000
11X1
1011
273 }
274  
275 23 !()__END__()!
276  
277 =encoding utf8
278  
279 =head1 NAME
280  
281 CSS::DOM - Document Object Model for Cascading Style Sheets
282  
283 =head1 VERSION
284  
285 Version 0.16
286  
287 This is an alpha version. The API is still subject to change. Many features
288 have not been implemented yet (but patches would be welcome :-).
289  
290 The interface for feeding CSS code to CSS::DOM changed incompatibly in
291 version 0.03.
292  
293 =for comment
294 This is an alpha version. If you could please test it and report any bugs
295 (via e-mail), I would be grateful.
296  
297 =head1 SYNOPSIS
298  
299 use CSS::DOM;
300  
301 my $sheet = CSS::DOM::parse( $css_source );
302  
303 use CSS::DOM::Style;
304 my $style = CSS::DOM::Style::parse(
305 'background: red; font-size: large'
306 );
307  
308 my $other_sheet = new CSS::DOM; # empty
309 $other_sheet->insertRule(
310 'a{ text-decoration: none }',
311 $other_sheet->cssRules->length,
312 );
313 # etc.
314
315 # access DOM properties
316 $other_sheet->cssRules->[0]->selectorText('p'); # change it
317 $style->fontSize; # returns 'large'
318 $style->fontSize('small'); # change it
319  
320 =head1 DESCRIPTION
321  
322 This set of modules provides the CSS-specific interfaces described in the
323 W3C DOM
324 recommendation.
325  
326 The CSS::DOM class itself implements the StyleSheet and CSSStyleSheet DOM
327 interfaces.
328  
329 This set of modules has two modes:
330  
331 =over
332  
333 =item 1
334  
335 It can validate property values,
336 ignoring those that are invalid (just like a real web browser), and support shorthand
337 properties. This means you can set font to '13px/15px My Font' and have the
338 font-size, line-height, and font-family properties (among others) set automatically. Also, C<color: green; color: kakariki> will assign 'green'
339 to the color
340 property, 'kakariki' not being a recognised color value.
341  
342 =item 2
343  
344 It can
345 blithely accept all property assignments as being valid. In the case of
346 C<color: green; color kakariki>, 'kakariki' will be assigned, since it overrides the previous
347 assignment.
348  
349 =back
350  
351 These two modes are controlled by the C<property_parser> option to the
352 constructors.
353  
354 =head1 CONSTRUCTORS
355  
356 =over 4
357  
358 =item CSS::DOM::parse( $string )
359  
360 This method parses the C<$string> and returns a style sheet object. If you
361 just have a CSS style declaration, e.g., from an HTML C<style> attribute,
362 see L<CSS::DOM::Style/parse>.
363  
364 =item new CSS::DOM
365  
366 Creates a new, empty style sheet object. Use this only if you plan to build
367 the style sheet piece by piece, instead of parsing a block of CSS code.
368  
369 =back
370  
371 You can pass named arguments to both of those. C<parse> accepts all of
372 them; C<new> understands only the first two, C<property_parser> and
373 C<url_fetcher>.
374  
375 =over
376  
377 =item property_parser
378  
379 Set this to a L<PropertyParser|CSS::DOM::PropertyParser> object to specify
380 which properties are supported and how they are parsed.
381  
382 If this option is not specified or is set to C<undef>, all property
383 values are treated as valid.
384  
385 See L<CSS::DOM::PropertyParser> for more details.
386  
387 =item url_fetcher
388  
389 This has to be a code ref that returns the contents
390 of the style sheet at the URL passed as the sole argument. E.g.,
391  
392 # Disclaimer: This does not work with relative URLs.
393 use LWP::Simple;
394 use CSS::DOM;
395 $css = '@import "file.css"; /* other stuff ... ';
396 $ss = CSS::DOM::parse $css, url_fetcher => sub { get shift };
397 $ss->cssRules->[0]->styleSheet; # returns a style sheet object
398 # corresponding to file.css
399  
400 The subroutine can choose to return C<undef> or an empty list, in which
401 case the @import
402 rule's C<styleSheet> method will return null (empty list or C<undef>), as
403 it would if no C<url_fetcher> were specified.
404  
405 It can also return named items after the CSS code, like this:
406  
407 return $css_code, decode => 1, encoding_hint => 'iso-8859-1';
408  
409 These correspond to the next two items:
410  
411 =item decode
412  
413 If this is specified and set to a true value, then CSS::DOM will treat the
414 CSS code as a string of bytes, and try to decode it based on @charset rules
415 and byte order marks.
416  
417 By default it assumes that it is already in Unicode (i.e., decoded).
418  
419 =item encoding_hint
420  
421 Use this to provide a hint as to what the encoding might be.
422  
423 If this is specified, and C<decode> is not, then C<< decode => 1 >> is
424 assumed.
425  
426 =back
427  
428 =head1 STYLE SHEET ENCODING
429  
430 See the options above. This section explains how and when you I<should> use
431 those options.
432  
433 According to the CSS spec, any encoding specified in the 'charset' field on
434 an HTTP Content-Type header, or the equivalent in other protocols, takes
435 precedence. In such a case, since CSS::DOM doesn't deal with HTTP, you have
436 to decode it yourself.
437  
438 Otherwise, you should use C<< decode => 1 >> to instruct CSS::DOM to use
439 byte order marks or @charset rules.
440  
441 If neither of those is present, then encoding data in the referencing
442 document (e.g., <link charset="..."> or an HTML document's own encoding),
443 if available/applicable, should be used. In this case, you should use the
444 C<< encoding_hint >> option, so that CSS::DOM has something to fall back
445 to.
446  
447 If you use C<< decode => 1 >> with no encoding hint, and no BOM or @charset
448 is to be found, UTF-8 is assumed.
449  
450 =head1 SYNTAX ERRORS
451  
452 The two constructors above, and also
453 L<C<CSS::DOM::Style::parse>|CSS::DOM::Style/parse>, set C<$@> to the empty
454 string upon success. If
455 they
456 encounter a syntax error, they set C<$@> to the error and return an object
457 that represents whatever was parsed up to that point.
458  
459 Other methods that parse CSS code might die on encountering
460 syntax errors, and should usually be wrapped in an C<eval>.
461  
462 The parser follows the 'future-compatible' syntax described in the CSS 2.1
463 specification, and also the spec's rules for handling parsing errors.
464 Anything not handled by those two is a syntax error.
465  
466 In other words, a syntax error is one of the following:
467  
468 =over 4
469  
470 =item *
471  
472 An unexpected closing bracket, as
473 in these examples
474  
475 a { text-decoration: none )
476 *[name=~'foo'} {}
477 #thing { clip: rect( ]
478  
479 =item *
480  
481 An HTML comment delimiter within a rule; e.g.,
482  
483 a { text-decoration : none <!-- /* Oops! */ }
484 <!-- /*ok*/ @media --> /* bad! */ print { }
485  
486 =item *
487  
488 An extra C<@> keyword or semicolon where it doesn't belong; e.g.,
489  
490 @media @print { .... }
491 @import "file.css" @print;
492 td, @page { ... }
493 #tabbar td; #tab1 { }
494  
495 =back
496  
497 =head1 OBJECT METHODS
498  
499 =head2 Attributes
500  
501 =over 4
502  
503 =item type
504  
505 Returns the string 'text/css'.
506  
507 =item disabled
508  
509 Allows one to specify whether the style sheet is used. (This attribute is
510 not actually used yet by CSS::DOM.) You can set it by passing an argument.
511  
512 =item ownerNode
513  
514 Returns the node that 'owns' this style sheet.
515  
516 =item parentStyleSheet
517  
518 If the style sheet belongs to an '@import' rule, this returns the style
519 sheet containing that rule. Otherwise it returns an empty list.
520  
521 =item href
522  
523 Returns the style sheet's URI, if applicable.
524  
525 =item title
526  
527 Returns the value of the owner node's title attribute.
528  
529 =item media
530  
531 Returns the MediaList associated with the style sheet (or a plain list in
532 list context). This defaults to an
533 empty list. You can pass a comma-delimited string to the MediaList's
534 C<mediaText> method to initialise it.
535  
536 (The medium information is not actually used [yet] by CSS::DOM, but you
537 can put it there.)
538  
539 =item ownerRule
540  
541 If this style sheet was created by an @import rule, this returns the rule;
542 otherwise it returns an empty list (or undef in scalar context).
543  
544 =item cssRules
545  
546 In scalar context, this returns a L<CSS::DOM::RuleList> object (simply a
547 blessed
548 array reference) of L<CSS::DOM::Rule> objects. In list context it returns a
549 list.
550  
551 =back
552  
553 =head2 Methods
554  
555 =over 4
556  
557 =item insertRule ( $css_code, $index )
558  
559 Parses the rule contained in the C<$css_code>, inserting it in the style
560 sheet's list of rules at the given C<$index>.
561  
562 =item deleteRule ( $index )
563  
564 Deletes the rule at the given C<$index>.
565  
566 =item hasFeature ( $feature, $version )
567  
568 You can call this either as an object or class method.
569  
570 This is actually supposed to be a method of the 'DOMImplementation' object.
571 (See, for instance, L<HTML::DOM::Interface>'s method of the same name,
572 which delegates to this one.) This returns a boolean indicating whether a
573 particular DOM module is implemented. Right now it returns true only for
574 the 'CSS2' and 'StyleSheets' features (version '2.0').
575  
576 =back
577  
578 =head2 Non-DOM Methods
579  
580 =over 4
581  
582 =item set_ownerNode
583  
584 This allows you to set the value of C<ownerNode>. Passing an argument to
585 C<ownerNode> does nothing, because it is supposed to be read-only. But you
586 have to be able to set it somehow, so that's why this method is here.
587  
588 The style sheet will hold a weak reference to the object passed to this
589 method.
590  
591 =item set_href
592  
593 Like C<set_ownerNode>, but for C<href>.
594  
595 =item property_parser
596  
597 =item url_fetcher
598  
599 These two both return what was passed to the constructor. The second one,
600 C<url_fetcher> also allows an assignment, but this is not propagated to
601 sub-rules and is intended mainly for internal use.
602  
603 =back
604  
605 =head1 FUNCTIONS
606  
607 =over
608  
609 =item CSS::DOM::parse
610  
611 See L</CONSTRUCTORS>, above.
612  
613 =item CSS::DOM::compute_style( %options )
614  
615 B<Warning:> This is still highly experimental and crawling with bugs.
616  
617 This computes the style for a given HTML element. It does not yet calculate
618 actual measurements (e.g., converting percentages to pixels), but simply
619 applies the cascading rules and selectors. Pseudo-classes are
620 not yet supported (but pseudo-elements are).
621  
622 The precedence rules for normal vs important declarations in the CSS 2
623 specification are used. (CSS 2.1 is unclear.) The precedence is as follows,
624 from lowest to highest:
625  
626 user agent normal declarations
627 user normal declarations
628 author normal "
629 user agent !important declarations
630 author !important "
631 user " "
632  
633 The C<%options> are as follows. They are all optional except for
634 C<element>.
635  
636 =over
637  
638 =item ua_sheet
639  
640 The user agent style sheet
641  
642 =item user_sheet
643  
644 The user style sheet
645  
646 =item author_sheets
647  
648 Array ref of style sheets that the HTML document defines or links to.
649  
650 =item element
651  
652 The element, as an L<HTML::DOM::Element> object.
653  
654 =item pseudo
655  
656 The pseudo-element (e.g., 'first-line'). This can be specified with no
657 colons (the way Opera
658 requires it) or
659 with one or two colons (the way Firefox requires it).
660  
661 =item medium
662  
663 =item height
664  
665 =item width
666  
667 =item ppi
668  
669 (To be implemented)
670  
671 =back
672  
673 The
674  
675 =back
676  
677 =head1 CLASSES AND DOM INTERFACES
678  
679 Here are the inheritance hierarchy of CSS::DOM's various classes and the
680 DOM interfaces those classes implement. For brevity's sake, a simple '::'
681 at the beginning of a class name in the left column is used for
682 'CSS::DOM::'. Items in brackets do not exist yet. (See also
683 L<CSS::DOM::Interface> for a
684 machine-readable list of standard methods.)
685  
686 Class Inheritance Hierarchy Interfaces
687 --------------------------- ----------
688
689 CSS::DOM StyleSheet, CSSStyleSheet
690 ::Array
691 ::MediaList MediaList
692 ::StyleSheetList StyleSheetList
693 ::RuleList CSSRuleList
694 ::Rule CSSRule, CSSUnknownRule
695 ::Rule::Style CSSStyleRule
696 ::Rule::Media CSSMediaRule
697 ::Rule::FontFace CSSFontFaceRule
698 ::Rule::Page CSSPageRule
699 ::Rule::Import CSSImportRule
700 ::Rule::Charset CSSCharsetRule
701 ::Style CSSStyleDeclaration, CSS2Properties
702 ::Value CSSValue
703 ::Value::Primitive CSSPrimitiveValue, RGBColor, Rect
704 ::Value::List CSSValueList
705 [::Counter Counter]
706  
707 CSS::DOM does not implement the following interfaces (see L<HTML::DOM> for
708 these):
709  
710 LinkStyle
711 DocumentStyle
712 ViewCSS
713 DocumentCSS
714 DOMImplementationCSS
715 ElementCSSInlineStyle
716  
717 =head1 IMPLEMENTATION NOTES
718  
719 =over 4
720  
721 =item *
722  
723 Attributes of objects are accessed via methods of the same name. When the
724 method
725 is invoked, the current value is returned. If an argument is supplied, the
726 attribute is set (unless it is read-only) and its old value returned.
727  
728 =item *
729  
730 Where the DOM spec. says to use null, undef or an empty list is used.
731  
732 =item *
733  
734 Instead of UTF-16 strings, CSS::DOM uses Perl's Unicode strings.
735  
736 =item *
737  
738 Each method that the specification says returns an array-like object (e.g.,
739 a RuleList) will return such an object in scalar context, or a simple list
740 in list context. You can use
741 the object as an array ref in addition to calling its C<item> and
742 C<length> methods.
743  
744 =begin for-me
745  
746 If I implement any methods that make use of the DOMTimeStamp interface, I
747 need to document that simple Perl scalars containing the time as returned
748 by Perl’s built-in ‘time’ function are used.
749  
750 =end for-me
751  
752 =back
753  
754 =head1 PREREQUISITES
755  
756 perl 5.8.2 or higher
757  
758 L<Exporter> 5.57 or later
759  
760 L<Encode> 2.10 or higher
761  
762 L<Clone> 0.09 or higher
763  
764 =head1 BUGS
765  
766 The parser has not been updated to conform to the April 2009 revision of
767 the CSS 2.1 candidate recommendation. Specifically, unexpected closing
768 brackets are not ignored, but cause syntax errors; and @media rules
769 containing unrecognised statements are themselves currently treated as
770 unrecognised (the unrecognised inner statements should be ignored,
771 rendering the outer @media rule itself valid).
772  
773 If you create a custom property parser that defines
774 'list-style-type' to include multiple tokens, then counters will become
775 C<CSS_CUSTOM> CSSValue objects instead of C<CSS_COUNTER> CSSPrimitiveValue
776 objects.
777  
778 If you change a property parser's property definitions such that a
779 primitive value becomes a list, or vice versa, and then try to modify the
780 C<cssText> property of an existing value object belonging to that property,
781 things will go awry.
782  
783 =for comment
784 This is because we can’t change a list into a prim and vice versa, because
785 one is a hash and the other is an array.
786  
787 Whitespace and comments are sometimes preserved in serialised CSS and
788 sometimes not.
789 Expect inconsistency.
790  
791 To report bugs, please e-mail the author.
792  
793 =head1 ACKNOWLEDGEMENTS
794  
795 Thanks to Ville Skyttä, Nicholas Bamber and Gregor Herrmann for their
796 contributions.
797  
798 =head1 AUTHOR & COPYRIGHT
799  
800 Copyright (C) 2007-15 Father Chrysostomos <sprout [at] cpan
801 [dot] org>
802  
803 This program is free software; you may redistribute it and/or modify
804 it under the same terms as perl. The full text of the license can be found
805 in the LICENSE file included with this module.
806  
807 =head1 SEE ALSO
808  
809 All the classes listed above under L</CLASSES AND DOM INTERFACES>.
810  
811 L<CSS::SAC>, L<CSS.pm|CSS> and L<HTML::DOM>
812  
813 The DOM Level 2 Style specification at
814 S<L<http://www.w3.org/TR/DOM-Level-2-Style>>
815  
816 The CSS 2.1 specification at S<L<http://www.w3.org/TR/CSS21/>>
« DOM/PropertyParser.pm ↑ Summary DOM/Style.pm »