| 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 | use strict; | ||
| 18 | use warnings; | ||
| 19 | warnings::register_categories( 'HTML::Object' ); | ||
| 20 | use parent qw( Module::Generic ); | ||
| 21 | use vars qw( $TRUE $FALSE $BASE_CLASS $DEBUG $VERSION ); | ||
| 22 | use HTML::Object::XPath::Boolean; | ||
| 23 | our $TRUE = HTML::Object::XPath::Boolean->True; | ||
| 24 | our $FALSE = HTML::Object::XPath::Boolean->False; | ||
| 25 | our $BASE_CLASS = 'HTML::Object::XPath'; | ||
| 26 | our $DEBUG = 0; | ||
| 27 | our $VERSION = 'v0.2.0'; | ||
| 28 | }; | ||
| 29 | |||
| 30 | use strict; | ||
| 31 | use warnings; | ||
| 32 | |||
| 33 | sub init | ||
| 34 | { | ||
| 35 | my $self = shift( @_ ); | ||
| 36 | # XPath Parser -> HTML::Object::XPath | ||
| 37 | $self->{pp} = shift( @_ ); | ||
| 38 | $self->{name} = shift( @_ ); | ||
| 39 | $self->{params} = shift( @_ ); | ||
| 40 | $self->{_init_strict_use_sub} = 1; | ||
| 41 | $self->SUPER::init( @_ ) || return( $self->pass_error ); | ||
| 42 | return( $self ); | ||
| 43 | } | ||
| 44 | |||
| 45 | sub as_string | ||
| 46 | { | ||
| 47 | my $self = shift( @_ ); | ||
| 48 | my $string = $self->{name} . '('; | ||
| 49 | my $second; | ||
| 50 | foreach( @{$self->{params}} ) | ||
| 51 | { | ||
| 52 | $string .= ',' if( $second++ ); | ||
| 53 | $string .= $_->as_string; | ||
| 54 | } | ||
| 55 | $string .= ')'; | ||
| 56 | return( $string ); | ||
| 57 | } | ||
| 58 | |||
| 59 | sub as_xml | ||
| 60 | { | ||
| 61 | my $self = shift( @_ ); | ||
| 62 | my $string = sprintf( '<Function name="%s"', $self->{name} ); | ||
| 63 | my $params = ''; | ||
| 64 | foreach( @{$self->{params}} ) | ||
| 65 | { | ||
| 66 | $params .= '<Param>' . $_->as_xml . "</Param>\n"; | ||
| 67 | } | ||
| 68 | if( $params ) | ||
| 69 | { | ||
| 70 | $string .= ">\n$params</Function>\n"; | ||
| 71 | } | ||
| 72 | else | ||
| 73 | { | ||
| 74 | $string .= " />\n"; | ||
| 75 | } | ||
| 76 | return( $string ); | ||
| 77 | } | ||
| 78 | |||
| 79 | sub boolean | ||
| 80 | { | ||
| 81 | my $self = shift( @_ ); | ||
| 82 | my( $node, @params ) = @_; | ||
| 83 | die( "boolean: Incorrect number of parameters\n" ) if( @params != 1 ); | ||
| 84 | return( $params[0]->to_boolean ); | ||
| 85 | } | ||
| 86 | |||
| 87 | sub ceiling | ||
| 88 | { | ||
| 89 | my $self = shift( @_ ); | ||
| 90 | my( $node, @params ) = @_; | ||
| 91 | require POSIX; | ||
| 92 | my $num = $self->number( $node, @params ); | ||
| 93 | return( $self->new_number( POSIX::ceil( $num->value ) ) ); | ||
| 94 | } | ||
| 95 | |||
| 96 | sub concat | ||
| 97 | { | ||
| 98 | my $self = shift( @_ ); | ||
| 99 | my( $node, @params ) = @_; | ||
| 100 | die( "concat: Too few parameters\n" ) if( @params < 2 ); | ||
| 101 | # TODO: Check for improvement | ||
| 102 | my $string = join( '', map{ $_->string_value } @params ); | ||
| 103 | return( $self->new_literal( $string ) ); | ||
| 104 | } | ||
| 105 | |||
| 106 | sub contains | ||
| 107 | { | ||
| 108 | my $self = shift( @_ ); | ||
| 109 | my( $node, @params ) = @_; | ||
| 110 | die( "starts-with: incorrect number of params\n" ) unless( @params == 2 ); | ||
| 111 | my $value = $params[1]->string_value; | ||
| 112 | if( $params[0]->string_value =~ /(.*?)\Q$value\E(.*)/ ) | ||
| 113 | { | ||
| 114 | return( $TRUE ); | ||
| 115 | } | ||
| 116 | return( $FALSE ); | ||
| 117 | } | ||
| 118 | |||
| 119 | sub count | ||
| 120 | { | ||
| 121 | my $self = shift( @_ ); | ||
| 122 | my( $node, @params ) = @_; | ||
| 123 | die( "count: Parameter must be a NodeSet\n" ) unless( $params[0]->isa( 'HTML::Object::XPath::NodeSet' ) ); | ||
| 124 | return( $self->new_number( $params[0]->size ) ); | ||
| 125 | } | ||
| 126 | |||
| 127 | sub evaluate | ||
| 128 | { | ||
| 129 | my $self = shift( @_ ); | ||
| 130 | my $node = shift( @_ ); | ||
| 131 | while( $node->isa( 'HTML::Object::XPath::NodeSet' ) ) | ||
| 132 | { | ||
| 133 | $node = $node->get_node(1); | ||
| 134 | } | ||
| 135 | my @params; | ||
| 136 | foreach my $param ( @{$self->{params}} ) | ||
| 137 | { | ||
| 138 | my $results = $param->evaluate( $node ); | ||
| 139 | push( @params, $results ); | ||
| 140 | } | ||
| 141 | return( $self->_execute( $self->{name}, $node, @params ) ); | ||
| 142 | } | ||
| 143 | |||
| 144 | sub false | ||
| 145 | { | ||
| 146 | my $self = shift( @_ ); | ||
| 147 | my( $node, @params ) = @_; | ||
| 148 | die( "true: function takes no parameters\n" ) if( @params > 0 ); | ||
| 149 | return( $FALSE ); | ||
| 150 | } | ||
| 151 | |||
| 152 | sub floor | ||
| 153 | { | ||
| 154 | my $self = shift( @_ ); | ||
| 155 | my( $node, @params ) = @_; | ||
| 156 | require POSIX; | ||
| 157 | my $num = $self->number( $node, @params ); | ||
| 158 | return( $self->new_number( POSIX::floor( $num->value ) ) ); | ||
| 159 | } | ||
| 160 | |||
| 161 | sub id | ||
| 162 | { | ||
| 163 | my $self = shift( @_ ); | ||
| 164 | my( $node, @params ) = @_; | ||
| 165 | die( "id: Function takes 1 parameter\n" ) unless( @params == 1 ); | ||
| 166 | my $results = $self->new_nodeset(); | ||
| 167 | if( $self->_is_a( $params[0] => 'HTML::Object::XPath::NodeSet' ) ) | ||
| 168 | { | ||
| 169 | # result is the union of applying id() to the | ||
| 170 | # string value of each node in the nodeset. | ||
| 171 | foreach my $node ( $params[0]->get_nodelist ) | ||
| 172 | { | ||
| 173 | my $string = $node->string_value; | ||
| 174 | $results->append( $self->id( $node, $self->new_literal( $string ) ) ); | ||
| 175 | } | ||
| 176 | } | ||
| 177 | # The actual id() function... | ||
| 178 | else | ||
| 179 | { | ||
| 180 | 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 | my @ids = split( /[[:blank:]]/, $string->value ); | ||
| 187 | if( $node->isAttributeNode ) | ||
| 188 | { | ||
| 189 | warn( "calling \($node->getParentNode->getRootNode->getChildNodes)->[0] on attribute node\n" ); | ||
| 190 | $node = ( $node->getParentNode->getRootNode->getChildNodes )->[0]; | ||
| 191 | } | ||
| 192 | foreach my $id ( @ids ) | ||
| 193 | { | ||
| 194 | if( my $found = $node->getElementById( $id ) ) | ||
| 195 | { | ||
| 196 | $results->push( $found ); | ||
| 197 | } | ||
| 198 | } | ||
| 199 | } | ||
| 200 | return( $results ); | ||
| 201 | } | ||
| 202 | |||
| 203 | sub lang | ||
| 204 | { | ||
| 205 | my $self = shift( @_ ); | ||
| 206 | my( $node, @params ) = @_; | ||
| 207 | die( "lang: function takes 1 parameter\n" ) if( @params != 1 ); | ||
| 208 | my $lang = $node->findvalue( '(ancestor-or-self::*[@xml:lang]/@xml:lang)[1]' ); | ||
| 209 | my $lclang = lc( $params[0]->string_value ); | ||
| 210 | # warn("Looking for lang($lclang) in $lang\n"); | ||
| 211 | if( substr( lc( $lang ), 0, length( $lclang ) ) eq $lclang ) | ||
| 212 | { | ||
| 213 | return( $TRUE ); | ||
| 214 | } | ||
| 215 | else | ||
| 216 | { | ||
| 217 | return( $FALSE ); | ||
| 218 | } | ||
| 219 | } | ||
| 220 | |||
| 221 | sub last | ||
| 222 | { | ||
| 223 | my $self = shift( @_ ); | ||
| 224 | my( $node, @params ) = @_; | ||
| 225 | die( "last: function doesn't take parameters\n" ) if( scalar( @params ) ); | ||
| 226 | return( $self->new_number( $self->{pp}->_get_context_size ) ); | ||
| 227 | } | ||
| 228 | |||
| 229 | sub local_name | ||
| 230 | { | ||
| 231 | my $self = shift( @_ ); | ||
| 232 | my( $node, @params ) = @_; | ||
| 233 | if( @params > 1 ) | ||
| 234 | { | ||
| 235 | die( "name() function takes one or no parameters\n" ); | ||
| 236 | } | ||
| 237 | elsif( @params ) | ||
| 238 | { | ||
| 239 | my $nodeset = shift( @params ); | ||
| 240 | $node = $nodeset->get_node(1); | ||
| 241 | } | ||
| 242 | return( $self->new_literal( $node->getLocalName ) ); | ||
| 243 | } | ||
| 244 | |||
| 245 | sub name | ||
| 246 | { | ||
| 247 | my $self = shift( @_ ); | ||
| 248 | my( $node, @params ) = @_; | ||
| 249 | if( @params > 1 ) | ||
| 250 | { | ||
| 251 | die( "name() function takes one or no parameters\n" ); | ||
| 252 | } | ||
| 253 | elsif( @params ) | ||
| 254 | { | ||
| 255 | my $nodeset = shift( @params ); | ||
| 256 | $node = $nodeset->get_node(1); | ||
| 257 | } | ||
| 258 | return( $self->new_literal( $node->getName ) ); | ||
| 259 | } | ||
| 260 | |||
| 261 | sub namespace_uri | ||
| 262 | { | ||
| 263 | my $self = shift( @_ ); | ||
| 264 | my( $node, @params ) = @_; | ||
| 265 | die( "namespace-uri: Function not supported\n" ); | ||
| 266 | } | ||
| 267 | |||
| 268 | sub new_literal { return( shift->_class_for( 'Literal' )->new( @_ ) ); } | ||
| 269 | |||
| 270 | sub new_nodeset { return( shift->_class_for( 'NodeSet' )->new( @_ ) ); } | ||
| 271 | |||
| 272 | sub new_number { return( shift->_class_for( 'Number' )->new( @_ ) ); } | ||
| 273 | |||
| 274 | sub normalize_space | ||
| 275 | { | ||
| 276 | my $self = shift( @_ ); | ||
| 277 | my( $node, @params ) = @_; | ||
| 278 | die( "normalize-space: Wrong number of params\n" ) if( @params > 1 ); | ||
| 279 | my $str; | ||
| 280 | if( @params ) | ||
| 281 | { | ||
| 282 | $str = $params[0]->string_value; | ||
| 283 | } | ||
| 284 | else | ||
| 285 | { | ||
| 286 | $str = $node->string_value; | ||
| 287 | } | ||
| 288 | # $str =~ s/^\s*//; | ||
| 289 | # $str =~ s/\s*$//; | ||
| 290 | $str =~ s/^[[:blank:]\h]+|[[:blank:]\h]+$//g; | ||
| 291 | # $str =~ s/\s+/ /g; | ||
| 292 | $str =~ s/[[:blank:]\h]+/ /g; | ||
| 293 | return( $self->new_literal( $str ) ); | ||
| 294 | } | ||
| 295 | |||
| 296 | sub not | ||
| 297 | { | ||
| 298 | my $self = shift( @_ ); | ||
| 299 | my( $node, @params ) = @_; | ||
| 300 | $params[0] = $params[0]->to_boolean unless( $params[0]->isa( 'HTML::Object::XPath::Boolean' ) ); | ||
| 301 | return( $params[0]->value ? $FALSE : $TRUE ); | ||
| 302 | } | ||
| 303 | |||
| 304 | sub number | ||
| 305 | { | ||
| 306 | my $self = shift( @_ ); | ||
| 307 | my( $node, @params ) = @_; | ||
| 308 | die( "number: Too many parameters\n" ) if( @params > 1 ); | ||
| 309 | if( @params ) | ||
| 310 | { | ||
| 311 | if( $params[0]->isa( 'HTML::Object::XPath::Node' ) ) | ||
| 312 | { | ||
| 313 | return( $self->new_number( $params[0]->string_value ) ); | ||
| 314 | } | ||
| 315 | return( $params[0]->to_number ); | ||
| 316 | } | ||
| 317 | return( $self->new_number( $node->string_value ) ); | ||
| 318 | } | ||
| 319 | |||
| 320 | sub position | ||
| 321 | { | ||
| 322 | my $self = shift( @_ ); | ||
| 323 | my( $node, @params ) = @_; | ||
| 324 | if( scalar( @params ) ) | ||
| 325 | { | ||
| 326 | die( "position: function doesn't take parameters [ ", @params, " ]\n" ); | ||
| 327 | } | ||
| 328 | # return pos relative to axis direction | ||
| 329 | return( $self->new_number( $self->{pp}->_get_context_pos ) ); | ||
| 330 | } | ||
| 331 | |||
| 332 | sub round | ||
| 333 | { | ||
| 334 | my $self = shift( @_ ); | ||
| 335 | my( $node, @params ) = @_; | ||
| 336 | my $num = $self->number( $node, @params ); | ||
| 337 | 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 | return( $self->new_number( CORE::sprintf( '%.*f', $num->value ) ) ); | ||
| 341 | } | ||
| 342 | |||
| 343 | sub starts_with | ||
| 344 | { | ||
| 345 | my $self = shift( @_ ); | ||
| 346 | my( $node, @params ) = @_; | ||
| 347 | die( "starts-with: incorrect number of params\n" ) unless( @params == 2 ); | ||
| 348 | my( $string1, $string2 ) = ( $params[0]->string_value, $params[1]->string_value ); | ||
| 349 | if( substr( $string1, 0, length( $string2 ) ) eq $string2 ) | ||
| 350 | { | ||
| 351 | return( $TRUE ); | ||
| 352 | } | ||
| 353 | return( $FALSE ); | ||
| 354 | } | ||
| 355 | |||
| 356 | sub string | ||
| 357 | { | ||
| 358 | my $self = shift( @_ ); | ||
| 359 | my( $node, @params ) = @_; | ||
| 360 | die( "string: Too many parameters\n" ) if( @params > 1 ); | ||
| 361 | if( @params ) | ||
| 362 | { | ||
| 363 | return( $self->new_literal( $params[0]->string_value ) ); | ||
| 364 | } | ||
| 365 | |||
| 366 | # TODO - this MUST be wrong! - not sure now. -matt | ||
| 367 | return( $self->new_literal( $node->string_value ) ); | ||
| 368 | # default to nodeset with just $node in. | ||
| 369 | } | ||
| 370 | |||
| 371 | sub string_length | ||
| 372 | { | ||
| 373 | my $self = shift( @_ ); | ||
| 374 | my( $node, @params ) = @_; | ||
| 375 | die( "string-length: Wrong number of params\n" ) if( @params > 1 ); | ||
| 376 | if( @params ) | ||
| 377 | { | ||
| 378 | return( $self->new_number( length( $params[0]->string_value ) ) ); | ||
| 379 | } | ||
| 380 | else | ||
| 381 | { | ||
| 382 | return( $self->new_number( length( $node->string_value ) ) ); | ||
| 383 | } | ||
| 384 | } | ||
| 385 | |||
| 386 | sub substring | ||
| 387 | { | ||
| 388 | my $self = shift( @_ ); | ||
| 389 | my( $node, @params ) = @_; | ||
| 390 | die( "substring: Wrong number of parameters\n" ) if( @params < 2 || @params > 3 ); | ||
| 391 | my( $str, $offset, $len ); | ||
| 392 | $str = $params[0]->string_value; | ||
| 393 | $offset = $params[1]->value; | ||
| 394 | # uses 1 based offsets | ||
| 395 | $offset--; | ||
| 396 | if( @params == 3 ) | ||
| 397 | { | ||
| 398 | $len = $params[2]->value; | ||
| 399 | return( $self->new_literal( substr( $str, $offset, $len ) ) ); | ||
| 400 | } | ||
| 401 | else | ||
| 402 | { | ||
| 403 | return( $self->new_literal( substr( $str, $offset ) ) ); | ||
| 404 | } | ||
| 405 | } | ||
| 406 | |||
| 407 | sub substring_after | ||
| 408 | { | ||
| 409 | my $self = shift( @_ ); | ||
| 410 | my( $node, @params ) = @_; | ||
| 411 | die( "starts-with: incorrect number of params\n" ) unless( @params == 2 ); | ||
| 412 | my $long = $params[0]->string_value; | ||
| 413 | my $short = $params[1]->string_value; | ||
| 414 | if( $long =~ m{\Q$short\E(.*)$} ) | ||
| 415 | { | ||
| 416 | return( $self->new_literal( $1 ) ); | ||
| 417 | } | ||
| 418 | else | ||
| 419 | { | ||
| 420 | return( $self->new_literal( '' ) ); | ||
| 421 | } | ||
| 422 | } | ||
| 423 | |||
| 424 | sub substring_before | ||
| 425 | { | ||
| 426 | my $self = shift( @_ ); | ||
| 427 | my( $node, @params ) = @_; | ||
| 428 | die( "starts-with: incorrect number of params\n" ) unless( @params == 2 ); | ||
| 429 | my $long = $params[0]->string_value; | ||
| 430 | my $short = $params[1]->string_value; | ||
| 431 | if( $long =~ m{^(.*?)\Q$short} ) | ||
| 432 | { | ||
| 433 | return( $self->new_literal( $1 ) ); | ||
| 434 | } | ||
| 435 | else | ||
| 436 | { | ||
| 437 | return( $self->new_literal( '' ) ); | ||
| 438 | } | ||
| 439 | } | ||
| 440 | |||
| 441 | sub sum | ||
| 442 | { | ||
| 443 | my $self = shift( @_ ); | ||
| 444 | my( $node, @params ) = @_; | ||
| 445 | die( "sum: Parameter must be a NodeSet\n" ) unless( $params[0]->isa( 'HTML::Object::XPath::NodeSet' ) ); | ||
| 446 | my $sum = 0; | ||
| 447 | foreach my $node ( $params[0]->get_nodelist ) | ||
| 448 | { | ||
| 449 | $sum += $self->number( $node )->value; | ||
| 450 | } | ||
| 451 | return( $self->new_number( $sum ) ); | ||
| 452 | } | ||
| 453 | |||
| 454 | sub translate | ||
| 455 | { | ||
| 456 | my $self = shift( @_ ); | ||
| 457 | my( $node, @params ) = @_; | ||
| 458 | die( "translate: Wrong number of params\n" ) if( @params != 3 ); | ||
| 459 | local $_ = $params[0]->string_value; | ||
| 460 | my $find = $params[1]->string_value; | ||
| 461 | my $repl = $params[2]->string_value; | ||
| 462 | $repl= substr( $repl, 0, length( $find ) ); | ||
| 463 | my %repl; | ||
| 464 | @repl{split( //, $find )} = split( //, $repl ); | ||
| 465 | s{(.)} | ||
| 466 | { | ||
| 467 | CORE::exists( $repl{$1} ) ? defined( $repl{$1} ) ? $repl{$1} : '' : $1 | ||
| 468 | }ges; | ||
| 469 | return( $self->new_literal( $_ ) ); | ||
| 470 | } | ||
| 471 | |||
| 472 | sub true | ||
| 473 | { | ||
| 474 | my $self = shift( @_ ); | ||
| 475 | my( $node, @params ) = @_; | ||
| 476 | die( "true: function takes no parameters\n" ) if( @params > 0 ); | ||
| 477 | return( $TRUE ); | ||
| 478 | } | ||
| 479 | |||
| 480 | sub _class_for | ||
| 481 | { | ||
| 482 | my( $self, $mod ) = @_; | ||
| 483 | eval( "require ${BASE_CLASS}\::${mod};" ); | ||
| 484 | die( $@ ) if( $@ ); | ||
| 485 | # ${"${BASE_CLASS}\::${mod}\::DEBUG"} = $DEBUG; | ||
| 486 | eval( "\$${BASE_CLASS}\::${mod}\::DEBUG = " . ( $DEBUG // 0 ) ); | ||
| 487 | return( "${BASE_CLASS}::${mod}" ); | ||
| 488 | } | ||
| 489 | |||
| 490 | sub _execute | ||
| 491 | { | ||
| 492 | my $self = shift( @_ ); | ||
| 493 | my( $name, $node, @params ) = @_; | ||
| 494 | return( $self->error( "No function name was provided to execute." ) ) if( !$name ); | ||
| 495 | $name =~ s/-/_/g; | ||
| 496 | no strict 'refs'; | ||
| 497 | return( $self->$name( $node, @params ) ); | ||
| 498 | } | ||
| 499 | |||
| 500 | 1; | ||
| 501 | # NOTE: POD | ||
| 502 | __END__ |