1 package SL::Presenter::EscapedText;
5 use overload '""' => \&escaped;
8 my ($class, %params) = @_;
10 return $params{text} if ref($params{text}) eq $class;
12 my $self = bless {}, $class;
13 $self->{text} = $params{is_escaped} ? $params{text} : $::locale->quote_special_chars('HTML', $params{text});
32 SL::Presenter::EscapedText - Thin proxy object around HTML-escaped strings
36 use SL::Presenter::EscapedText;
40 return SL::Presenter::EscapedText->new(text => $text);
44 my $output_of_other_component = blackbox('Hello & Goodbye');
46 # The following is safe, text will not be escaped twice:
47 return SL::Presenter::EscapedText->new(text => $output_of_other_component);
50 my $output = build_output();
51 print "Yeah: $output\n";
55 Sometimes it's nice to let a sub-component build its own
56 representation. However, you always have to be very careful about
57 whose responsibility escaping is. Only the building function knows
58 enough about the structure to be able to HTML escape properly.
60 But higher functions should not have to care if the output is already
61 escaped -- they should be able to simply escape it again. Without
62 producing stuff like '&'.
64 Stringification is overloaded. It will return the same as L<escaped>.
66 This works together with the template plugin
67 L<SL::Template::Plugin::P> and its C<escape> method.
75 Creates an instance of C<EscapedText>.
77 The parameter C<text> is the text to escape. If it is already an
78 instance of C<EscapedText> then C<$params{text}> is returned
81 Otherwise C<text> is HTML-escaped and stored in the new instance. This
82 can be overridden by setting C<$params{is_escaped}> to a trueish
87 Returns the escaped string (not an instance of C<EscapedText> but an
98 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>