Refactoring: {strip,restrict}_html in eigene Module ausgelagert
[kivitendo-erp.git] / SL / HTML / Util.pm
1 package SL::HTML::Util;
2
3 use strict;
4 use warnings;
5
6 use HTML::Parser;
7
8 my %stripper;
9
10 sub strip {
11   my ($class_or_value) = @_;
12
13   my $value = !ref($class_or_value) && (($class_or_value // '') eq 'SL::HTML::Util') ? $_[1] : $class_or_value;
14
15   if (!%stripper) {
16     %stripper = ( parser => HTML::Parser->new );
17
18     $stripper{parser}->handler(text => sub { $stripper{text} .= $_[1]; });
19   }
20
21   $stripper{text} = '';
22   $stripper{parser}->parse($value);
23   $stripper{parser}->eof;
24
25   return delete $stripper{text};
26 }
27
28 1;
29 __END__
30
31 =pod
32
33 =encoding utf8
34
35 =head1 NAME
36
37 SL::HTML::Util - Utility functions dealing with HTML
38
39 =head1 SYNOPSIS
40
41   my $plain_text = SL::HTML::Util->strip('<h1>Hello World</h1>');
42
43 =head1 FUNCTIONS
44
45 =over 4
46
47 =item C<strip $html_content>
48
49 Removes all HTML elements and tags from C<$html_content> and returns
50 the remaining plain text.
51
52 =back
53
54 =head1 BUGS
55
56 Nothing here yet.
57
58 =head1 AUTHOR
59
60 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
61
62 =cut