HTML-Object

 view release on metacpan or  search on metacpan

lib/HTML/Object/XQuery.pod  view on Meta::CPAN

The argument should always be a plain hash reference, and not a blessed hash. To determine if an hash reference is a plain hash reference, use C<$.isPlainObject()>

For example:

    jQuery->isEmptyObject({}); # true
    jQuery->isEmptyObject({ foo => "bar" }); # false

L<https://api.jquery.com/jQuery.isEmptyObject/>

=head2 isFunction

Determines if its argument is callable as a function, meaning it is either a function name, or a code reference.

For example:

    sub stub {}
    my $objs = [
        sub{},
        { x => 15, y => 20 },
        undef,
        'stub',
        'sub',
    ];

    xQuery->each( $objs, sub
    {
        my $i = shift( @_ );
        my $isFunc = xQuery->isFunction( $objs->[$i] );
        say "xQuery->isFunction( \$objs->[$i] ) = ", ( $isFunc ? 'true' : 'false' );
    });

This would yield:

    xQuery->isFunction( $objs->[0] ) = true
    xQuery->isFunction( $objs->[1] ) = false
    xQuery->isFunction( $objs->[2] ) = false
    xQuery->isFunction( $objs->[3] ) = true
    xQuery->isFunction( $objs->[4] ) = false

L<https://api.jquery.com/jQuery.isFunction/>

=head2 isNumeric

Determines whether its argument represents a number.

The C<$.isNumeric()> method checks whether its argument represents a numeric value. If so, it returns true. Otherwise it returns false. The argument can be of any type.

For example:

    # true (numeric)
    $.isNumeric( "-10" );
    $.isNumeric( "0" );
    $.isNumeric( 0xFF );
    $.isNumeric( "0xFF" );
    $.isNumeric( "8e5" );
    $.isNumeric( "3.1415" );
    $.isNumeric( +10 );
    $.isNumeric( 0144 );
    # Yes, under perl, nan and Inf are considered a number. Don't believe me? Try:
    # perl -MScalar::Util=looks_like_number -lE 'say looks_like_number("nan")'
    $.isNumeric( NaN );
    $.isNumeric( Inf );
     
    # false (non-numeric)
    $.isNumeric( "-0x42" );
    $.isNumeric( "7.2acdgs" );
    $.isNumeric( "" );
    $.isNumeric( {} );
    $.isNumeric( true );
    $.isNumeric( undef );

L<https://api.jquery.com/jQuery.isNumeric/>

=head2 isPlainObject

Check to see if an object is a plain hash reference, and not a blessed one.

For example:

    xQuery->isPlainObject({}) # true
    xQuery->isPlainObject( "test" ) # false

L<https://api.jquery.com/jQuery.isPlainObject/>

=head2 isWindow

Determine whether the argument is a L<window|HTML::Object::DOM::Window>.

Normally, this is used, under jQuery to determine if it is operating against a browser window (such as the current window or an iframe). However, since this is perl, this is merely used to determine if the given value is a L<window object|HTML::Objec...

For example:

    my $window = HTML::Object::DOM::Window->new;
    $.isWindow( $window ); # true

L<https://api.jquery.com/jQuery.isWindow/>

=head2 makeArray

Many methods, both in xQuery and in L<HTML::Object::XQuery> class in general, return objects that are array-like. For example, the xQuery (jQuery) factory function C<$()> returns an L<array object|Module::Generic::Array> that has many of the properti...

Note that after the conversion, any special features the array object had (such as the L<xQuery|HTML::Object::XQuery> methods in our example) will no longer be present. The object is now a plain array reference.

For example:

    <!DOCTYPE html>
    <html lang="en-GB">
        <head>
            <meta charset="utf-8" />
            <title></title>
        </head>
        <body>
            <div>First</div>
            <div>Second</div>
            <div>Third</div>
            <div>Fourth</div>
        </body>
    </html>

    my $elems = $('div');
    my $arr = xQuery->makeArray( $elems );

lib/HTML/Object/XQuery.pod  view on Meta::CPAN

=head2 now

Return a L<DateTime object|DateTime> representing the current time and that stringifies to the number of seconds since epoch, resulting into the equivalent of L<perlfunc/time>

Under jQuery, this is an alias for C<Date.now()>

    my $time = $.now();
    say $time; # 1714619232
    say $time->epoch; # 1714619232
    say $time->iso8601; # 2024-05-02T03:07:12

L<https://api.jquery.com/jQuery.now/>

=head2 parseHTML

Parses a string and returns an L<array object|Module::Generic::Array> of  L<nodesHTML::Object::DOM::Element>. In jQuery, this parses a string into an array of DOM nodes.

This method render all trailing or leading text (even if they are just whitespaces). To prevent trailing and leading whitespaces from being converted to L<space nodes|HTML::Object::DOM::Space> you can pass the HTML string through L<jQuery.trim|/trim>...

Under jQuery, this method takes 3 parameters (HTML string, a context document, and C<keepScripts>), but since the last 2 have no bearing under perl, this method only takes a single argument: an HTML string.

For example:

    my $str = "hello, <b>my name is</b> xQuery.";
    my $html = $.parseHTML( $str );
    my $nodeNames = [];

    # Gather the parsed HTML's node names
    $.each( $html, sub
    {
        my( $i, $el ) = @_;
        $nodeNames->[$i] = el->nodeName;
    }) || die( $xQuery::ERROR );
    
    say $nodeNames->join( ', ' );

This yields:

    #text, B, #text

L<https://api.jquery.com/jQuery.parseHTML/>

=head2 parseJSON

Takes a well-formed JSON string and returns the resulting value, which could be a string, a number, a boolean, an array reference or an hash reference.

Passing in a malformed JSON string results in an error being returned, which can be retrieved with C<$xQuery::ERROR>. For example, the following are all invalid JSON strings:

=over 4

=item * "{test: 1}" (test does not have double quotes around it).

=item * "{'test': 1}" ('test' is using single quotes instead of double quotes).

=item * "'test'" ('test' is using single quotes instead of double quotes).

=item * ".1" (a number must start with a digit; "0.1" would be valid).

=item * "undefined" (undefined cannot be represented in a JSON string; null, however, can be).

=item * "NaN" (NaN cannot be represented in a JSON string; direct representation of Infinity is also not permitted).

=back

The JSON standard does not permit "control characters" such as a tab or newline. An example like C<< $.parseJSON( '{ "testing":"1\t2\n3" }' ) >> will result in an error, because the JSON parser converts the string's tab and newline escapes into liter...

For details on the JSON format, see https://json.org/.

For example:

    my $ref = $.parseJSON( '{ "name": "John" }' );
    say( $ref->{name} eq 'John' );

L<https://api.jquery.com/jQuery.parseJSON/>

=head2 parseXML

Parses a string into an L<XML document|XML::LibXML::Document> and returns it.

This requires the module L<XML::LibXML> to be installed.

C<xQuery.parseXML> uses L<XML::LibXML> to create a valid L<XML document|XML::LibXML::Document>. This document is returned, and this is all. Unlike the jQuery interface, B<you cannot pass> this object to L<HTML::Object::XQuery>, such as:

    my $doc = $.parseXML( $str ) || die( $xQuery::ERROR );
    my $xml = $($doc);
    my $title = $xml->find( 'title' );

Maybe in the future, I will implement it.

L<https://api.jquery.com/jQuery.parseXML/>

=head2 proxy

This method is deprecated in jQuery

=head2 queue

This method is unsupported with no meaning under perl.

=head2 removeData

Remove a previously-stored piece of data and returns C<undef> in scalar context and an empty list in list context.

This C<$.removeData()> method takes an L<element object|HTML::Object::DOM::Element> and a name, and removes values that were previously set using C<$.data()>. When called with the name of a key, C<$.removeData()> deletes that particular value; when c...

For example:

    my $div = $('<div />');
    say $div->data( 'test1' );
    $.data( $div, 'test1', 'VALUE-1' );
    $.data( $div, 'test2', 'VALUE-2' );
    say $.data( $div, 'test1' );
    $.removeData( $div, 'test1' );
    say $.data( $div, 'test1' );
    say $.data( $div, 'test2' );

This would yield:

    undef
    VALUE-1
    undef



( run in 1.363 second using v1.01-cache-2.11-cpan-97f6503c9c8 )