Whatpm::HTML - An HTML Parser and Serializer
use Whatpm::HTML; my $s = q<<!DOCTYPE html><html>...</html>>; # $doc = an empty DOM |Document| object my $on_error = sub { my %error = @_; warn $error{type}, "\n"; }; Whatpm::HTML->parse_char_string ($s => $doc, $onerror); ## Now, |$doc| is the DOM representation of |$s|.
The Whatpm::HTML
module contains HTML parser and serializer.
The HTML parser can be used to construct the DOM tree representation from an HTML document. The parsing and tree construction are done as described in the Web Application 1.0 specification.
The HTML serializer can be used to obtain the HTML document representation
of a DOM tree (or a tree fragment thereof). The serialization
is performed as described in the Web Applications 1.0 specification
for innerHTML
DOM attribute.
This module is part of Whatpm - Perl Modules for Web Hypertext Application Technologies.
The first argument, $s, MUST be a string. It is parsed as a sequence of characters representing an HTML document.
The second argument, $doc, MUST be an empty read-write
DOM Document
object. The HTML DOM tree is constructed
onto this Document
object.
The third argument, $onerror, MUST be a reference to
the error handler code. Whenever a parse error is detected,
this code is invoked with an argument that contains a
useless string that might describe what is wrong.
The code MAY throw an exception, so that whole the parsing
process aborts. Otherwise, the parser will continue to
process the input. The code MUST NOT modify $s or $doc.
If it does, then the result is undefined.
This argument is optional; if missing, any
parse error makes that string being warn
ed.
NOTE: To be a conforming user agent, the code MUST either abort the processing by throwing an exception at the first invocation or MUST continue the processing until the parser stops.
The method returns the DOM Document
object (i.e. the second argument).
Note that the Whatpm::NanoDOM
module provides a non-conforming
implementation of DOM that only implements a subset that
is necessary for the purpose of Whatpm::HTML
's parsing and
serializing.
With this module, creating a new HTML Document
object
from a string containing HTML document might be coded as:
use Whatpm::HTML; use Whatpm::NanoDOM; my $doc = Whatpm::HTML->parse_char_string ($s => Whatpm::NanoDOM::Document->new, $onerror);
@@ TBW
Once a parser $p is instantiated by method new
,
a CODE
reference can be set to $p->{application_cache_selection}
.
That CODE
will be called back when the application cache selection
algorithm MUST be run per HTML5. By default,
$p->{application_cache_selection}
is set to an empty subroutine.
The subroutine will be invoked with an argument manifest_uri,
which is set to the manifest URI when the algorithm MUST be invoked
with a manifest URI, or is set to undef
when the algorithm MUST
be invoked without no manifest URI.
@@ TBW
The list of the error types is available in Whatpm Error Types <http://suika.fam.cx/gate/2005/sw/Whatpm%20Error%20Types>.
Documentation for parse_byte_string.
Tokenizer should emit a sequence of character tokens as one token to improve performance.
A method that accepts a byte stream as an input.
Charset detection algorithm.
Documentation for the setter of inner_html.
And there are many ``TODO''s and ``ISSUE''s in the source code.
This module requires Error. That module is available at CPAN <http://search.cpan.org/author/SHLOMIF/Error-0.17009/lib/Error.pm>. It is also part of manakai-core distribution <http://suika.fam.cx/www/2006/manakai/>.
Whatpm <http://suika.fam.cx/www/markup/html/whatpm/readme>.
Whatpm Error Types <http://suika.fam.cx/gate/2005/sw/Whatpm%20Error%20Types>.
HTML5 <http://whatwg.org/html5>.
the Whatpm::HTML::Serializer manpage.
the Whatpm::ContentChecker::HTML manpage.
Wakaba <w@suika.fam.cx>.
Copyright 2007 Wakaba <w@suika.fam.cx>
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.