The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

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

closest_up

  my $closest_up_dom = $dom->at('p')->closest_up('h1');

Returns the node closest to the tag node of interest by searching upward through the DOM.

closest_down

  my $closest_down_dom = $dom->at('h1')->closest_down('p');

Returns the node closest to the tag node of interest by searching downward through the DOM.

closest_down

distance

$dom->at($selector)->distance($selector)

$dom->at($selector)->distance($dom)

$dom->distance($dom1, $dom2)

Finds the distance between two nodes. The value is calculated by finding the lowest common ancestor node for the two nodes and then adding the distance from each individual node to the lowest common ancestor node.

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.

common

$dom->at($tag1)->common($tag2)

$dom->common($dom1, $dom2)

$dom->common($selector_str1, $selector_str2)

  my $common_dom = $dom->at('div.bar')->common('div.foo');    # 'div.foo' is relative to root

  # OR

  my $dom1 = $dom->at('div.bar');
  my $dom2 = $dom->at('div.foo');
  my $common = $dom->common($dom1, $dom2);

  # OR

  my $common = $dom->common($dom->at('p')->selector, $dom->at('h1')->selector);

Returns the lowest common ancestor node between two tag nodes or two selector strings.

compare

$dom->at($tag1)->compare($tag2)

compare($dom1, $dom2)

$dom1 cmp $dom2

  $dom->at('p.first')->compare('p.last');    # 'p.last' is relative to root

  # OR

  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;

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

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.