| 1 | ##---------------------------------------------------------------------------- | ||
| 2 | ## HTML Object - ~/lib/HTML/Object/DOM/XPathEvaluator.pm | ||
| 3 | ## Version v0.2.0 | ||
| 4 | ## Copyright(c) 2022 DEGUEST Pte. Ltd. | ||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||
| 6 | ## Created 2022/01/01 | ||
| 7 | ## Modified 2022/09/18 | ||
| 8 | ## All rights reserved | ||
| 9 | ## | ||
| 10 | ## | ||
| 11 | ## This program is free software; you can redistribute it and/or modify it | ||
| 12 | ## under the same terms as Perl itself. | ||
| 13 | ##---------------------------------------------------------------------------- | ||
| 14 | package HTML::Object::DOM::XPathEvaluator; | ||
| 15 | BEGIN | ||
| 16 | { | ||
| 17 | use strict; | ||
| 18 | use warnings; | ||
| 19 | warnings::register_categories( 'HTML::Object' ); | ||
| 20 | use parent qw( Module::Generic ); | ||
| 21 | use vars qw( $VERSION ); | ||
| 22 | use HTML::Object::XPath; | ||
| 23 | our $VERSION = 'v0.2.0'; | ||
| 24 | }; | ||
| 25 | |||
| 26 | use strict; | ||
| 27 | use warnings; | ||
| 28 | |||
| 29 | sub init | ||
| 30 | { | ||
| 31 | my $self = shift( @_ ); | ||
| 32 | $self->{_init_strict_use_sub} = 1; | ||
| 33 | $self->SUPER::init( @_ ) || return( $self->pass_error ); | ||
| 34 | $self->{_xp} = HTML::Object::XPath->new; | ||
| 35 | return( $self ); | ||
| 36 | } | ||
| 37 | |||
| 38 | sub createExpression { return( shift->{_xp}->parse( @_ ) ); } | ||
| 39 | |||
| 40 | sub createNSResolver { return; } | ||
| 41 | |||
| 42 | sub evaluate | ||
| 43 | { | ||
| 44 | my $self = shift( @_ ); | ||
| 45 | my $expr = shift( @_ ); | ||
| 46 | if( $self->_is_a( $expr => 'HTML::Object::XPath::Expr' ) ) | ||
| 47 | { | ||
| 48 | return( $expr->evaluate( @_ ) ); | ||
| 49 | } | ||
| 50 | elsif( !ref( $expr ) || overload::Method( $expr, '""' ) ) | ||
| 51 | { | ||
| 52 | my $xpath = $self->{_xp}->parse( $expr ) || return( $self->pass_error( $self->{_xp}->error ) ); | ||
| 53 | return( $xpath->evaluate( @_ ) ); | ||
| 54 | } | ||
| 55 | else | ||
| 56 | { | ||
| 57 | return( $self->error({ | ||
| 58 | message => 'Value provided (' . overload::StrVal( $expr ) . ' is not a HTML::Object::XPath::Expr object. You can create one using createExpression()', | ||
| 59 | class => 'HTML::Object::TypeError', | ||
| 60 | }) ); | ||
| 61 | } | ||
| 62 | return( $self ); | ||
| 63 | } | ||
| 64 | |||
| 65 | 1; | ||
| 66 | # NOTE: POD | ||
| 67 | __END__ | ||
| 68 | |||
| 69 | =encoding utf-8 | ||
| 70 | |||
| 71 | =head1 NAME | ||
| 72 | |||
| 73 | HTML::Object::DOM::XPathEvaluator - HTML Object DOM XPathEvaluator Class | ||
| 74 | |||
| 75 | =head1 SYNOPSIS | ||
| 76 | |||
| 77 | use HTML::Object::DOM::XPathEvaluator; | ||
| 78 | my $this = HTML::Object::DOM::XPathEvaluator->new || | ||
| 79 | die( HTML::Object::DOM::XPathEvaluator->error, "\n" ); | ||
| 80 | |||
| 81 | =head1 VERSION | ||
| 82 | |||
| 83 | v0.2.0 | ||
| 84 | |||
| 85 | =head1 DESCRIPTION | ||
| 86 | |||
| 87 | The C<XPathEvaluator> interface allows to compile and evaluate XPath expressions. | ||
| 88 | |||
| 89 | =head1 PROPERTIES | ||
| 90 | |||
| 91 | There are no properties. | ||
| 92 | |||
| 93 | =head1 METHODS | ||
| 94 | |||
| 95 | =head2 createExpression | ||
| 96 | |||
| 97 | Creates a parsed XPath expression with resolved namespaces. | ||
| 98 | |||
| 99 | Example: | ||
| 100 | |||
| 101 | <div>XPath example</div> | ||
| 102 | <div>Number of <div>s: <output></output></div> | ||
| 103 | |||
| 104 | use HTML::Object::DOM; | ||
| 105 | my $parser = HTML::Object::DOM->new; | ||
| 106 | my $doc = $parser->parse_data( $html ) || die( $parser->error ); | ||
| 107 | my $evaluator = HTML::Object::DOM::XPathEvaluator->new; | ||
| 108 | my $expression = $evaluator->createExpression( '//div' ); | ||
| 109 | my $result = $expression->evaluate( $document ); | ||
| 110 | $doc->querySelector( 'output' )->textContent = $result->snapshotLength; | ||
| 111 | |||
| 112 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/XPathEvaluator/createExpression> | ||
| 113 | |||
| 114 | =head2 createNSResolver | ||
| 115 | |||
| 116 | This always returns C<undef>, since this L<HTML::Object> does not work on XML documents. | ||
| 117 | |||
| 118 | Normally, this would adapt any L<DOM node|HTML::Object::DOM::Node> to resolve namespaces allowing the XPath expression to be evaluated relative to the context of the node where it appeared within the document. | ||
| 119 | |||
| 120 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/XPathEvaluator/createNSResolver> | ||
| 121 | |||
| 122 | =head2 evaluate | ||
| 123 | |||
| 124 | Evaluates an XPath expression string and returns a result of the specified type if possible. | ||
| 125 | |||
| 126 | Parameters: | ||
| 127 | |||
| 128 | =over 4 | ||
| 129 | |||
| 130 | =item expression | ||
| 131 | |||
| 132 | A string representing the XPath expression to be parsed and evaluated, or an L<XPath expression object|HTML::Object::XPath::Expr> to be evaluated. The latter would be equivalent to: | ||
| 133 | |||
| 134 | $xpath_expression->evaluate( $context ); | ||
| 135 | |||
| 136 | With C<$context> being a L<node object|HTML::Object::DOM::Node> provided, such as L<HTML::Object::DOM::Document>. | ||
| 137 | |||
| 138 | =item contextNode | ||
| 139 | |||
| 140 | A L<Node|HTML::Object::DOM::Node> representing the context to use for evaluating the expression. | ||
| 141 | |||
| 142 | =back | ||
| 143 | |||
| 144 | Example: | ||
| 145 | |||
| 146 | <div>XPath example</div> | ||
| 147 | <div>Number of <div>s: <output></output></div> | ||
| 148 | |||
| 149 | use HTML::Object::DOM; | ||
| 150 | my $parser = HTML::Object::DOM->new; | ||
| 151 | my $doc = $parser->parse_data( $html ) || die( $parser->error ); | ||
| 152 | my $evaluator = HTML::Object::DOM::XPathEvaluator->new; | ||
| 153 | my $result = $evaluator->evaluate( '//div', $doc ); | ||
| 154 | $doc->querySelector( 'output' )->textContent = $result->snapshotLength; | ||
| 155 | |||
| 156 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/XPathEvaluator/evaluate> | ||
| 157 | |||
| 158 | =head1 AUTHOR | ||
| 159 | |||
| 160 | Jacques Deguest E<lt>F<jack@deguest.jp>E<gt> | ||
| 161 | |||
| 162 | =head1 SEE ALSO | ||
| 163 | |||
| 164 | L<HTML::Selector::XPath>, L<HTML::Object::XPath>, L<HTML::Object::XPath::Expr> | ||
| 165 | |||
| 166 | =head1 COPYRIGHT & LICENSE | ||
| 167 | |||
| 168 | Copyright(c) 2022 DEGUEST Pte. Ltd. | ||
| 169 | |||
| 170 | All rights reserved | ||
| 171 | |||
| 172 | This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 173 | |||
| 174 | =cut |