HTML::StripScripts::LibXML - XSS filter - outputs a LibXML Document or DocumentFragment
use HTML::StripScripts::LibXML(); my $hss = HTML::StripScripts::LibXML->new( { Context => 'Document', ## HTML::StripScripts configuration Rules => { ... }, }, strict_comment => 1, ## HTML::Parser options strict_names => 1, ); $hss->parse_file("foo.html"); $xml_doc = $hss->filtered_document; OR $xml_doc = $hss->filter_html($html);
This class provides an easy interface to HTML::StripScripts, using HTML::Parser to parse the HTML, and returns an XML::LibXML::Document or XML::LibXML::DocumentFragment.
HTML::StripScripts
HTML::Parser
See HTML::Parser for details of how to customise how the raw HTML is parsed into tags, and HTML::StripScripts for details of how to customise the way those tags are filtered. This module is a subclass of HTML::StripScripts::Parser.
HTML::StripScripts::LibXML still allows you to specify the Context of the HTML (Document, Flow, Inline, NoTags). If Context is Document, then it returns an XML::LibXML::Document object, otherwise it returns an XML::LibXML::DocumentFragment object.
Context
Document
XML::LibXML::Document
XML::LibXML::DocumentFragment
HTML::StripScripts allows you to use tag callbacks, for instance:
$hss = HTML::StripScripts->new({ Rules => { a => \&a_callback } }); sub a_callback { my ($filter,$element) = @_; # where $element = { # tag => 'a', # attr => { href => '/index.html' }, # content => 'Go to <b>Home</b> page', # } return 1; }
HTML::StripScripts::LibXML still gives you tag callbacks, but they look like this:
sub a_callback { my ($filter,$element) = @_; # where $element = { # tag => 'a', # attr => { href => '/index.html' }, # children => [ # XML::LibXML::Text --> 'Go to ', # XML::LibXML::Element --> 'b' # with child Text --> 'Home', # XML::LibXML::Text --> ' page', # ], # } return 1; }
The subs output, output_start and output_end are not called. Instead, this module uses output_stack_entry which handles the tag callback, (and depending on the result of the tag callback) creates an element and adds its child nodes. Then it adds the element to the list of children for the parent tag.
output
output_start
output_end
output_stack_entry
Creates a new HTML::StripScripts::LibXML object.
HTML::StripScripts::LibXML
See HTML::StripScripts::Parser for details.
This is the first draft of this module, and currently there are no configuration options for the XML. I would welcome feedback from XML users as to how I could improve the interface.
For this reason, the API may change.
Please report any bugs or feature requests to bug-html-stripscripts-libxml@rt.cpan.org, or through the web interface at http://rt.cpan.org.
HTML::Parser, HTML::StripScripts::Parser, HTML::StripScripts::Regex
Clinton Gormley <clint@traveljury.com>
Copyright (C) 2007 Clinton Gormley. All Rights Reserved.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install HTML::StripScripts::LibXML, copy and paste the appropriate command in to your terminal.
cpanm
cpanm HTML::StripScripts::LibXML
CPAN shell
perl -MCPAN -e shell install HTML::StripScripts::LibXML
For more information on module installation, please visit the detailed CPAN module installation guide.