Mojo::DOM::Role::Analyzer - miscellaneous methods for analyzing a DOM
use strict; use warnings; use Mojo::Dom; my $html = '<html><head></head><body><p class="first">A paragraph.</p><p class="last">boo<a>blah<span>kdj</span></a></p><h1>hi</h1></body></html>'; my $analyzer = Mojo::DOM->with_roles('+Analyzer')->new($html); # return the count of elements inside a dom objec my $count = $analyzer->at('body')->element_count; # get the smallest containing dom object that contains all the paragraph tags my $containing_dom = $analyzer->parent_ptags; # compare DOM objects to see which comes first in the document my $tag1 = $analyzer->at('p.first'); my $tag2 = $analyzer->at('p.last'); my $result = $analyzer->compare($tag1, $tag2); # ALTERNATIVELY $analyzer->at('p.first')->compare('p.last'); # 'p.last' is relative to root # get the depth level of a dom object relative to root # root node returns '1' my $depth = $analyzer->at('p.first')->depth; # get the deepest depth of the documented my $deepest = $analyzer->deepest;
my $result = $dom1 cmp $dom2;
Compares the selectors of two $dom objects to determine which comes first in the dom. See compare method below for return values.
compare
$count = $dom->element_count;
Returns the number of elements in a dom object, including children of children of children, etc.
$dom = $dom->parent_all('a'); # finds parent within root that contains all 'a' tags $dom = $dom->at('div.article')->parent_all('ul'); # finds parent within C<div.article> that has all 'ul' tags
Returns the smallest containing $dom within the $dom the method is called on that wraps all the tags indicated in the argument.
$dom = $dom->parent_ptags; $dom = $dom->at('div.article')->parent_ptags;
A conveniece method that works like the parent_all method but automatically supplies a 'p' tag argument for you.
parent_all
'p'
my $dom1 = $dom->at('p.first'); my $dom2 = $dom->at('p.last'); my $result = $dom->compare($dom1, $dom2); # OR with overloaded 'cmp' operator my $result = $dom1 cmp $dom2; # OR $dom->at('p.first')->compare('p.last'); # 'p.last' is relative to root
Compares the selectors of two $dom objects to see which comes first in the DOM.
my $depth = $dom->at('p.first')->depth;
Finds the nested depth level of a node. The root node returns 1.
my $deepest_depth = $dom->deepest;
Finds the deeepest nested level within a node.
version 0.008
You can find documentation for this module with the perldoc command.
perldoc Mojo::DOM::Role::Analyzer
The following websites have more information about this module, and may be of help to you. As always, in addition to those websites please use your favorite search engine to discover more resources.
MetaCPAN
A modern, open-source CPAN search engine, useful to view POD in HTML format.
https://metacpan.org/release/Mojo-DOM-Role-Analyzer
The code is open to the world, and available for you to hack on. Please feel free to browse it and play with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull from your repository :)
https://github.com/sdondley/Mojo-DOM-Role-Analyzer
git clone git://github.com/sdondley/Mojo-DOM-Role-Analyzer.git
Steve Dondley <s@dondley.com>
This software is copyright (c) 2020 by Steve Dondley.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Mojo::DOM::Role::Analyzer, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojo::DOM::Role::Analyzer
CPAN shell
perl -MCPAN -e shell install Mojo::DOM::Role::Analyzer
For more information on module installation, please visit the detailed CPAN module installation guide.