File Coverage

blib/lib/Test/DoubleEncodedEntities.pm
Criterion Covered Total %
statement 40 40 100.0
branch 5 6 83.3
condition 1 2 50.0
subroutine 9 9 100.0
pod 1 1 100.0
total 56 58 96.5


line stmt bran cond sub pod time code
1             package Test::DoubleEncodedEntities;
2 1     1   43855 use base qw(Exporter);
  1         2  
  1         139  
3              
4 1     1   25 use 5.006;
  1         6  
  1         55  
5 1     1   6 use strict;
  1         8  
  1         41  
6 1     1   7 use warnings;
  1         1  
  1         74  
7              
8             our @EXPORT;
9             our $VERSION = "2.00";
10              
11 1     1   2642 use HTML::TokeParser::Simple;
  1         37301  
  1         35  
12 1     1   801 use Test::DoubleEncodedEntities::Entities;
  1         5  
  1         162  
13              
14 1     1   9 use Carp qw(croak);
  1         2  
  1         61  
15              
16 1     1   6 use Test::Builder;
  1         2  
  1         358  
17             my $tester = Test::Builder->new();
18              
19             my $entities = join "|", @entities;
20              
21             sub ok_dee {
22 8     8 1 14957 my $input = shift;
23 8   50     30 my $name = shift || "double encoded entity test";
24              
25             # parse the input
26 8 50       50 my $p = HTML::TokeParser::Simple->new( \$input )
27             or croak "Can't parse input";
28              
29 8         1275 $p->unbroken_text(1);
30              
31             # search all text bits for problems
32 8         12 my %oops;
33 8         29 while ( my $token = $p->get_token ) {
34 42 100       2065 next unless $token->is_text;
35 8         51 my $string = $token->as_is;
36              
37             # look for bad entities
38 8         4945 $oops{$_}++ foreach $string =~ m/(&(?:amp|\#0*38);(?:$entities|\#\d+);)/gox;
39             }
40              
41             # did we get away okay?
42 8 100       213 unless(%oops) {
43 2         10 return $tester->ok(1,$name)
44             }
45              
46             # report the problem
47 6         28 $tester->ok(0, $name);
48 6         5549 foreach (sort { $a cmp $b } keys %oops) {
  1         6  
49 7         114 $tester->diag(qq{Found $oops{$_} "$_"\n})
50             }
51              
52             # return 0 as we got an error
53 6         5472 return 0;
54             }
55             push @EXPORT, "ok_dee";
56              
57             =head1 NAME
58              
59             Test::DoubleEncodedEntities - check for double encoded entities
60              
61             =head1 SYNOPSIS
62              
63             use Test::More tests => 1;
64             use Test::DoubleEncodedEntities;
65              
66             ok_dee('é', "ent test");
67              
68             =head1 DESCRIPTION
69              
70             This testing module huristically checks for double
71             encoded HTML entities in your string.
72              
73             =head2 Functions
74              
75             This module automatically exports the following function:
76              
77             =over
78              
79             =item ok_dee($string)
80              
81             =item ok_dee($string, $test_description)
82              
83             This module knows about all the entities defined in the HTML5
84             working draft and numerical entities.
85              
86             =back
87              
88             =head1 BUGS
89              
90             This module only checks the body text; Entities in attributes
91             are ignored as often you may want to double encoded entities on
92             purpose in things like URLs.
93              
94             Bugs (and requests for new features) can be reported though the CPAN
95             RT system:
96             L
97              
98             Alternatively, you can simply fork this project on github and
99             send me pull requests. Please see L
100              
101             =head1 AUTHOR
102              
103             Written by Mark Fowler B
104              
105             Copyright Mark Fowler 2004, 2011, 2012.
106              
107             This program is free software; you can redistribute it and/or modify
108             it under the same terms as Perl itself.
109              
110             =head1 SEE ALSO
111              
112             L
113              
114             =cut
115              
116             1;