File Coverage

blib/lib/HTML/Blitz/SSSelector.pm
Criterion Covered Total %
statement 59 60 98.3
branch 34 44 77.2
condition 27 30 90.0
subroutine 5 5 100.0
pod 0 3 0.0
total 125 142 88.0


line stmt bran cond sub pod time code
1             # This code can be redistributed and modified under the terms of the GNU
2             # General Public License as published by the Free Software Foundation, either
3             # version 3 of the License, or (at your option) any later version.
4             # See the "COPYING" file for details.
5             package HTML::Blitz::SSSelector 0.1001;
6 11     11   123 use HTML::Blitz::pragma;
  11         29  
  11         238  
7 11         3898 use HTML::Blitz::SelectorType qw(
8             ST_FALSE
9             ST_TAG_NAME
10             ST_ATTR_HAS
11             ST_ATTR_EQ
12             ST_ATTR_PREFIX
13             ST_ATTR_SUFFIX
14             ST_ATTR_INFIX
15             ST_ATTR_LIST_HAS
16             ST_ATTR_LANG_PREFIX
17             ST_NTH_CHILD
18             ST_NTH_CHILD_OF_TYPE
19 11     11   20477 );
  11         32  
20              
21 290 50 33 290 0 869 method new($class: :$simple_selectors, :$link_type) {
  290 50       1462  
  290 50       696  
  290 50       1128  
  290 50       911  
  290         894  
  290         770  
  290         464  
22 290         1939 bless {
23             simplesel => \@$simple_selectors,
24             link_type => $link_type,
25             }, $class
26             }
27              
28 521 50   521 0 1213 method link_type() {
  521 50       7784  
  521         856  
  521         651  
29             $self->{link_type}
30 521         1270 }
31              
32 1806 50   1806 0 3883 method matches($tag, $attributes, $nth, $nth_of_type) {
  1806 50       3721  
  1806         2693  
  1806         7935  
  1806         2462  
33 1806         2585 for my $sel (@{$self->{simplesel}}) {
  1806         4662  
34 1916         2816 my $match;
35 1916         5156 my $type = $sel->{type};
36 1916 100 66     7728 if ($type eq ST_FALSE) {
    100          
    100          
    100          
    100          
    100          
    100          
    100          
    100          
    50          
37 16         30 $match = 0;
38             } elsif ($type eq ST_TAG_NAME) {
39 755   100     2941 $match = $sel->{name} eq '*' || $sel->{name} eq $tag;
40             } elsif ($type eq ST_ATTR_HAS) {
41 33         103 $match = exists $attributes->{$sel->{attr}};
42             } elsif ($type eq ST_ATTR_EQ) {
43 548         1034 my $attr = $sel->{attr};
44 548   100     1614 $match = exists $attributes->{$attr} && $attributes->{$attr} eq $sel->{value};
45             } elsif ($type eq ST_ATTR_PREFIX) {
46 52         155 my $attr = $sel->{attr};
47 52         100 my $value = $sel->{value};
48 52   100     236 $match = exists $attributes->{$attr} && substr($attributes->{$attr}, 0, length $value) eq $value;
49             } elsif ($type eq ST_ATTR_SUFFIX) {
50 12         29 my $attr = $sel->{attr};
51 12         26 my $value = $sel->{value};
52 12   100     54 $match = exists $attributes->{$attr} && substr($attributes->{$attr}, -length $value) eq $value;
53             } elsif ($type eq ST_ATTR_INFIX) {
54 12         25 my $attr = $sel->{attr};
55 12         25 my $value = $sel->{value};
56 12   100     57 $match = exists $attributes->{$attr} && index($attributes->{$attr}, $value) >= 0;
57             } elsif ($type eq ST_ATTR_LIST_HAS) {
58 325         629 my $attr = $sel->{attr};
59 325         623 my $value = $sel->{value};
60 325   100     1044 $match = exists $attributes->{$attr} && do {
61             my $r = 0;
62             for my $elem ($attributes->{$attr} =~ /[^ \t\n\r\f]+/g) {
63             if ($elem eq $value) {
64             $r = 1;
65             last;
66             }
67             }
68             $r
69             };
70             } elsif ($type eq ST_ATTR_LANG_PREFIX) {
71 8         51 my $attr = $sel->{attr};
72 8         17 my $value = $sel->{value};
73 8   100     95 $match = exists $attributes->{$attr} && $attributes->{$attr} =~ /\A\Q$value\E(?![^\-])/;
74             } elsif ($type eq ST_NTH_CHILD || $type eq ST_NTH_CHILD_OF_TYPE) {
75 155 100       291 my $x = $type eq ST_NTH_CHILD ? $nth : $nth_of_type;
76 155         235 my $ka = $sel->{a};
77 155         815 my $kb = $sel->{b};
78 155         220 my $d = $x - $kb;
79 155   100     707 $match = $d == 0 || ($ka != 0 && ($d < 0) == ($ka < 0) && $d % $ka == 0);
80             } else {
81 0         0 die "Internal error: invalid simple selector type '$type'";
82             }
83              
84 1916 100       4017 if ($sel->{negated}) {
85 66         87 $match = !$match;
86             }
87              
88 1916 100       8693 $match or return 0;
89             }
90              
91             1
92 521         2154 }
93              
94             1