| 1 | #!/usr/bin/env perl | ||||||||||||||||||||||||||||
| 2 | package HTML::RewriteAttributes::Links; | ||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use base 'HTML::RewriteAttributes'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use HTML::Tagset (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use URI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 4 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 4 | my %rewritable_attrs; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 4 | for my $tag (keys %HTML::Tagset::linkElements) { | |||||||||||||||||||||||||||
| 14 | 112 | for my $attr (@{ $HTML::Tagset::linkElements{$tag} }) { | |||||||||||||||||||||||||||
| 15 | 172 | $rewritable_attrs{$tag}{$attr} = 1; | |||||||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | sub _should_rewrite { | ||||||||||||||||||||||||||||
| 20 | 36 | ▶ | my ($self, $tag, $attr) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_should_rewrite: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 36 | ▶ | return ( $rewritable_attrs{$tag} || {} )->{$attr}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$rewritable_attrs{$tag} || {}
MC/DC100%1/1
$rewritable_attrs{$tag} || {}
$rewritable_attrs{$tag}
Truth table100%2/2
A
A= $rewritable_attrs{$tag}
| |||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | sub _rewrite { | ||||||||||||||||||||||||||||
| 26 | 7 | ▶ | my ($self, $html, $arg) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_rewrite: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 7 | ▶ | if (!ref($arg)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 5 | $self->{rewrite_link_base} = $arg; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | $arg = sub { | ||||||||||||||||||||||||||||
| 32 | 18 | ▶ | my ($tag, $attr, $value) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 33 | 18 | my $uri = URI->new($value); | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | $uri = $uri->abs($self->{rewrite_link_base}) | ||||||||||||||||||||||||||||
| 36 | 18 | ▶ | unless defined $uri->scheme; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 18 | return $uri->as_string; | |||||||||||||||||||||||||||
| 39 | 5 | }; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 7 | $self->SUPER::_rewrite($html, $arg); | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | # if we see a base tag, steal its href for future link resolution | ||||||||||||||||||||||||||||
| 46 | sub _start_tag { | ||||||||||||||||||||||||||||
| 47 | 39 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_start_tag: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 48 | 39 | my ($tag, $attr, $attrseq, $text) = @_; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 39 | ▶ | if ($tag eq 'base' && defined $attr->{href}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$tag eq "base" and defined $attr->{"href"}
MC/DC50%1/2
$tag eq "base" and defined $attr->{"href"}
$tag eq "base"
defined $attr->{"href"}
Truth table66%2/3
A and B
A= $tag eq "base"B= defined $attr->{"href"}
| |||||||||||||||||||||||||||||
| 51 | 2 | $self->{rewrite_link_base} = $attr->{href}; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 39 | $self->SUPER::_start_tag(@_); | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 4 | 1; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | __END__ | ||||||||||||||||||||||||||||