1 package SL::HTML::Util;
16 my ($class_or_value) = @_;
18 my $value = !ref($class_or_value) && (($class_or_value // '') eq 'SL::HTML::Util') ? $_[1] : $class_or_value;
20 return '' unless defined $value;
22 # Remove HTML comments.
23 $value =~ s{ <!-- .*? --> }{}gx;
26 %stripper = ( parser => HTML::Parser->new );
28 $stripper{parser}->handler(text => sub { $stripper{text} .= $_[1]; });
32 $stripper{parser}->parse($value);
33 $stripper{parser}->eof;
35 $stripper{text} =~ s{\&([^;]+);}{ $entities{$1} || "\&$1;" }eg;
37 return delete $stripper{text};
49 SL::HTML::Util - Utility functions dealing with HTML
53 my $plain_text = SL::HTML::Util->strip('<h1>Hello World</h1>');
59 =item C<strip $html_content>
61 Removes all HTML elements and tags from C<$html_content> and returns
62 the remaining plain text.
72 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>