lib/HTML/Object/DOM/XPathEvaluator.pm

filter: Statementstmt 61.5% 24 / 39 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 66.6% 8 / 12 Podpod 100% 4 / 4 totaltotal 52.1% 36 / 69 CC 6 SCAR 23.3
CC
6
Cov
48%
evaluate
30.0
init
6.0
createExpression
2.0
CRAP 10.2
« lib/HTML/Object/DOM/TextTrack.pm ↑ Summary blib/lib/HTML/Object/XPath/Function.pm »
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 1 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶ warnings::register_categories( 'HTML::Object' );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶ use parent qw( Module::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶ use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶ use HTML::Object::XPath;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 our $VERSION = 'v0.2.0';
24 };
25  
26 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 sub init
30 {
31 0 ▶ my $self = shift( @_ );
Subroutine init: not called CC=2 SCAR=17.9
Pod documented
32 0 $self->{_init_strict_use_sub} = 1;
33 0 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 0 0 0
34 0 $self->{_xp} = HTML::Object::XPath->new;
35 0 return( $self );
36 }
37  
38 0 ▶sub createExpression { return( shift->{_xp}->parse( @_ ) ); }
Subroutine createExpression: not called CC=1 SCAR=6.9
Pod documented
39  
40 0 ▶sub createNSResolver { return; }
Subroutine createNSResolver: not called CC=1 SCAR=6.9
Pod documented
41  
42 sub evaluate
43 {
44 0 ▶ my $self = shift( @_ );
Subroutine evaluate: not called CC=5 SCAR=34.0
Pod documented
45 0 my $expr = shift( @_ );
46 0 ▶ if( $self->_is_a( $expr => 'HTML::Object::XPath::Expr' ) )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_is_a($expr, 'HTML::Object::XPath::Expr')) { } 0 0 0
elsif (not ref $expr or overload::Method($expr, '""')) { } 0 0 0
47 {
48 0 return( $expr->evaluate( @_ ) );
49 }
50 ▶ elsif( !ref( $expr ) || overload::Method( $expr, '""' ) )
Condition0%0/3
not ref $expr or overload::Method($expr, "\"\"")
l!l&&r!l&&!r
000
MC/DC0%0/2
not ref $expr or overload::Method($expr, "\"\"")
not ref $expr overload::Method($expr, "\"\"")
Truth table0%0/3
not(A) or B
A= not ref $exprB= overload::Method($expr, "\"\"")
ABresult
1X1
011
000
51 {
52 0 ▶ my $xpath = $self->{_xp}->parse( $expr ) || return( $self->pass_error( $self->{_xp}->error ) );
Condition0%0/2
$self->{'_xp'}->parse($expr) || (return $self->pass_error($self->{'_xp'}->error))
l!l
00
MC/DC0%0/1
$self->{'_xp'}->parse($expr) || (return $self->pass_error($self->{'_xp'}->error))
$self->{'_xp'}->parse($expr)
Truth table0%0/2
A
A= $self->{'_xp'}->parse($expr)
Aresult
11
00
53 0 return( $xpath->evaluate( @_ ) );
54 }
55 else
56 {
57 0 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 0 return( $self );
63 }
64  
65 1 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 &lt;div&gt;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 &lt;div&gt;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
« lib/HTML/Object/DOM/TextTrack.pm ↑ Summary blib/lib/HTML/Object/XPath/Function.pm »