| 1 | ##---------------------------------------------------------------------------- | ||||||||||
| 2 | ## HTML Object - ~/lib/HTML/Object/DOM/DocumentFragment.pm | ||||||||||
| 3 | ## Version v0.2.0 | ||||||||||
| 4 | ## Copyright(c) 2021 DEGUEST Pte. Ltd. | ||||||||||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||||||||||
| 6 | ## Created 2021/12/20 | ||||||||||
| 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::DocumentFragment; | ||||||||||
| 15 | BEGIN | ||||||||||
| 16 | { | ||||||||||
| 17 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | 2 | ▶ | warnings::register_categories( 'HTML::Object' ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | 2 | ▶ | use parent qw( HTML::Object::DOM::Node ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | 2 | ▶ | use vars qw( $VERSION ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 2 | our $VERSION = 'v0.2.0'; | |||||||||
| 23 | }; | ||||||||||
| 24 | |||||||||||
| 25 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 27 | |||||||||||
| 28 | 0 | ▶ | sub append { return( shift->_element_method( 'append', @_ ) ); } | ||||||||
|
Subroutine
append: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 29 | |||||||||||
| 30 | sub as_string | ||||||||||
| 31 | { | ||||||||||
| 32 | 0 | ▶ | my $self = shift( @_ ); | ||||||||
|
Subroutine
as_string: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 33 | 0 | my $a = $self->new_array; | |||||||||
| 34 | # We get the string version of all of our children, except ourself, since a | ||||||||||
| 35 | # document fragment is transparent; has no impact on the dom tree. | ||||||||||
| 36 | $self->children->foreach(sub | ||||||||||
| 37 | { | ||||||||||
| 38 | 0 | ▶ | $a->push( $_->as_string ); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 39 | 0 | }); | |||||||||
| 40 | 0 | return( $a->join( '' )->scalar ); | |||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | # Note: Property | ||||||||||
| 44 | 0 | ▶ | sub childElementCount { return( shift->_elements->length ); } | ||||||||
|
Subroutine
childElementCount: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 45 | |||||||||||
| 46 | # Note: Property | ||||||||||
| 47 | 0 | ▶ | sub firstElementChild { return( shift->_elements->first ); } | ||||||||
|
Subroutine
firstElementChild: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 48 | |||||||||||
| 49 | 0 | ▶ | sub getElementById { return( shift->_element_method( 'getElementById', @_ ) ); } | ||||||||
|
Subroutine
getElementById: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 50 | |||||||||||
| 51 | # Note: Property | ||||||||||
| 52 | 0 | ▶ | sub lastElementChild { return( shift->_elements->last ); } | ||||||||
|
Subroutine
lastElementChild: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 53 | |||||||||||
| 54 | 2 | ▶ | sub parent { return; } | ||||||||
|
Subroutine
parent: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 55 | |||||||||||
| 56 | 0 | ▶ | sub parentNode { return; } | ||||||||
|
Subroutine
parentNode: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 57 | |||||||||||
| 58 | 0 | ▶ | sub parentElement { return; } | ||||||||
|
Subroutine
parentElement: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 59 | |||||||||||
| 60 | 0 | ▶ | sub prepend { return( shift->_element_method( 'prepend', @_ ) ); } | ||||||||
|
Subroutine
prepend: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 61 | |||||||||||
| 62 | 0 | ▶ | sub querySelector { return( shift->_element_method( 'querySelector', @_ ) ); } | ||||||||
|
Subroutine
querySelector: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 63 | |||||||||||
| 64 | 0 | ▶ | sub querySelectorAll { return( shift->_element_method( 'querySelectorAll', @_ ) ); } | ||||||||
|
Subroutine
querySelectorAll: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 65 | |||||||||||
| 66 | 0 | ▶ | sub replaceChildren { return( shift->_element_method( 'replaceChildren', @_ ) ); } | ||||||||
|
Subroutine
replaceChildren: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 67 | |||||||||||
| 68 | 0 | ▶ | sub _elements { return( shift->children->filter(sub{ $_->isElementNode }) ); } | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Subroutine
_elements: not called CC=1 SCAR=6.9
| |||||||||||
| 69 | |||||||||||
| 70 | sub _element_method | ||||||||||
| 71 | { | ||||||||||
| 72 | 0 | ▶ | my $self = shift( @_ ); | ||||||||
|
Subroutine
_element_method: not called CC=2 SCAR=17.9
| |||||||||||
| 73 | 0 | my $meth = shift( @_ ); | |||||||||
| 74 | 0 | require HTML::Object::DOM::Element; | |||||||||
| 75 | 0 | my $code = HTML::Object::DOM::Element->can( $meth ); | |||||||||
| 76 | 0 | ▶ | return( $self->error( "Unknown method \"$meth\" in HTML::Object::DOM::Element" ) ) if( !$code ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | 0 | return( $self->$code( @_ ) ); | |||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | 2 | 1; | |||||||||
| 81 | # NOTE: POD | ||||||||||
| 82 | __END__ | ||||||||||
| 83 | |||||||||||
| 84 | =encoding utf-8 | ||||||||||
| 85 | |||||||||||
| 86 | =head1 NAME | ||||||||||
| 87 | |||||||||||
| 88 | HTML::Object::DOM::DocumentFragment - HTML Object DOM Document Fragment Class | ||||||||||
| 89 | |||||||||||
| 90 | =head1 SYNOPSIS | ||||||||||
| 91 | |||||||||||
| 92 | use HTML::Object::DOM::DocumentFragment; | ||||||||||
| 93 | my $frag = HTML::Object::DOM::DocumentFragment->new || | ||||||||||
| 94 | die( HTML::Object::DOM::DocumentFragment->error, "\n" ); | ||||||||||
| 95 | |||||||||||
| 96 | <ul id="list"></ul> | ||||||||||
| 97 | |||||||||||
| 98 | use Module::Generic::Array; | ||||||||||
| 99 | my $list = $doc->querySelector('#list') | ||||||||||
| 100 | my $fruits = Module::Generic::Array->new( [qw( Apple Orange Banana Melon )] ); | ||||||||||
| 101 | |||||||||||
| 102 | my $fragment = HTML::Object::DOM::DocumentFragment->new; | ||||||||||
| 103 | # or | ||||||||||
| 104 | my $fragment = $doc->createDocumentFragment(); | ||||||||||
| 105 | |||||||||||
| 106 | $fruits->foreach(sub | ||||||||||
| 107 | { | ||||||||||
| 108 | my $fruit = shift( @_ ); | ||||||||||
| 109 | my $li = $doc->createElement('li'); | ||||||||||
| 110 | $li->innerHTML = $fruit; | ||||||||||
| 111 | $fragment->appendChild( $li ); | ||||||||||
| 112 | }) | ||||||||||
| 113 | |||||||||||
| 114 | $list->appendChild( $fragment ); | ||||||||||
| 115 | |||||||||||
| 116 | would yield: | ||||||||||
| 117 | |||||||||||
| 118 | =over 4 | ||||||||||
| 119 | |||||||||||
| 120 | =item * Apple | ||||||||||
| 121 | |||||||||||
| 122 | =item * Orange | ||||||||||
| 123 | |||||||||||
| 124 | =item * Banana | ||||||||||
| 125 | |||||||||||
| 126 | =item * Melon | ||||||||||
| 127 | |||||||||||
| 128 | =back | ||||||||||
| 129 | |||||||||||
| 130 | =head1 VERSION | ||||||||||
| 131 | |||||||||||
| 132 | v0.2.0 | ||||||||||
| 133 | |||||||||||
| 134 | =head1 DESCRIPTION | ||||||||||
| 135 | |||||||||||
| 136 | This implements the interface for document fragments, which is a minimal document object that has no parent. | ||||||||||
| 137 | |||||||||||
| 138 | It is used as a lightweight version of L<Document|HTML::Object::DOM::Document> that stores a segment of a document structure comprised of L<nodes|HTML::Object::DOM::Node> just like a L<standard document|HTML::Object::DOM::Document>. The key difference is due to the fact that the document fragment is not part of the active document tree structure. Changes made to the fragment do not affect the document (even on reflow) or incur any performance impact when changes are made. | ||||||||||
| 139 | |||||||||||
| 140 | It inherits from L<HTML::Object::DOM::Node> | ||||||||||
| 141 | |||||||||||
| 142 | =head1 INHERITANCE | ||||||||||
| 143 | |||||||||||
| 144 | +-----------------------+ +---------------------------+ +-------------------------+ +-------------------------------------+ | ||||||||||
| 145 | | HTML::Object::Element | --> | HTML::Object::EventTarget | --> | HTML::Object::DOM::Node | --> | HTML::Object::DOM::DocumentFragment | | ||||||||||
| 146 | +-----------------------+ +---------------------------+ +-------------------------+ +-------------------------------------+ | ||||||||||
| 147 | |||||||||||
| 148 | =head1 PROPERTIES | ||||||||||
| 149 | |||||||||||
| 150 | =head2 childElementCount | ||||||||||
| 151 | |||||||||||
| 152 | Read-only | ||||||||||
| 153 | |||||||||||
| 154 | Returns the amount of child elements the L<DocumentFragment|HTML::Object::DOM::DocumentFragment> has. | ||||||||||
| 155 | |||||||||||
| 156 | =head2 children | ||||||||||
| 157 | |||||||||||
| 158 | Read-only | ||||||||||
| 159 | |||||||||||
| 160 | Returns an L<array object|Module::Generic::Array> containing all objects of type L<Element|HTML::Object::DOM::Element> that are children of the L<DocumentFragment|HTML::Object::DOM::DocumentFragment> object. | ||||||||||
| 161 | |||||||||||
| 162 | =head2 firstElementChild | ||||||||||
| 163 | |||||||||||
| 164 | Read-only | ||||||||||
| 165 | |||||||||||
| 166 | Returns the L<Element|HTML::Object::DOM::Element> that is the first child of the L<DocumentFragment|HTML::Object::DOM::DocumentFragment> object, or C<undef> if there is none. | ||||||||||
| 167 | |||||||||||
| 168 | =head2 lastElementChild | ||||||||||
| 169 | |||||||||||
| 170 | Read-only | ||||||||||
| 171 | |||||||||||
| 172 | Returns the L<Element|HTML::Object::DOM::Element> that is the last child of the L<DocumentFragment|HTML::Object::DOM::DocumentFragment> object, or C<undef> if there is | ||||||||||
| 173 | |||||||||||
| 174 | =head1 CONSTRUCTOR | ||||||||||
| 175 | |||||||||||
| 176 | =head2 new | ||||||||||
| 177 | |||||||||||
| 178 | Instantiates returns a new L<DocumentFragment|HTML::Object::DOM::DocumentFragment> object. | ||||||||||
| 179 | |||||||||||
| 180 | =head1 METHODS | ||||||||||
| 181 | |||||||||||
| 182 | This interface inherits the methods of its parent L<Node|HTML::Object::DOM::Node> and implements the following ones: | ||||||||||
| 183 | |||||||||||
| 184 | =head2 append | ||||||||||
| 185 | |||||||||||
| 186 | Inserts a set of Node objects or HTML string after the last child of the L<document fragment|HTML::Object::DOM::DocumentFragment>. | ||||||||||
| 187 | |||||||||||
| 188 | =head2 as_string | ||||||||||
| 189 | |||||||||||
| 190 | Returns a string representation of all the children contained. | ||||||||||
| 191 | |||||||||||
| 192 | =head2 getElementById | ||||||||||
| 193 | |||||||||||
| 194 | Returns the first L<Element node|HTML::Object::DOM::Element> within the L<DocumentFragment|HTML::Object::DOM::DocumentFragment>, in document order, that matches the specified ID. Functionally equivalent to L<getElementById()|HTML::Object::DOM::Element/getElementById>. | ||||||||||
| 195 | |||||||||||
| 196 | =head2 parent | ||||||||||
| 197 | |||||||||||
| 198 | Returns always C<undef> | ||||||||||
| 199 | |||||||||||
| 200 | =head2 parentElement | ||||||||||
| 201 | |||||||||||
| 202 | Returns always C<undef> | ||||||||||
| 203 | |||||||||||
| 204 | =head2 parentNode | ||||||||||
| 205 | |||||||||||
| 206 | Returns always C<undef> | ||||||||||
| 207 | |||||||||||
| 208 | =head2 prepend | ||||||||||
| 209 | |||||||||||
| 210 | Inserts a set of L<Node objects|HTML::Object::DOM::Node> or HTML string before the first child of the L<document fragment|HTML::Object::DOM::DocumentFragment>. | ||||||||||
| 211 | |||||||||||
| 212 | =head2 querySelector | ||||||||||
| 213 | |||||||||||
| 214 | Returns the first L<Element node|HTML::Object::DOM::Element> within the L<DocumentFragment|HTML::Object::DOM::DocumentFragment>, in document order, that matches the specified selectors. | ||||||||||
| 215 | |||||||||||
| 216 | =head2 querySelectorAll | ||||||||||
| 217 | |||||||||||
| 218 | Returns a L<array object|Module::Generic::Array> of all the L<Element|HTML::Object::DOM::Element> nodes within the L<DocumentFragment|HTML::Object::DOM::DocumentFragment> that match the specified selectors. | ||||||||||
| 219 | |||||||||||
| 220 | =head2 replaceChildren | ||||||||||
| 221 | |||||||||||
| 222 | Replaces the existing children of a L<DocumentFragment|HTML::Object::DOM::DocumentFragment> with a specified new set of children. | ||||||||||
| 223 | |||||||||||
| 224 | =head1 AUTHOR | ||||||||||
| 225 | |||||||||||
| 226 | Jacques Deguest E<lt>F<jack@deguest.jp>E<gt> | ||||||||||
| 227 | |||||||||||
| 228 | =head1 SEE ALSO | ||||||||||
| 229 | |||||||||||
| 230 | L<HTML::Object::Document>, L<HTML::Object::Element>, L<HTML::Object::Node> | ||||||||||
| 231 | |||||||||||
| 232 | L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/DocumentFragment> | ||||||||||
| 233 | |||||||||||
| 234 | =head1 COPYRIGHT & LICENSE | ||||||||||
| 235 | |||||||||||
| 236 | Copyright(c) 2021 DEGUEST Pte. Ltd. | ||||||||||
| 237 | |||||||||||
| 238 | All rights reserved | ||||||||||
| 239 | |||||||||||
| 240 | This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||||||||||
| 241 | |||||||||||
| 242 | =cut | ||||||||||