File Coverage

lib/HTML/Object/DOM/DocumentFragment.pm
Criterion Covered Total %
statement 20 44 45.4
branch 0 2 0.0
condition n/a
subroutine 8 24 33.3
pod 13 13 100.0
total 41 83 49.4


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