File Coverage

blib/lib/XML/Entities.pm
Criterion Covered Total %
statement 49 55 89.0
branch 13 22 59.0
condition n/a
subroutine 10 11 90.9
pod 2 3 66.6
total 74 91 81.3


line stmt bran cond sub pod time code
1             package XML::Entities;
2              
3 2     2   67409 use strict;
  2         5  
  2         85  
4 2     2   58 use 5.008; # Unicode all over
  2         8  
  2         224  
5 2     2   56 use Carp;
  2         15  
  2         207  
6 2     2   2028 use XML::Entities::Data;
  2         127  
  2         925  
7              
8             our $VERSION = '1.0001';
9              
10             eval { require HTML::Parser }; # for fast XS implemented _decode_entities
11              
12             if ( eval { HTML::Entities::UNICODE_SUPPORT() } ) {
13             *_decode_entities = \&HTML::Entities::_decode_entities;
14             }
15             else {
16             sub ord2chr {
17 0 0   0 0 0 if (substr($_[0], 0, 1) eq 'x') {
18 0         0 return chr(hex '0'.$_[0])
19             }
20             else {
21 0         0 return chr($_[0])
22             }
23             }
24             *_decode_entities = sub {
25             my $hash = pop;
26             $_[0] =~ s/ & (\#?) (\w+) \b (;?) /
27             $1
28             ? ord2chr($2)
29             : exists $hash->{$2}
30             ? $hash->{$2}
31             : exists $hash->{$2.';'}
32             ? $hash->{$2.';'}
33             : '&'.$1.$2.$3
34             /xeg;
35             };
36             }
37              
38             sub decode {
39 3500     3500 1 80908 my $set = shift;
40 3500         10142 my @set_names = XML::Entities::Data::names;
41 3500         8366 my $entity2char;
42 3500 100       7685 if (ref $set eq 'HASH') {
43 1         3 $entity2char = $set;
44             }
45             else {
46 80477         127635 croak "'$set' is not a valid entity set name. Choose one of: all @set_names"
47 3499 50       5181 if not grep {$_ eq $set} 'all', @set_names;
48 2     2   13 no strict 'refs';
  2         4  
  2         93  
49 3499         20619 $entity2char = "XML::Entities::Data::$set"->();
50 2     2   9 use strict;
  2         5  
  2         411  
51             }
52 3500 100       541298 if (defined wantarray) { @_ = @_ };
  3499         9954  
53 3500         9162 for (@_) {
54 3501         20391 _decode_entities($_, $entity2char);
55             }
56 3500         721299 return @_[0 .. $#_]
57             }
58              
59             sub numify {
60 1     1 1 2 my $set = shift;
61 1         8 my @set_names = XML::Entities::Data::names;
62 1         3 my $entity2char;
63 1 50       5 if (ref $set eq 'HASH') {
64 1         2 $entity2char = $set;
65             }
66             else {
67 0         0 croak "'$set' is not a valid entity set name. Choose one of: all @set_names"
68 0 0       0 if not grep {$_ eq $set} 'all', @set_names;
69 2     2   11 no strict 'refs';
  2         4  
  2         92  
70 0         0 $entity2char = "XML::Entities::Data::$set"->();
71 2     2   8 use strict;
  2         5  
  2         619  
72             }
73 1 50       5 if (defined wantarray) {
74 1         3 @_ = @_;
75             }
76 1         6 my %set = %$entity2char;
77             s/(?<= & ) ( [^\#] \w*? ) (?= ; )/
78 4 100       37 exists $set{$1}
    100          
79             ? '#'.ord($set{$1})
80             : exists $set{$1.';'}
81             ? '#'.ord($set{$1.';'})
82             : $1
83 1         11 /xeg for @_;
84 1 50       4 return @_ if wantarray;
85 1 50       10 return pop @_ if defined wantarray;
86             }
87              
88             1
89              
90             __END__