File Coverage

blib/lib/XML/Liberal/Remedy/EntityRef.pm
Criterion Covered Total %
statement 12 14 85.7
branch 7 8 87.5
condition n/a
subroutine 2 2 100.0
pod 0 1 0.0
total 21 25 84.0


line stmt bran cond sub pod time code
1             package XML::Liberal::Remedy::EntityRef;
2 5     5   5920 use strict;
  5         10  
  5         916  
3              
4             sub apply {
5 149     149 0 234 my $class = shift;
6 149         1353 my($driver, $error, $xml_ref) = @_;
7              
8 149 100       267 return 0 if $error->message !~
9             /^parser error : (?:EntityRef: expecting ';'|xmlParseEntityRef: no name)/;
10              
11             # If the document doesn't contain any PIs or CDATA sections, we might as
12             # well try to fix all broken entity references and named character
13             # references in one go. (If it does contain one of those, fixing
14             # everything would risk changing the content in ways the author wouldn't
15             # expect.)
16             #
17             # In principle, we should take care not to change comments either; but
18             # in practice, I'm prepared to consider comments fair game, given that
19             # the author can preserve them by merely generating a well-formed XML
20             # document.
21 22 100       388 if ($$xml_ref !~ / .<\? |
22 7 50       66 return 1 if $$xml_ref =~
23             s/&(?!\w+;|#(?:x[a-fA-F0-9]+|\d+);)/&/g;
24             }
25             else {
26 15         39 my $pos = $error->location;
27 15         127 while ($pos > 0) {
28 45         78 pos($$xml_ref) = $pos--;
29 45 100       158 return 1 if $$xml_ref =~ s/\G&/&/
30             }
31             }
32              
33 0           Carp::carp("Can't find unescaped &, error was: ", $error->summary);
34 0           return 0;
35             }
36              
37             1;