NAME

Mojo::DOM::Role::Analyzer - miscellaneous methods for analyzing a DOM

SYNOPSIS

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;

DESCRIPTION

Operators

cmp

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.

Methods

element_count

$count = $dom->element_count;

Returns the number of elements in a dom object, including children of children of children, etc.

parent_all

$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.

parent_ptags

$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.

compare($dom1, $dom2)

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.

Returns a value of '-1' if the first argument comes before (is less than) the second.
Returns a value of '0' if the first and second arguments are the same.
Returns a value of '1' if the first argument comes after (is greater than) the second.

depth

my $depth = $dom->at('p.first')->depth;

Finds the nested depth level of a node. The root node returns 1.

deepest

my $deepest_depth = $dom->deepest;

Finds the deeepest nested level within a node.

VERSION

version 0.008

SUPPORT

Perldoc

You can find documentation for this module with the perldoc command.

perldoc Mojo::DOM::Role::Analyzer

Websites

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.

Source Code

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

AUTHOR

Steve Dondley <s@dondley.com>

COPYRIGHT AND LICENSE

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.