File Coverage

blib/lib/JavaScript/Bookmarklet.pm
Criterion Covered Total %
statement 12 24 50.0
branch 0 2 0.0
condition n/a
subroutine 4 5 80.0
pod 1 1 100.0
total 17 32 53.1


line stmt bran cond sub pod time code
1             package JavaScript::Bookmarklet;
2              
3 1     1   930 use strict;
  1         2  
  1         42  
4 1     1   6 use warnings;
  1         1  
  1         38  
5              
6 1     1   15 use base 'Exporter';
  1         1  
  1         125  
7             our @EXPORT_OK = qw(make_bookmarklet);
8             our $VERSION = '0.02';
9              
10 1     1   876 use URI::Escape qw(uri_escape_utf8);
  1         2037  
  1         356  
11              
12             sub make_bookmarklet {
13 0     0 1   my $src = shift;
14 0           my $no_src = shift;
15 0           $src =~ s{^// ?javascript:.+\n}{}
16             ; # Zap the first line if there's already a bookmarklet comment:
17 0           my $bookmarklet = $src;
18 0           $bookmarklet =~ s{^\s*//.+\n}{}gm; # Kill comments.
19 0           $bookmarklet =~ s{\t}{ }gm; # Tabs to spaces
20 0           $bookmarklet =~ s{ +}{ }gm; # Space runs to one space
21 0           $bookmarklet =~ s{^\s+}{}gm; # Kill line-leading whitespace
22 0           $bookmarklet =~ s{\s+$}{}gm; # Kill line-ending whitespace
23 0           $bookmarklet =~ s{\n}{}gm; # Kill newlines
24 0           $bookmarklet =
25             "javascript:"
26             . uri_escape_utf8($bookmarklet, qq('" \x00-\x1f\x7f-\xff))
27             ; # Escape single- and double-quotes, spaces, control chars, unicode:
28 0 0         return $no_src ? $bookmarklet : "// $bookmarklet\n" . $src;
29             }
30              
31             1;
32              
33             __END__