SAC.pm

filter: Statementstmt 58.5% 284 / 485 Branchbran 48.9% 142 / 290 Conditioncond 42.1% 27 / 64 MC/DCmcdc 25.0% 9 / 36 Subroutinesub 81.8% 27 / 33 Podpod 94.7% 18 / 19 totaltotal 54.6% 507 / 927 CC 171 SCAR 48.2
CC
171
Cov
54%
parse_property_value
420.0
parse
252.4
parse_simple_selector
118.7
CRAP 123.9
« SAC/Writer.pm ↑ Summary SAC/Selector/ProcessingInstruction.pm »
1
2 ###
3 # CSS::SAC - a SAC implementation for Perl
4 # Robin Berjon <robin@knowscape.com>
5 # 17/08/2001 - bugfixes...
6 # 23/04/2001 - more enhancements
7 # 19/03/2001 - second version, various suggestions and enhancements
8 # 24/02/2001 - prototype mark I of the new model
9 ###
10
11 package CSS::SAC;
12 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use vars qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
14 $VERSION
15 $RE_STRING
16 $RE_NAME
17 $RE_IDENT
18 $RE_RANGE
19 $RE_NUM
20 %DIM_MAP
21 %FUNC_MAP
22 2 );
23 2 $VERSION = '0.08';
24
25 2 ▶use CSS::SAC::ConditionFactory qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use CSS::SAC::SelectorFactory qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use CSS::SAC::LexicalUnit qw(:constants);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use CSS::SAC::Selector::Sibling qw(:constants);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶use CSS::SAC::SelectorList qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
30
31 2 ▶use Text::Balanced qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
32
33 2 ▶use constant DEBUG => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
34
35 #---------------------------------------------------------------------#
36 # build a few useful regexen and maps
37 #---------------------------------------------------------------------#
38
39 # matches a quoted string
40 2 $RE_STRING = Text::Balanced::gen_delimited_pat(q{'"}); #"
41 2 $RE_STRING = qr/$RE_STRING/s;
42
43 # matches a name token
44 2 $RE_NAME = qr/
45 (?:(?:\\(?:(?:[a-fA-F0-9]{1,6}[\t\x20])|[\x32-\xff]))|[a-zA-Z\x80-\xff0-9-])+
46 /xs;
47
48 # matches a valid CSS ident (this may be wrong, needs testing)
49 2 $RE_IDENT = qr/
50 (?:(?:\\(?:(?:[a-fA-F0-9]{1,6}[\t\x20])|[ \x32-\xff]))|[a-zA-Z\x80-\xff])
51 (?:(?:\\(?:(?:[a-fA-F0-9]{1,6}[\t\x20])|[ \x32-\xff]))|[a-zA-Z\x80-\xff0-9_-])*
52 /xs;
53
54 # matches a unicode range
55 2 $RE_RANGE = qr/(?:
56 (?:U\+)
57 (?:
58 (?:[0-9a-fA-F]{1,6}-[0-9a-fA-F]{1,6})
59 |
60 (?:\?{1,6})
61 |
62 (?:[0-9a-fA-F](?:
63 (?:\?{0,5}|[0-9a-fA-F])(?:
64 (?:\?{0,4}|[0-9a-fA-F])(?:
65 (?:\?{0,3}|[0-9a-fA-F])(?:
66 (?:\?{0,2}|[0-9a-fA-F])(?:
67 (?:\?{0,1}|[0-9a-fA-F])))))))
68 )
69 )
70 /xs;
71
72
73 # matches a number
74 2 $RE_NUM = qr/(?:(?:[0-9]*\.[0-9]+)|(?:[0-9]+))/;
75
76
77 # maps a length or assoc value to it's constant
78 2 %DIM_MAP = (
79 em => EM,
80 ex => EX,
81 px => PIXEL,
82 cm => CENTIMETER,
83 mm => MILLIMETER,
84 in => INCH,
85 pt => POINT,
86 pc => PICA,
87 deg => DEGREE,
88 rad => RADIAN,
89 grad => GRADIAN,
90 ms => MILLISECOND,
91 s => SECOND,
92 hz => HERTZ,
93 khz => KILOHERTZ,
94 '%' => PERCENTAGE,
95 );
96
97 # maps a length or assoc value to it's constant
98 2 %FUNC_MAP = (
99 attr => ATTR,
100 counter => COUNTER_FUNCTION,
101 counters => COUNTERS_FUNCTION,
102 rect => RECT_FUNCTION,
103 url => URI,
104 rgb => RGBCOLOR,
105 );
106
107 #---------------------------------------------------------------------#
108
109
110 #---------------------------------------------------------------------#
111 # build the fields for an array based object
112 #---------------------------------------------------------------------#
113 2 ▶use Class::ArrayObjects define => {
Subroutine BEGIN: called CC=1 SCAR=0.0
114 fields => [qw(
115 _cf_
116 _sf_
117 _dh_
118 _eh_
119 _dh_can_
120 _allow_charset_
121 _ns_map_
122 _tmp_media_
123 )],
124 2 };
125 #---------------------------------------------------------------------#
126
127
128
129
130 ### Constructor #######################################################
131 # #
132 # #
133
134
135 #---------------------------------------------------------------------#
136 # CSS::SAC->new(\%options)
137 # creates a new sac parser
138 #---------------------------------------------------------------------#
139 2 ▶sub new {
Subroutine new: called CC=6 SCAR=19.1
Pod documented
140 2 my $class = shift;
141 2 ▶ my $options = shift || {};
Condition100%2/2
shift() || {}
l!l
11
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
142
143 # set our options
144 2 my $self = bless [], $class;
145 2 ▶ $self->[_cf_] = $options->{ConditionFactory} || CSS::SAC::ConditionFactory->new;
Condition33%1/3
$options->{'ConditionFactory'} || 'CSS::SAC::ConditionFactory'->new
l!l&&r!l&&!r
020
MC/DC0%0/2
$options->{'ConditionFactory'} || 'CSS::SAC::ConditionFactory'->new
$options->{'ConditionFactory'} 'CSS::SAC::ConditionFactory'->new
Truth table33%1/3
A || B
A= $options->{'ConditionFactory'}B= 'CSS::SAC::ConditionFactory'->new
ABresult
1X1
011
000
146 2 ▶ $self->[_sf_] = $options->{SelectorFactory} || CSS::SAC::SelectorFactory->new;
Condition33%1/3
$options->{'SelectorFactory'} || 'CSS::SAC::SelectorFactory'->new
l!l&&r!l&&!r
020
MC/DC0%0/2
$options->{'SelectorFactory'} || 'CSS::SAC::SelectorFactory'->new
$options->{'SelectorFactory'} 'CSS::SAC::SelectorFactory'->new
Truth table33%1/3
A || B
A= $options->{'SelectorFactory'}B= 'CSS::SAC::SelectorFactory'->new
ABresult
1X1
011
000
147 2 ▶ $self->[_eh_] = $options->{ErrorHandler} || CSS::SAC::DefaultErrorHandler->new;
Condition33%1/3
$options->{'ErrorHandler'} || 'CSS::SAC::DefaultErrorHandler'->new
l!l&&r!l&&!r
020
MC/DC0%0/2
$options->{'ErrorHandler'} || 'CSS::SAC::DefaultErrorHandler'->new
$options->{'ErrorHandler'} 'CSS::SAC::DefaultErrorHandler'->new
Truth table33%1/3
A || B
A= $options->{'ErrorHandler'}B= 'CSS::SAC::DefaultErrorHandler'->new
ABresult
1X1
011
000
148 2 $self->[_dh_can_] = {};
149 2 ▶ $self->DocumentHandler($options->{DocumentHandler}) if $options->{DocumentHandler};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"DocumentHandler"} 1 1 2
150
151 2 return $self;
152 }
153 #---------------------------------------------------------------------#
154
155
156 # #
157 # #
158 ### Constructor #######################################################
159
160
161
162 ### Accessors #########################################################
163 # #
164 # #
165
166
167 #---------------------------------------------------------------------#
168 # $sac->ParserVersion
169 # returns the supported CSS version
170 #---------------------------------------------------------------------#
171 1 ▶sub ParserVersion {
Subroutine ParserVersion: called CC=1 SCAR=0.0
Pod undocumented
172 # IMP
173 # this should perhaps return http://www.w3.org/TR/REC-CSS{1,2,3}
174 # as per http://www.w3.org/TR/SAC, but it's tricky for CSS3 which
175 # is modularized
176 1 return 'CSS3';
177 }
178 #---------------------------------------------------------------------#
179 2 *CSS::SAC::getParserVersion = \&ParserVersion;
180
181
182 #---------------------------------------------------------------------#
183 # my $cf = $sac->ConditionFactory
184 # $sac->ConditionFactory($cf)
185 # get/set the ConditionFactory that we use
186 #---------------------------------------------------------------------#
187 1 ▶sub ConditionFactory {
Subroutine ConditionFactory: called CC=2 SCAR=7.6
Pod documented
188 1 ▶ (@_==2) ? $_[0]->[_cf_] = $_[1] :
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 ? : 0 1 1
189 $_[0]->[_cf_];
190 }
191 #---------------------------------------------------------------------#
192 2 *CSS::SAC::setConditionFactory = \&ConditionFactory;
193
194
195 #---------------------------------------------------------------------#
196 # my $sf = $sac->SelectorFactory
197 # $sac->SelectorFactory($sf)
198 # get/set the SelectorFactory that we use
199 #---------------------------------------------------------------------#
200 1 ▶sub SelectorFactory {
Subroutine SelectorFactory: called CC=2 SCAR=7.6
Pod documented
201 1 ▶ (@_==2) ? $_[0]->[_sf_] = $_[1] :
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 ? : 0 1 1
202 $_[0]->[_sf_];
203 }
204 #---------------------------------------------------------------------#
205 2 *CSS::SAC::setSelectorFactory = \&SelectorFactory;
206
207
208 #---------------------------------------------------------------------#
209 # my $dh = $sac->DocumentHandler
210 # $sac->DocumentHandler($dh)
211 # get/set the DocumentHandler that we use
212 #---------------------------------------------------------------------#
213 1 ▶sub DocumentHandler {
Subroutine DocumentHandler: called CC=3 SCAR=11.0
Pod documented
214 1 my $sac = shift;
215 1 my $dh = shift;
216
217 # set the doc handler, and see what it can do
218 1 ▶ if ($dh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dh) 1 0 1
219 1 $sac->[_dh_] = $dh;
220
221 1 my @dh_methods = qw(
222 comment
223 charset
224 end_document
225 end_font_face
226 end_media
227 end_page
228 end_selector
229 ignorable_at_rule
230 import_style
231 namespace_declaration
232 property
233 start_document
234 start_font_face
235 start_media
236 start_page
237 start_selector
238 );
239 1 for my $meth (@dh_methods) {
240 16 $sac->[_dh_can_]->{$meth} = $dh->can($meth);
241 }
242 }
243
244 1 return $sac->[_dh_];
245 }
246 #---------------------------------------------------------------------#
247 2 *CSS::SAC::setDocumentHandler = \&DocumentHandler;
248
249
250 #---------------------------------------------------------------------#
251 # my $eh = $sac->ErrorHandler
252 # $sac->ErrorHandler($eh)
253 # get/set the ErrorHandler that we use
254 #---------------------------------------------------------------------#
255 1 ▶sub ErrorHandler {
Subroutine ErrorHandler: called CC=2 SCAR=7.6
Pod documented
256 1 ▶ (@_==2) ? $_[0]->[_eh_] = $_[1] :
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 ? : 0 1 1
257 $_[0]->[_eh_];
258 }
259 #---------------------------------------------------------------------#
260 2 *CSS::SAC::setErrorHandler = \&ErrorHandler;
261
262
263 # #
264 # #
265 ### Accessors #########################################################
266
267
268
269
270 ### Parsing Methods ###################################################
271 # #
272 # #
273
274
275 #---------------------------------------------------------------------#
276 # $sac->parse(\%options)
277 # parses a style sheet
278 #---------------------------------------------------------------------#
279 1 ▶sub parse {
Subroutine parse: called CC=29 SCAR=55.3
Pod documented
280 1 my $sac = shift;
281 1 my $options = shift;
282
283 # we always load the style sheet into memory because style sheets
284 # are usually small. If this is a problem we'll change that.
285 1 my $css;
286 1 ▶ if ($options->{string}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($options->{'string'}) { } 1 0 1
elsif ($options->{'ioref'}) { } 0 0 0
elsif ($options->{'filename'}) { } 0 0 0
287 1 $css = $options->{string};
288 }
289 elsif ($options->{ioref}) {
290 0 my $io = $options->{ioref};
291 0 local $/ = undef;
292 0 $css = <$io>;
293 0 close $io;
294 }
295 elsif ($options->{filename}) {
296 0 ▶ open CSS, "$options->{filename}" or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open CSS, qq[$options->{"filename"}] or die $! 0 0 0
297 0 local $/ = undef;
298 0 $css = <CSS>;
299 0 close CSS;
300 }
301 else {
302 0 return undef;
303 }
304
305 ### look at the other options
306 # charsets are forbidden in embedded style sheets
307 1 ▶ if ($options->{embedded}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{'embedded'}) { } 0 1 1
308 0 $sac->[_allow_charset_] = 0;
309 }
310 else {
311 1 $sac->[_allow_charset_] = 1;
312 }
313
314
315
316 #---> Start Parsing <---------------------------------------------#
317
318 # start doc
319 1 warn "[SAC] start parsing\n" if DEBUG;
320 1 ▶ $sac->[_dh_]->start_document if $sac->[_dh_can_]->{start_document};
Branch: 1 branch
BranchTrueFalseTotal
if $sac->[4]{"start_document"} 0 1 1
321
322 # before anything else occurs there can be a charset
323 1 warn "[SAC] parsing charset\n" if DEBUG;
324 1 $sac->parse_charset(\$css);
325 1 $sac->[_allow_charset_] = 0;
326
327 # remove an eventual HTML open comment (not reported to handler)
328 1 warn "[SAC] removing HTML comments\n" if DEBUG;
329 1 $css =~ s/^\s*<!--//;
330
331 # parse some possible comments
332 1 warn "[SAC] parsing comments\n" if DEBUG;
333 1 $sac->parse_comments(\$css);
334
335 # parse some possible imports
336 1 warn "[SAC] parsing imports\n" if DEBUG;
337 1 $sac->parse_imports(\$css);
338
339 # parse some possible ns declarations
340 1 warn "[SAC] parsing ns decl\n" if DEBUG;
341 1 $sac->parse_namespace_declarations(\$css);
342
343 # enter the main parsing loop
344 # what we can have right now is comments, at-rules and selectors
345 1 ▶ while (length($css)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $css) 45 0 45
346
347 # parse some possible comments
348 45 warn "[SAC] parsing comments\n" if DEBUG;
349 45 $sac->parse_comments(\$css);
350
351 # if we've got a closing block, it's a closing @media
352 45 ▶ if ($css =~ s/^\s*\}//) {
Branch: 5 branches
BranchTrueFalseTotal
if ($css =~ s/^\s*\}//) { } 0 45 45
elsif ($css =~ /^\s*\@/) { } 0 45 45
elsif ($css =~ s/^\s*-->\s*//) { } 0 45 45
elsif (my $sel_list = $sac->parse_selector_list(\$css)) { } 44 1 45
elsif ($css =~ s/^\s+//) { } 0 1 1
353 0 warn "[SAC] closing media rule\n" if DEBUG;
354 0 ▶ $sac->[_dh_]->end_media($sac->[_tmp_media_]) if $sac->[_dh_can_]->{end_media};
Branch: 1 branch
BranchTrueFalseTotal
if $sac->[4]{"end_media"} 0 0 0
355 0 $sac->[_tmp_media_] = undef;
356 }
357
358 # there's an at-rule coming up
359 elsif ($css =~ m/^\s*\@/) {
360
361 # @media
362 0 ▶ if ($css =~ s/^\s*\@media\s+//i) {
Branch: 3 branches
BranchTrueFalseTotal
if ($css =~ s/^\s*\@media\s+//i) { } 0 0 0
elsif ($css =~ s/^\s*\@font-face\s+//i) { } 0 0 0
elsif ($css =~ s/^\s*\@page\s+//i) { } 0 0 0
363 0 warn "[SAC] parsing media\n" if DEBUG;
364 0 my $medialist = $sac->parse_medialist(\$css);
365 0 $sac->[_tmp_media_] = $medialist;
366 0 ▶ $sac->[_dh_]->start_media($medialist) if $sac->[_dh_can_]->{start_media};
Branch: 1 branch
BranchTrueFalseTotal
if $sac->[4]{"start_media"} 0 0 0
367 0 $css =~ s/^\s*{//;
368 }
369
370 # @font-face
371 elsif ($css =~ s/^\s*\@font-face\s+//i) {
372 0 warn "[SAC] parsing font-face\n" if DEBUG;
373 # parse the block
374 0 my $rule;
375 0 ($rule,$css,undef) = Text::Balanced::extract_bracketed($css,q/{}'"/,qr/\s*/); #"
376 0 ▶ $sac->[_dh_]->start_font_face if $sac->[_dh_can_]->{start_font_face};
Branch: 1 branch
BranchTrueFalseTotal
if $sac->[4]{"start_font_face"} 0 0 0
377 0 $sac->parse_rule(\$rule);
378 0 ▶ $sac->[_dh_]->end_font_face if $sac->[_dh_can_]->{end_font_face};
Branch: 1 branch
BranchTrueFalseTotal
if $sac->[4]{"end_font_face"} 0 0 0
379 }
380
381 # @page
382 elsif ($css =~ s/^\s*\@page\s+//i) {
383 0 warn "[SAC] parsing page\n" if DEBUG;
384 # grab the name and pseudo-page if they're there
385 0 $css =~ s/^($RE_IDENT)?\s*(?::($RE_IDENT))?//;
386 0 my ($name,$pseudo) = ($1,$2);
387
388 # parse the block
389 0 my $rule;
390 0 ($rule,$css,undef) = Text::Balanced::extract_bracketed($css,q/{}'"/,qr/\s*/); #"
391 0 ▶ $sac->[_dh_]->start_page($name,$pseudo) if $sac->[_dh_can_]->{start_page};
Branch: 1 branch
BranchTrueFalseTotal
if $sac->[4]{"start_page"} 0 0 0
392 0 $sac->parse_rule(\$rule);
393 0 ▶ $sac->[_dh_]->end_page($name,$pseudo) if $sac->[_dh_can_]->{end_page};
Branch: 1 branch
BranchTrueFalseTotal
if $sac->[4]{"end_page"} 0 0 0
394 }
395
396 # unknown
397 # this is a little harder because we have to guess what
398 # at rules that we know nothing about will be like
399 else {
400 0 my $at_rule;
401 0 warn "[SAC] parsing unknown at rule ($css)\n" if DEBUG;
402
403 # take off the @rule first
404 0 $css =~ s/^\s*(\@$RE_IDENT\s*)//;
405 0 $at_rule .= $1;
406
407 # then grab whatever is not a block
408 0 $css =~ s/^([^;{]*)//;
409 0 $at_rule .= $1;
410
411 # now we either terminate, or have a block
412 0 ▶ if ($css =~ s/^;//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($css =~ s/^;//) { } 0 0 0
413 0 $at_rule .= ';';
414 }
415 else {
416 0 my $block;
417 0 ($block,$css,undef) = Text::Balanced::extract_bracketed($css,q/{}'"/,qr/\s*/); #"
418 0 $at_rule .= $block;
419 }
420
421 0 ▶ $sac->[_dh_]->ignorable_at_rule($at_rule) if $sac->[_dh_can_]->{ignorable_at_rule};
Branch: 1 branch
BranchTrueFalseTotal
if $sac->[4]{"ignorable_at_rule"} 0 0 0
422 }
423 }
424
425 # html end comment
426 elsif ($css =~ s/^\s*-->\s*//) {
427 # we don't do anything with those presently
428 0 warn "[SAC] removing HTML comments\n" if DEBUG;
429 }
430
431 # we have selectors
432 elsif (my $sel_list = $sac->parse_selector_list(\$css)) {
433 44 warn "[SAC] parsed selectors\n" if DEBUG;
434 44 ▶ next unless @$sel_list;
Branch: 1 branch
BranchTrueFalseTotal
unless @$sel_list 0 44 44
435 # callbacks
436 44 ▶ $sac->[_dh_]->start_selector($sel_list) if $sac->[_dh_can_]->{start_selector};
Branch: 1 branch
BranchTrueFalseTotal
if $sac->[4]{"start_selector"} 44 0 44
437
438 # parse the rule
439 44 my $rule;
440 44 warn "[SAC] parsing rule\n" if DEBUG;
441
442 ### BUG
443 # The Text::Balanced extractions below are not correct since they don't take
444 # comments into account. With the first one, it'll fail on apostrophes in
445 # comments, with the latter, on unbalanced apos and } in comments and
446 # apos-strings. The latter is used currently because it is less likely to fail,
447 # but what is needed is a real parser that steps inside the black parsing out
448 # comments and property values. The good news is that we have most of the bits
449 # to do that right already.
450
451 #($rule,$css,undef) = Text::Balanced::extract_bracketed($css,q/{}'"/,qr/\s*/); #"
452 44 ($rule,$css,undef) = Text::Balanced::extract_bracketed($css,q/{}"/,qr/\s*/); #"
453 44 $sac->parse_rule(\$rule);
454
455 # end of the rule
456 44 ▶ $sac->[_dh_]->end_selector($sel_list) if $sac->[_dh_can_]->{end_selector};
Branch: 1 branch
BranchTrueFalseTotal
if $sac->[4]{"end_selector"} 0 44 44
457 }
458
459 # trailing whitespace, should only happen at the very end
460 elsif ($css =~ s/^\s+//) {
461 # do nothing
462 0 warn "[SAC] just whitespace\n" if DEBUG;
463 }
464
465 # error
466 else {
467 1 ▶ last if ! length $css;
Branch: 1 branch
BranchTrueFalseTotal
unless length $css 1 0 1
468 0 $sac->[_eh_]->fatal_error('Unknown trailing tokens in style sheet: "' . $css . '"');
469 0 last;
470 }
471 }
472
473 # end doc
474 1 warn "[SAC] end of document\n" if DEBUG;
475 1 ▶ $sac->[_dh_]->end_document if $sac->[_dh_can_]->{end_document};
Branch: 1 branch
BranchTrueFalseTotal
if $sac->[4]{"end_document"} 0 1 1
476
477 #---> Finish Parsing <--------------------------------------------#
478
479 }
480 #---------------------------------------------------------------------#
481 2 *CSS::SAC::parseStyleSheet = \&parse;
482
483
484 #---------------------------------------------------------------------#
485 # $sac->parse_charset($string_ref)
486 # parses a charset
487 #---------------------------------------------------------------------#
488 1 ▶sub parse_charset {
Subroutine parse_charset: called CC=5 SCAR=28.5
Pod documented
489 1 my $sac = shift;
490 1 my $css = shift;
491
492 # we don't remove leading ws, the charset must come first
493 1 ▶ return unless $$css =~ s/^\@charset\s+//i;
Branch: 1 branch
BranchTrueFalseTotal
unless $$css =~ s/^\@charset\s+//i 1 0 1
494
495 # extract the string
496 0 ▶ if ($$css =~ s/^($RE_STRING)\s*;//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$css =~ s/^($CSS::SAC::RE_STRING)\s*;//) { } 0 0 0
497 0 my $charset = $1;
498 0 $charset =~ s/^(?:'|")//; #"
499 0 $charset =~ s/(?:'|")$//; #'
500
501 0 ▶ $sac->[_dh_]->charset($charset) if $sac->[_dh_can_]->{charset};
Branch: 1 branch
BranchTrueFalseTotal
if $sac->[4]{"charset"} 0 0 0
502 }
503 else {
504 0 ▶ if ($$css =~ s/[^;]*;//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$css =~ s/[^;]*;//) { } 0 0 0
505 0 $sac->[_eh_]->warning('Unknown token in charset declaration');
506 }
507 else {
508 0 $sac->[_eh_]->fatal_error('Unknown token in charset declaration');
509 }
510 }
511 }
512 #---------------------------------------------------------------------#
513
514
515 #---------------------------------------------------------------------#
516 # $sac->parse_imports($string_ref)
517 # parses import rules at the beginning
518 #---------------------------------------------------------------------#
519 1 ▶sub parse_imports {
Subroutine parse_imports: called CC=6 SCAR=33.9
Pod documented
520 1 my $sac = shift;
521 1 my $css = shift;
522
523
524 # we may have several imports separated by comments
525 1 ▶ while ($$css =~ s/^\s*\@import\s+//i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$css =~ s/^\s*\@import\s+//i) 0 1 1
526 # first get the uri
527 0 my $uri;
528 0 ▶ if ($$css =~ s/^url\(//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$css =~ s/^url\(//) { } 0 0 0
529 0 $$css =~ s/^((?:$RE_STRING)|([^\)]*))\s*//;
530 0 $uri = $1;
531 0 $uri =~ s/^(?:'|")//; # "
532 0 $uri =~ s/(?:'|")$//; # '
533 0 $$css =~ s/^\)//;
534 }
535 else {
536 0 $$css =~ s/^($RE_STRING)//;
537 0 $uri = $1;
538 0 $uri =~ s/^(?:'|")//; #"
539 0 $uri =~ s/(?:'|")$//; #'
540 }
541
542 # a possible medialist
543 0 my $medialist = $sac->parse_medialist($css);
544
545 # we must have a terminating token now
546 0 ▶ if ($$css =~ s/^\s*;//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$css =~ s/^\s*;//) { } 0 0 0
547 0 ▶ $sac->[_dh_]->import_style($uri,$medialist) if $sac->[_dh_can_]->{import_style};
Branch: 1 branch
BranchTrueFalseTotal
if $sac->[4]{"import_style"} 0 0 0
548 }
549 else {
550 0 ▶ if ($$css =~ s/[^;]*;//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$css =~ s/[^;]*;//) { } 0 0 0
551 0 $sac->[_eh_]->warning('Unknown token in import rule');
552 }
553 else {
554 0 $sac->[_eh_]->fatal_error('Unknown token in import rule');
555 }
556 }
557
558 # remove comments and run again
559 0 $sac->parse_comments($css);
560 }
561 }
562 #---------------------------------------------------------------------#
563
564
565 #---------------------------------------------------------------------#
566 # $sac->parse_namespace_declarations($string_ref)
567 # parses ns declarations
568 #---------------------------------------------------------------------#
569 1 ▶sub parse_namespace_declarations {
Subroutine parse_namespace_declarations: called CC=10 SCAR=27.3
Pod documented
570 1 my $sac = shift;
571 1 my $css = shift;
572
573 # we may have several ns decls separated by comments
574 1 ▶ while ($$css =~ s/^\s*\@namespace\s+//i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$css =~ s/^\s*\@namespace\s+//i) 1 1 2
575 1 my ($prefix,$uri);
576 # first get the prefix
577 1 ▶ if ($$css !~ /^url\(/ and $$css =~ s/^($RE_IDENT)\s+//) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $$css =~ /^url\(/ and $$css =~ s/^($CSS::SAC::RE_IDENT)\s+//) 1 0 1
Condition33%1/3
not $$css =~ /^url\(/ and $$css =~ s/^($CSS::SAC::RE_IDENT)\s+//
!ll&&!rl&&r
001
MC/DC0%0/2
not $$css =~ /^url\(/ and $$css =~ s/^($CSS::SAC::RE_IDENT)\s+//
not $$css =~ /^url\(/ $$css =~ s/^($CSS::SAC::RE_IDENT)\s+//
Truth table33%1/3
not(A) and B
A= not $$css =~ /^url\(/B= $$css =~ s/^($CSS::SAC::RE_IDENT)\s+//
ABresult
0X0
100
111
578 1 $prefix = $1;
579 }
580
581 # then get the uri
582 1 ▶ if ($$css =~ s/^url\(//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$css =~ s/^url\(//) { } 1 0 1
583 1 $$css =~ s/^((?:$RE_STRING)|([^\)]*))\s*//;
584 1 $uri = $1;
585 1 $uri =~ s/^(?:'|")//; # "
586 1 $uri =~ s/(?:'|")$//; # '
587 1 $$css =~ s/^\)//;
588 }
589 else {
590 0 $$css =~ s/^($RE_STRING)//;
591 0 $uri = $1;
592 0 $uri =~ s/^(?:'|")//; #"
593 0 $uri =~ s/(?:'|")$//; #'
594 }
595
596 # we must have a terminating token now
597 1 ▶ if ($$css =~ s/^\s*;//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$css =~ s/^\s*;//) { } 1 0 1
598 # store the prefix-ns in our ns map
599 1 my $map_prefix = $prefix;
600 1 ▶ $map_prefix = '#default' unless $prefix;
Branch: 1 branch
BranchTrueFalseTotal
unless $prefix 0 1 1
601 1 $sac->[_ns_map_]->{$map_prefix} = $uri;
602
603 # throw a callback
604 1 ▶ $prefix ||= '';
Condition50%1/2
$prefix ||= ""
l!l
10
MC/DC0%0/1
$prefix ||= ""
$prefix
Truth table50%1/2
A
A= $prefix
Aresult
11
00
605 1 ▶ $sac->[_dh_]->namespace_declaration($prefix,$uri) if $sac->[_dh_can_]->{namespace_declaration};
Branch: 1 branch
BranchTrueFalseTotal
if $sac->[4]{"namespace_declaration"} 1 0 1
606 }
607 else {
608 0 ▶ if ($$css =~ s/[^;]*;//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$css =~ s/[^;]*;//) { } 0 0 0
609 0 $sac->[_eh_]->warning('Unknown token in namespace declaration');
610 }
611 else {
612 0 $sac->[_eh_]->fatal_error('Unknown token in namespace declaration');
613 }
614 }
615
616 # remove comments and run again
617 1 $sac->parse_comments($css);
618 }
619 }
620 #---------------------------------------------------------------------#
621
622
623 #---------------------------------------------------------------------#
624 # $sac->parse_medialist($string_ref)
625 # parses a list of media values
626 # returns that list as an arrayref
627 #---------------------------------------------------------------------#
628 0 ▶sub parse_medialist {
Subroutine parse_medialist: not called CC=2 SCAR=17.9
Pod documented
629 0 my $sac = shift;
630 0 my $css = shift;
631
632 # test for the right content and return a list if found
633 0 ▶ return [] unless $$css =~ s/^\s*($RE_IDENT(?:\s*,\s*$RE_IDENT)*)//;
Branch: 1 branch
BranchTrueFalseTotal
unless $$css =~ s/^\s*($CSS::SAC::RE_IDENT(?:\s*,\s*$CSS::SAC::RE_IDENT)*)// 0 0 0
634 0 return [map { $_ =~ s/^\s+//; $_ =~ s/\s+$//; $_; } split /,/, $1];
635 }
636 #---------------------------------------------------------------------#
637
638
639 #---------------------------------------------------------------------#
640 # $sac->parse_comments($string_ref)
641 # parses as many comments as there are at the beginning of the string
642 #---------------------------------------------------------------------#
643 284 ▶sub parse_comments {
Subroutine parse_comments: called CC=7 SCAR=30.7
Pod documented
644 284 my $sac = shift;
645 284 my $css = shift;
646
647 # we may have several comments in a row
648 284 my $ws;
649 284 ▶ while ($$css =~ s|^(\s*)/\*||) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$css =~ s[^(\s*)/\*][]) 0 284 284
650 0 $ws .= $1;
651 0 ▶ if ($$css =~ s{^((?:(?:\\\\)|(?:\\[^\*])|(?:\\\*)|[^\\])*?)\*/}{}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$css =~ s<^((?:(?:\\\\)|(?:\\[^\*])|(?:\\\*)|[^\\])*?)\*/>[]) { } 0 0 0
652 0 ▶ $sac->[_dh_]->comment($1) if $sac->[_dh_can_]->{comment};
Branch: 1 branch
BranchTrueFalseTotal
if $sac->[4]{"comment"} 0 0 0
653 }
654 else {
655 0 ▶ if ($$css =~ s/.*\*\///) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$css =~ s[.*\*/][]) { } 0 0 0
656 0 $sac->[_eh_]->warning('Strange comment token, guessing the parse');
657 }
658 else {
659 0 $sac->[_eh_]->fatal_error('Unterminated comment: unrecoverable');
660 }
661 }
662 }
663
664 # we need to keep the whitespace around for certain
665 # occurences of comments, it may be significant
666 284 ▶ $$css = $ws . $$css if defined $ws and defined $$css;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ws and defined $$css 0 284 284
Condition33%1/3
defined $ws and defined $$css
!ll&&!rl&&r
28400
MC/DC0%0/2
defined $ws and defined $$css
defined $ws defined $$css
Truth table33%1/3
A and B
A= defined $wsB= defined $$css
ABresult
0X0
100
111
667 }
668 #---------------------------------------------------------------------#
669
670
671 #---------------------------------------------------------------------#
672 # $sac->parse_selector_list($string_ref)
673 # parses a list of selectors
674 # returns an array ref of selectors
675 #---------------------------------------------------------------------#
676 45 ▶sub parse_selector_list {
Subroutine parse_selector_list: called CC=7 SCAR=20.5
Pod documented
677 45 my $sac = shift;
678 45 my $css = shift;
679
680 # this is a long and hairy process
681 45 my @sels;
682 45 $$css =~ s/^\s*//;
683 45 while (1) {
684
685 # we've reached the rule, or there isn't anything left to parse
686 89 ▶ if ($$css =~ m/^\s*\{/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($$css =~ /^\s*\{/) { } 44 45 89
elsif (not length $$css) { } 1 44 45
elsif (my $sel = $sac->parse_simple_selector($css)) { } 44 0 44
687 44 ▶ if (!@sels) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@sels) 0 44 44
688 0 @sels = ($sac->[_sf_]->create_element_selector(undef,undef));
689 }
690 44 last;
691 }
692
693 elsif (!length $$css) {
694 1 last;
695 }
696
697 # a simple selector
698 elsif (my $sel = $sac->parse_simple_selector($css)) {
699 44 push @sels, $sel;
700
701 # delete the rest
702 44 $sac->parse_comments($css);
703 44 $$css =~ s/^\s*,\s*//;
704 44 $sac->parse_comments($css);
705 44 $$css =~ s/^\s*//;
706 }
707
708 # error
709 else {
710 # something wrong must have happened
711 0 ▶ if ($$css =~ s/[^{]*//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$css =~ s/[^{]*//) { } 0 0 0
712 0 $sac->[_eh_]->warning('Unknown token in selector list');
713 }
714 else {
715 0 $sac->[_eh_]->fatal_error('Unknown token in selector list');
716 }
717 }
718 }
719
720 45 ▶ return unless @sels; # this returns nothing, there were no selectors (needed for parse)
Branch: 1 branch
BranchTrueFalseTotal
unless @sels 1 44 45
721 44 return CSS::SAC::SelectorList->new(\@sels);
722 }
723 #---------------------------------------------------------------------#
724 2 *CSS::SAC::parseSelectors = \&parse_selector_list;
725
726
727 #---------------------------------------------------------------------#
728 # $sac->parse_simple_selector($string_ref)
729 # parses a simple selector
730 # returns the selector object
731 #---------------------------------------------------------------------#
732 46 ▶sub parse_simple_selector {
Subroutine parse_simple_selector: called CC=71 SCAR=47.8
Pod documented
733 46 my $sac = shift;
734 46 my $css = shift;
735
736 46 $$css =~ s/^\s*//;
737
738 ### eat the content piece by piece
739 46 my @tokens;
740
741 46 my ($attr,$func,$args);
742 46 while (1) {
743
744 105 $sac->parse_comments($css);
745
746 # end of selector
747 105 ▶ if ($$css =~ m/^\s*(?:,|{)/ or !length $$css) {
Branch: 10 branches
BranchTrueFalseTotal
if ($$css =~ /^\s*(?:,|{)/ or not length $$css) { } 46 59 105
elsif ($$css =~ s/^(?:($CSS::SAC::RE_IDENT|\*)?(\|))?($CSS::SAC::RE_IDENT|\*)//) { } 19 40 59
elsif ($$css =~ s/^#($CSS::SAC::RE_NAME)//) { } 2 38 40
elsif ($$css =~ s/^\.($CSS::SAC::RE_IDENT)//) { } 2 36 38
elsif ($$css =~ s/^::($CSS::SAC::RE_IDENT)//) { } 2 34 36
elsif (($attr, $$css, undef) = &Text::Balanced::extract_bracketed($$css, q[[]'"], qr/\s*/) and length $attr) { } 9 25 34
elsif (($args, $$css, $func) = &Text::Balanced::extract_bracketed($$css, q[()'"], qr/:$CSS::SAC::RE_IDENT/) and length $func) { } 10 15 25
elsif ($$css =~ s/^\:($CSS::SAC::RE_IDENT)//) { } 11 4 15
elsif ($$css =~ s/^\s*((?:\+|>|~))\s*//) { } 3 1 4
elsif ($$css =~ s/^\s+//) { } 1 0 1
Condition100%3/3
$$css =~ /^\s*(?:,|{)/ or not length $$css
l!l&&r!l&&!r
44259
MC/DC100%2/2
$$css =~ /^\s*(?:,|{)/ or not length $$css
$$css =~ /^\s*(?:,|{)/ not length $$css
Truth table100%3/3
A or not(B)
A= $$css =~ /^\s*(?:,|{)/B= not length $$css
ABresult
1X1
011
000
748 46 last;
749 }
750
751 # element
752 elsif ($$css =~ s/^(?:($RE_IDENT|\*)?(\|))?($RE_IDENT|\*)//) {
753 # create element selector
754 19 my ($ns,$lname);
755 19 ▶ $lname = ($3 eq '*')?undef:$3;
Branch: 1 branch
BranchTrueFalseTotal
$3 eq '*' ? : 2 17 19
756 19 ▶ if (defined $2 and $2 eq '|') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $2 and $2 eq '|') { } 2 17 19
Condition66%2/3
defined $2 and $2 eq "|"
!ll&&!rl&&r
1702
MC/DC50%1/2
defined $2 and $2 eq "|"
defined $2 $2 eq "|"
Truth table66%2/3
A and B
A= defined $2B= $2 eq "|"
ABresult
0X0
100
111
757 2 ▶ if (!$1) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $1) { } 0 2 2
elsif ($1 eq '*') { } 0 2 2
758 0 $ns = ''; # |E matches elements in no namespace
759 }
760 elsif ($1 eq '*') {
761 0 $ns = undef; # undef means all, '' means default
762 }
763 else {
764 2 $ns = $sac->[_ns_map_]->{$1};
765 }
766 }
767 else {
768 # E matches elements in the default namespace or
769 # any namespace if no default namespace is declared
770 17 ▶ $ns = $sac->[_ns_map_]->{'#default'} || undef;
Condition50%1/2
$sac->[6]{'#default'} || undef
l!l
017
MC/DC0%0/1
$sac->[6]{'#default'} || undef
$sac->[6]{'#default'}
Truth table50%1/2
A
A= $sac->[6]{'#default'}
Aresult
11
00
771 }
772
773 # push it
774 19 push @tokens, $sac->[_sf_]->create_element_selector($ns,$lname);
775 }
776
777 # hash id
778 elsif ($$css =~ s/^#($RE_NAME)//) {
779 2 push @tokens, $sac->[_cf_]->create_id_condition($1);
780 }
781
782 # dot class
783 elsif ($$css =~ s/^\.($RE_IDENT)//) {
784 2 push @tokens, $sac->[_cf_]->create_class_condition(undef,$1);
785 }
786
787 # CSS3 pseudo-elements
788 elsif ($$css =~ s/^::($RE_IDENT)//) {
789 2 push @tokens, $sac->[_sf_]->create_pseudo_element_selector(undef,$1);
790 }
791
792 # [attr]
793 ▶ elsif (
Condition66%2/3
($attr, $$css, undef) = &Text::Balanced::extract_bracketed($$css, "[]'\"", qr/\s*/) and length $attr
!ll&&!rl&&r
0259
MC/DC50%1/2
($attr, $$css, undef) = &Text::Balanced::extract_bracketed($$css, "[]'\"", qr/\s*/) and length $attr
($attr, $$css, undef) = &Text::Balanced::extract_bracketed($$css, "[]'\"", qr/\s*/) length $attr
Truth table66%2/3
A and B
A= ($attr, $$css, undef) = &Text::Balanced::extract_bracketed($$css, "[]'\"", qr/\s*/)B= length $attr
ABresult
0X0
100
111
794 (($attr,$$css,undef) = Text::Balanced::extract_bracketed($$css,q/[]'"/,qr/\s*/))
795 and
796 length $attr
797 ) {
798 9 $attr =~ s/^\[\s*//;
799 9 $attr =~ s/\s*\]$//;
800
801 # get the attr lname and ns
802 9 $attr =~ s/^(?:($RE_IDENT|\*)?(\|))?($RE_IDENT|\*)//;
803 9 my ($ns,$lname);
804 9 ▶ $lname = ($3 eq '*')?undef:$3;
Branch: 1 branch
BranchTrueFalseTotal
$3 eq '*' ? : 0 9 9
805 9 ▶ if (defined $2 and $2 eq '|') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $2 and $2 eq '|') { } 1 8 9
Condition66%2/3
defined $2 and $2 eq "|"
!ll&&!rl&&r
801
MC/DC50%1/2
defined $2 and $2 eq "|"
defined $2 $2 eq "|"
Truth table66%2/3
A and B
A= defined $2B= $2 eq "|"
ABresult
0X0
100
111
806 1 ▶ if (!$1) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $1) { } 0 1 1
elsif ($1 eq '*') { } 0 1 1
807 0 $ns = '' # [|a] matches attributes in no namespace;
808 }
809 elsif ($1 eq '*') {
810 0 $ns = undef; # undef means all, '' means default
811 }
812 else {
813 1 $ns = $sac->[_ns_map_]->{$1};
814 }
815 }
816 else {
817 8 $ns = ''; # [a] is equivalent to [|a]
818 }
819
820 # if there's more, parse on
821 9 my ($op,$value);
822 9 ▶ if (length $attr) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $attr) 6 3 9
823 6 ▶ if ($attr =~ s/^((?:\^|\$|\*|\~|\|)?=)//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr =~ s/^((?:\^|\$|\*|\~|\|)?=)//) { } 6 0 6
824 6 $op = $1;
825 6 $attr =~ s/^(?:'|")//; #"
826 6 $attr =~ s/(?:'|")$//; #"
827 6 $value = $attr;
828 }
829 else {
830 0 $sac->[_eh_]->warning('Unknown token in attribute condition');
831 0 ▶ if ($$css =~ s/[^;]*;//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$css =~ s/[^;]*;//) { } 0 0 0
832 0 $sac->[_eh_]->warning('Unknown token in import rule');
833 }
834 else {
835 0 $sac->[_eh_]->fatal_error('Unknown token in import rule');
836 }
837 }
838 }
839
840 # create the right condition
841 9 my $acond;
842 9 ▶ if (!$op or $op eq '=') {
Branch: 6 branches
BranchTrueFalseTotal
if (not $op or $op eq '=') { } 5 4 9
elsif ($op eq '^=') { } 1 3 4
elsif ($op eq '$=') { } 1 2 3
elsif ($op eq '*=') { } 0 2 2
elsif ($op eq '~=') { } 1 1 2
elsif ($op eq '|=') { } 1 0 1
Condition100%3/3
not $op or $op eq "="
l!l&&r!l&&!r
324
MC/DC100%2/2
not $op or $op eq "="
not $op $op eq "="
Truth table100%3/3
not(A) or B
A= not $opB= $op eq "="
ABresult
1X1
011
000
843 5 ▶ my $spec = (defined $value)?1:0;
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 2 3 5
844 5 $acond = $sac->[_cf_]->create_attribute_condition($lname,$ns,$spec,$value);
845 }
846 elsif ($op eq '^=') {
847 1 $acond = $sac->[_cf_]->create_starts_with_attribute_condition($lname,$ns,1,$value);
848 }
849 elsif ($op eq '$=') {
850 1 $acond = $sac->[_cf_]->create_ends_with_attribute_condition($lname,$ns,1,$value);
851 }
852 elsif ($op eq '*=') {
853 0 $acond = $sac->[_cf_]->create_contains_attribute_condition($lname,$ns,1,$value);
854 }
855 elsif ($op eq '~=') {
856 1 $acond = $sac->[_cf_]->create_one_of_attribute_condition($lname,$ns,1,$value);
857 }
858 elsif ($op eq '|=') {
859 1 $acond = $sac->[_cf_]->create_begin_hyphen_attribute_condition($lname,$ns,1,$value);
860 }
861
862 9 push @tokens, $acond;
863 }
864
865 # :pseudo()
866 ▶ elsif (
Condition66%2/3
($args, $$css, $func) = &Text::Balanced::extract_bracketed($$css, "()'\"", qr/:$CSS::SAC::RE_IDENT/) and length $func
!ll&&!rl&&r
01510
MC/DC50%1/2
($args, $$css, $func) = &Text::Balanced::extract_bracketed($$css, "()'\"", qr/:$CSS::SAC::RE_IDENT/) and length $func
($args, $$css, $func) = &Text::Balanced::extract_bracketed($$css, "()'\"", qr/:$CSS::SAC::RE_IDENT/) length $func
Truth table66%2/3
A and B
A= ($args, $$css, $func) = &Text::Balanced::extract_bracketed($$css, "()'\"", qr/:$CSS::SAC::RE_IDENT/)B= length $func
ABresult
0X0
100
111
867 ($args,$$css,$func) = Text::Balanced::extract_bracketed($$css,q/()'"/,qr/:$RE_IDENT/)
868 and
869 length $func
870 ) {
871
872 # cleanup the func and args
873 10 $func =~ s/^://;
874 10 $args =~ s/^\(\s*//;
875 10 $args =~ s/\s*\)$//;
876 10 $args =~ s/^(?:'|")//; #"
877 10 $args =~ s/(?:'|")$//; #"
878
879 # lang(lang_tag)
880 10 ▶ if (lc ($func) eq 'lang') {
Branch: 4 branches
BranchTrueFalseTotal
if (lc $func eq 'lang') { } 1 9 10
elsif (lc $func eq 'contains') { } 1 8 9
elsif (lc $func eq 'not') { } 2 6 8
elsif ($func =~ /^nth-(last-)?((?:child)|(?:of-type))$/i) { } 6 0 6
881 1 push @tokens, $sac->[_cf_]->create_lang_condition($args);
882 }
883
884 # contains("text")
885 elsif (lc ($func) eq 'contains') {
886 1 push @tokens, $sac->[_cf_]->create_content_condition($args);
887 }
888
889 # not(selector)
890 elsif (lc ($func) eq 'not') {
891 2 my $sel = $sac->parse_simple_selector(\$args);
892 # push @tokens, $sac->[_sf_]->create_negative_selector($sel);
893 2 push @tokens, $sac->[_cf_]->create_negative_condition($sel);
894 }
895
896 # positional: nth-child, nth-last-child, nth-of-type, nth-last-of-type,
897 elsif ($func =~ m/^nth-(last-)?((?:child)|(?:of-type))$/i) {
898 6 my $pos = $args;
899 6 ▶ my $of_type = (lc($2) eq 'of-type')?1:0;
Branch: 1 branch
BranchTrueFalseTotal
lc $2 eq 'of-type' ? : 2 4 6
900 6 ▶ $pos = (lc($1) eq 'last-')?("-$pos"):($pos);
Branch: 1 branch
BranchTrueFalseTotal
lc $1 eq 'last-' ? : 2 4 6
901 # PositionalCondition will take care of parsing
902 # the expressions, and will provide appropriate accessors
903 6 push @tokens, $sac->[_cf_]->create_positional_condition($pos,$of_type,0);
904 }
905
906 # something else we don't know about
907 else {
908 0 push @tokens, $sac->[_cf_]->create_pseudo_class_condition(undef,$func);
909 }
910 }
911
912 # :pseudo (not a function)
913 elsif ($$css =~ s/^\:($RE_IDENT)//) {
914
915 # root
916 11 ▶ if (lc($1) eq 'root') {
Branch: 9 branches
BranchTrueFalseTotal
if (lc $1 eq 'root') { } 2 9 11
elsif (lc $1 eq 'empty') { } 1 8 9
elsif (lc $1 eq 'only-child') { } 1 7 8
elsif (lc $1 eq 'only-of-type') { } 1 6 7
elsif (lc $1 eq 'first-child') { } 1 5 6
elsif (lc $1 eq 'last-child') { } 1 4 5
elsif (lc $1 eq 'first-of-type') { } 1 3 4
elsif (lc $1 eq 'last-of-type') { } 1 2 3
elsif (lc $1 eq 'first-line' or lc $1 eq 'first-letter' or lc $1 eq 'selection' or lc $1 eq 'before' or lc $1 eq 'after') { } 0 2 2
917 2 push @tokens, $sac->[_cf_]->create_is_root_condition;
918 }
919
920 # empty
921 elsif (lc($1) eq 'empty') {
922 1 push @tokens, $sac->[_cf_]->create_is_empty_condition;
923 }
924
925 # only-child
926 elsif (lc($1) eq 'only-child') {
927 1 my $fcond = $sac->[_cf_]->create_positional_condition(1,0,0);
928 1 my $lcond = $sac->[_cf_]->create_positional_condition(-1,0,0);
929 1 my $ocond = $sac->[_cf_]->create_and_condition($fcond,$lcond);
930 1 push @tokens, $ocond;
931 }
932
933 # only-of-type
934 elsif (lc($1) eq 'only-of-type') {
935 1 my $fcond = $sac->[_cf_]->create_positional_condition(1,1,0);
936 1 my $lcond = $sac->[_cf_]->create_positional_condition(-1,1,0);
937 1 my $ocond = $sac->[_cf_]->create_and_condition($fcond,$lcond);
938 1 push @tokens, $ocond;
939 }
940
941 # first-child
942 elsif (lc($1) eq 'first-child') {
943 1 push @tokens, $sac->[_cf_]->create_positional_condition(1,0,0);
944 }
945
946 # last-child
947 elsif (lc($1) eq 'last-child') {
948 1 push @tokens, $sac->[_cf_]->create_positional_condition(-1,0,0);
949 }
950
951 # first-of-type
952 elsif (lc($1) eq 'first-of-type') {
953 1 push @tokens, $sac->[_cf_]->create_positional_condition(1,1,0);
954 }
955
956 # last-of-type
957 elsif (lc($1) eq 'last-of-type') {
958 1 push @tokens, $sac->[_cf_]->create_positional_condition(-1,1,0);
959 }
960
961 # pseudo-elements in disguise
962 ▶ elsif (
Condition33%4/12
lc $1 eq "first-line" or lc $1 eq "first-letter" or lc $1 eq "selection" or lc $1 eq "before" or lc $1 eq "after"
l!l&&r!l&&!r
002
lc $1 eq "first-line" or lc $1 eq "first-letter" or lc $1 eq "selection" or lc $1 eq "before"
l!l&&r!l&&!r
002
lc $1 eq "first-line" or lc $1 eq "first-letter" or lc $1 eq "selection"
l!l&&r!l&&!r
002
lc $1 eq "first-line" or lc $1 eq "first-letter"
l!l&&r!l&&!r
002
MC/DC0%0/5
lc $1 eq "first-line" or lc $1 eq "first-letter" or lc $1 eq "selection" or lc $1 eq "before" or lc $1 eq "after"
lc $1 eq "first-line" lc $1 eq "first-letter" lc $1 eq "selection" lc $1 eq "before" lc $1 eq "after"
Truth table16%1/6
A or B or C or D or E
A= lc $1 eq "first-line"B= lc $1 eq "first-letter"C= lc $1 eq "selection"D= lc $1 eq "before"E= lc $1 eq "after"
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
963 lc($1) eq 'first-line' or lc($1) eq 'first-letter' or
964 lc($1) eq 'selection' or lc($1) eq 'before' or lc($1) eq 'after'
965 ) {
966 0 push @tokens, $sac->[_sf_]->create_pseudo_element_selector(undef,lc($1));
967 }
968
969 # regular: link, visited, hover, active, focus, target, enabled, disabled,
970 # checked, indeterminate
971 else {
972 2 push @tokens, $sac->[_cf_]->create_pseudo_class_condition(undef,$1);
973 }
974 }
975
976 # combinators
977 elsif ($$css =~ s/^\s*((?:\+|>|~))\s*//) {
978 3 push @tokens, $1;
979 }
980
981 # special case empty combinator
982 elsif ($$css =~ s/^\s+//) {
983 1 push @tokens, ' ';
984 }
985
986 # an error
987 else {
988 0 ▶ if (s/^.*?(,|{)/$1/) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/^.*?(,|{)/$1/) { } 0 0 0
989 0 $sac->[_eh_]->warning('Unknown token in simple selector');
990 }
991 else {
992 0 $sac->[_eh_]->fatal_error('Unknown token in simple selector');
993 }
994 }
995 }
996
997 ### process the tokens list
998
999 # if the first token isn't an element selector then create a *|* one
1000 # evaling is lame, but it's the only test I could think of
1001 46 eval { $tokens[0]->SelectorType };
1002 46 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 29 17 46
1003 29 unshift @tokens, $sac->[_sf_]->create_element_selector(undef,undef);
1004 }
1005
1006 # start looping over the tokens to reduce the list
1007 46 my $selector = shift @tokens;
1008 46 eval { $selector->SelectorType };
1009 46 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 46 46
1010 # this is a serious exception
1011 0 $sac->[_eh_]->fatal_error('Really weird input in simple selector');
1012 }
1013
1014 # here we need to check whether the next token is also a selector
1015 # if it is, we need to make an AND_CONDITION containing the two selectors
1016 # and to attach it to a universal selector
1017 # then we'll have to mix it into the $cond below.
1018 46 ▶ if (@tokens) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tokens) 38 8 46
1019 38 eval { $tokens[0]->SelectorType };
1020 38 ▶ if (!$@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 0 38 38
1021 0 my $and_cond = $sac->[_cf_]->create_and_condition($selector,shift @tokens);
1022 0 $selector = $sac->[_sf_]->create_element_selector(undef,undef);
1023 }
1024 }
1025
1026
1027 # create a conditional selector with all conditions
1028 46 my $cond = $sac->build_condition(\@tokens);
1029 46 ▶ if ($cond) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cond) 34 12 46
1030 34 $selector = $sac->[_sf_]->create_conditional_selector($selector,$cond);
1031 }
1032
1033 46 ▶ while (@tokens) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tokens) 4 46 50
1034
1035 # here there should be a combinator or nothing
1036 4 my $comb = shift @tokens;
1037 4 ▶ if ($comb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comb) 4 0 4
1038 # pretty serious error
1039 4 ▶ $sac->[_eh_]->fatal_error('Really weird input in simple selector') if ref $comb;
Branch: 1 branch
BranchTrueFalseTotal
if ref $comb 0 4 4
1040
1041 # get the next selector
1042 4 my $new_selector = shift @tokens;
1043 4 eval { $new_selector->SelectorType };
1044 4 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 4 4
1045 # last unless length $new_selector;
1046 0 ▶ if (ref $new_selector) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $new_selector) { } 0 0 0
1047 0 unshift @tokens, $new_selector;
1048 0 $new_selector = $sac->[_sf_]->create_element_selector(undef,undef);
1049 }
1050 else {
1051 # this is a serious exception (we don't know what's here)
1052 0 $sac->[_eh_]->fatal_error('Really weird input in simple selector: "' . $$css . '"');
1053 }
1054 }
1055
1056 # create a conditional selector with all conditions
1057 4 my $cond = $sac->build_condition(\@tokens);
1058 4 ▶ if ($cond) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cond) 0 4 4
1059 0 $new_selector = $sac->[_sf_]->create_conditional_selector($new_selector,$cond);
1060 }
1061
1062 # various possible combinators
1063 4 ▶ if ($comb eq ' ') {
Branch: 4 branches
BranchTrueFalseTotal
if ($comb eq ' ') { } 1 3 4
elsif ($comb eq '>') { } 1 2 3
elsif ($comb eq '+') { } 1 1 2
elsif ($comb eq '~') { } 1 0 1
1064 1 $selector = $sac->[_sf_]->create_descendant_selector($selector,$new_selector);
1065 }
1066 elsif ($comb eq '>') {
1067 1 $selector = $sac->[_sf_]->create_child_selector($selector,$new_selector);
1068 }
1069 elsif ($comb eq '+') {
1070 1 $selector = $sac->[_sf_]->create_direct_adjacent_selector(
1071 ELEMENT_NODE,
1072 $selector,
1073 $new_selector
1074 );
1075 }
1076 elsif ($comb eq '~') {
1077 1 $selector = $sac->[_sf_]->create_indirect_adjacent_selector(
1078 ELEMENT_NODE,
1079 $selector,
1080 $new_selector
1081 );
1082 }
1083 }
1084 }
1085
1086 46 return $selector;
1087 }
1088 #---------------------------------------------------------------------#
1089
1090
1091 #---------------------------------------------------------------------#
1092 # $sac->build_condition(\@tokens)
1093 # helper to build conditions
1094 #---------------------------------------------------------------------#
1095 50 ▶sub build_condition {
Subroutine build_condition: called CC=5 SCAR=16.1
Pod documented
1096 50 my $sac = shift;
1097 50 my $tokens = shift;
1098
1099 # get all conditions
1100 50 my @conditions;
1101 50 ▶ while (@$tokens) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$tokens) 38 46 84
1102 # eval { $tokens->[0]->SelectorType };
1103 # if (not $@) {
1104 # $sac->[_eh_]->fatal_error('Really weird input in simple selector');
1105 # }
1106 38 ▶ last if ! ref $tokens->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tokens->[0] 4 34 38
1107 34 push @conditions, shift @$tokens;
1108 }
1109
1110 # build a single condition out of the others
1111 50 my $cond;
1112 50 ▶ if (@conditions) {
Branch: 1 branch
BranchTrueFalseTotal
if (@conditions) 34 16 50
1113 34 $cond = shift @conditions;
1114 34 for my $c (@conditions) {
1115 0 $cond = $sac->[_cf_]->create_and_condition($cond,$c);
1116 }
1117 }
1118
1119 50 return $cond;
1120 }
1121 #---------------------------------------------------------------------#
1122
1123
1124 #---------------------------------------------------------------------#
1125 # $sac->parse_rule($string_ref)
1126 # parses a rule (with { and })
1127 #---------------------------------------------------------------------#
1128 44 ▶sub parse_rule {
Subroutine parse_rule: called CC=2 SCAR=7.0
Pod documented
1129 44 my $sac = shift;
1130 44 my $css = shift;
1131 44 ▶ return unless defined $$css;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$css 0 44 44
1132
1133 # remove { and }, and parse the content
1134 44 $$css =~ s/^\s*{//;
1135 44 $$css =~ s/}\s*$//;
1136 44 warn "[SAC] removed curlies\n" if DEBUG;
1137 44 $sac->parse_style_declaration($css);
1138 }
1139 #---------------------------------------------------------------------#
1140 2 *CSS::SAC::parseRule = \&parse_rule;
1141
1142
1143 #---------------------------------------------------------------------#
1144 # $sac->parse_style_declaration($string_ref)
1145 # same as parse_rule, but without the { and }. Cool for HTML, SVG...
1146 #---------------------------------------------------------------------#
1147 44 ▶sub parse_style_declaration {
Subroutine parse_style_declaration: called CC=7 SCAR=35.8
Pod documented
1148 44 my $sac = shift;
1149 44 my $css = shift;
1150
1151 # take those prop-val one by one
1152 44 $sac->parse_comments($css);
1153 44 $$css =~ s/^\s*//;
1154 44 ▶ while (length $$css) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $$css) 0 44 44
1155 # the property
1156 0 $$css =~ s/^(-?$RE_IDENT)\s*//; # includes the - prefix
1157 0 my $prop = $1;
1158 0 $sac->parse_comments($css);
1159
1160 # the separator
1161 0 $$css =~ s/^\s*:\s*//;
1162 0 $sac->parse_comments($css);
1163
1164 # the value
1165 0 my $lu = $sac->parse_property_value($css);
1166 0 ▶ if (!@$lu) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$lu) 0 0 0
1167 0 ▶ last unless length $$css;
Branch: 1 branch
BranchTrueFalseTotal
unless length $$css 0 0 0
1168 0 ▶ if ($$css =~ s/[^;}]*(?:;|\})?//) { # this is a bit dodgy...
Branch: 1 branch
BranchTrueFalseTotal
if ($$css =~ s/[^;}]*(?:;|\})?//) { } 0 0 0
1169 0 $sac->[_eh_]->warning('Unknown token in style declaration');
1170 }
1171 else {
1172 0 $sac->[_eh_]->fatal_error('Unknown token in style declaration: "' . $$css . '"');
1173 }
1174 0 next;
1175 }
1176 0 $sac->parse_comments($css);
1177
1178 # the priority
1179 0 my $prio = $sac->parse_priority($css);
1180 0 $sac->parse_comments($css);
1181
1182 # the terminator
1183 0 $$css =~ s/^\s*;\s*//;
1184
1185 # callback
1186 0 ▶ $prio ||= 0;
Condition0%0/2
$prio ||= 0
l!l
00
MC/DC0%0/1
$prio ||= 0
$prio
Truth table0%0/2
A
A= $prio
Aresult
11
00
1187 0 ▶ $sac->[_dh_]->property($prop,$lu,$prio) if $sac->[_dh_can_]->{property};
Branch: 1 branch
BranchTrueFalseTotal
if $sac->[4]{"property"} 0 0 0
1188
1189 # remove cruft
1190 0 $sac->parse_comments($css);
1191 0 $$css =~ s/^\s*//;
1192 }
1193 }
1194 #---------------------------------------------------------------------#
1195 2 *CSS::SAC::parseStyleDeclaration = \&parse_style_declaration;
1196
1197
1198 #---------------------------------------------------------------------#
1199 # $sac->parse_property_value($string_ref)
1200 # parses a value
1201 # returns an array ref of lexical units
1202 #---------------------------------------------------------------------#
1203 0 ▶sub parse_property_value {
Subroutine parse_property_value: not called CC=20 SCAR=60.4
Pod documented
1204 0 my $sac = shift;
1205 0 my $css = shift;
1206 0 ▶ my $att = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
1207
1208 0 $$css =~ s/^\s*//;
1209
1210 # parse it by value chunks
1211 0 my @lus;
1212 0 while (1) {
1213 0 my ($type,$text,$value);
1214
1215 0 $sac->parse_comments($css);
1216
1217 # exit conditions
1218 0 ▶ if (! length($$css) or $$css =~ m/^\s*(?:;|!)/ or ($att and $$css =~ s/^\s*(?:\))//)) {
Branch: 12 branches
BranchTrueFalseTotal
if (not length $$css or $$css =~ /^\s*(?:;|!)/ or $att and $$css =~ s/^\s*(?:\))//) { } 0 0 0
elsif ($$css =~ s[^\s*(,|/)\s*][]) { } 0 0 0
elsif ($$css =~ s/^\s+//) { } 0 0 0
elsif ($$css =~ s/^inherit//) { } 0 0 0
elsif ($$css =~ s/^((?:\+|-)?$CSS::SAC::RE_NUM) (em|ex|px|cm|mm|in|pt|pc|deg|rad|grad|ms|s|hz|khz|%) //xi) { } 0 0 0
elsif ($$css =~ s/^((?:\+|-)?$CSS::SAC::RE_NUM)($CSS::SAC::RE_IDENT)//) { } 0 0 0
elsif ($$css =~ s/^((?:\+|-)?$CSS::SAC::RE_NUM)//) { } 0 0 0
elsif ($$css =~ s/^($CSS::SAC::RE_RANGE)//) { } 0 0 0
elsif ($$css =~ s/^#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})//) { } 0 0 0
elsif ($$css =~ s/^($CSS::SAC::RE_IDENT)\(//) { } 0 0 0
elsif ($$css =~ s/^($CSS::SAC::RE_IDENT)//) { } 0 0 0
elsif ($$css =~ s/^($CSS::SAC::RE_STRING)//) { } 0 0 0
Condition0%0/9
not length $$css or $$css =~ /^\s*(?:;|!)/ or $att and $$css =~ s/^\s*(?:\))//
l!l&&r!l&&!r
000
not length $$css or $$css =~ /^\s*(?:;|!)/
l!l&&r!l&&!r
000
$att and $$css =~ s/^\s*(?:\))//
!ll&&!rl&&r
000
MC/DC0%0/4
not length $$css or $$css =~ /^\s*(?:;|!)/ or $att and $$css =~ s/^\s*(?:\))//
not length $$css $$css =~ /^\s*(?:;|!)/ $att $$css =~ s/^\s*(?:\))//
Truth table0%0/5
not(A) or B or C and D
A= not length $$cssB= $$css =~ /^\s*(?:;|!)/C= $attD= $$css =~ s/^\s*(?:\))//
ABCDresult
1XXX1
01XX1
00111
000X0
00100
1219 0 last;
1220 }
1221
1222 # ops
1223 elsif ($$css =~ s{^\s*(,|/)\s*}{}) {
1224 0 $value = $1;
1225 0 ▶ if ($value eq ',') {
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq ',') { } 0 0 0
1226 0 $type = OPERATOR_COMMA;
1227 0 $text = 'comma';
1228 }
1229 else {
1230 0 $type = OPERATOR_SLASH;
1231 0 $text = 'slash';
1232 }
1233 }
1234
1235 # special case empty op
1236 elsif ($$css =~ s{^\s+}{}) {
1237 0 next;
1238 }
1239
1240 # inherit
1241 elsif ($$css =~ s/^inherit//) {
1242 0 $type = INHERIT;
1243 0 $text = 'inherit';
1244 0 $value = undef;
1245 }
1246
1247 # lengths and assoc
1248 elsif ($$css =~ s/^((?:\+|-)?$RE_NUM)
1249 (em|ex|px|cm|mm|in|pt|pc|deg|rad|grad|ms|s|hz|khz|%)
1250 //xi) {
1251 0 $value = $1;
1252 0 $text = lc $2;
1253 0 $type = $DIM_MAP{$text};
1254 }
1255
1256 # dimension
1257 elsif ($$css =~ s/^((?:\+|-)?$RE_NUM)($RE_IDENT)//) {
1258 0 $value = $1;
1259 0 $text = lc $2;
1260 0 $type = DIMENSION;
1261 }
1262
1263 # number
1264 elsif ($$css =~ s/^((?:\+|-)?$RE_NUM)//) {
1265 0 $value = $1;
1266 0 $text = 'number';
1267 0 ▶ if ($value =~ m/\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /\./) { } 0 0 0
1268 0 $type = REAL;
1269 }
1270 else {
1271 0 $type = INTEGER;
1272 }
1273 }
1274
1275 # unicode range
1276 elsif ($$css =~ s/^($RE_RANGE)//) {
1277 0 $value = $1;
1278 0 $text = 'unicode-range';
1279 0 $type = UNICODERANGE;
1280 }
1281
1282 # hex rgb
1283 elsif ($$css =~ s/^#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})//) {
1284 0 $value = $1;
1285 0 $text = '#';
1286 0 $type = RGBCOLOR;
1287 }
1288
1289 # functions
1290 # elsif (
1291 # ($value,$$css,$text) = Text::Balanced::extract_bracketed($$css,q/()'"/,qr/$RE_IDENT/)
1292 # and
1293 # length $text
1294 # ) {
1295 elsif ($$css =~ s/^($RE_IDENT)\(//) {
1296
1297 # cleanup the func and args
1298 # $text = lc $text;
1299 # $value =~ s/^\(\s*//;
1300 # $value =~ s/\s*\)$//;
1301 # $value =~ s/^(?:"|')//; #"
1302 # $value =~ s/(?:"|')$//; #"
1303 0 $text = lc $1;
1304 0 $value = $sac->parse_property_value($css, 1);
1305
1306 # get the appropriate type
1307 0 ▶ if ($FUNC_MAP{$text}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($CSS::SAC::FUNC_MAP{$text}) { } 0 0 0
1308 0 $type = $FUNC_MAP{$text};
1309 }
1310 else {
1311 0 $type = FUNCTION;
1312 }
1313 }
1314
1315 # ident
1316 elsif ($$css =~ s/^($RE_IDENT)//) {
1317 0 $value = $1;
1318 0 $text = 'ident';
1319 0 $type = IDENT;
1320 }
1321
1322 # string
1323 elsif ($$css =~ s/^($RE_STRING)//) {
1324 0 $value = $1;
1325 0 $value =~ s/^(?:"|')//; #"
1326 0 $value =~ s/(?:"|')$//; #"
1327 0 $text = 'string';
1328 0 $type = STRING_VALUE;
1329 }
1330
1331 # error
1332 else {
1333 0 return [];
1334 }
1335
1336 # add a lu
1337 0 push @lus, CSS::SAC::LexicalUnit->new($type,$text,$value);
1338 }
1339
1340 0 return \@lus;
1341 }
1342 #---------------------------------------------------------------------#
1343 2 *CSS::SAC::parsePropertyValue = \&parse_property_value;
1344
1345
1346 #---------------------------------------------------------------------#
1347 # $sac->parse_priority($string_ref)
1348 # parses a priority
1349 # returns true if there is a priority value there
1350 #---------------------------------------------------------------------#
1351 0 ▶sub parse_priority {
Subroutine parse_priority: not called CC=2 SCAR=17.9
Pod documented
1352 0 my $sac = shift;
1353 0 my $css = shift;
1354
1355 0 ▶ return 1 if $$css =~ s/^\s*!\s*important//i;
Branch: 1 branch
BranchTrueFalseTotal
if $$css =~ s/^\s*!\s*important//i 0 0 0
1356 }
1357 #---------------------------------------------------------------------#
1358 2 *CSS::SAC::parsePriority = \&parse_priority;
1359
1360
1361 # #
1362 # #
1363 ### Parsing Methods ###################################################
1364
1365
1366
1367 ### Default Error Handler #############################################
1368 # #
1369 # #
1370
1371 # This is pretty much a non package, it is just there to provide the
1372 # default error handler.
1373
1374 package CSS::SAC::DefaultErrorHandler;
1375
1376 2 ▶sub new { return bless [], __PACKAGE__; }
Subroutine new: called CC=1 SCAR=0.0
1377 0 ▶sub warning { warn "[warning] $_[1] (line " . (caller)[2] . ")"; }
Subroutine warning: not called CC=1 SCAR=6.9
1378 0 ▶sub error { warn "[error] $_[1] (line " . (caller)[2] . ")"; }
Subroutine error: not called CC=1 SCAR=6.9
1379 0 ▶sub fatal_error { die "[fatal] $_[1] (line " . (caller)[2] . ")"; }
Subroutine fatal_error: not called CC=1 SCAR=6.9
1380
1381
1382 # #
1383 # #
1384 ### Default Error Handler #############################################
1385
1386
1387
1388 2 1;
1389
1390 =pod
1391
1392 =head1 NAME
1393
1394 CSS::SAC - SAC CSS parser
1395
1396 =head1 SYNOPSIS
1397
1398 use CSS::SAC qw();
1399 use My::SACHandler ();
1400 use My::SACErrors ();
1401
1402 my $doc_handler = My::SACHandler->new;
1403 my $err_handler = My::SACErrors->new;
1404 my $sac = CSS::SAC->new({
1405 DocumentHandler => $doc_handler,
1406 ErrorHandler => $err_handler,
1407 });
1408
1409 # generate a stream of events
1410 $sac->parse({ filename => 'foo.css' });
1411
1412 =head1 DESCRIPTION
1413
1414 SAC (Simple API for CSS) is an event-based API much like SAX for XML.
1415 If you are familiar with the latter, you should have little trouble
1416 getting used to SAC. More information on SAC can be found online at
1417 http://www.w3.org/TR/SAC.
1418
1419 CSS having more constructs than XML, core SAC is still more complex
1420 than core SAX. However, if you need to parse a CSS style sheet, SAC
1421 probably remains the easiest way to get it done.
1422
1423 Most of the spec is presently implemented. The following interfaces
1424 are not yet there: Locator, CSSException, CSSParseException,
1425 ParserFactory. They may or may not be implemented at a later date
1426 (the most likely candidates are the exception classes, for which I
1427 still have to find an appropriate model).
1428
1429 Some places differ slightly from what is in the spec. I have tried to
1430 keep those to a justified minimum and to flag them correctly.
1431
1432 =head2 the CSS::SAC module itself
1433
1434 The Parser class doesn't exist separately, it's defined in CSS::SAC.
1435 It doesn't expose the locale interface because we don't localize
1436 errors (yet). It also doesn't have C<parse_style_sheet> but rather
1437 C<parse>, which is more consistent with other Perl parsing interfaces.
1438
1439 I have added the C<charset($charset)> callback to the DocumentHandler
1440 interface. There are valid reasons why it wasn't there (it can be
1441 trusted only ever so often, and one should look at the actual encoding
1442 instead) but given that it's a token in the grammar, I believe that
1443 there should still be a way to access it.
1444
1445 =head1 METHODS
1446
1447 =over 4
1448
1449 =item * CSS::SAC->new(\%options) or $sac->new(\%options)
1450
1451 Constructs a new parser object. The options can be:
1452
1453 - ConditionFactory and SelectorFactory
1454 the factory classes used to build selector and condition objects.
1455 See CSS::SAC::{Condition,Selector}Factory for more details on the
1456 interfaces those classes must expose.
1457
1458 - DocumentHandler and ErrorHandler
1459 the handler classes used as sinks for the event stream received
1460 from a SAC Driver. See CSS::SAC::{Document,Error}Factory for more
1461 details on the interfaces those classes must expose.
1462
1463 Methods will be called on whatever it is you pass as values to those
1464 options. Thus, you may pass in objects as well as class names (I
1465 haven't tested this yet, there may be a problem).
1466
1467 NOTE: an error handler should implement all callbacks, while a document
1468 handler may only implement those it is interested in. There is a default
1469 error handler (which dies and warns depending on the type of error) but
1470 not default document handler.
1471
1472 =item * $sac->ParserVerion or $sac->getParserVerion
1473
1474 Returns the supported CSS version.
1475
1476 Requesting this parser's ParserVersion will return the string 'CSS3'.
1477 While that is (modulo potential bugs of course) believed to be
1478 generally true, several caveats apply:
1479
1480 To begin with, CSS3 has been modularised, and various modules are at
1481 different stages of development. Evolving modules may require evolving
1482 this parser. I hesitated between making ParserVersion return CSS2,
1483 CSS3-pre, or simply CSS3. I chose the latter because I intend to
1484 update it as I become aware of the necessity of changes to accommodate
1485 new CSS3 stuff, and because it already supports a number of constructs
1486 alien to CSS2 (of which namespaces is imho important enough to justify
1487 a CSS3 tag). If you are aware of incompatibilities, please contact me.
1488
1489 More importantly, it is now considered wrong for a parser to return
1490 CSSx as its version and instead it is expected to return an uri
1491 corresponding to the uri of the CSS version that it supports. However,
1492 there is no uri for CSS3, but instead one uri per module. While this
1493 issue hasn't been resolved by the WG, I will stick to returning CSS3.
1494 However, B<the behaviour of this attribute is certain to change> in
1495 the future, so please avoid relying on it.
1496
1497 =item * $cf = $sac->ConditionFactory
1498
1499 =item * $sac->ConditionFactory($cf) or $sac->setConditionFactory($cf)
1500
1501 =item * $cf = $sac->SelectorFactory
1502
1503 =item * $sac->SelectorFactory($sf) or $sac->setSelectorFactory($sf)
1504
1505 =item * $cf = $sac->DocumentHandler
1506
1507 =item * $sac->DocumentHandler($dh) or $sac->setDocumentHandler($dh)
1508
1509 =item * $cf = $sac->ErrorHandler
1510
1511 =item * $sac->ErrorHandler($eh) or $sac->setErrorHandler($eh)
1512
1513 get/set the ConditionFactory, SelectorFactory, DocumentHandler,
1514 ErrorHandler that we use
1515
1516 =item * $sac->parse(\%options)
1517
1518 =item * $sac->parseStyleSheet(\%options)
1519
1520 parses a style sheet and sends events to the defined handlers. The
1521 options that you can use are:
1522
1523 =over 8
1524
1525 =item * string
1526
1527 =item * ioref
1528
1529 =item * filename
1530
1531 passes either a string, an open filehandle, or a filename to read the
1532 stylesheet from
1533
1534 =item * embedded
1535
1536 tells whether the stylesheet is embedded or not. This is most of the
1537 time useless but it will influence the interpretation of @charset
1538 rules. The latter being forbidden in embedded style sheets they will
1539 generate an ignorable_style_sheet event instead of a charset event if
1540 embedded is set to a true value.
1541
1542 =back
1543
1544 =item * $sac->parse_rule($string_ref)
1545
1546 =item * $sac->parseRule($string_ref)
1547
1548 parses a rule (with { and }). You probably don't need this one. It
1549 returns nothing, but generates the events.
1550
1551 =item * $sac->parse_style_declaration($string_ref)
1552
1553 =item * $sac->parseStyleDeclaration($string_ref)
1554
1555 same as parse_rule, but without the { and }. This is useful when you
1556 want to parse style declarations embedded using style attributes in
1557 HTML, SVG, etc... It returns nothing, but generates the events.
1558
1559 =item * $sac->parse_property_value($string_ref)
1560
1561 =item * $sac->parsePropertyValue($string_ref)
1562
1563 parses a property value and returns an array ref of lexical units
1564 (see CSS::SAC::LexicalUnit)
1565
1566 =item * $sac->parse_priority($string_ref)
1567
1568 =item * $sac->parsePriority($string_ref)
1569
1570 parses a priority and returns true if there is a priority value there.
1571
1572 =item * $sac->parse_selector_list($string_ref)
1573
1574 =item * $sac->parseSelectors($string_ref)
1575
1576 parses a list of selectors and returns an array ref of selectors
1577
1578 =back
1579
1580 =head1 OTHER METHODS
1581
1582 Methods in this section are of relevance mostly to the internal
1583 workings of the parser. I document them here but I don't really
1584 consider them part of the interface, and thus may change them if need
1585 be. If you are using them directly tell me about it and I will
1586 "officialize" them. These have no Java style equivalent.
1587
1588 =over 4
1589
1590 =item * $sac->parse_charset($string_ref)
1591
1592 parses a charset. It returns nothing, but generates the events.
1593
1594 =item * $sac->parse_imports($string_ref)
1595
1596 parses import rules. It returns nothing, but generates the events.
1597
1598 =item * $sac->parse_namespace_declarations($string_ref)
1599
1600 parses ns declarations. It returns nothing, but generates the events.
1601
1602 =item * $sac->parse_medialist($string_ref)
1603
1604 parses a list of media values and returns that list as an arrayref
1605
1606 =item * $sac->parse_comments($string_ref)
1607
1608 parses as many comments as there are at the beginning of the string.
1609 It returns nothing, but generates the events.
1610
1611 =item * $sac->parse_simple_selector($string_ref)
1612
1613 parses a simple selector and returns the selector object
1614
1615 =item * $sac->build_condition(\@tokens)
1616
1617 helper to build conditions (you probably don't want to use this at
1618 all...)
1619
1620 =back
1621
1622 =head1 CSS::SAC::DefaultErrorHandler
1623
1624 This is pretty much a non package, it is just there to provide the
1625 default error handler if you are too lazy to provide one yourself.
1626
1627 All it does is pretty simple. There are three error levels:
1628 C<warning>, C<error>, and C<fatal_error>. What it does is warn on the
1629 two first and die on the last. Yes, it ain't fancy but then you can
1630 plug anything more intelligent into it at any moment.
1631
1632
1633 =head1 CSS3 ISSUES
1634
1635 One problem is that I have modelled this parser after existing SAC
1636 implementations that do not take into account as much of CSS3 as it is
1637 possible to. Some parts of that are trivial, and I have provided
1638 support on my own in this module. Other parts though are more
1639 important and I believe that coordination between the SAC authors
1640 would be beneficial on these points (once the relevant CSS3 modules
1641 will have moved to REC).
1642
1643 =over 4
1644
1645 =item * new attribute conditions
1646
1647 CSS3-selectors introduces a bunch of new things, including new
1648 attribute conditions ^= (starts with), $= (ends with) and *=
1649 (contains). There are no corresponding constants for conditions, so I
1650 suggested SAC_STARTS_WITH_ATTRIBUTE_CONDITION,
1651 SAC_ENDS_WITH_ATTRIBUTE_CONDITION, SAC_CONTAINS_ATTRIBUTE_CONDITION.
1652
1653 Note that these constants have been added, together with the
1654 corresponding factory methods. However, they will remain undocumented
1655 and considered experimental until some consensus is reached on the
1656 matter.
1657
1658 =item * :root condition
1659
1660 The :root token confuses some people because they think it is
1661 equivalent to XPath's / root step. That is not so. XPath's root
1662 selects "above" the document element. CSS's :root tests whether an
1663 element is the document element, there is nothing above a document
1664 element. Thus :root on its own is equivalent to *:root. It's a
1665 condition, not a selector. E:root matches the E element that is also
1666 the document element (if there is one).
1667
1668 Thus, SAC_ROOT_NODE_SELECTOR does not apply and we need a new
1669 SAC_IS_ROOT_CONDITION constant.
1670
1671 Note that this constant has been added, together with the
1672 corresponding factory method. However, it will remain undocumented
1673 and considered experimental until some consensus is reached on the
1674 matter.
1675
1676 =item * other new pseudo-classes
1677
1678 :empty definitely needs a constant too I'd say.
1679
1680 Note that this constant has been added, together with the
1681 corresponding factory method. However, it will remain undocumented
1682 and considered experimental until some consensus is reached on the
1683 matter.
1684
1685 =item * an+b syntax in positional conditions
1686
1687 There is new syntax that allows for very customisable positional
1688 selecting. PositionalCondition needs to be updated to deal with that.
1689
1690 =back
1691
1692 =head1 BUGS
1693
1694 - the problem with attaching pseudo-elements to elements as
1695 coselectors. I'm not sure which is the right representation. Don't
1696 forget to update CSS::SAC::Writer too so that it writes it out
1697 properly.
1698
1699 - see Bjoern's list
1700
1701 =head1 ACKNOWLEDGEMENTS
1702
1703 - Bjoern Hoehrmann for his immediate reaction and much valuable
1704 feedback and suggestions. It's certainly much harder to type with all
1705 those fingers that all those Mafia padres have cut off, but at least
1706 I get work done much faster than before. And also those nasty bugs he
1707 kindly uncovered.
1708
1709 - Steffen Goeldner for spotting bugs and providing patches.
1710
1711 - Ian Hickson for very very very kind testing support, and all sorts
1712 of niceties.
1713
1714 - Manos Batsis for starting a very long discussion on this that
1715 eventually deviated into other very interesting topics, and for
1716 giving me some really weird style sheets to feed into this module.
1717
1718 - Simon St.Laurent for posting this on xmlhack.com and thus pointing a
1719 lot of people to this module (as seen in my referer logs).
1720
1721 And of course all the other people that have sent encouragement notes
1722 and feature requests.
1723
1724 =head1 TODO
1725
1726 - add a pointer to the SAC W3 page
1727
1728 - create the Exception classes
1729
1730 - update PositionalCondition to include logic that can normalize the
1731 an+n notation and add a method that given a position will return a
1732 boolean indicating whether it matches the condition.
1733
1734 - add stringify overloading to all classes so that they may be
1735 printed directly
1736
1737 - have parser version return an overloaded object that circumvents the
1738 current problems
1739
1740 - add docs on how to write a {Document,Error}Handler, right now there
1741 is example code in Writer, but it isn't all clearly explained.
1742
1743 - find a way to make the '-' prefix to properties optional
1744
1745 - add a filter that switches events to spec names, and that can be used
1746 directly through an option
1747
1748 - add DOM-like hasFeature support (in view of SAC 3)
1749
1750 - prefix all constants with SAC_. Keep the old ones around for a few
1751 versions, importable with :old-constants.
1752
1753 - update docs
1754
1755 =head1 AUTHOR
1756
1757 Robin Berjon <robin@knowscape.com>
1758
1759 This module is licensed under the same terms as Perl itself.
1760
1761 =cut
« SAC/Writer.pm ↑ Summary SAC/Selector/ProcessingInstruction.pm »