File Coverage

blib/lib/Rose/HTML/Util.pm
Criterion Covered Total %
statement 17 22 77.2
branch 4 12 33.3
condition 1 6 16.6
subroutine 5 7 71.4
pod 2 4 50.0
total 29 51 56.8


line stmt bran cond sub pod time code
1             package Rose::HTML::Util;
2              
3 43     43   301 use strict;
  43         90  
  43         3782  
4              
5             require Exporter;
6             our @ISA = qw(Exporter);
7              
8             our @EXPORT_OK =
9             qw(escape_html unescape_html escape_uri escape_uri_component
10             encode_entities strip_html html_attrs_string);
11              
12             our %EXPORT_TAGS =
13             (
14             all =>
15             [
16             qw(escape_html unescape_html escape_uri escape_uri_component
17             encode_entities)
18             ]
19             );
20              
21 43     43   22976 use HTML::Entities();
  43         266370  
  43         1566  
22 43     43   18302 use URI::Escape;
  43         53692  
  43         20924  
23              
24             if(exists $ENV{'MOD_PERL'} && require mod_perl && $mod_perl::VERSION < 1.99)
25             {
26             require Apache::Util;
27              
28             #*escape_html = \&HTML::Entities::encode;
29             *escape_html = \&encode_entities;
30             *unescape_html = \&HTML::Entities::decode;
31             *escape_uri_component = \&Apache::Util::escape_uri;
32             }
33             else
34             {
35             #*escape_html = \&HTML::Entities::encode;
36             *escape_html = \&encode_entities;
37             *unescape_html = \&HTML::Entities::decode;
38             *escape_uri_component = \&URI::Escape::uri_escape;
39             }
40              
41             our $VERSION = '0.011';
42              
43 1093 50   1093 1 4347 sub encode_entities { HTML::Entities::encode_entities($_[0], @_ > 1 ? $_[1] : '<>&"') }
44              
45             sub escape_uri
46             {
47 0 0   0 1 0 URI::Escape::uri_escape($_[0],
    0          
48             (@_ > 1) ? (defined $_[1] ? $_[1] : ()) : q(^A-Za-z0-9\-_.,'!~*#?&()/?@\:\[\]=));
49             }
50              
51             sub html_attrs_string
52             {
53 61     61 0 536 my %attrs;
54              
55 61 50 33     236 if(@_ == 1 && ref $_[0] eq 'HASH')
    0 0        
56             {
57 61         89 %attrs = %{$_[0]};
  61         189  
58             }
59             elsif(@_ && @_ % 2 == 0)
60             {
61 0         0 %attrs = @_;
62             }
63              
64 61 100       201 return '' unless(keys %attrs);
65              
66 43         109 return ' ' . join(' ', map { $_ . q(=") . escape_html($attrs{$_}) . q(") }
  55         577  
67             sort keys(%attrs));
68             }
69              
70             sub strip_html
71             {
72 0     0 0   my($text) = shift;
73              
74             # XXX: dumb for now...
75 0           $text =~ s{<[^>]*?/?>}{}g;
76              
77 0           return $text;
78             }
79              
80             1;
81              
82              
83             __END__
84              
85             =head1 NAME
86              
87             Rose::HTML::Util - Utility functions for manipulating HTML.
88              
89             =head1 SYNOPSIS
90              
91             use Rose::HTML::Util qw(:all);
92              
93             $esc = escape_html($str);
94             $str = unescape_html($esc);
95              
96             $esc = escape_uri($str);
97             $str = unescape_uri($esc);
98              
99             $comp = escape_uri_component($str);
100              
101             $esc = encode_entities($str);
102              
103             =head1 DESCRIPTION
104              
105             L<Rose::HTML::Util> provides aliases and wrappers for common HTML manipulation functions. When running in a mod_perl 1.x web server environment, Apache's C-based functions are used in some cases.
106              
107             This all may seem silly, but I like to be able to pull these functions from a single location and get the fastest possible versions.
108              
109             =head1 EXPORTS
110              
111             L<Rose::HTML::Util> does not export any function names by default.
112              
113             The 'all' tag:
114              
115             use Rose::HTML::Util qw(:all);
116              
117             will cause the following function names to be imported:
118              
119             escape_html()
120             unescape_html()
121             escape_uri()
122             escape_uri_component()
123             encode_entities()
124              
125             =head1 FUNCTIONS
126              
127             =over 4
128              
129             =item B<escape_html STRING [, UNSAFE]>
130              
131             This method passes its arguments to L<HTML::Entities::encode_entities()|HTML::Entities/encode_entities>. If the list of unsafe characters is omitted, it defaults to C<E<lt>E<gt>&">
132              
133             =item B<unescape_html STRING>
134              
135             This method is an alias for L<HTML::Entities::decode()|HTML::Entities/decode>.
136              
137             =item B<escape_uri STRING>
138              
139             This is a wrapper for L<URI::Escape::uri_escape()|URI::Escapeuri_escape> that is intended to escape entire URIs. Example:
140              
141             $str = 'http://foo.com/bar?baz=1%&blay=foo bar'
142             $esc = escape_uri($str);
143              
144             print $esc; # http://foo.com/bar?baz=1%25&blay=foo%20bar
145              
146             In other words, it tries to escape all characters that need to be escaped in a URI I<except> those characters that are legitimately part of the URI: forward slashes, the question mark before the query, etc.
147              
148             The current implementation escapes all characters except those in this set:
149              
150             A-Za-z0-9\-_.,'!~*#?&()/?@:[]=
151              
152             Note that the URI-escaped string is not HTML-escaped. In order make a URI safe to include in an HTML page, call L<escape_html()|/escape_html> as well:
153              
154             $h = '<a href="' . escape_html(escape_uri($str)) . '">foo</a>';
155              
156             =item B<escape_uri_component STRING>
157              
158             When running under mod_perl 1.x, this is an alias for L<Apache::Util::escape_uri()|Apache::Util/escape_uri>. Otherwise, it's an alias for L<URI::Escape::uri_escape()|URI::Escapeuri_escape>.
159              
160             =item B<encode_entities STRING [, UNSAFE]>
161              
162             This method passes its arguments to L<HTML::Entities::encode_entities()|HTML::Entities/encode_entities>. If the list of unsafe characters is omitted, it defaults to C<E<lt>E<gt>&">
163              
164             =back
165              
166             =head1 AUTHOR
167              
168             John C. Siracusa (siracusa@gmail.com)
169              
170             =head1 LICENSE
171              
172             Copyright (c) 2010 by John C. Siracusa. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.