The constructor does nothing interesting besides take one flag argument, no_cache => 1, to disable the global element metadata cache. Disabling the cache is handy for conserving memory if you parse a large number of documents, however, class methods such as /source_line will not work, and must be run from an instance of this parser.

This function parses an HTML document from a file or network; $html_file_name can be either a filename or an URL.

Options include 'encoding' to indicate file encoding (e.g. 'utf-8') and 'user_agent' which should be a blessed LWP::UserAgent (or HTTP::Tiny) object to be used when retrieving URLs.

If requesting a URL and the response Content-Type header indicates an XML-based media type (such as XHTML), XML::LibXML::Parser will be used automatically (instead of the tag soup parser). The XML parser can be told to use a DTD catalogue by setting the option 'xml_catalogue' to the filename of the catalogue.

HTML (tag soup) parsing can be forced using the option 'force_html', even when an XML media type is returned. If an options hashref was passed, parse_file will set $options->{'parser_used'} to the name of the class used to parse the URL, to allow the calling code to double-check which parser was used afterwards.

If an options hashref was passed, parse_file will set $options->{'response'} to the HTTP::Response object obtained by retrieving the URI.

This method is roughly equivalent to XML::LibXML's method of the same name, but unlike XML::LibXML, and despite its name it does not require the chunk to be "balanced". This method is somewhat black magic, but should work, and do the proper thing in most cases. Of course, the proper thing might not be what you'd expect! I'll try to keep this explanation as brief as possible...

Consider the following string:

<b>Hello</b></td></tr> <i>World</i>

What is the proper way to parse that? If it were found in a document like this:

If you don't pass a "within" option, then the chunk is parsed as if it were within a <div> element. This is often the most sensible option. If you pass something like { within => "foobar" } where "foobar" is not a real HTML element name (as found in the HTML5 spec), then this method will croak; if you pass the name of a void element (e.g. "br" or "meta") then this method will croak; there are a handful of other unsupported elements which will croak (namely: "noscript", "noembed", "noframes").

Note that the second time around, although we parsed the string "as if it were within a <td> element", the <i>Hello</i> bit did not strictly end up within the <td> element (not even within the <table> element!) yet it still gets returned. We'll call things such as this "outliers". There is a "force_within" option which tells parse_balanced_chunk to ignore outliers:

There is a boolean option "mark_outliers" which marks each outlier with an attribute (data-perl-html-html5-parser-outlier) to indicate its outlier status. Clearly, this is ignored when you use "force_within" because no outliers are returned. Some outliers may be XML::LibXML::Text elements; text nodes don't have attributes, so these will not be marked with an attribute.

A last note is to mention what gets returned by this method. Normally it's an XML::LibXML::DocumentFragment object, but if you call the method in list context, a list of the individual node elements is returned. Alternatively you can request the data to be returned as an XML::LibXML::NodeList object:

The exact implementation of this method may change from version to version, but the long-term goal will be to approach how common desktop browsers parse HTML fragments when implementing the setter for DOM's innerHTML attribute.

The push parser and SAX-based parser are not supported. Trying to change an option (such as recover_silently) will make HTML::HTML5::Parser carp a warning. (But you can inspect the options.)

For an XML::LibXML::Document which has been returned by HTML::HTML5::Parser, using this method will tell you the root element declared in the DTD used (if any). That is, if the document has this doctype:

<!doctype html>

... it will return "html".

This may return the empty string if a DTD was present but did not contain a root element; or undef if no DTD was present.

$implicitness indicates that the node was not explicitly marked up in the source code, but its existence was inferred by the parser. For example, in the following markup, the HTML, TITLE and P elements are explicit, but the HEAD and BODY elements are implicit.

(Note that implicit elements do still have a line number and column number.) The implictness indicator is a new feature, and I'd appreciate any bug reports where it gets things wrong.

XML::LibXML::Node has a line_number method. In general this will always return 0 and HTML::HTML5::Parser has no way of influencing it. However, if you install XML::LibXML::Devel::SetLineNumber on your system, the line_number method will start working (at least for elements).