RewriteAttributes/Resources.pm

filter: Statementstmt 100% 65 / 65 Branchbran 90.9% 20 / 22 Conditioncond 70.5% 12 / 17 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 100% 12 / 12 Podpod n/a totaltotal 90.3% 112 / 124 SCAR 29.0
CC
18
Cov
93%
_start_tag
11.4
_handle_imports
4.0
_absolutify
2.0
CRAP 18.1
↑ Summary RewriteAttributes.pm »
1 #!/usr/bin/env perl
2 package HTML::RewriteAttributes::Resources;
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use base 'HTML::RewriteAttributes';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 6 our $VERSION = '0.03';
9  
10 6 my %rewritable_attrs = (
11 bgsound => { src => 1 },
12 body => { background => 1 },
13 img => { src => 1 },
14 input => { src => 1 },
15 table => { background => 1 },
16 td => { background => 1 },
17 th => { background => 1 },
18 tr => { background => 1 },
19 );
20  
21 sub _rewrite {
22 6 ▶ my $self = shift;
Subroutine _rewrite: called CC=1 SCAR=0.0
23 6 my $html = shift;
24 6 my $cb = shift;
25 6 my %args = @_;
26  
27 6 $self->{rewrite_inline_css_cb} = $args{inline_css};
28 6 $self->{rewrite_inline_imports} = $args{inline_imports};
29 6 $self->{rewrite_inline_imports_seen} = {};
30  
31 6 $self->SUPER::_rewrite($html, $cb);
32 }
33  
34 sub _should_rewrite {
35 19 ▶ my ($self, $tag, $attr) = @_;
Subroutine _should_rewrite: called CC=2 SCAR=6.9
36  
37 19 ▶ return ( $rewritable_attrs{$tag} || {} )->{$attr};
Condition100%2/2
$rewritable_attrs{$tag} || {}
l!l
910
MC/DC100%1/1
$rewritable_attrs{$tag} || {}
$rewritable_attrs{$tag}
Truth table100%2/2
A
A= $rewritable_attrs{$tag}
Aresult
11
00
38 }
39  
40 sub _invoke_callback {
41 7 ▶ my $self = shift;
Subroutine _invoke_callback: called CC=1 SCAR=0.0
42 7 my ($tag, $attr, $value) = @_;
43  
44 7 return $self->{rewrite_callback}->($value, tag => $tag, attr => $attr, rewriter => $self);
45 }
46  
47 sub _start_tag {
48 39 ▶ my $self = shift;
Subroutine _start_tag: called CC=11 SCAR=24.4
49 39 my ($tag, $attr, $attrseq, $text) = @_;
50  
51 39 ▶ if ($self->{rewrite_inline_css_cb}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"rewrite_inline_css_cb"}) 25 14 39
52 25 ▶ if ($tag eq 'link' and defined $attr->{type} and $attr->{type} eq 'text/css' and defined $attr->{href}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "link" and defined $attr->{"type"} and $attr->{"type"} eq "text/css" and defined $attr->{"href"}) 7 18 25
Condition66%6/9
$tag eq "link" and defined $attr->{"type"} and $attr->{"type"} eq "text/css" and defined $attr->{"href"}
!ll&&!rl&&r
1807
$tag eq "link" and defined $attr->{"type"} and $attr->{"type"} eq "text/css"
!ll&&!rl&&r
1807
$tag eq "link" and defined $attr->{"type"}
!ll&&!rl&&r
1807
MC/DC25%1/4
$tag eq "link" and defined $attr->{"type"} and $attr->{"type"} eq "text/css" and defined $attr->{"href"}
$tag eq "link" defined $attr->{"type"} $attr->{"type"} eq "text/css" defined $attr->{"href"}
Truth table40%2/5
A and B and C and D
A= $tag eq "link"B= defined $attr->{"type"}C= $attr->{"type"} eq "text/css"D= defined $attr->{"href"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
53 7 my $content = $self->_import($attr->{href});
54 7 ▶ if (defined $content) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $content) 7 0 7
55 7 $content = $self->_handle_imports($content, $attr->{href});
56 7 $self->{rewrite_html} .= "\n<style type=\"text/css\"";
57 7 ▶ $self->{rewrite_html} .= " media=\"$attr->{media}\"" if $attr->{media};
Branch: 1 branch
BranchTrueFalseTotal
if $attr->{"media"} 1 6 7
58 7 $self->{rewrite_html} .= ">\n<!--\n$content\n-->\n</style>\n";
59 7 return;
60 }
61 }
62 18 ▶ if ($tag eq 'style' and defined $attr->{type} and $attr->{type} eq 'text/css') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "style" and defined $attr->{"type"} and $attr->{"type"} eq "text/css") 2 16 18
Condition66%4/6
$tag eq "style" and defined $attr->{"type"} and $attr->{"type"} eq "text/css"
!ll&&!rl&&r
1602
$tag eq "style" and defined $attr->{"type"}
!ll&&!rl&&r
1602
MC/DC33%1/3
$tag eq "style" and defined $attr->{"type"} and $attr->{"type"} eq "text/css"
$tag eq "style" defined $attr->{"type"} $attr->{"type"} eq "text/css"
Truth table50%2/4
A and B and C
A= $tag eq "style"B= defined $attr->{"type"}C= $attr->{"type"} eq "text/css"
ABCresult
0XX0
10X0
1100
1111
63 2 $self->{rewrite_look_for_style} = 1;
64 }
65 }
66  
67 32 $self->SUPER::_start_tag(@_);
68 }
69  
70 sub _default {
71 97 ▶ my ($self, $tag, $attrs, $text) = @_;
Subroutine _default: called CC=2 SCAR=6.9
72 97 ▶ if (delete $self->{rewrite_look_for_style}) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $self->{"rewrite_look_for_style"}) 2 95 97
73 2 $text = $self->_handle_imports($text, '.');
74 }
75  
76 97 $self->SUPER::_default($tag, $attrs, $text);
77 }
78  
79 sub _handle_imports {
80 19 ▶ my $self = shift;
Subroutine _handle_imports: called CC=4 SCAR=13.9
81 19 my $content = shift;
82 19 my $base = shift;
83  
84 19 ▶ return $content if !$self->{rewrite_inline_imports};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"rewrite_inline_imports"} 9 10 19
85  
86 # here we both try to preserve comments *and* ignore any @import
87 # statements that are in comments
88 10 $content =~ s{
89 ( /\* .*? \*/ )
90 |
91 (//[^\n]*)
92 |
93 \@import \s* " ([^"]+) " \s* ;
94 }{
95 25 ▶ defined($1) ? $1
Branch: 2 branches
BranchTrueFalseTotal
defined $1 ? : 16 9 25
defined $2 ? : 2 7 9
96 : defined($2) ? $2
97 : $self->_import($self->_absolutify($3, $base))
98 }xsmeg;
99  
100 10 return $content;
101 }
102  
103 sub _absolutify {
104 7 ▶ my $self = shift;
Subroutine _absolutify: called CC=2 SCAR=7.0
105 7 my $path = shift;
106 7 my $base = shift;
107  
108 7 my $uri = URI->new($path);
109 7 ▶ unless (defined $uri->scheme) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $uri->scheme) 7 0 7
110 7 $uri = $uri->abs($base);
111 }
112  
113 7 return $uri->as_string;
114 }
115  
116 sub _import {
117 14 ▶ my $self = shift;
Subroutine _import: called CC=2 SCAR=6.9
118 14 my $path = shift;
119  
120 14 ▶ return '' if $self->{rewrite_inline_imports_seen}{$path}++;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"rewrite_inline_imports_seen"}{$path}++ 4 10 14
121  
122 my $content = "\n/* $path */\n"
123 10 . $self->{rewrite_inline_css_cb}->($path);
124 10 return $self->_handle_imports($content, $path);
125 }
126  
127 6 1;
128  
129 __END__
↑ Summary RewriteAttributes.pm »