File Coverage

blib/lib/OCBNET/CSS3/Regex/Selectors.pm
Criterion Covered Total %
statement 15 15 100.0
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 22 22 100.0


line stmt bran cond sub pod time code
1             ###################################################################################################
2             # Copyright 2013/2014 by Marcel Greter
3             # This file is part of OCBNET-CSS3 (GPL3)
4             ####################################################################################################
5             # regular expressions to match css2/css3 selectors
6             ####################################################################################################
7             package OCBNET::CSS3::Regex::Selectors;
8             ####################################################################################################
9             our $VERSION = '0.2.5';
10             ####################################################################################################
11              
12 11     11   57 use strict;
  11         22  
  11         348  
13 11     11   54 use warnings;
  11         17  
  11         383  
14              
15             ####################################################################################################
16              
17             # load exporter and inherit from it
18 11     11   55 BEGIN { use Exporter qw(); our @ISA = qw(Exporter); }
  11     11   20  
  11         265  
  11         455  
19              
20             # define our functions that will be exported
21 11     11   327 BEGIN { our @EXPORT = qw($re_selector_rules); }
22              
23             # define our functions than can be exported
24 11     11   238 BEGIN { our @EXPORT_OK = qw($re_selector_rule $re_options); }
25              
26             ####################################################################################################
27              
28 11     11   71 use OCBNET::CSS3::Regex::Base;
  11         24  
  11         4352  
29              
30             ####################################################################################################
31              
32             # create matchers for the various css selector types
33             our $re_id = qr/\#$re_identifier/; # select single id
34             our $re_tag = qr/(?:$re_identifier|\*)/; # select single tag
35             our $re_class = qr/\.$re_identifier/; # select single class
36             our $re_pseudo = qr/\:{1,2}$re_identifier/; # select single pseudo
37              
38             ####################################################################################################
39              
40             # select attributes and values
41             # advanced css2/css3 selectors
42             our $re_attr = qr/
43             # open attribute
44             \[
45             # fetch a name
46             $re_identifier
47             \s*
48             (?:
49             # operator prefix
50             [\~\^\$\*\|]?
51             # the equal sign
52             \s* = \s*
53             # find value
54             (?:
55             # quoted string
56             \' $re_apo \'
57             | \" $re_quot \"
58             # escape char sequence
59             | (?: [^\)\\]+ | \\. )*
60             )
61             # has value
62             )?
63             # close attribute
64             \]
65             /x;
66              
67             ####################################################################################################
68              
69             # create expression to match a single rule
70             # example : DIV#id.class1.class2:hover
71             our $re_selector = qr/(?:
72             \*
73             | $re_attr* $re_pseudo+
74             | $re_class+ $re_attr* $re_pseudo*
75             | $re_id $re_class* $re_attr* $re_pseudo*
76             | $re_tag $re_id? $re_class* $re_attr* $re_pseudo*
77             )/x;
78              
79             ####################################################################################################
80              
81             # create expression to match complex rules
82             # example : #id DIV.class FORM A:hover
83             our $re_selector_rule = qr/$re_selector(?:(?:\s*[\>\+\~]\s*|\s+)$re_selector)*/;
84              
85             ####################################################################################################
86              
87             # create expression to match multiple complex rules
88             # example : #id DIV.class FORM A:hover, BODY DIV.header
89             our $re_selector_rules = qr/$re_selector_rule(?:\s*,\s*$re_selector_rule)*/;
90              
91             ####################################################################################################
92             ####################################################################################################
93             1;