1 package SL::HTML::Util;
13 'nbsp' => ' ', # should be => "\x{00A0}", but this can lead to problems with
14 # a non-visible character in csv-exports for example
18 my ($class_or_value) = @_;
20 my $value = !ref($class_or_value) && (($class_or_value // '') eq 'SL::HTML::Util') ? $_[1] : $class_or_value;
22 return '' unless defined $value;
24 # Remove HTML comments.
25 $value =~ s{ <!-- .*? --> }{}gx;
28 %stripper = ( parser => HTML::Parser->new );
30 $stripper{parser}->handler(text => sub { $stripper{text} .= $_[1]; });
34 $stripper{parser}->parse($value);
35 $stripper{parser}->eof;
37 $stripper{text} =~ s{\&([^;]+);}{ $entities{$1} || "\&$1;" }eg;
39 return delete $stripper{text};
51 SL::HTML::Util - Utility functions dealing with HTML
55 my $plain_text = SL::HTML::Util->strip('<h1>Hello World</h1>');
61 =item C<strip $html_content>
63 Removes all HTML elements and tags from C<$html_content> and returns
64 the remaining plain text.
74 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>