lib/HTML/Object/XPath/Function.pm

filter: Statementstmt 33.9% 89 / 262 Branchbran 14.5% 14 / 96 Conditioncond 20.0% 1 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 44.4% 20 / 45 Podpod 100% 34 / 34 totaltotal 35.5% 158 / 445 CC 57 SCAR 21.6
CC
57
Cov
29%
number
20.0
substring
20.0
translate
20.0
CRAP 8.7
« lib/HTML/Object/DOM/Element/Track.pm ↑ Summary lib/HTML/Object/TokenList.pm »
1 ##----------------------------------------------------------------------------
2 ## HTML Object - ~/lib/HTML/Object/XPath/Function.pm
3 ## Version v0.2.0
4 ## Copyright(c) 2021 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2021/12/05
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::XPath::Function;
15 BEGIN
16 {
17 12 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 12 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 12 ▶ warnings::register_categories( 'HTML::Object' );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 12 ▶ use parent qw( Module::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 12 ▶ use vars qw( $TRUE $FALSE $BASE_CLASS $DEBUG $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 12 ▶ use HTML::Object::XPath::Boolean;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 12 our $TRUE = HTML::Object::XPath::Boolean->True;
24 12 our $FALSE = HTML::Object::XPath::Boolean->False;
25 12 our $BASE_CLASS = 'HTML::Object::XPath';
26 12 our $DEBUG = 0;
27 12 our $VERSION = 'v0.2.0';
28 };
29  
30 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 sub init
34 {
35 22 ▶ my $self = shift( @_ );
Subroutine init: called CC=2 SCAR=7.0
Pod documented
36 # XPath Parser -> HTML::Object::XPath
37 22 $self->{pp} = shift( @_ );
38 22 $self->{name} = shift( @_ );
39 22 $self->{params} = shift( @_ );
40 22 $self->{_init_strict_use_sub} = 1;
41 22 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 0 22 22
42 22 return( $self );
43 }
44  
45 sub as_string
46 {
47 0 ▶ my $self = shift( @_ );
Subroutine as_string: not called CC=3 SCAR=24.8
Pod documented
48 0 my $string = $self->{name} . '(';
49 0 my $second;
50 0 foreach( @{$self->{params}} )
51 {
52 0 ▶ $string .= ',' if( $second++ );
Branch: 1 branch
BranchTrueFalseTotal
if $second++ 0 0 0
53 0 $string .= $_->as_string;
54 }
55 0 $string .= ')';
56 0 return( $string );
57 }
58  
59 sub as_xml
60 {
61 0 ▶ my $self = shift( @_ );
Subroutine as_xml: not called CC=3 SCAR=24.8
Pod documented
62 0 my $string = sprintf( '<Function name="%s"', $self->{name} );
63 0 my $params = '';
64 0 foreach( @{$self->{params}} )
65 {
66 0 $params .= '<Param>' . $_->as_xml . "</Param>\n";
67 }
68 0 ▶ if( $params )
Branch: 1 branch
BranchTrueFalseTotal
if ($params) { } 0 0 0
69 {
70 0 $string .= ">\n$params</Function>\n";
71 }
72 else
73 {
74 0 $string .= " />\n";
75 }
76 0 return( $string );
77 }
78  
79 sub boolean
80 {
81 0 ▶ my $self = shift( @_ );
Subroutine boolean: not called CC=2 SCAR=17.9
Pod documented
82 0 my( $node, @params ) = @_;
83 0 ▶ die( "boolean: Incorrect number of parameters\n" ) if( @params != 1 );
Branch: 1 branch
BranchTrueFalseTotal
if @params != 1 0 0 0
84 0 return( $params[0]->to_boolean );
85 }
86  
87 sub ceiling
88 {
89 0 ▶ my $self = shift( @_ );
Subroutine ceiling: not called CC=1 SCAR=6.9
Pod documented
90 0 my( $node, @params ) = @_;
91 0 require POSIX;
92 0 my $num = $self->number( $node, @params );
93 0 return( $self->new_number( POSIX::ceil( $num->value ) ) );
94 }
95  
96 sub concat
97 {
98 0 ▶ my $self = shift( @_ );
Subroutine concat: not called CC=2 SCAR=17.9
Pod documented
99 0 my( $node, @params ) = @_;
100 0 ▶ die( "concat: Too few parameters\n" ) if( @params < 2 );
Branch: 1 branch
BranchTrueFalseTotal
if @params < 2 0 0 0
101 # TODO: Check for improvement
102 0 my $string = join( '', map{ $_->string_value } @params );
103 0 return( $self->new_literal( $string ) );
104 }
105  
106 sub contains
107 {
108 0 ▶ my $self = shift( @_ );
Subroutine contains: not called CC=3 SCAR=24.8
Pod documented
109 0 my( $node, @params ) = @_;
110 0 ▶ die( "starts-with: incorrect number of params\n" ) unless( @params == 2 );
Branch: 1 branch
BranchTrueFalseTotal
unless @params == 2 0 0 0
111 0 my $value = $params[1]->string_value;
112 0 ▶ if( $params[0]->string_value =~ /(.*?)\Q$value\E(.*)/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($params[0]->string_value =~ /(.*?)\Q$value\E(.*)/) 0 0 0
113 {
114 0 return( $TRUE );
115 }
116 0 return( $FALSE );
117 }
118  
119 sub count
120 {
121 20 ▶ my $self = shift( @_ );
Subroutine count: called CC=2 SCAR=7.0
Pod documented
122 20 my( $node, @params ) = @_;
123 20 ▶ die( "count: Parameter must be a NodeSet\n" ) unless( $params[0]->isa( 'HTML::Object::XPath::NodeSet' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $params[0]->isa("HTML::Object::XPath::NodeSet") 0 20 20
124 20 return( $self->new_number( $params[0]->size ) );
125 }
126  
127 sub evaluate
128 {
129 26 ▶ my $self = shift( @_ );
Subroutine evaluate: called CC=3 SCAR=11.0
Pod documented
130 26 my $node = shift( @_ );
131 26 ▶ while( $node->isa( 'HTML::Object::XPath::NodeSet' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($node->isa("HTML::Object::XPath::NodeSet")) 6 26 32
132 {
133 6 $node = $node->get_node(1);
134 }
135 26 my @params;
136 26 foreach my $param ( @{$self->{params}} )
137 {
138 28 my $results = $param->evaluate( $node );
139 28 push( @params, $results );
140 }
141 26 return( $self->_execute( $self->{name}, $node, @params ) );
142 }
143  
144 sub false
145 {
146 0 ▶ my $self = shift( @_ );
Subroutine false: not called CC=2 SCAR=17.9
Pod documented
147 0 my( $node, @params ) = @_;
148 0 ▶ die( "true: function takes no parameters\n" ) if( @params > 0 );
Branch: 1 branch
BranchTrueFalseTotal
if @params > 0 0 0 0
149 0 return( $FALSE );
150 }
151  
152 sub floor
153 {
154 0 ▶ my $self = shift( @_ );
Subroutine floor: not called CC=1 SCAR=6.9
Pod documented
155 0 my( $node, @params ) = @_;
156 0 require POSIX;
157 0 my $num = $self->number( $node, @params );
158 0 return( $self->new_number( POSIX::floor( $num->value ) ) );
159 }
160  
161 sub id
162 {
163 4 ▶ my $self = shift( @_ );
Subroutine id: called CC=7 SCAR=22.3
Pod documented
164 4 my( $node, @params ) = @_;
165 4 ▶ die( "id: Function takes 1 parameter\n" ) unless( @params == 1 );
Branch: 1 branch
BranchTrueFalseTotal
unless @params == 1 0 4 4
166 4 my $results = $self->new_nodeset();
167 4 ▶ if( $self->_is_a( $params[0] => 'HTML::Object::XPath::NodeSet' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_a($params[0], 'HTML::Object::XPath::NodeSet')) { } 0 4 4
168 {
169 # result is the union of applying id() to the
170 # string value of each node in the nodeset.
171 0 foreach my $node ( $params[0]->get_nodelist )
172 {
173 0 my $string = $node->string_value;
174 0 $results->append( $self->id( $node, $self->new_literal( $string ) ) );
175 }
176 }
177 # The actual id() function...
178 else
179 {
180 4 my $string = $self->string( $node, $params[0] );
181 # get perl scalar
182 # $_ = $string->value;
183 # split $_
184 # my @ids = split;
185 # die( "Splitting '", $string->value, "' with result: '", join( "', '", @ids ), "'\n" );
186 4 my @ids = split( /[[:blank:]]/, $string->value );
187 4 ▶ if( $node->isAttributeNode )
Branch: 1 branch
BranchTrueFalseTotal
if ($node->isAttributeNode) 0 4 4
188 {
189 0 warn( "calling \($node->getParentNode->getRootNode->getChildNodes)->[0] on attribute node\n" );
190 0 $node = ( $node->getParentNode->getRootNode->getChildNodes )->[0];
191 }
192 4 foreach my $id ( @ids )
193 {
194 4 ▶ if( my $found = $node->getElementById( $id ) )
Branch: 1 branch
BranchTrueFalseTotal
if (my $found = $node->getElementById($id)) 4 0 4
195 {
196 4 $results->push( $found );
197 }
198 }
199 }
200 4 return( $results );
201 }
202  
203 sub lang
204 {
205 0 ▶ my $self = shift( @_ );
Subroutine lang: not called CC=3 SCAR=24.8
Pod documented
206 0 my( $node, @params ) = @_;
207 0 ▶ die( "lang: function takes 1 parameter\n" ) if( @params != 1 );
Branch: 1 branch
BranchTrueFalseTotal
if @params != 1 0 0 0
208 0 my $lang = $node->findvalue( '(ancestor-or-self::*[@xml:lang]/@xml:lang)[1]' );
209 0 my $lclang = lc( $params[0]->string_value );
210 # warn("Looking for lang($lclang) in $lang\n");
211 0 ▶ if( substr( lc( $lang ), 0, length( $lclang ) ) eq $lclang )
Branch: 1 branch
BranchTrueFalseTotal
if (substr(lc $lang, 0, length $lclang) eq $lclang) { } 0 0 0
212 {
213 0 return( $TRUE );
214 }
215 else
216 {
217 0 return( $FALSE );
218 }
219 }
220  
221 sub last
222 {
223 0 ▶ my $self = shift( @_ );
Subroutine last: not called CC=2 SCAR=17.9
Pod documented
224 0 my( $node, @params ) = @_;
225 0 ▶ die( "last: function doesn't take parameters\n" ) if( scalar( @params ) );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @params 0 0 0
226 0 return( $self->new_number( $self->{pp}->_get_context_size ) );
227 }
228  
229 sub local_name
230 {
231 0 ▶ my $self = shift( @_ );
Subroutine local_name: not called CC=3 SCAR=24.8
Pod documented
232 0 my( $node, @params ) = @_;
233 0 ▶ if( @params > 1 )
Branch: 2 branches
BranchTrueFalseTotal
if (@params > 1) { } 0 0 0
elsif (@params) { } 0 0 0
234 {
235 0 die( "name() function takes one or no parameters\n" );
236 }
237 elsif( @params )
238 {
239 0 my $nodeset = shift( @params );
240 0 $node = $nodeset->get_node(1);
241 }
242 0 return( $self->new_literal( $node->getLocalName ) );
243 }
244  
245 sub name
246 {
247 0 ▶ my $self = shift( @_ );
Subroutine name: not called CC=3 SCAR=24.8
Pod documented
248 0 my( $node, @params ) = @_;
249 0 ▶ if( @params > 1 )
Branch: 2 branches
BranchTrueFalseTotal
if (@params > 1) { } 0 0 0
elsif (@params) { } 0 0 0
250 {
251 0 die( "name() function takes one or no parameters\n" );
252 }
253 elsif( @params )
254 {
255 0 my $nodeset = shift( @params );
256 0 $node = $nodeset->get_node(1);
257 }
258 0 return( $self->new_literal( $node->getName ) );
259 }
260  
261 sub namespace_uri
262 {
263 0 ▶ my $self = shift( @_ );
Subroutine namespace_uri: not called CC=1 SCAR=6.9
Pod documented
264 0 my( $node, @params ) = @_;
265 0 die( "namespace-uri: Function not supported\n" );
266 }
267  
268 6 ▶sub new_literal { return( shift->_class_for( 'Literal' )->new( @_ ) ); }
Subroutine new_literal: called CC=1 SCAR=0.0
Pod documented
269  
270 4 ▶sub new_nodeset { return( shift->_class_for( 'NodeSet' )->new( @_ ) ); }
Subroutine new_nodeset: called CC=1 SCAR=0.0
Pod documented
271  
272 20 ▶sub new_number { return( shift->_class_for( 'Number' )->new( @_ ) ); }
Subroutine new_number: called CC=1 SCAR=0.0
Pod documented
273  
274 sub normalize_space
275 {
276 0 ▶ my $self = shift( @_ );
Subroutine normalize_space: not called CC=3 SCAR=24.8
Pod documented
277 0 my( $node, @params ) = @_;
278 0 ▶ die( "normalize-space: Wrong number of params\n" ) if( @params > 1 );
Branch: 1 branch
BranchTrueFalseTotal
if @params > 1 0 0 0
279 0 my $str;
280 0 ▶ if( @params )
Branch: 1 branch
BranchTrueFalseTotal
if (@params) { } 0 0 0
281 {
282 0 $str = $params[0]->string_value;
283 }
284 else
285 {
286 0 $str = $node->string_value;
287 }
288 # $str =~ s/^\s*//;
289 # $str =~ s/\s*$//;
290 0 $str =~ s/^[[:blank:]\h]+|[[:blank:]\h]+$//g;
291 # $str =~ s/\s+/ /g;
292 0 $str =~ s/[[:blank:]\h]+/ /g;
293 0 return( $self->new_literal( $str ) );
294 }
295  
296 sub not
297 {
298 0 ▶ my $self = shift( @_ );
Subroutine not: not called CC=3 SCAR=24.8
Pod documented
299 0 my( $node, @params ) = @_;
300 0 ▶ $params[0] = $params[0]->to_boolean unless( $params[0]->isa( 'HTML::Object::XPath::Boolean' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $params[0]->isa("HTML::Object::XPath::Boolean") 0 0 0
301 0 ▶ return( $params[0]->value ? $FALSE : $TRUE );
Branch: 1 branch
BranchTrueFalseTotal
$params[0]->value ? : 0 0 0
302 }
303  
304 sub number
305 {
306 0 ▶ my $self = shift( @_ );
Subroutine number: not called CC=4 SCAR=30.0
Pod documented
307 0 my( $node, @params ) = @_;
308 0 ▶ die( "number: Too many parameters\n" ) if( @params > 1 );
Branch: 1 branch
BranchTrueFalseTotal
if @params > 1 0 0 0
309 0 ▶ if( @params )
Branch: 1 branch
BranchTrueFalseTotal
if (@params) 0 0 0
310 {
311 0 ▶ if( $params[0]->isa( 'HTML::Object::XPath::Node' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($params[0]->isa("HTML::Object::XPath::Node")) 0 0 0
312 {
313 0 return( $self->new_number( $params[0]->string_value ) );
314 }
315 0 return( $params[0]->to_number );
316 }
317 0 return( $self->new_number( $node->string_value ) );
318 }
319  
320 sub position
321 {
322 0 ▶ my $self = shift( @_ );
Subroutine position: not called CC=2 SCAR=17.9
Pod documented
323 0 my( $node, @params ) = @_;
324 0 ▶ if( scalar( @params ) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @params) 0 0 0
325 {
326 0 die( "position: function doesn't take parameters [ ", @params, " ]\n" );
327 }
328 # return pos relative to axis direction
329 0 return( $self->new_number( $self->{pp}->_get_context_pos ) );
330 }
331  
332 sub round
333 {
334 0 ▶ my $self = shift( @_ );
Subroutine round: not called CC=1 SCAR=6.9
Pod documented
335 0 my( $node, @params ) = @_;
336 0 my $num = $self->number( $node, @params );
337 0 require POSIX;
338 # Yes, I know the spec says do not do this...
339 # return( $self->new_number( POSIX::floor( $num->value + 0.5 ) ) );
340 0 return( $self->new_number( CORE::sprintf( '%.*f', $num->value ) ) );
341 }
342  
343 sub starts_with
344 {
345 0 ▶ my $self = shift( @_ );
Subroutine starts_with: not called CC=3 SCAR=24.8
Pod documented
346 0 my( $node, @params ) = @_;
347 0 ▶ die( "starts-with: incorrect number of params\n" ) unless( @params == 2 );
Branch: 1 branch
BranchTrueFalseTotal
unless @params == 2 0 0 0
348 0 my( $string1, $string2 ) = ( $params[0]->string_value, $params[1]->string_value );
349 0 ▶ if( substr( $string1, 0, length( $string2 ) ) eq $string2 )
Branch: 1 branch
BranchTrueFalseTotal
if (substr($string1, 0, length $string2) eq $string2) 0 0 0
350 {
351 0 return( $TRUE );
352 }
353 0 return( $FALSE );
354 }
355  
356 sub string
357 {
358 4 ▶ my $self = shift( @_ );
Subroutine string: called CC=3 SCAR=11.8
Pod documented
359 4 my( $node, @params ) = @_;
360 4 ▶ die( "string: Too many parameters\n" ) if( @params > 1 );
Branch: 1 branch
BranchTrueFalseTotal
if @params > 1 0 4 4
361 4 ▶ if( @params )
Branch: 1 branch
BranchTrueFalseTotal
if (@params) 4 0 4
362 {
363 4 return( $self->new_literal( $params[0]->string_value ) );
364 }
365
366 # TODO - this MUST be wrong! - not sure now. -matt
367 0 return( $self->new_literal( $node->string_value ) );
368 # default to nodeset with just $node in.
369 }
370  
371 sub string_length
372 {
373 0 ▶ my $self = shift( @_ );
Subroutine string_length: not called CC=3 SCAR=24.8
Pod documented
374 0 my( $node, @params ) = @_;
375 0 ▶ die( "string-length: Wrong number of params\n" ) if( @params > 1 );
Branch: 1 branch
BranchTrueFalseTotal
if @params > 1 0 0 0
376 0 ▶ if( @params )
Branch: 1 branch
BranchTrueFalseTotal
if (@params) { } 0 0 0
377 {
378 0 return( $self->new_number( length( $params[0]->string_value ) ) );
379 }
380 else
381 {
382 0 return( $self->new_number( length( $node->string_value ) ) );
383 }
384 }
385  
386 sub substring
387 {
388 0 ▶ my $self = shift( @_ );
Subroutine substring: not called CC=4 SCAR=30.0
Pod documented
389 0 my( $node, @params ) = @_;
390 0 ▶ die( "substring: Wrong number of parameters\n" ) if( @params < 2 || @params > 3 );
Branch: 1 branch
BranchTrueFalseTotal
if @params < 2 or @params > 3 0 0 0
Condition0%0/3
@params < 2 or @params > 3
l!l&&r!l&&!r
000
MC/DC0%0/2
@params < 2 or @params > 3
@params < 2 @params > 3
Truth table0%0/3
A or B
A= @params < 2B= @params > 3
ABresult
1X1
011
000
391 0 my( $str, $offset, $len );
392 0 $str = $params[0]->string_value;
393 0 $offset = $params[1]->value;
394 # uses 1 based offsets
395 0 $offset--;
396 0 ▶ if( @params == 3 )
Branch: 1 branch
BranchTrueFalseTotal
if (@params == 3) { } 0 0 0
397 {
398 0 $len = $params[2]->value;
399 0 return( $self->new_literal( substr( $str, $offset, $len ) ) );
400 }
401 else
402 {
403 0 return( $self->new_literal( substr( $str, $offset ) ) );
404 }
405 }
406  
407 sub substring_after
408 {
409 2 ▶ my $self = shift( @_ );
Subroutine substring_after: called CC=3 SCAR=11.4
Pod documented
410 2 my( $node, @params ) = @_;
411 2 ▶ die( "starts-with: incorrect number of params\n" ) unless( @params == 2 );
Branch: 1 branch
BranchTrueFalseTotal
unless @params == 2 0 2 2
412 2 my $long = $params[0]->string_value;
413 2 my $short = $params[1]->string_value;
414 2 ▶ if( $long =~ m{\Q$short\E(.*)$} )
Branch: 1 branch
BranchTrueFalseTotal
if ($long =~ /\Q$short\E(.*)$/) { } 2 0 2
415 {
416 2 return( $self->new_literal( $1 ) );
417 }
418 else
419 {
420 0 return( $self->new_literal( '' ) );
421 }
422 }
423  
424 sub substring_before
425 {
426 0 ▶ my $self = shift( @_ );
Subroutine substring_before: not called CC=3 SCAR=24.8
Pod documented
427 0 my( $node, @params ) = @_;
428 0 ▶ die( "starts-with: incorrect number of params\n" ) unless( @params == 2 );
Branch: 1 branch
BranchTrueFalseTotal
unless @params == 2 0 0 0
429 0 my $long = $params[0]->string_value;
430 0 my $short = $params[1]->string_value;
431 0 ▶ if( $long =~ m{^(.*?)\Q$short} )
Branch: 1 branch
BranchTrueFalseTotal
if ($long =~ /^(.*?)\Q$short\E/) { } 0 0 0
432 {
433 0 return( $self->new_literal( $1 ) );
434 }
435 else
436 {
437 0 return( $self->new_literal( '' ) );
438 }
439 }
440  
441 sub sum
442 {
443 0 ▶ my $self = shift( @_ );
Subroutine sum: not called CC=3 SCAR=24.8
Pod documented
444 0 my( $node, @params ) = @_;
445 0 ▶ die( "sum: Parameter must be a NodeSet\n" ) unless( $params[0]->isa( 'HTML::Object::XPath::NodeSet' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $params[0]->isa("HTML::Object::XPath::NodeSet") 0 0 0
446 0 my $sum = 0;
447 0 foreach my $node ( $params[0]->get_nodelist )
448 {
449 0 $sum += $self->number( $node )->value;
450 }
451 0 return( $self->new_number( $sum ) );
452 }
453  
454 sub translate
455 {
456 0 ▶ my $self = shift( @_ );
Subroutine translate: not called CC=4 SCAR=30.0
Pod documented
457 0 my( $node, @params ) = @_;
458 0 ▶ die( "translate: Wrong number of params\n" ) if( @params != 3 );
Branch: 1 branch
BranchTrueFalseTotal
if @params != 3 0 0 0
459 0 local $_ = $params[0]->string_value;
460 0 my $find = $params[1]->string_value;
461 0 my $repl = $params[2]->string_value;
462 0 $repl= substr( $repl, 0, length( $find ) );
463 0 my %repl;
464 0 @repl{split( //, $find )} = split( //, $repl );
465 0 s{(.)}
466 {
467 0 ▶ CORE::exists( $repl{$1} ) ? defined( $repl{$1} ) ? $repl{$1} : '' : $1
Branch: 2 branches
BranchTrueFalseTotal
exists $repl{$1} ? : 0 0 0
defined $repl{$1} ? : 0 0 0
468 }ges;
469 0 return( $self->new_literal( $_ ) );
470 }
471  
472 sub true
473 {
474 0 ▶ my $self = shift( @_ );
Subroutine true: not called CC=2 SCAR=17.9
Pod documented
475 0 my( $node, @params ) = @_;
476 0 ▶ die( "true: function takes no parameters\n" ) if( @params > 0 );
Branch: 1 branch
BranchTrueFalseTotal
if @params > 0 0 0 0
477 0 return( $TRUE );
478 }
479  
480 sub _class_for
481 {
482 30 ▶ my( $self, $mod ) = @_;
Subroutine _class_for: called CC=3 SCAR=11.3
483 30 eval( "require ${BASE_CLASS}\::${mod};" );
484 30 ▶ die( $@ ) if( $@ );
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 30 30
485 # ${"${BASE_CLASS}\::${mod}\::DEBUG"} = $DEBUG;
486 30 ▶ eval( "\$${BASE_CLASS}\::${mod}\::DEBUG = " . ( $DEBUG // 0 ) );
Condition50%1/2
$HTML::Object::XPath::Function::DEBUG // 0
l!l
300
MC/DC0%0/1
$HTML::Object::XPath::Function::DEBUG // 0
$HTML::Object::XPath::Function::DEBUG
Truth table50%1/2
A
A= $HTML::Object::XPath::Function::DEBUG
Aresult
11
00
487 30 return( "${BASE_CLASS}::${mod}" );
488 }
489  
490 sub _execute
491 {
492 26 ▶ my $self = shift( @_ );
Subroutine _execute: called CC=2 SCAR=7.0
493 26 my( $name, $node, @params ) = @_;
494 26 ▶ return( $self->error( "No function name was provided to execute." ) ) if( !$name );
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 26 26
495 26 $name =~ s/-/_/g;
496 12 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
497 26 return( $self->$name( $node, @params ) );
498 }
499  
500 12 1;
501 # NOTE: POD
502 __END__
503  
504 =encoding utf-8
505  
506 =head1 NAME
507  
508 HTML::Object::XPath::Function - HTML Object XPath Functions
509  
510 =head1 SYNOPSIS
511  
512 use HTML::Object::XPath::Function;
513 my $func = HTML::Object::XPath::Function->new ||
514 die( HTML::Object::XPath::Function->error, "\n" );
515  
516 =head1 VERSION
517  
518 v0.2.0
519  
520 =head1 DESCRIPTION
521  
522 This module implements various XPath functions described below.
523  
524 =head1 CONSTRUCTOR
525  
526 Provided with a L<XPath object|HTML::Object::XPath>, a function C<name> and function C<parameters> and this will instantiate a new L<HTML::Object::Function> object and return it.
527  
528 =head1 METHODS
529  
530 =head2 as_string
531  
532 Returns a string representation of this function object.
533  
534 =head2 as_xml
535  
536 Returns a xml representation of this function object.
537  
538 =head2 boolean
539  
540 Provided with a L<node object|HTML::Object::Element> and a list of parameters and this will return the first parameter as a L<boolean object|HTML::Object::XPath::Booleab>. It will raise an exception if there are more than one arguments provided.
541  
542 =head2 ceiling
543  
544 Provided with a L<node object|HTML::Object::Element> and an optional parameter, and this will return the L<ceiling|POSIX/ceil> of the string value of the node or that of the parameter provided, if any.
545  
546 It returns its value as a new L<number object|HTML::Object::XPath::Number>. It will raise an exception if more than one parameter was provided.
547  
548 =head2 concat
549  
550 Provided with a L<node object|HTML::Object::Element> and a list of 2 parameters or more and this will return a new L<literal object|HTML::Object::XPath::Literal> from the concatenation of each parameter string value.
551  
552 =head2 contains
553  
554 Provided with a L<node object|HTML::Object::Element> and a list of exactly 2 parameters and this will return L<true|HTML::Object::XPath::Boolean> if the first parameter match the second one as a regular expression, or returns L<false|HTML::Object::XPath::Boolean> otherwise.
555  
556 =head2 count
557  
558 Provided with a L<node object|HTML::Object::Element> and a L<node set|HTML::Object::XPath::NodeSet> and this will return the size of the node set as a L<number|HTML::Object::XPath::Number>
559  
560 =head2 evaluate
561  
562 Provided with a L<node object|HTML::Object::Element> and this will evaluate the node using each parameter set during object instantiation and add the result for each in a new array.
563  
564 It will then return the execution of the function name on the node passing it the list of previously collected results.
565  
566 =head2 false
567  
568 Returns L<false|HTML::Object::XPath::Boolean> and it will raise an exception if any arguments was provided.
569  
570 =head2 floor
571  
572 Provided with a L<node object|HTML::Object::Element> and an optional parameter, and this will return the L<floor|
573 POSIX/floor> of the string value of the node or that of the parameter provided, if any.
574  
575 It returns its value as a new L<number object|HTML::Object::XPath::Number>. It will raise an exception if more than one parameter was provided.
576  
577 =head2 id
578  
579 Provided with a L<node object|HTML::Object::Element> and 1 parameter, and this will return a new L<node set|HTML::Object::XPath::NodeSet> of L<element object|HTML::Object::Element> who match the id found in the node provided.
580  
581 =head2 lang
582  
583 Provided with a L<node object|HTML::Object::Element> and 1 parameter, and this will return L<true|HTML::Object::XPath::Boolean> if the node lang, if any at all, match the one provide as a parameter, otherwise it returns L<false|HTML::Object::XPath::Boolean>.
584  
585 =head2 last
586  
587 This takes no argument and returns the size, as a L<number|HTML::Object::XPath::Number> of the nodes in the context set in XPath.
588  
589 =head2 local_name
590  
591 Provided with a L<node object|HTML::Object::Element> and optionally some parameter and this will return the local name of the node, or that of the first parameter provided, if any, as a L<literal object|HTML::Object::XPath::Literal>
592  
593 =head2 name
594  
595 Provided with a L<node object|HTML::Object::Element> and optionally some parameter and this will return the name of the node, or that of the first parameter provided, if any, as a L<literal object|HTML::Object::XPath::Literal>
596  
597 =head2 namespace_uri
598  
599 This is an unsupported function and it will raise an exception when called.
600  
601 =head2 new_literal
602  
603 Returns a new L<literal object|HTML::Object::XPath::Literal> passing it whatever arguments was provided.
604  
605 =head2 new_nodeset
606  
607 Returns a new L<node set object|HTML::Object::XPath::NodeSet> passing it whatever arguments was provided.
608  
609 =head2 new_number
610  
611 Returns a new L<number object|HTML::Object::XPath::Number> passing it whatever arguments was provided.
612  
613 =head2 normalize_space
614  
615 Provided with a L<node object|HTML::Object::Element> and optionally some parameter and this will take the string value of the node, or that of the first parameter, if any at al, and remove any leading or trailing spaces as well as replacing multiple spaces by just one space, and return the new string as a L<literal object|HTML::Object::XPath::Literal>
616  
617 =head2 not
618  
619 Provided with a L<node object|HTML::Object::Element> and one parameter and this will return L<true|HTML::Object::XPath::Boolean> if the value of the first parameter is B<not> true, or L<false|HTML::Object::XPath::Boolean> otherwise.
620  
621 =head2 number
622  
623 Provided with a L<node object|HTML::Object::Element> and optionally one parameter and this will return the node string value, or that of the parameter provided, if any, as a new L<number object|HTML::Object::XPath::Number>
624  
625 =head2 position
626  
627 Returns th context position as a L<number|HTML::Object::XPath::Number>. It will raise an exception if any parameter was provided.
628  
629 =head2 round
630  
631 =head2 starts_with
632  
633 Provided with a L<node object|HTML::Object::Element> and two parameters and this will return L<true|HTML::Object::XPath::Boolean> if the string value of the second parameter is at the beginning of the first parameter, or L<false|HTML::Object::XPath::Boolean> otherwise.
634  
635 =head2 string
636  
637 Provided with a L<node object|HTML::Object::Element> and one parameter and this returns the parameter string value as a new L<literal object|HTML::Object::XPath::Literal>
638  
639 It will raise an exception if more than one parameter was provided.
640  
641 =head2 string_length
642  
643 Provided with a L<node object|HTML::Object::Element> and optionally one parameter and this will return the size, as a L<number|HTML::Object::XPath::Number>, of the string value of the node or that of the parameter if any was provided.
644  
645 =head2 substring
646  
647 Provided with a L<node object|HTML::Object::Element> and two or three parameters and this returns the substring of of the first parameter as a value, at offset specified by the second parameter, and optionally for a length defined by a third parameter, if any. If no third parameter is provided, then it will be until the end of the string.
648  
649 It returns the substring as a new L<literal object|HTML::Object::XPath::Literal>. It will raise an exception if less than 2 or more than 3 parameters were provided.
650  
651 =head2 substring_after
652  
653 Provided with a L<node object|HTML::Object::Element> and two parameters and this will return the string that follows the string value of the second parameter in the first one up to its end.
654  
655 It returns the substring as a new L<literal object|HTML::Object::XPath::Literal>. It will raise an exception if less or more than 2 parameters were provided.
656  
657 =head2 substring_before
658  
659 Provided with a L<node object|HTML::Object::Element> and two parameters and this will return the string that precede the string value of the second parameter in the first one up to its start.
660  
661 It returns the substring as a new L<literal object|HTML::Object::XPath::Literal>. It will raise an exception if less or more than 2 parameters were provided.
662  
663 =head2 sum
664  
665 Provided with a L<node object|HTML::Object::Element> and a parameters that must be a L<node set|HTML::Object::XPath::NodeSet> and this will return the cumulative string value of each node as a number.
666  
667 It returns the resulting total as a new L<number> object|HTML::Object::XPath::Number>. It will raise an exception if the parameter provided is not a L<HTML::Object::XPath::NodeSet> object.
668  
669 =head2 translate
670  
671 Provided with a L<node object|HTML::Object::Element> and three parameters, and this will search in the first parameter for any occurrence of characters found in the second parameter and replace them with their alternative at the exact same position in string in the third parameter.
672  
673 It returns the substring as a new L<literal object|HTML::Object::XPath::Literal>. It will raise an exception if less or more than 3 parameters were provided.
674  
675 =head2 true
676  
677 Returns L<true|HTML::Object::XPath::Boolean> and it will raise an exception if any arguments was provided.
678  
679 =head1 AUTHOR
680  
681 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
682  
683 =head1 SEE ALSO
684  
685 L<HTML::Object::XPath>, L<HTML::Object::XPath::Boolean>, L<HTML::Object::XPath::Expr>, L<HTML::Object::XPath::Function>, L<HTML::Object::XPath::Literal>, L<HTML::Object::XPath::LocationPath>, L<HTML::Object::XPath::NodeSet>, L<HTML::Object::XPath::Number>, L<HTML::Object::XPath::Root>, L<HTML::Object::XPath::Step>, L<HTML::Object::XPath::Variable>
686  
687 =head1 COPYRIGHT & LICENSE
688  
689 Copyright(c) 2021 DEGUEST Pte. Ltd.
690  
691 All rights reserved
692  
693 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
694  
695 =cut
« lib/HTML/Object/DOM/Element/Track.pm ↑ Summary lib/HTML/Object/TokenList.pm »