documentationify
[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
7our @EXPORT = qw(html);
8
9sub html {
10 HTML::String::Value->new($_[0]);
11}
12
131;
d86bdf82 14
15__END__
16
17=head1 NAME
18
19HTML::String - mark strings as HTML to get auto-escaping
20
21=head1 SYNOPSIS
22
23 use HTML::String;
24
25 my $not_html = 'Hello, Bob & Jake';
26
27 my $html = html('<h1>').$not_html.html('</h1>');
28
29 print html($html); # <h1>Hello, Bob &quot; Jake</h1>
30
31or, alternatively,
32
33 use HTML::String::Overload;
34
35 my $not_html = 'Hello, Bob & Jake';
36
37 my $html = do {
38 use HTML::String::Overload;
39 "<h1>${not_html}</h1>";
40 }
41
42 print html($html); # <h1>Hello, Bob &quot; Jake</h1>
43
44(but see the L<HTML::String::Overload> documentation for details and caveats).
45
46See also L<HTML::String::TT> for L<Template Toolkit|Template> integration.
47
48=head1 DESCRIPTION
49
50Tired of trying to remember which strings in your program need HTML escaping?
51
52Working on something small enough to not need a templating engine - or code
53heavy enough to be better done with strings - but wanting to be careful about
54user supplied data?
55
56Yeah, me too, sometimes. So I wrote L<HTML::String>.
57
58The idea here is to have pervasive HTML escaping that fails closed - i.e.
59escapes everything that it isn't explicitly told not to. Since in the era
60of XSS (cross site scripting) attacks it's a matter of security as well as
61of not serving mangled markup, I've preferred to err on the side of
62inconvenience in places in order to make it as hard as possible to screw up.
63
64We export a single subroutine, L</html>, whose sole purpose in life
65is to construct an L<HTML::String::Value> object from a string, which then
66obsessively refuses to be concatenated to anything else without escaping it
67unless you asked for that not to happen by marking the other thing as HTML
68too.
69
70So
71
72 html($thing).$other_thing
73
74will return an object where C<$thing> won't be escaped, but C<$other_thing>
75will. Keeping concatenating stuff is fine; internally it's an array of parts.
76
77Because html() will happily take something that's already wrapped into a
78value object, when we print it out we can do:
79
80 print html($final_result);
81
82safe in the knowledge that if we got passed a value object that won't break
83anything, but if by some combination of alarums, excursions and murphy
84strikes we still have just a plain string by that point, the plain string
85will still get escaped on the way out.
86
87If you've got distinct blocks of code where you're assembling HTML, instead
88of using L</html> a lot you can say "all strings in this block are HTML
89so please mark them all to not be escaped" using L<HTML::String::Overload> -
90
91 my $string = 'This is a "normal" string';
92
93 my $html;
94
95 {
96 use HTML::String::Overload; # valid until the end of the block
97
98 $html = '<foo>'.$string.'</foo>'; # the two strings are html()ified
99 }
100
101 print $html; # prints <foo>This is a &quot;normal&quot; string</foo>
102
103Note however that due to a perl bug, you can't use backslash escapes in
104a string and have it still get marked as an HTML value, so instead of
105
106 "<h1>\n<div>"
107
108you need to write
109
110 "<h1>"."\n"."</div>"
111
112at least as far as 5.16.1, which is current as I write this. See
113L<HTML::String::Overload> for more details.
114
115For integration with L<Template Toolkit|Template>, see L<HTML::String::TT>.
116
117=head1 EXPORTS
118
119=head2 html
120
121 my $html = html($do_not_escape_this);
122
123Returns an L<HTML::String::Value> object containing a single string part
124marked not to be escaped.
125
126If you need to do something clever such as specifying packages for which
127to ignore escaping requests, see the L<HTML::String::Value> documentation
128and write your own subroutine - this one is as simple as
129
130 sub html {
131 return HTML::String::Value->new($_[0]);
132 }
133
134so providing configuration options would likely be more complicated and
135confusing than just writing the code.
136
137=head1 AUTHOR
138
139mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
140
141=head1 CONTRIBUTORS
142
143None yet - maybe this software is perfect! (ahahahahahahahahaha)
144
145=head1 COPYRIGHT
146
147Copyright (c) 2012 the HTML::String L</AUTHOR> and L</CONTRIBUTORS>
148as listed above.
149
150=head1 LICENSE
151
152This library is free software and may be distributed under the same terms
153as perl itself.
154
155=cut