File Coverage

lib/HTML/Object/DOM/Declaration.pm
Criterion Covered Total %
statement 31 74 41.8
branch 3 18 16.6
condition 0 10 0.0
subroutine 11 19 57.8
pod 11 11 100.0
total 56 132 42.4


line stmt bran cond sub pod time code
1             ##----------------------------------------------------------------------------
2             ## HTML Object - ~/lib/HTML/Object/DOM/Declaration.pm
3             ## Version v0.2.0
4             ## Copyright(c) 2021 DEGUEST Pte. Ltd.
5             ## Author: Jacques Deguest <jack@deguest.jp>
6             ## Created 2021/12/13
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::Declaration;
15             BEGIN
16             {
17 28     28   192 use strict;
  28         52  
  28         870  
18 28     28   146 use warnings;
  28         54  
  28         867  
19 28     28   143 use parent qw( HTML::Object::Declaration HTML::Object::DOM::Node );
  28         59  
  28         145  
20 28     28   2435 use vars qw( $VERSION );
  28         70  
  28         1259  
21 28     28   498 our $VERSION = 'v0.2.0';
22             };
23              
24 28     28   159 use strict;
  28         52  
  28         502  
25 28     28   130 use warnings;
  28         47  
  28         18256  
26              
27             sub init
28             {
29 18     18 1 1610 my $self = shift( @_ );
30 18         1618 $self->{name} = 'html';
31 18         70 $self->{_init_strict_use_sub} = 1;
32 18 50       239 $self->HTML::Object::Declaration::init( @_ ) || return( $self->pass_error );
33 18         97 return( $self );
34             }
35              
36             sub after
37             {
38 0     0 1 0 my $self = shift( @_ );
39 0   0     0 my $parent = $self->parent ||
40             return( $self->error( "No parent document is set for this DTD" ) );
41 0 0       0 return if( !$parent );
42 0         0 my $nodes = $self->_list_to_nodes( @_ );
43 0 0       0 return if( $nodes->is_empty );
44 0         0 my $refNode = $self;
45 0         0 foreach my $node ( @$nodes )
46             {
47 0         0 $parent->insertAfter( $node, $refNode );
48 0         0 $refNode = $node;
49             }
50 0         0 return( $self );
51             }
52              
53             sub before
54             {
55 0     0 1 0 my $self = shift( @_ );
56 0   0     0 my $parent = $self->parent ||
57             return( $self->error( "No parent document is set for this DTD" ) );
58 0 0       0 return if( !$parent );
59 0         0 my $nodes = $self->_list_to_nodes( @_ );
60 0 0       0 return if( $nodes->is_empty );
61 0         0 my $refNode = $self;
62 0         0 foreach my $node ( @$nodes )
63             {
64 0         0 $parent->insertBefore( $node, $refNode );
65 0         0 $refNode = $node;
66             }
67 0         0 return( $self );
68             }
69              
70             # Note: property internalSubset read-only
71 0     0 1 0 sub internalSubset { return( shift->new_scalar ); }
72              
73             # Note: property name read-only
74 1     1 1 552 sub name : lvalue { return( shift->_set_get_scalar_as_object( 'name', @_ ) ); }
75              
76             # Note: property notations read-only
77 0     0 1 0 sub notations { return; }
78              
79             # Note: property publicId read-only
80             sub publicId
81             {
82 1     1 1 1595 my $self = shift( @_ );
83 1 50       8 if( my $rv = $self->original->match( qr/PUBLIC[[:blank:]\h]+\"([^\"]+)\"/ ) )
84             {
85 0         0 return( $rv->capture->first );
86             }
87 1         92959 return( '' );
88             }
89              
90             sub remove
91             {
92 0     0 1 0 my $self = shift( @_ );
93 0   0     0 my $parent = $self->parent || return;
94 0         0 $parent->removeChild( $self );
95 0         0 return( $self );
96             }
97              
98             sub replaceWith
99             {
100 0     0 1 0 my $self = shift( @_ );
101 0   0     0 my $parent = $self->parent || return;
102 0   0     0 my $list = $self->_list_to_nodes( @_ ) || return( $self->pass_error );
103 0         0 my $pos = $parent->nodes->pos( $self );
104 0 0       0 return( $self->error( "Unable to find our DTD object in our parent's nodes" ) ) if( !defined( $pos ) );
105 0         0 my $dtd;
106             $list->foreach(sub
107             {
108 0     0   0 $_->parent( $parent );
109 0 0       0 if( $self->_is_a( $_ => 'HTML::Object::DOM::Declaration' ) )
110             {
111 0         0 $dtd = $_;
112             }
113 0         0 });
114 0         0 $parent->nodes->splice( $pos, 1, $list->list );
115             # Either an DTD object, if we found one, or undef
116 0         0 $parent->declaration( $dtd );
117 0         0 $parent->reset(1);
118 0         0 return( $self );
119             }
120              
121 0     0 1 0 sub string_value { return; }
122              
123             # Note: property systemId read-only
124             sub systemId
125             {
126 1     1 1 1297 my $self = shift( @_ );
127 1 50       19 if( my $rv = $self->original->match( qr/[[:blank:]\h]+\"(http[^\"]+)\"/ ) )
128             {
129 0         0 return( $rv->capture->first );
130             }
131 1         87707 return( '' );
132             }
133              
134             1;
135             # NOTE: POD
136             __END__
137              
138             =encoding utf-8
139              
140             =head1 NAME
141              
142             HTML::Object::DOM::Declaration - HTML Object DOM DTD
143              
144             =head1 SYNOPSIS
145              
146             use HTML::Object::DOM::Declaration;
147             my $decl = HTML::Object::DOM::Declaration->new ||
148             die( HTML::Object::DOM::Declaration->error, "\n" );
149              
150             =head1 VERSION
151              
152             v0.2.0
153              
154             =head1 DESCRIPTION
155              
156             This module implements an HTTML declaration for the DOM. It inherits from L<HTML::Object::Declaration> and L<HTTML::Object::DOM::Node>
157              
158             =head1 INHERITANCE
159              
160             +---------------------------+ +---------------------------+ +-------------------------+ +--------------------------------+
161             | HTML::Object::Element | --> | HTML::Object::EventTarget | --> | HTML::Object::DOM::Node | --> | HTML::Object::DOM::Declaration |
162             +---------------------------+ +---------------------------+ +-------------------------+ +--------------------------------+
163             | ^
164             | |
165             v |
166             +---------------------------+ |
167             | HTML::Object::Declaration | ------------------------------------------------------------------------+
168             +---------------------------+
169              
170             =head1 PROPERTIES
171              
172             Inherits properties from its parents L<HTML::Object::Declaration> and L<HTML::Object::DOM::Node>
173              
174             =head2 internalSubset
175              
176             Read-only.
177              
178             A string of the internal subset, or C<undef> if there is none. Eg "<!ELEMENT foo (bar)>".
179              
180             See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/DocumentType/internalSubset>
181              
182             =head2 name
183              
184             Read-only.
185              
186             A string, eg "html" for <!DOCTYPE HTML>.
187              
188             See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/DocumentType/name>
189              
190             =head2 notations
191              
192             Always returns C<undef> under perl.
193              
194             Normally, under JavaScript, this returns s C<NamedNodeMap> with notations declared in the DTD.
195              
196             See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/DocumentType/notations>
197              
198             =head2 publicId
199              
200             Read-only.
201              
202             A string, eg "-//W3C//DTD HTML 4.01//EN", empty string for HTML5.
203              
204             See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/DocumentType/publicId>
205              
206             =head2 systemId
207              
208             Read-only.
209              
210             A string, eg "http://www.w3.org/TR/html4/strict.dtd", empty string for HTML5.
211              
212             See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/DocumentType/systemId>
213              
214             =head1 METHODS
215              
216             Inherits methods from its parents L<HTML::Object::Declaration> and L<HTML::Object::DOM::Node>
217              
218             =head2 after
219              
220             Inserts a set of L<Node|HTML::Object::DOM::Node> or string objects in the children list of the
221             C<DocumentType>'s parent, just after the C<DocumentType> object.
222              
223             Example:
224              
225             my $docType = $doc->implementation->createDocumentType("html", "", "");
226             my $myDoc = $doc->implementation->createDocument("", "", $docType);
227              
228             $docType->after($doc->createElement('html'));
229              
230             $myDoc->childNodes;
231             # NodeList [<!DOCTYPE html>, <html>]
232              
233             See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/DocumentType/after>
234              
235             =head2 before
236              
237             Inserts a set of Node or string objects in the children list of the
238             C<DocumentType>'s parent, just before the C<DocumentType> object.
239              
240             Example:
241              
242             my $docType = $doc->implementation->createDocumentType("html", "", "");
243             my $myDoc = $doc->implementation->createDocument("", "", $docType);
244              
245             $docType->before( $doc->createComment('<!--[if !IE]> conditional comment <![endif]-->') );
246              
247             $myDoc->childNodes;
248             # NodeList [<!--[if !IE]> conditional comment <![endif]-->, <!DOCTYPE html>]
249              
250             See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/DocumentType/before>
251              
252             =head2 remove
253              
254             Removes the object from its parent children list.
255              
256             Example:
257              
258             $doc->doctype; # "<!DOCTYPE html>'
259             $doc->doctype->remove();
260             $doc->doctype; # null
261              
262             See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/DocumentType/remove>
263              
264             =head2 replaceWith
265              
266             Replaces the document type with a set of given nodes.
267              
268             Example:
269              
270             my $svg_dt = $doc->implementation->createDocumentType(
271             'svg:svg',
272             '-//W3C//DTD SVG 1.1//EN',
273             'http://www->w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'
274             );
275              
276             $doc->doctype->replaceWith($svg_dt);
277              
278             See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/DocumentType/replaceWith>
279              
280             =head2 string_value
281              
282             Always returns C<undef>
283              
284             =head1 AUTHOR
285              
286             Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
287              
288             =head1 SEE ALSO
289              
290             L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/DocumentType>
291              
292             =head1 COPYRIGHT & LICENSE
293              
294             Copyright(c) 2021 DEGUEST Pte. Ltd.
295              
296             All rights reserved
297              
298             This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
299              
300             =cut