| 1 | ##---------------------------------------------------------------------------- | ||
| 2 | ## HTML Object - ~/lib/HTML/Object/DOM/NodeList.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::NodeList; | ||
| 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::Array ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 21 | 1 | ▶ | use vars qw( $VERSION ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 22 | 1 | our $VERSION = 'v0.2.0'; | |
| 23 | }; | ||
| 24 | |||
| 25 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 26 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 27 | |||
| 28 | 0 | ▶ | sub forEach { return( shift->foreach( @_ ) ); } |
|
Subroutine
forEach: not called CC=1 SCAR=6.9
Pod
documented
| |||
| 29 | |||
| 30 | 0 | ▶ | sub item { return( shift->index( @_ ) ); } |
|
Subroutine
item: not called CC=1 SCAR=6.9
Pod
documented
| |||
| 31 | |||
| 32 | 1 | 1; | |
| 33 | # NOTE: POD | ||
| 34 | __END__ | ||
| 35 | |||
| 36 | =encoding utf-8 | ||
| 37 | |||
| 38 | =head1 NAME | ||
| 39 | |||
| 40 | HTML::Object::DOM::NodeList - HTML Object DOM NodeList Class | ||
| 41 | |||
| 42 | =head1 SYNOPSIS | ||
| 43 | |||
| 44 | use HTML::Object::DOM::NodeList; | ||
| 45 | my $list = HTML::Object::DOM::NodeList->new || | ||
| 46 | die( HTML::Object::DOM::NodeList->error, "\n" ); | ||
| 47 | |||
| 48 | =head1 VERSION | ||
| 49 | |||
| 50 | v0.2.0 | ||
| 51 | |||
| 52 | =head1 DESCRIPTION | ||
| 53 | |||
| 54 | C<NodeList> objects are collections of L<nodes|HTML::Object::DOM::Node>, usually returned by properties such as L<HTML::Object::DOM::Node/childNodes> and methods such as L<HTML::Object::DOM::Document/querySelectorAll>. | ||
| 55 | |||
| 56 | =head1 PROPERTIES | ||
| 57 | |||
| 58 | =head2 length | ||
| 59 | |||
| 60 | The number of nodes in the C<NodeList>. | ||
| 61 | |||
| 62 | Example: | ||
| 63 | |||
| 64 | # All the paragraphs in the document | ||
| 65 | my $items = $doc->getElementsByTagName("p"); | ||
| 66 | |||
| 67 | # For each item in the list, | ||
| 68 | # append the entire element as a string of HTML | ||
| 69 | my $gross = ""; | ||
| 70 | for( my $i = 0; $i < $items->length; $i++ ) | ||
| 71 | { | ||
| 72 | $gross += $items->[$i]->innerHTML; | ||
| 73 | } | ||
| 74 | # $gross is now all the HTML for the paragraphs | ||
| 75 | |||
| 76 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/NodeList/length> | ||
| 77 | |||
| 78 | =head1 METHODS | ||
| 79 | |||
| 80 | Inherits methods from its parent L<Module::Generic::Array> | ||
| 81 | |||
| 82 | =head2 forEach | ||
| 83 | |||
| 84 | Executes a provided code reference (reference to a subroutine or anonymous subroutine) once per C<NodeList> element, passing the element as an argument to the subroutine. | ||
| 85 | |||
| 86 | Example: | ||
| 87 | |||
| 88 | my $node = $doc->createElement("div"); | ||
| 89 | my $kid1 = $doc->createElement("p"); | ||
| 90 | my $kid2 = $doc->createTextNode("hey"); | ||
| 91 | my $kid3 = $doc->createElement("span"); | ||
| 92 | |||
| 93 | $node->appendChild( $kid1 ); | ||
| 94 | $node->appendChild( $kid2 ); | ||
| 95 | $node->appendChild( $kid3 ); | ||
| 96 | |||
| 97 | my $list = $node->childNodes; | ||
| 98 | $list->forEach(sub(currentValue, currentIndex, listObj) | ||
| 99 | { | ||
| 100 | my( $currentValue, $currentIndex, $listObj ) = @_; | ||
| 101 | say( "$currentValue, $currentIndex, $_" ); | ||
| 102 | }); | ||
| 103 | |||
| 104 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/NodeList/forEach> | ||
| 105 | |||
| 106 | =head2 item | ||
| 107 | |||
| 108 | Returns an item in the list by its index, or C<undef> if the index is out-of-bounds. | ||
| 109 | An alternative to accessing nodeList[i] (which instead returns undefined when i is out-of-bounds). This is mostly useful for non-JavaScript DOM implementations. | ||
| 110 | |||
| 111 | Example: | ||
| 112 | |||
| 113 | my $tables = $doc->getElementsByTagName( 'table' ); | ||
| 114 | my $firstTable = $tables->item(1); # or $tables->[1] - returns the second table in the DOM | ||
| 115 | |||
| 116 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/NodeList/item> | ||
| 117 | |||
| 118 | =head2 keys | ||
| 119 | |||
| 120 | Returns an iterator, allowing code to go through all the keys of the key/value pairs contained in the collection. (In this case, the keys are numbers starting from 0.) | ||
| 121 | |||
| 122 | Example: | ||
| 123 | |||
| 124 | my $node = $doc->createElement("div"); | ||
| 125 | my $kid1 = $doc->createElement("p"); | ||
| 126 | my $kid2 = $doc->createTextNode("hey"); | ||
| 127 | my $kid3 = $doc->createElement("span"); | ||
| 128 | |||
| 129 | $node->appendChild( $kid1 ); | ||
| 130 | $node->appendChild( $kid2 ); | ||
| 131 | $node->appendChild( $kid3 ); | ||
| 132 | |||
| 133 | my $list = $node->childNodes; | ||
| 134 | |||
| 135 | # Using for..of | ||
| 136 | foreach my $key ( $list->keys->list ) | ||
| 137 | { | ||
| 138 | say( $key ); | ||
| 139 | } | ||
| 140 | |||
| 141 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/NodeList/keys> | ||
| 142 | |||
| 143 | =head2 values | ||
| 144 | |||
| 145 | Returns an iterator allowing code to go through all values (nodes) of the key/value pairs contained in the collection. | ||
| 146 | |||
| 147 | Example: | ||
| 148 | |||
| 149 | my $node = $doc->createElement("div"); | ||
| 150 | my $kid1 = $doc->createElement("p"); | ||
| 151 | my $kid2 = $doc->createTextNode("hey"); | ||
| 152 | my $kid3 = $doc->createElement("span"); | ||
| 153 | |||
| 154 | $node->appendChild( $kid1 ); | ||
| 155 | $node->appendChild( $kid2 ); | ||
| 156 | $node->appendChild( $kid3 ); | ||
| 157 | |||
| 158 | my $list = $node->childNodes; | ||
| 159 | |||
| 160 | # Using for..of | ||
| 161 | foreach my $value ( $list->values ) | ||
| 162 | { | ||
| 163 | say( $value ); | ||
| 164 | } | ||
| 165 | |||
| 166 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/NodeList/values> | ||
| 167 | |||
| 168 | =head1 AUTHOR | ||
| 169 | |||
| 170 | Jacques Deguest E<lt>F<jack@deguest.jp>E<gt> | ||
| 171 | |||
| 172 | =head1 SEE ALSO | ||
| 173 | |||
| 174 | L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/NodeList> | ||
| 175 | |||
| 176 | =head1 COPYRIGHT & LICENSE | ||
| 177 | |||
| 178 | Copyright(c) 2022 DEGUEST Pte. Ltd. | ||
| 179 | |||
| 180 | All rights reserved | ||
| 181 | |||
| 182 | This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 183 | |||
| 184 | =cut | ||