HTML-HTML5-DOM
view release on metacpan or search on metacpan
lib/HTML/HTML5/DOM.pm view on Meta::CPAN
my $for_the_benefit_of_module_install_metadata = q{
package HTML::HTML5::DOM;
};
{ package HTML::HTML5::DOM;
use 5.010;
use strict qw(vars subs);
use match::simple ();
use mro 'c3';
BEGIN {
$HTML::HTML5::DOM::AUTHORITY = 'cpan:TOBYINK';
$HTML::HTML5::DOM::VERSION = '0.002';
};
use constant XHTML_NS => 'http://www.w3.org/1999/xhtml';
my $me;
BEGIN { $me = bless {}, __PACKAGE__ }
use DateTime qw//;
use IO::Detect qw//;
use Scalar::Util qw/blessed/;
use URI qw//;
sub getDOMImplementation
{
return $me;
}
our @FEATURES = (
HTML::HTML5::DOMutil::Feature->new(Core => '3.0'),
HTML::HTML5::DOMutil::Feature->new(XML => '3.0'),
HTML::HTML5::DOMutil::Feature->new(XMLVersion => '1.1'),
HTML::HTML5::DOMutil::Feature->new(HTML => '2.0'),
HTML::HTML5::DOMutil::Feature->new(XHTML => '2.0'),
);
sub getFeature
{
my $self = shift;
my @has = $self->hasFeature(@_);
@has ? $has[0] : undef;
}
sub hasFeature
{
my $self = shift;
my $test = blessed $_[0] ? $_[0] : HTML::HTML5::DOMutil::Feature->new(@_);
grep match::simple::match($_, $test), @FEATURES;
}
sub registerFeature
{
my ($class, $feature) = @_;
push @FEATURES, $feature;
$feature->install_subs;
}
sub parseString
{
my ($self, $string, %options) = @_;
my $pclass = $options{using} =~ /libxml/i ? 'XML::LibXML' : 'HTML::HTML5::Parser';
my $dom = $pclass->new->parse_string($string);
XML::LibXML::Augment->upgrade($dom);
return $dom;
}
sub parse
{
my ($self, $file, %options) = @_;
my $pclass = $options{using} =~ /libxml/i ? 'XML::LibXML' : 'HTML::HTML5::Parser';
my $dom = IO::Detect::is_filehandle $file
? $pclass->new->parse_fh($file)
: $pclass->new->parse_file($file);
XML::LibXML::Augment->upgrade($dom);
return $dom;
}
sub createDocument
{
my $self = shift;
die "Can only be used to create HTML documents."
unless (shift//XHTML_NS) eq XHTML_NS;
die "Can only be used to create HTML documents."
unless lc(shift//'html') eq 'html';
my $dtd = shift//$self->createDocumentType;
$dtd = $dtd->toString if ref $dtd;
my $html = "$dtd<html><head></head><body></body></html>";
my $dom = $self->parseString($html);
$dom->setURI('about:blank');
return $dom;
}
sub createDocumentType
{
lib/HTML/HTML5/DOM.pm view on Meta::CPAN
$forms->get_index(1)->submit;
=head1 DESCRIPTION
HTML::HTML5::DOM is a layer on top of XML::LibXML which provides a number
of additional classes and methods for elements. Because it wraps almost
every XML::LibXML method, it is not as fast as using XML::LibXML directly
(which is an XS module), but it is more convenient.
=head2 DOM Support
HTML::HTML5::DOM implements those parts of the HTML5 DOM which are
convenient to do so, and also supports a lot of the pre-HTML5 DOM which
was obsoleted by the HTML5 spec. Additionally a number of DOM extensions
(methods prefixed with C<p5_>) are provided.
DOM events and event handlers (e.g. C<onclick>) are not supported, and
are unlikely to be supported in the foreseeable future.
The CSS bits of DOM are not supported, but may be in the future.
=head2 Perl specifics
DOM attributes are typically implemented as get/set/clear methods. For
example:
warn $my_div->id; # get
$my_div->id($new_id); # set
$my_div->id(undef); # clear
Methods which return a list usually return a normal Perl list when called
in list context, and an XML::LibXML::NodeList (or a subclass of that) when
called in list context.
Methods that return a URI generally return one blessed into the L<URI>
class.
Methods that return a datetime, generally return one blessed into the
L<DateTime> class.
Methods that result in hypertext navigation (e.g. clicking a link or
submitting a form) generally return an L<HTTP::Request> object (which
you can pass to an L<LWP::UserAgent> or L<WWW::Mechanize> instance).
The standard Perl C<isa> method is overridden to support two additional
calling styles:
$elem->isa( 'h1' ); # element tag name
$elem->isa( -HTMLHeadingElement ); # DOM interface name
=head2 HTML::HTML5::DOM class methods
While most of the interesting stuff is in L<HTML::HTML5::DOM::HTMLElement>
and other classes like that, the HTML::HTML5::DOM package itself provides a
handful of methods of its own.
=over
=item * C<< XHTML_NS >>
Constant. The XHTML namespace URI as a string.
=item * C<< getDOMImplementation >>
Gets a singleton object blessed into the HTML::HTML5::DOM class.
=item * C<< hasFeature >>
Given a feature and version, returns true if the feature is supported.
my $impl = HTML::HTML5::DOM->getDOMImplementation;
if ($impl->hasFeature('Core', '2.0')) {
# ... do stuff
}
=item * C<< getFeature >>
Given a feature and version, returns an HTML::HTML5::DOMutil::Feature object.
=item * C<< registerFeature >>
Experimental method to extend HTML::HTML5::DOM.
my $monkey = HTML::HTML5::DOMutil::Feature->new(Monkey => '1.0');
$monkey->add_sub(
HTMLElement => 'talk',
sub { print "screech!\n" },
);
$impl->registerFeature($monkey);
$element->talk if $impl->hasFeature(Monkey => '1.0');
=item * C<< parse >>
Given a file handle, file name or URL (as a string or L<URI> object),
parses the file, returning an L<HTML::HTML5::DOM::HTMLDocument> object.
This function uses HTML::HTML5::Parser but you can alternatively use
XML::LibXML's XML parser:
my $dom = HTML::HTML5::DOM->parse($fh, using => 'libxml');
=item * C<< parseString >>
As per C<parse>, but parses a string.
=item * C<< createDocument >>
Returns an HTML::HTML5::DOM::HTMLDocument representing a blank page.
=item * C<< createDocumentType >>
Given a qualified name, public identifier (which might be undef) and system
identifier (also perhaps undef), returns a doctype tag.
This is currently returned as a string, but in an ideal world would be an
L<XML::LibXML::Dtd> object.
=back
=head1 BUGS
( run in 1.956 second using v1.01-cache-2.11-cpan-411bb0df24b )