bump version
[scpubgit/HTML-String.git] / lib / HTML / String.pm
CommitLineData
e1b4b35c 1package HTML::String;
2
3use strictures 1;
4use HTML::String::Value;
5use Exporter 'import';
6
d43ba904 7our $VERSION = '1.000006'; # 1.0.6
a38082d4 8
9$VERSION = eval $VERSION;
10
e1b4b35c 11our @EXPORT = qw(html);
12
13sub html {
14 HTML::String::Value->new($_[0]);
15}
16
171;
d86bdf82 18
19__END__
20
21=head1 NAME
22
23HTML::String - mark strings as HTML to get auto-escaping
24
25=head1 SYNOPSIS
26
27 use HTML::String;
28
29 my $not_html = 'Hello, Bob & Jake';
30
31 my $html = html('<h1>').$not_html.html('</h1>');
32
882026d2 33 print html($html); # <h1>Hello, Bob &amp; Jake</h1>
d86bdf82 34
35or, alternatively,
36
37 use HTML::String::Overload;
38
39 my $not_html = 'Hello, Bob & Jake';
40
41 my $html = do {
42 use HTML::String::Overload;
43 "<h1>${not_html}</h1>";
44 }
45
882026d2 46 print html($html); # <h1>Hello, Bob &amp; Jake</h1>
d86bdf82 47
48(but see the L<HTML::String::Overload> documentation for details and caveats).
49
50See also L<HTML::String::TT> for L<Template Toolkit|Template> integration.
51
52=head1 DESCRIPTION
53
54Tired of trying to remember which strings in your program need HTML escaping?
55
56Working on something small enough to not need a templating engine - or code
57heavy enough to be better done with strings - but wanting to be careful about
58user supplied data?
59
60Yeah, me too, sometimes. So I wrote L<HTML::String>.
61
62The idea here is to have pervasive HTML escaping that fails closed - i.e.
63escapes everything that it isn't explicitly told not to. Since in the era
64of XSS (cross site scripting) attacks it's a matter of security as well as
65of not serving mangled markup, I've preferred to err on the side of
66inconvenience in places in order to make it as hard as possible to screw up.
67
68We export a single subroutine, L</html>, whose sole purpose in life
69is to construct an L<HTML::String::Value> object from a string, which then
70obsessively refuses to be concatenated to anything else without escaping it
71unless you asked for that not to happen by marking the other thing as HTML
72too.
73
74So
75
76 html($thing).$other_thing
77
78will return an object where C<$thing> won't be escaped, but C<$other_thing>
79will. Keeping concatenating stuff is fine; internally it's an array of parts.
80
81Because html() will happily take something that's already wrapped into a
82value object, when we print it out we can do:
83
84 print html($final_result);
85
86safe in the knowledge that if we got passed a value object that won't break
87anything, but if by some combination of alarums, excursions and murphy
88strikes we still have just a plain string by that point, the plain string
89will still get escaped on the way out.
90
91If you've got distinct blocks of code where you're assembling HTML, instead
92of using L</html> a lot you can say "all strings in this block are HTML
93so please mark them all to not be escaped" using L<HTML::String::Overload> -
94
95 my $string = 'This is a "normal" string';
96
97 my $html;
98
99 {
100 use HTML::String::Overload; # valid until the end of the block
101
102 $html = '<foo>'.$string.'</foo>'; # the two strings are html()ified
103 }
104
105 print $html; # prints <foo>This is a &quot;normal&quot; string</foo>
106
107Note however that due to a perl bug, you can't use backslash escapes in
108a string and have it still get marked as an HTML value, so instead of
109
110 "<h1>\n<div>"
111
112you need to write
113
114 "<h1>"."\n"."</div>"
115
116at least as far as 5.16.1, which is current as I write this. See
117L<HTML::String::Overload> for more details.
118
119For integration with L<Template Toolkit|Template>, see L<HTML::String::TT>.
120
fc076557 121=head1 CHARACTERS THAT WILL BE ESCAPED
122
123HTML::String concerns itself with characters that have special meaning in
124HTML. Those which begin and end tags (< and >), those which begin an entity
125(&) and those which delimit attribute values (" and '). It outputs them
126in a fashion compatible with HTML 4 and newer and all versions of XHTML
127(assuming support for named entities in the parser). There are no known
128incompatibilities with browsers.
129
130HTML::String does not concern itself with other characters, it is assumed
131that HTML documents will be marked with a suitable character encoding via
132a Content-Type HTTP header and/or a meta element.
133
d86bdf82 134=head1 EXPORTS
135
136=head2 html
137
138 my $html = html($do_not_escape_this);
139
140Returns an L<HTML::String::Value> object containing a single string part
141marked not to be escaped.
142
143If you need to do something clever such as specifying packages for which
144to ignore escaping requests, see the L<HTML::String::Value> documentation
145and write your own subroutine - this one is as simple as
146
147 sub html {
148 return HTML::String::Value->new($_[0]);
149 }
150
151so providing configuration options would likely be more complicated and
152confusing than just writing the code.
153
154=head1 AUTHOR
155
156mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
157
158=head1 CONTRIBUTORS
159
fc076557 160dorward - David Dorward (cpan:DORWARD) <david@dorward.me.uk>
0d5353da 161rafl - Florian Ragwitz (cpan:FLORA) <rafl@debian.org>
d86bdf82 162
163=head1 COPYRIGHT
164
165Copyright (c) 2012 the HTML::String L</AUTHOR> and L</CONTRIBUTORS>
166as listed above.
167
168=head1 LICENSE
169
170This library is free software and may be distributed under the same terms
171as perl itself.
172
173=cut