1 package HTML::String::TT::Directive;
4 use HTML::String::Overload ();
7 use base qw(Template::Directive);
11 s/sub {/sub { package HTML::String::TT::_TMPL; use HTML::String::Overload { ignore => { q{Template::Provider} => 1, q{Template::Directive} => 1, q{Template::Document} => 1, q{Template::Plugins} => 1 } };/;
12 } Template::Directive::pad(shift->SUPER::template(@_), 2);
15 # TT code does &text(), no idea why
18 my ($self, $text) = @_;
19 return $Template::Directive::OUTPUT.' '.$self->text($text).';';
22 # https://rt.perl.org/rt3/Ticket/Display.html?id=49594
25 my ($class, $text) = @_;
27 # We need to turn everything into escapes, including wide chars
28 # that will end up as e.g. \342\200\223 if 'use utf8' isn't in
29 # scope or \x{...} if it is. So we run it through perlstring first
30 # so everything is already a backslash escape sequence (because the
31 # exact same bug can apply to wide chars in place), and then hit
32 # it with an ugly regexp to turn it into e.g.
34 # "<li>foo "."\342".""."\200".""."\223"." bar.</li>"
36 # which then gets overload::constant'ed appropriately.
38 # The first two lines of the s! were assembled from the escape sequences
39 # table in "Quote and Quote-like Operators" in perlop by Lucas Mai, then
40 # the . handles any other single character escape (\$, \@, \\ etc.)
42 my $str = perlstring $text;
44 \\ ( [abefnrt] | c. | o \{ [0-7]+ \} | x (?: \{ [[:xdigit:]]+ \}
45 | [[:xdigit:]]{1,2} ) | N \{ [^{}]* \} | [0-7]{1,3}
58 HTML::String::TT::Directive - L<Template::Directive> overrides to forcibly escape HTML strings
62 This is not user serviceable, and is documented only for your edification.
64 Please use L<HTML::String::TT> as this module could change, be renamed, or
65 if I figure out a better way of implementing the functionality disappear
66 entirely at any moment.
72 We override this top-level method in order to pretend two things to the
73 perl subroutine definition that TT has generated - firstly,
75 package HTML::String::TT::_TMPL;
77 to ensure that no packages marked to be ignored are the current one when
78 the template code is executed. Secondly,
80 use HTML::String::Overload { ignore => { ... } };
82 where the C<...> contains a list of TT internal packages to ignore so that
83 things actually work. This list is not duplicated here since it may also
84 change without warning.
86 Additionally, the hashref option to L<HTML::String::Overload> is not
87 documented there since I'm not yet convinced that's a public API either.
91 Due to a perl bug (L<https://rt.perl.org/rt3/Ticket/Display.html?id=49594>)
92 we overload this method to change
100 since any string containing a backslash escape doesn't get marked as HTML.
101 Since we don't want to escape things that backslash escapes are normally
102 used for, this isn't really a problem for us.
106 For no reason I can comprehend at all, L<Template::Directive>'s C<textblock>
107 method calls C<&text> instead of using a method call so we have to override
112 See L<HTML::String> for authors.
114 =head1 COPYRIGHT AND LICENSE
116 See L<HTML::String> for the copyright and license.