HTML::Element::Tiny - lightweight DOM-like elements
Version 0.006
use HTML::Element::Tiny; my $tree = HTML::Element::Tiny->new( [ div => { id => "stuff" }, "some text", [ div => "another div inside that one" ], "some more text" ] ); my $elems = $tree->find({ tag => 'div' });
HTML::Element::Tiny is a simple module for dealing with trees of HTML elements, including traversing them and manipulating the tree structure and individual element attributes, and then for generating HTML.
Though it lives under HTML::, there's no reason that you couldn't use this for processing arbitrary XML, maybe with XML::Tiny in front of it.
This module does not make very many efforts to check its input as far as HTML validation goes. For example, nothing will stop you from having a tree with a repeated id attribute; this will cause you grief if you try to find by that id, since you will only ever get the first such element, and you won't get an error.
id
The parent of this element, or undef if it is a root.
A list of the children of this element.
print $elem->tag; # "div"
The HTML tag for this element.
print $elem->id; # "mylist" print $elem->class # "menu alert"
Shortcuts for commonly-used attributes. These are not mutators.
my @classes = $elem->classes;
Where ->class returns all classes joined with a space, this method returns a list of classnames.
->class
print $elem->attr('href'); # "http://...something.../" $elem->attr({ src => "http://somewhere.com" });
Get or set attributes of an element.
With a single scalar, return the requested attribute.
With a hashref, set attributes based on keys and values of that hashref.
my $elem = HTML::Element::Tiny->new([ ... ]);
Build a new element based on the given arrayref, which is the same format expected by HTML::Element->new_from_lol, namely:
HTML::Element->new_from_lol
[ <tag>, <optional \%attributes>, <strings or more arrayrefs> ]
Any children (elements past the tag and optional attributes) will recursively have new called on them as well.
new
my $clone = $elem->clone(\%attributes);
Return a clone of this element and its descendents, deleting the clone's parent (making it a root of its own tree) and adding any extra attributes specified.
Clone (0.28 or later) will be used, if installed. This is about twice as fast as the internal manual clone that HTML::Element::Tiny does.
my $iter = $elem->iter;
Returns an iterator for this node and all its descendents. See "ITERATORS".
my $elems = $elem->all;
Returns a collection of this node and all its descendents. See "COLLECTIONS".
my $elems = $tree->find(\%arg); my $elem = $tree->find_one(\%arg); # or die my $iter = $tree->find_iter(\%arg);
These are the main traversal methods of HTML::Element::Tiny. Each takes a hashref argument which is interpreted as attributes to be matched, and searches the invocant element and all its descendents. The special -tag attribute matches the element tag. Use an empty string to indicate that an attribute should not be present.
-tag
For example:
{ -tag => 'div', class => 'alert', id => "" }
will match divs that have a class of 'alert' (and possibly others), but not divs with an id attribute.
find returns a collection of matched elements. In list context, this is simply a list of the elements. See "COLLECTIONS".
find
find_one either returns a single element or dies, complaining that it couldn't find any elements or it found too many.
find_one
find_iter returns an iterator of matched elements. See "ITERATORS".
find_iter
$elem->append(@elements, @text, @lists_of_lists);
Add all arguments to the element's list of children, either at the beginning or at the end.
Strings and arrayrefs will be passed through new first. Objects will be cloned if they already have a parent or simply attached if they do not.
$elem->remove_child(@elements, @indices);
Remove all listed children from the element. Arguments may either be element objects or indices (starting at 0).
Returns a collection of removed children.
print $tree->as_HTML;
Return the element and its descendents as HTML. If HTML::Entities is installed, it will be used to escape any text nodes; otherwise, minimal entity escaping is done (&<>"').
&<>"'
Several methods in this class return iterators. These iterators return elements as you call ->next on them. They have no other methods and return undef when exhausted.
->next
undef
Several methods in this class return element collections. These objects represent aggregates, often a sort of 'current selection'. Most of their methods are chainable -- each method notes whether it returns a collection (either the same object or a clone) or some other value. Any method that returns a collection only does so in scalar context. In list context, those methods return a normal list of elements.
my %seen; $elems->each(sub { $seen{$_->tag}++ });
Call the passed-in coderef once for each element. The current element is available as $_.
$_
Returns: collection, unchanged except for whatever your sub does (may change elements in-place)
my $elems_without_id = $elems->filter({ id => "" }); my $elems_with_id = $elems->not({ id => "" });
Using the same syntax as "find", select only matching elements.
grep is a synonym for filter.
grep
filter
not selects only elements that do not match.
not
Returns: new collection
$elems->attr({ class => "bogus" }); my @ids = $elems->attr('id');
Get or set element attributes.
With a hashref, this is a shortcut for each and $_->attr(\%arg).
each
$_->attr(\%arg)
With a scalar, this is a shortcut for map, with the added advantage that it removes all empty values.
map
Returns: same collection OR list of values
my $e = $elems->one;
Return a single element, verifying that the collection does contain exactly one element.
Return: element
my @elems = $elems->all;
Return every element in the collection, as a normal Perl list.
Return: list of elements (not collection)
my @frobbed = $elems->map(sub { Frobber->frob($_) });
Shortcut for map { $code->() } $elems->all.
map { $code->() } $elems->all
Return: list of values
my $size = $elems->size;
Return: number of elements in collection
It is possible to change the classes into which new elements are blessed. When new is called, it looks for a %TAG_CLASS in the invocant's package. If present, it will use the tag name as a key and expect a class as the value. Thus:
%TAG_CLASS
package My::Element; use base 'HTML::Element::Tiny'; our %TAG_CLASS = (img => "My::Element::Image"); ... # elsewhere my $img = My::Element->new([ img => { src => "http://..." } ]); # $img isa My::Element::Image
Some keys and values in this hash are magical. Classes that start with '-' have it replaced with $class::, e.g.
$class::
our %TAG_CLASS = (img => '-Image');
If there is a -base key, it is used instead of the class name when doing this sort of expansion.
-base
To change the default element class, add a -default key.
-default
A class that used all these options might have a %TAG_CLASS that looked like this:
our %TAG_CLASS = ( -default => 'My::Element::Base', -base => 'My::Element', img => '-Image', map => '-Map', );
The default is to use the invocant's package for both -default and -base.
Hans Dieter Pearcey, <hdp at cpan.org>
<hdp at cpan.org>
Please report any bugs or feature requests to bug-html-element-tiny at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=HTML-Element-Tiny. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
bug-html-element-tiny at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc HTML::Element::Tiny
You can also look for information at:
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/HTML-Element-Tiny
CPAN Ratings
http://cpanratings.perl.org/d/HTML-Element-Tiny
RT: CPAN's request tracker
http://rt.cpan.org/NoAuth/Bugs.html?Dist=HTML-Element-Tiny
Search CPAN
http://search.cpan.org/dist/HTML-Element-Tiny
A lot of the HTML generation is either directly from or inspired by Andy Armstrong's excellent HTML::Tiny module.
The concept of element collections is shamelessly lifted from jQuery. http://jquery.com/
Copyright 2007 Hans Dieter Pearcey, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install HTML::Element::Tiny, copy and paste the appropriate command in to your terminal.
cpanm
cpanm HTML::Element::Tiny
CPAN shell
perl -MCPAN -e shell install HTML::Element::Tiny
For more information on module installation, please visit the detailed CPAN module installation guide.