File Coverage

lib/URL/Transform/using/CSS/RegExp.pm
Criterion Covered Total %
statement 29 36 80.5
branch 2 6 33.3
condition 6 9 66.6
subroutine 6 7 85.7
pod 3 3 100.0
total 46 61 75.4


line stmt bran cond sub pod time code
1             package URL::Transform::using::CSS::RegExp;
2              
3             =head1 NAME
4              
5             URL::Transform::using::CSS::RegExp - regular expression parsing of the C 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 using regular expressions.
21              
22             This module is used by L.
23              
24             =cut
25              
26 2     2   49655 use warnings;
  2         5  
  2         72  
27 2     2   61 use strict;
  2         5  
  2         114  
28              
29             our $VERSION = '0.01';
30              
31 2     2   844 use Carp::Clan;
  2         4562  
  2         20  
32              
33 2     2   358 use base 'Class::Accessor::Fast';
  2         3  
  2         1572  
34              
35              
36             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             __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             sub new {
97 6     6 1 2209 my $class = shift;
98              
99 6         49 my $self = $class->SUPER::new({ @_ });
100              
101 6         85 my $output_function = $self->output_function;
102 6         52 my $transform_function = $self->transform_function;
103            
104 6 50       40 croak 'pass print function'
105             if not (ref $output_function eq 'CODE');
106            
107 6 50       16 croak 'pass transform url function'
108             if not (ref $transform_function eq 'CODE');
109            
110 6         18 return $self;
111             }
112              
113              
114             =head2 parse_string($string)
115              
116             Submit meta content string for parsing.
117              
118             =cut
119              
120             sub parse_string {
121 6     6 1 14 my $self = shift;
122 6         10 my $string = shift;
123            
124             # match css url-s and store the matches for later replacement
125 6         10 my @found_urls;
126 6         86 while ($string =~ m/$STYLE_URL_REGEXP/g) {
127 9   66     176 push @found_urls, {
      66        
      66        
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         39 while (my $url_with_pos = pop @found_urls) {
135             # transform the url
136 9         18 my $original_url = $url_with_pos->{'url'};
137 9         28 my $url = $self->transform_function->(
138             'url' => $original_url,
139             );
140            
141             # replace the original url with the new one
142 9         147 substr(
143             $string,
144             $url_with_pos->{'pos'},
145             length($original_url)
146             ) = $url;
147             }
148            
149 6         24 $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             sub parse_file {
160 0     0 1   my $self = shift;
161 0           my $file_name = shift;
162              
163 0 0         open my $fh, '<', $file_name or croak 'Can not open '.$file_name.': '.$!;
164 0           my $string = do { local $/; <$fh> }; # slurp!
  0            
  0            
165 0           $self->parse_string($string);
166             }
167              
168              
169             1;
170              
171              
172             __END__