Transform/using/CSS/RegExp.pm

filter: Statementstmt 82.5% 33 / 40 Branchbran 60.0% 6 / 10 Conditioncond 66.6% 6 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 85.7% 6 / 7 Podpod 100% 3 / 3 totaltotal 72.0% 54 / 75 CC 9 SCAR 14.1
CC
9
Cov
76%
parse_string
6.1
parse_file
6.0
new
3.1
CRAP 4.1
« Transform/using/HTML/Parser.pm ↑ Summary Transform/SAX/Filter.pm »
1 package URL::Transform::using::CSS::RegExp;
2  
3 =head1 NAME
4  
5 URL::Transform::using::CSS::RegExp - regular expression parsing of the C<text/css> for url transformation
6  
7 =head1 SYNOPSIS
8  
9 my $urlt = URL::Transform::using::CSS::RegExp->new(
10 'output_function' => sub { $output .= "@_" },
11 'transform_function' => sub { return (join '|', @_) },
12 );
13 $urlt->parse_string("background: transparent url(/site/images/logo.png)");
14  
15 print "and this is the output: ", $output;
16  
17  
18 =head1 DESCRIPTION
19  
20 Performs an url transformation inside C<text/css> using regular expressions.
21  
22 This module is used by L<URL::Transform>.
23  
24 =cut
25  
26 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 3 our $VERSION = '0.01';
30  
31 3 ▶use Carp::Clan;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 3 ▶use base 'Class::Accessor::Fast';
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35  
36 3 our $STYLE_URL_REGEXP = qr{
37 (?:
38 # ex. "url('/site.css')"
39 ( # capture non url path of the string
40 url # url
41 \s* #
42 \( # (
43 \s* #
44 (['"]?) # opening ' or "
45 )
46 ( # the rest is url
47 .+? # non greedy "everything"
48 )
49 (
50 \2 # closing ' or "
51 \s* #
52 \) # )
53 )
54 |
55 # ex. "@import '/site.css'"
56 ( # capture non url path of the string
57 \@import # @import
58 \s+ #
59 (['"]) # opening ' or "
60 )
61 ( # the rest is url
62 .+? # non greedy "everything"
63 )
64 (
65 \6 # closing ' or "
66 )
67 )
68 }xmsi;
69  
70  
71 =head1 PROPERTIES
72  
73 output_function
74 transform_function
75  
76 =cut
77  
78 3 __PACKAGE__->mk_accessors(qw{
79 output_function
80 transform_function
81 });
82  
83 =head1 METHODS
84  
85 =head2 new
86  
87 Object constructor.
88  
89 Requires:
90  
91 output_function
92 transform_function
93  
94 =cut
95  
96 6 ▶sub new {
Subroutine new: called CC=3 SCAR=11.2
Pod documented
97 6 my $class = shift;
98  
99 6 my $self = $class->SUPER::new({ @_ });
100  
101 6 my $output_function = $self->output_function;
102 6 my $transform_function = $self->transform_function;
103
104 6 ▶ croak 'pass print function'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $output_function eq "CODE" 0 6 6
105 if not (ref $output_function eq 'CODE');
106
107 6 ▶ croak 'pass transform url function'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $transform_function eq "CODE" 0 6 6
108 if not (ref $transform_function eq 'CODE');
109
110 6 return $self;
111 }
112  
113  
114 =head2 parse_string($string)
115  
116 Submit meta content string for parsing.
117  
118 =cut
119  
120 6 ▶sub parse_string {
Subroutine parse_string: called CC=6 SCAR=18.0
Pod documented
121 6 my $self = shift;
122 6 my $string = shift;
123
124 # match css url-s and store the matches for later replacement
125 6 my @found_urls;
126 6 ▶ while ($string =~ m/$STYLE_URL_REGEXP/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /$URL::Transform::using::CSS::RegExp::STYLE_URL_REGEXP/g) 9 6 15
127 9 ▶ push @found_urls, {
Condition66%6/9
$3 || $7
l!l&&r!l&&!r
630
$3 || $7
l!l&&r!l&&!r
630
$4 || $8
l!l&&r!l&&!r
630
MC/DC0%0/2
$3 || $7
$3 $7
MC/DC0%0/2
$3 || $7
$3 $7
MC/DC0%0/2
$4 || $8
$4 $8
Truth table66%2/3
A || B
A= $3B= $7
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= $3B= $7
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= $4B= $8
ABresult
1X1
011
000
128 'url' => $3 || $7,
129 'pos' => (pos $string)-length($3 || $7)-length($4 || $8),
130 };
131 }
132
133 # replace the url-s backwards in the string
134 6 ▶ while (my $url_with_pos = pop @found_urls) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $url_with_pos = pop @found_urls) 9 6 15
135 # transform the url
136 9 my $original_url = $url_with_pos->{'url'};
137 9 my $url = $self->transform_function->(
138 'url' => $original_url,
139 );
140
141 # replace the original url with the new one
142 substr(
143 $string,
144 9 $url_with_pos->{'pos'},
145 length($original_url)
146 ) = $url;
147 }
148
149 6 $self->output_function->($string);
150 }
151  
152  
153 =head2 parse_file($file_name)
154  
155 Slurps the file and call $self->parse_string($content).
156  
157 =cut
158  
159 0 ▶sub parse_file {
Subroutine parse_file: not called CC=2 SCAR=17.9
Pod documented
160 0 my $self = shift;
161 0 my $file_name = shift;
162  
163 0 ▶ open my $fh, '<', $file_name or croak 'Can not open '.$file_name.': '.$!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file_name or croak('Can not open ' . $file_name . ': ' . $!) 0 0 0
164 0 my $string = do { local $/; <$fh> }; # slurp!
165 0 $self->parse_string($string);
166 }
167  
168  
169 3 1;
170  
171  
172 __END__
« Transform/using/HTML/Parser.pm ↑ Summary Transform/SAX/Filter.pm »