Object/Parser/Default.pm

filter: Statementstmt 96.7% 59 / 61 Branchbran 58.3% 7 / 12 Conditioncond 50.0% 6 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 10 / 10 Podpod 100% 2 / 2 totaltotal 81.5% 84 / 103 CC 16 SCAR 21.5
CC
16
Cov
85%
add_rule
14.6
parse_string
4.1
BEGIN
1.0
CRAP 8.6
« Object/Element.pm ↑ Summary Object/Property.pm »
1 ##----------------------------------------------------------------------------
2 ## CSS Object Oriented - ~/lib/CSS/Object/Parser/Default.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::Default;
14 BEGIN
15 4 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶ use Module::Generic;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶ use parent qw( CSS::Object::Parser );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶ use CSS::Object::Rule;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶ use CSS::Object::Selector;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶ use CSS::Object::Property;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 our $VERSION = 'v0.2.0';
24 };
25  
26 ## add a style to the style list
27 # From css spec at http://www.w3.org/TR/REC-CSS2/selector.html#q1
28 # * Matches any element. Universal selector
29 # E Matches any E element (i.e., an element of type E).
30 # E F Matches any F element that is a descendant of an E element.
31 # E > F Matches any F element that is a child of an element E.
32 # E:first-child Matches element E when E is the first child of its parent.
33 # E + F Matches any F element immediately preceded by a sibling element E.
34 # E[foo] Matches any E element with the "foo" attribute set (whatever the value).
35 # E[foo="warning"] Matches any E element whose "foo" attribute value is exactly equal to "warning".
36 # E[foo~="warning"] Matches any E element whose "foo" attribute value is a list of space-separated values,
37 # one of which is exactly equal to "warning".
38 # E[lang|="en"] Matches any E element whose "lang" attribute has a hyphen-separated list of values
39 # beginning (from the left) with "en".
40 # DIV.warning Language specific. (In HTML, the same as DIV[class~="warning"].)
41 # E#myid Matches any E element with ID equal to "myid". ID selectors
42 sub add_rule
43 18 ▶{
Subroutine add_rule: called CC=13 SCAR=26.8
Pod documented
44 18 my $self = shift( @_ );
45 18 my $style = shift( @_ );
46 18 my $contents = shift( @_ );
47 18 ▶ my $css = $self->css || return( $self->error( "Our css object is gone!" ) );
Condition50%1/2
$self->css || (return $self->error('Our css object is gone!'))
l!l
180
MC/DC0%0/1
$self->css || (return $self->error('Our css object is gone!'))
$self->css
Truth table50%1/2
A
A= $self->css
Aresult
11
00
48
49 # my $rule = CSS::Object::Rule->new(
50 18 ▶ my $rule = $css->new_rule(
Condition50%1/2
$css->new_rule('debug', $self->debug) || (return $self->pass_error('CSS::Object::Rule'->error))
l!l
180
MC/DC0%0/1
$css->new_rule('debug', $self->debug) || (return $self->pass_error('CSS::Object::Rule'->error))
$css->new_rule('debug', $self->debug)
Truth table50%1/2
A
A= $css->new_rule('debug', $self->debug)
Aresult
11
00
51 # format => $self->format,
52 debug => $self->debug,
53 ) || return( $self->pass_error( CSS::Object::Rule->error ) );
54  
55 ## parse the selectors
56 18 for my $name ( split( /[[:blank:]\h]*,[[:blank:]\h]*/, $style ) )
57 {
58 ## my $sel = CSS::Object::Selector->new({
59 33 ▶ my $sel = $css->new_selector(
Condition50%1/2
$css->new_selector('name', $name, 'debug', $self->debug) || (return $self->error('Unable to create a new CSS::Object::Selector objet: ', 'CSS::Object::Selector'->error))
l!l
330
MC/DC0%0/1
$css->new_selector('name', $name, 'debug', $self->debug) || (return $self->error('Unable to create a new CSS::Object::Selector objet: ', 'CSS::Object::Selector'->error))
$css->new_selector('name', $name, 'debug', $self->debug)
Truth table50%1/2
A
A= $css->new_selector('name', $name, 'debug', $self->debug)
Aresult
11
00
60 name => $name,
61 # format => $self->format,
62 debug => $self->debug,
63 ) || return( $self->error( "Unable to create a new CSS::Object::Selector objet: ", CSS::Object::Selector->error ) );
64 33 ▶ $rule->add_selector( $sel ) || return( $self->error( "Unable to add selector name '$name' to rule: ", $rule->error ) );
Branch: 1 branch
BranchTrueFalseTotal
$rule->add_selector($sel) or return $self->error("Unable to add selector name '${name}' to rule: ", $rule->error) 33 0 33
65 }
66  
67 ## parse the properties
68 ## Check possible comments and replace any ';' inside so they do not mess up this parsing here
69 18 $contents =~ s{\/\*[[:blank:]\h]*(.*?)[[:blank:]\h]*\*\/}
70 {
71 2 my $cmt = $1;
72 2 $cmt =~ s/\;/__SEMI_COLON__/gs;
73 2 $cmt =~ s/\:/__COLON__/gs;
74 2 "/* $cmt */";
75 }sex;
76 18 foreach( grep{ /\S/ } split( /\;/, $contents ) )
77 {
78 ## Found one or more comments before the property
79 29 ▶ while( s/^[[:blank:]\h]*\/\*[[:blank:]\h]*(.*?)[[:blank:]\h]*\*\///s )
Branch: 1 branch
BranchTrueFalseTotal
if (s[^[[:blank:]\h]*/\*[[:blank:]\h]*(.*?)[[:blank:]\h]*\*/][]s) 2 29 31
80 {
81 2 my $txt = $1;
82 2 $txt =~ s/__SEMI_COLON__/\;/gs;
83 2 $txt =~ s/__COLON__/\:/gs;
84 2 ▶ my $cmt = $css->new_comment( [split( /\r?\n/, $txt )] ) || return( $self->error( "Unable to create a new CSS::Object::Comment object: ", CSS::Object::Comment->error ) );
Condition50%1/2
$css->new_comment([split(/\r?\n/, $txt, 0)]) || (return $self->error('Unable to create a new CSS::Object::Comment object: ', 'CSS::Object::Comment'->error))
l!l
20
MC/DC0%0/1
$css->new_comment([split(/\r?\n/, $txt, 0)]) || (return $self->error('Unable to create a new CSS::Object::Comment object: ', 'CSS::Object::Comment'->error))
$css->new_comment([split(/\r?\n/, $txt, 0)])
Truth table50%1/2
A
A= $css->new_comment([split(/\r?\n/, $txt, 0)])
Aresult
11
00
85 2 ▶ $rule->add_element( $cmt ) || return( $self->error( "Unable to add comment element to our rule: ", $rule->error ) );
Branch: 1 branch
BranchTrueFalseTotal
$rule->add_element($cmt) or return $self->error("Unable to add comment element to our rule: ", $rule->error) 2 0 2
86 }
87
88 29 ▶ unless( /^[[:blank:]\h]*(?<name>[\w\.\_\-]+)[[:blank:]\h]*:[[:blank:]\h]*(?<value>.*?)[[:blank:]\h]*$/ )
Branch: 1 branch
BranchTrueFalseTotal
unless (/^[[:blank:]\h]*(?<name>[\w\.\_\-]+)[[:blank:]\h]*:[[:blank:]\h]*(?<value>.*?)[[:blank:]\h]*$/) 0 29 29
89 {
90 0 return( $self->error( "Invalid or unexpected property '$_' in style '$style'" ) );
91 }
92 ## Put back the colon we temporarily substituted to avoid confusion in the parser
93 29 $+{value} =~ s/__COLON__/\:/gs;
94 # my( $prop_name, $prop_val ) = @+{qw(name value)};
95 my $prop = CSS::Object::Property->new({
96 debug => $self->debug,
97 name => $+{name},
98 value => $+{value},
99 # format => $rule->format,
100 29 ▶ }) || return( $self->error( "Unable to create a new CSS::Object::Property object: ", CSS::Object::Property->error ) );
Condition50%1/2
'CSS::Object::Property'->new({'debug', $self->debug, 'name', $+{'name'}, 'value', $+{'value'}}) || (return $self->error('Unable to create a new CSS::Object::Property object: ', 'CSS::Object::Property'->error))
l!l
290
MC/DC0%0/1
'CSS::Object::Property'->new({'debug', $self->debug, 'name', $+{'name'}, 'value', $+{'value'}}) || (return $self->error('Unable to create a new CSS::Object::Property object: ', 'CSS::Object::Property'->error))
'CSS::Object::Property'->new({'debug', $self->debug, 'name', $+{'name'}, 'value', $+{'value'}})
Truth table50%1/2
A
A= 'CSS::Object::Property'->new({'debug', $self->debug, 'name', $+{'name'}, 'value', $+{'value'}})
Aresult
11
00
101 29 ▶ $rule->add_property( $prop ) || return( $self->error( "Unable to add property name '$+{name}' to rule: ", $rule->error ) );
Branch: 1 branch
BranchTrueFalseTotal
$rule->add_property($prop) or return $self->error(qq[Unable to add property name '$+{"name"}' to rule: ], $rule->error) 29 0 29
102 }
103 # push( @{$self->{parent}->{styles}}, $rule );
104 18 return( $rule );
105 }
106  
107 sub parse_string
108 5 ▶{
Subroutine parse_string: called CC=4 SCAR=14.2
Pod documented
109 5 my $self = shift( @_ );
110 5 my $string = shift( @_ );
111  
112 5 $string =~ s/\r\n|\r|\n/ /g;
113
114 5 my $rules = Module::Generic::Array->new;
115 ## Split into styles
116 5 foreach( grep{ /\S/ } split( /(?<=\})/, $string ) )
117 {
118 18 ▶ unless( /^[[:blank:]\h]*([^{]+?)[[:blank:]\h]*\{(.*)\}[[:blank:]\h]*$/ )
Branch: 1 branch
BranchTrueFalseTotal
unless (/^[[:blank:]\h]*([^{]+?)[[:blank:]\h]*\{(.*)\}[[:blank:]\h]*$/) 0 18 18
119 {
120 0 return( $self->error( "Invalid or unexpected style data '$_'" ) );
121 }
122 18 ▶ my $rule = $self->add_rule( $1, $2 ) || return( $self->pass_error );
Condition50%1/2
$self->add_rule($1, $2) || (return $self->pass_error)
l!l
180
MC/DC0%0/1
$self->add_rule($1, $2) || (return $self->pass_error)
$self->add_rule($1, $2)
Truth table50%1/2
A
A= $self->add_rule($1, $2)
Aresult
11
00
123 18 $rules->push( $rule );
124 }
125 5 return( $rules );
126 }
127  
128 4 1;
129 # NOTE: POD
130 __END__
« Object/Element.pm ↑ Summary Object/Property.pm »