Object/Parser/Enhanced.pm

filter: Statementstmt 21.7% 23 / 106 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 48 MC/DCmcdc 0.0% 0 / 27 Subroutinesub 61.5% 8 / 13 Podpod 100% 3 / 3 totaltotal 13.2% 34 / 257 CC 49 SCAR 58.0
CC
49
Cov
11%
parse_string
600.0
parse_element
306.0
_split
56.0
CRAP 328.7
↑ Summary Object/Value.pm »
1 ##----------------------------------------------------------------------------
2 ## CSS Object Oriented - ~/lib/CSS/Object/Parser/Enhanced.pm
3 ## Version v0.2.0
4 ## Copyright(c) 2020 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2020/08/09
7 ## Modified 2024/09/05
8 ## All rights reserved
9 ##
10 ## This program is free software; you can redistribute it and/or modify it
11 ## under the same terms as Perl itself.
12 ##----------------------------------------------------------------------------
13 package CSS::Object::Parser::Enhanced;
14 BEGIN
15 1 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶ use Module::Generic;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶ use parent qw( CSS::Object::Parser );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶ use CSS::Object::Rule;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶ use CSS::Object::Selector;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶ use CSS::Object::Property;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 our $VERSION = 'v0.2.0';
24 };
25  
26 sub parse_string
27 0 ▶{
Subroutine parse_string: not called CC=24 SCAR=64.0
Pod documented
28 0 my $self = shift( @_ );
29 0 ▶ my $string = shift( @_ ) || return;
Condition0%0/2
shift @_ || (return)
l!l
00
MC/DC0%0/1
shift @_ || (return)
shift @_
Truth table0%0/2
A
A= shift @_
Aresult
11
00
30 0 ▶ my $css = $self->css || return( $self->error( "Our css object is gone!" ) );
Condition0%0/2
$self->css || (return $self->error('Our css object is gone!'))
l!l
00
MC/DC0%0/1
$self->css || (return $self->error('Our css object is gone!'))
$self->css
Truth table0%0/2
A
A= $self->css
Aresult
11
00
31 0 my $this = {};
32 0 ▶ for( my $pos = 0; $pos < length( $string ); $pos++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < length $string) 0 0 0
33 {
34 0 my $c = substr( $string, $pos, 1 );
35 0 my $next = substr( $string, $pos + 1, 1 );
36 0 ▶ my $prev = $pos > 0 ? substr( $string, $pos - 1, 1 ) : '';
Branch: 1 branch
BranchTrueFalseTotal
$pos > 0 ? : 0 0 0
37 0 ▶ if( $c eq '*' && $next eq '/' )
Branch: 5 branches
BranchTrueFalseTotal
if ($c eq '*' and $next eq '/') { } 0 0 0
elsif ($c eq '/' and $next eq '*') { } 0 0 0
elsif ($this->{'inside_comment'}) { } 0 0 0
elsif ($this->{'inside_statement'}) { } 0 0 0
elsif (not $this->{'inside_statement'} and $c =~ /^[\@\:\[\#\.a-zA-Z0-9]$/) { } 0 0 0
Condition0%0/3
$c eq "*" and $next eq "/"
!ll&&!rl&&r
000
MC/DC0%0/2
$c eq "*" and $next eq "/"
$c eq "*" $next eq "/"
Truth table0%0/3
A and B
A= $c eq "*"B= $next eq "/"
ABresult
0X0
100
111
38 {
39 0 $css->new_comment( [ split( /\r?\n/, $this->{line} ) ] )->add_to( $css );
40 0 $this->{line} = '';
41 0 $this->{inside_comment} = 0;
42 0 next;
43 }
44 ## We found a comment in between rules. Comments within rules are processed separately in parse_element
45 ▶ elsif( $c eq '/' && $next eq '*' )
Condition0%0/3
$c eq "/" and $next eq "*"
!ll&&!rl&&r
000
MC/DC0%0/2
$c eq "/" and $next eq "*"
$c eq "/" $next eq "*"
Truth table0%0/3
A and B
A= $c eq "/"B= $next eq "*"
ABresult
0X0
100
111
46 {
47 0 $this->{inside_comment}++;
48 0 next;
49 }
50 elsif( $this->{inside_comment} )
51 {
52 0 $this->{line} .= $c;
53 }
54 elsif( $this->{inside_statement} )
55 {
56 ## If we found a space and the next character is an opening brace, we are inside the element definition
57 0 ▶ if( $c =~ /^[[:space:]\h]$/ &&
Branch: 4 branches
BranchTrueFalseTotal
if ($c =~ /^[[:space:]\h]$/ and not $this->{'inside_quote'} and $next eq '{') { } 0 0 0
elsif ($this->{'inside_quote'}) { } 0 0 0
elsif ($c eq '"' || $c eq q['] and $prev ne '\\') { } 0 0 0
elsif ($this->{'inside_quote'}) { } 0 0 0
Condition0%0/6
$c =~ /^[[:space:]\h]$/ and not $this->{"inside_quote"} and $next eq "{"
!ll&&!rl&&r
000
$c =~ /^[[:space:]\h]$/ and not $this->{"inside_quote"}
!ll&&!rl&&r
000
MC/DC0%0/3
$c =~ /^[[:space:]\h]$/ and not $this->{"inside_quote"} and $next eq "{"
$c =~ /^[[:space:]\h]$/ not $this->{"inside_quote"} $next eq "{"
Truth table0%0/4
A and not(B) and C
A= $c =~ /^[[:space:]\h]$/B= not $this->{"inside_quote"}C= $next eq "{"
ABCresult
0XX0
10X0
1100
1111
58 !$this->{inside_quote} &&
59 $next eq '{' )
60 {
61 0 $this->{name} = $this->{buffer};
62 0 $this->{name} = $self->_trim( $this->{name} );
63 ## $pos + 1 because we skip the opening brace
64 $pos = $self->parse_element({
65 data => \$string,
66 pos => $pos + 1,
67 name => $this->{name},
68 0 });
69 }
70 elsif( $this->{inside_quote} )
71 {
72 0 ▶ if( $c eq $this->{inside_quote} && $prev ne '\\' )
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq $this->{"inside_quote"} and $prev ne "\\") 0 0 0
Condition0%0/3
$c eq $this->{"inside_quote"} and $prev ne "\\"
!ll&&!rl&&r
000
MC/DC0%0/2
$c eq $this->{"inside_quote"} and $prev ne "\\"
$c eq $this->{"inside_quote"} $prev ne "\\"
Truth table0%0/3
A and B
A= $c eq $this->{"inside_quote"}B= $prev ne "\\"
ABresult
0X0
100
111
73 {
74 0 $this->{inside_quote} = '';
75 }
76 0 $this->{buffer} .= $c;
77 }
78 ▶ elsif( ( $c eq '"' || $c eq "'" ) && $prev ne '\\' )
Condition0%0/6
$c eq "\"" || $c eq "'" and $prev ne "\\"
!ll&&!rl&&r
000
$c eq "\"" or $c eq "'"
l!l&&r!l&&!r
000
MC/DC0%0/3
$c eq "\"" || $c eq "'" and $prev ne "\\"
$c eq "\"" $c eq "'" $prev ne "\\"
Truth table0%0/5
A or B and C
A= $c eq "\""B= $c eq "'"C= $prev ne "\\"
ABCresult
00X0
1X00
0100
1X11
0111
79 {
80 0 ▶ if( $this->{inside_quote} )
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'inside_quote'}) { } 0 0 0
81 {
82 0 $this->{inside_quote} = '';
83 }
84 else
85 {
86 0 $this->{inside_quote} = $c;
87 }
88 0 $this->{buffer} .= $c;
89 }
90 elsif( $this->{inside_quote} )
91 {
92 0 $this->{buffer} .= $c;
93 }
94 else
95 {
96 0 $this->{buffer} .= $c;
97 }
98 }
99 ## We may have found an element, check the first character
100 ## NOTE: Confirm with rfc for the lawful characters
101 ▶ elsif( !$this->{inside_statement} && $c =~ /^[\@\:\[\#\.a-zA-Z0-9]$/ )
Condition0%0/3
not $this->{"inside_statement"} and $c =~ /^[\@\:\[\#\.a-zA-Z0-9]$/
!ll&&!rl&&r
000
MC/DC0%0/2
not $this->{"inside_statement"} and $c =~ /^[\@\:\[\#\.a-zA-Z0-9]$/
not $this->{"inside_statement"} $c =~ /^[\@\:\[\#\.a-zA-Z0-9]$/
Truth table0%0/3
not(A) and B
A= not $this->{"inside_statement"}B= $c =~ /^[\@\:\[\#\.a-zA-Z0-9]$/
ABresult
0X0
100
111
102 {
103 0 $this->{inside_statement}++;
104 0 $this->{buffer} = $c;
105 }
106 }
107 }
108  
109 sub parse_element
110 0 ▶{
Subroutine parse_element: not called CC=17 SCAR=57.2
Pod documented
111 0 my $self = shift( @_ );
112 0 my $opts = {};
113 0 ▶ $opts = shift( @_ ) if( $self->_is_hash( $_[0] ) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_hash($_[0]) 0 0 0
114 ## String reference
115 0 my $sref = $opts->{data};
116 0 my $pos = $opts->{pos};
117 0 ▶ die( "Value provided is not a scalar reference\n" ) if( ref( $sref ) ne 'SCALAR' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $sref ne "SCALAR" 0 0 0
118 0 ▶ die( "Position provided is not an integer\n" ) if( $pos !~ /^\-?\d+$/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $pos =~ /^\-?\d+$/ 0 0 0
119 0 ▶ my $css = $self->css || return( $self->error( "Our css object is gone!" ) );
Condition0%0/2
$self->css || (return $self->error('Our css object is gone!'))
l!l
00
MC/DC0%0/1
$self->css || (return $self->error('Our css object is gone!'))
$self->css
Truth table0%0/2
A
A= $self->css
Aresult
11
00
120 0 ▶ my $rule = $self->rule_from_token( $opts->{name} ) || return( $self->pass_error );
Condition0%0/2
$self->rule_from_token($opts->{'name'}) || (return $self->pass_error)
l!l
00
MC/DC0%0/1
$self->rule_from_token($opts->{'name'}) || (return $self->pass_error)
$self->rule_from_token($opts->{'name'})
Truth table0%0/2
A
A= $self->rule_from_token($opts->{'name'})
Aresult
11
00
121 0 my $p;
122 0 my $this = {};
123 0 ▶ for( $p = $pos; $p < length( $$sref ); $p++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($p < length $$sref) 0 0 0
124 {
125 0 my $c = substr( $string, $p, 1 );
126 0 my $next = substr( $string, $p + 1, 1 );
127 0 ▶ my $prev = $p > 0 ? substr( $string, $p - 1, 1 ) : '';
Branch: 1 branch
BranchTrueFalseTotal
$p > 0 ? : 0 0 0
128 0 ▶ if( ( $c eq "'" || $c eq '"' ) && $prev ne '\\' )
Branch: 4 branches
BranchTrueFalseTotal
if ($c eq q['] || $c eq '"' and $prev ne '\\') { } 0 0 0
elsif ($this->{'inside_quote'}) { } 0 0 0
elsif (not length $this->{'prop'}) { } 0 0 0
elsif ($c eq '{' and $prev ne '\\') { } 0 0 0
Condition0%0/6
$c eq "'" || $c eq "\"" and $prev ne "\\"
!ll&&!rl&&r
000
$c eq "'" or $c eq "\""
l!l&&r!l&&!r
000
MC/DC0%0/3
$c eq "'" || $c eq "\"" and $prev ne "\\"
$c eq "'" $c eq "\"" $prev ne "\\"
Truth table0%0/5
A or B and C
A= $c eq "'"B= $c eq "\""C= $prev ne "\\"
ABCresult
00X0
1X00
0100
1X11
0111
129 {
130 0 ▶ if( $this->{inside_quote} )
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'inside_quote'}) { } 0 0 0
131 {
132 0 $this->{inside_quote} = '';
133 }
134 else
135 {
136 0 $this->{inside_quote} = $c;
137 }
138 0 $this->{buffer} .= $c;
139 }
140 elsif( $this->{inside_quote} )
141 {
142 0 $this->{buffer} .= $c;
143 }
144 elsif( !length( $this->{prop} ) )
145 {
146 0 $this->{prop} = $c;
147 }
148 ## We are done with this property, and either this is the start of a sub element, such as with keyframes containing braces for the definitions of each frames
149 ▶ elsif( $c eq '{' && $prev ne '\\' )
Condition0%0/3
$c eq "{" and $prev ne "\\"
!ll&&!rl&&r
000
MC/DC0%0/2
$c eq "{" and $prev ne "\\"
$c eq "{" $prev ne "\\"
Truth table0%0/3
A and B
A= $c eq "{"B= $prev ne "\\"
ABresult
0X0
100
111
150 {
151 0 $this->{prop} .= $this->{buffer};
152 0 $this->{buffer} = '';
153 0 $this->{prop} = $self->_trim( $this->{prop} );
154 my $res = $self->parse_element({
155 name => $this->{prop},
156 0 ▶ data => $sref,
Condition0%0/2
$self->parse_element({'name', $this->{'prop'}, 'data', $sref, 'pos', $p + 1}) || (return $self->pass_error)
l!l
00
MC/DC0%0/1
$self->parse_element({'name', $this->{'prop'}, 'data', $sref, 'pos', $p + 1}) || (return $self->pass_error)
$self->parse_element({'name', $this->{'prop'}, 'data', $sref, 'pos', $p + 1})
Truth table0%0/2
A
A= $self->parse_element({'name', $this->{'prop'}, 'data', $sref, 'pos', $p + 1})
Aresult
11
00
157 ## After the opening brace we just found
158 pos => $p + 1,
159 }) || return( $self->pass_error );
160 0 my $props = $res->{properties};
161 0 $p = $res->{pos};
162 }
163 ## or we found semicolon which signals the start of the property value
164 else
165 {
166 0 $this->{buffer} .= $c;
167 }
168 }
169 0 $pos = $p;
170 0 return({ pos => $pos });
171 }
172  
173 ## "There are two kinds of statements"
174 ## https://developer.mozilla.org/en-US/docs/Web/CSS/Syntax#CSS_statements
175 sub rule_from_token
176 0 ▶{
Subroutine rule_from_token: not called CC=3 SCAR=24.8
Pod documented
177 0 my $self = shift( @_ );
178 0 ▶ my $token = shift( @_ ) || return( $self->error( "No token was provided to create associated rule" ) );
Condition0%0/2
shift @_ || (return $self->error('No token was provided to create associated rule'))
l!l
00
MC/DC0%0/1
shift @_ || (return $self->error('No token was provided to create associated rule'))
shift @_
Truth table0%0/2
A
A= shift @_
Aresult
11
00
179 ## If it's an at-rule
180 0 ▶ if( substr( $token, 0, 1 ) eq '@' )
Branch: 1 branch
BranchTrueFalseTotal
if (substr($token, 0, 1) eq '@') { } 0 0 0
181 {
182 }
183 else
184 {
185 0 my $selectors = $self->_split( $token );
186 }
187 }
188  
189 sub _split
190 0 ▶{
Subroutine _split: not called CC=7 SCAR=40.3
191 0 my $self = shift( @_ );
192 0 my $token = shift( @_ );
193 0 ▶ return( [] ) if( !length( $token ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $token 0 0 0
194 0 my $this = {};
195 0 ▶ for( my $i = 0; $i < length( $token ); $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $token) 0 0 0
196 {
197 0 my $c = substr( $token, $i, 1 );
198 0 my $next = substr( $token, $i + 1, 1 );
199 0 ▶ my $prev = $i > 0 ? substr( $token, $i - 1, 1 ) : '';
Branch: 1 branch
BranchTrueFalseTotal
$i > 0 ? : 0 0 0
200 0 ▶ if( $this->{inside_quote} )
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"inside_quote"}) 0 0 0
201 {
202 0 ▶ if( $c eq $this->{inside_quote} && $prev ne '\\' )
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq $this->{"inside_quote"} and $prev ne "\\") 0 0 0
Condition0%0/3
$c eq $this->{"inside_quote"} and $prev ne "\\"
!ll&&!rl&&r
000
MC/DC0%0/2
$c eq $this->{"inside_quote"} and $prev ne "\\"
$c eq $this->{"inside_quote"} $prev ne "\\"
Truth table0%0/3
A and B
A= $c eq $this->{"inside_quote"}B= $prev ne "\\"
ABresult
0X0
100
111
203 {
204 0 $this->{inside_quote} = '';
205 }
206 0 $this->{buffer} .= $c;
207 }
208 }
209 }
210  
211 sub _trim
212 0 ▶{
Subroutine _trim: not called CC=2 SCAR=17.9
213 0 my $self = shift( @_ );
214 0 my $text = shift( @_ );
215 0 ▶ return if( !length( $text ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $text 0 0 0
216 0 $text =~ s/^[[:blank:]\h\r\n\v]+|[[:blank:]\h\r\n\v]+$//gs;
217 0 return( $text );
218 }
219  
220 1 1;
221 # NOTE: POD
222 __END__
↑ Summary Object/Value.pm »