lib/HTML/Object/DOM/Implementation.pm

filter: Statementstmt 53.8% 28 / 52 Branchbran 6.2% 1 / 16 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 69.2% 9 / 13 Podpod 100% 5 / 5 totaltotal 40.9% 43 / 105 CC 14 SCAR 30.8
CC
14
Cov
36%
createDocument
56.0
createDocumentType
20.0
createHTMLDocument
20.0
CRAP 21.8
« lib/HTML/Object/XPath.pm ↑ Summary lib/HTML/Object/DOM/Document.pm »
1 ##----------------------------------------------------------------------------
2 ## HTML Object - ~/lib/HTML/Object/DOM/Implementation.pm
3 ## Version v0.2.0
4 ## Copyright(c) 2022 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2022/01/05
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::Implementation;
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( Module::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶ use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶ use HTML::Object::DOM::Declaration;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 our $VERSION = 'v0.2.0';
24 };
25  
26 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 sub init
30 {
31 1 ▶ my $self = shift( @_ );
Subroutine init: called CC=2 SCAR=7.0
Pod documented
32 1 $self->{_init_strict_use_sub} = 1;
33 1 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 0 1 1
34 1 return( $self );
35 }
36  
37 sub createDocument
38 {
39 0 ▶ my $self = shift( @_ );
Subroutine createDocument: not called CC=7 SCAR=40.3
Pod documented
40 0 ▶ return( $self->error({
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ < 2 0 0 0
41 message => sprintf( "At least 2 arguments required, but only %d passed: \$document->implementation->createDocument( \$namespaceURI, \$qualifiedNameStr, \$documentType )", scalar( @_ ) ),
42 class => 'HTML::Object::TypeError',
43 }) ) if( scalar( @_ ) < 2 );
44 0 my( $namespaceURI, $qualifiedNameStr, $documentType ) = @_;
45 0 ▶ return( $self->error({
Branch: 1 branch
BranchTrueFalseTotal
if lc $qualifiedNameStr ne "html" 0 0 0
46 message => "The second argument, qualified name string, can only be \"html\".",
47 class => 'HTML::Object::TypeError',
48 }) ) if( lc( $qualifiedNameStr ) ne 'html' );
49 0 ▶ return( $self->error({
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ >= 3 and not $self->_is_a($documentType, "HTML::Object::DOM::Declaration") 0 0 0
Condition0%0/3
scalar @_ >= 3 and not $self->_is_a($documentType, "HTML::Object::DOM::Declaration")
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @_ >= 3 and not $self->_is_a($documentType, "HTML::Object::DOM::Declaration")
scalar @_ >= 3 not $self->_is_a($documentType, "HTML::Object::DOM::Declaration")
Truth table0%0/3
A and not(B)
A= scalar @_ >= 3B= not $self->_is_a($documentType, "HTML::Object::DOM::Declaration")
ABresult
0X0
100
111
50 message => 'Document type argument provided, but it is not a HTML::Object::DOM::Declaration object.',
51 class => 'HTML::Object::TypeError',
52 }) ) if( scalar( @_ ) >= 3 && !$self->_is_a( $documentType => 'HTML::Object::DOM::Declaration' ) );
53 0 ▶ my $doc = $self->createHTMLDocument || return( $self->pass_error );
Condition0%0/2
$self->createHTMLDocument || (return $self->pass_error)
l!l
00
MC/DC0%0/1
$self->createHTMLDocument || (return $self->pass_error)
$self->createHTMLDocument
Truth table0%0/2
A
A= $self->createHTMLDocument
Aresult
11
00
54 0 ▶ $doc->declaration( $documentType ) if( defined( $documentType ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $documentType 0 0 0
55 0 return( $doc );
56 }
57  
58 sub createDocumentType
59 {
60 0 ▶ my $self = shift( @_ );
Subroutine createDocumentType: not called CC=4 SCAR=30.0
Pod documented
61 # return( $self->error({
62 # message => sprintf( "At least 3 arguments required, but only %d passed: \$document->implementation->createDocumentType( \$qualifiedNameStr, \$publicId, \$systemId )", scalar( @_ ) ),
63 # class => 'HTML::Object::TypeError',
64 # }) ) if( scalar( @_ ) < 3 );
65 0 my( $qualifiedNameStr, $publicId, $systemId ) = @_;
66 0 ▶ $qualifiedNameStr = 'html' if( !defined( $qualifiedNameStr ) || !CORE::length( "$qualifiedNameStr" ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $qualifiedNameStr and length "$qualifiedNameStr" 0 0 0
Condition0%0/3
defined $qualifiedNameStr and length "$qualifiedNameStr"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $qualifiedNameStr and length "$qualifiedNameStr"
defined $qualifiedNameStr length "$qualifiedNameStr"
Truth table0%0/3
A and B
A= defined $qualifiedNameStrB= length "$qualifiedNameStr"
ABresult
0X0
100
111
67 0 ▶ my $dtd = HTML::Object::DOM::Declaration->new( name => $qualifiedNameStr ) ||
Condition0%0/2
'HTML::Object::DOM::Declaration'->new('name', $qualifiedNameStr) || (return $self->pass_error('HTML::Object::DOM::Declaration'->error))
l!l
00
MC/DC0%0/1
'HTML::Object::DOM::Declaration'->new('name', $qualifiedNameStr) || (return $self->pass_error('HTML::Object::DOM::Declaration'->error))
'HTML::Object::DOM::Declaration'->new('name', $qualifiedNameStr)
Truth table0%0/2
A
A= 'HTML::Object::DOM::Declaration'->new('name', $qualifiedNameStr)
Aresult
11
00
68 return( $self->pass_error( HTML::Object::DOM::Declaration->error ) );
69 0 return( $dtd );
70 }
71  
72 sub createHTMLDocument
73 {
74 0 ▶ my $self = shift( @_ );
Subroutine createHTMLDocument: not called CC=4 SCAR=30.0
Pod documented
75 0 my $title;
76 0 ▶ $title = shift( @_ ) if( @_ );
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
77 0 my $html;
78 0 ▶ if( defined( $title ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $title) { } 0 0 0
79 {
80 0 $html = <<EOT;
81 <!DOCTYPE html>
82 <html><head><title>${title}<title></head><body></body></html>
83 EOT
84 }
85 else
86 {
87 0 $html = <<EOT;
88 <!DOCTYPE html>
89 <html><head></head><body></body></html>
90 EOT
91 }
92 0 my $p = HTML::Object::DOM->new;
93 0 ▶ my $doc = $p->parse_data( $html ) || return( $self->pass_error( $p->error ) );
Condition0%0/2
$p->parse_data($html) || (return $self->pass_error($p->error))
l!l
00
MC/DC0%0/1
$p->parse_data($html) || (return $self->pass_error($p->error))
$p->parse_data($html)
Truth table0%0/2
A
A= $p->parse_data($html)
Aresult
11
00
94 0 return( $doc );
95 }
96  
97 0 ▶sub hasFeature { return(1); }
Subroutine hasFeature: not called CC=1 SCAR=6.9
Pod documented
98  
99 2 1;
100 # NOTE: POD
101 __END__
102  
103 =encoding utf-8
104  
105 =head1 NAME
106  
107 HTML::Object::DOM::Implementation - HTML Object DOM Implementation
108  
109 =head1 SYNOPSIS
110  
111 use HTML::Object::DOM::Implementation;
112 my $impl = HTML::Object::DOM::Implementation->new ||
113 die( HTML::Object::DOM::Implementation->error, "\n" );
114  
115 =head1 VERSION
116  
117 v0.2.0
118  
119 =head1 DESCRIPTION
120  
121 The C<Implementation> interface represents an object providing methods which are not dependent on any particular document. Such an object is returned by the L<HTML::Object::DOM::Document/implementation> property.
122  
123 =head1 PROPERTIES
124  
125 There are no properties
126  
127 =head1 METHODS
128  
129 =head2 createDocument
130  
131 Provided with a namespace URI (which could be an empty string) and a qualified name for the top element, which should be C<html> and optionally a L<document type definition object|HTML::Object::DOM::Declaration> and this creates and returns a L<document object|HTML::Object::DOM::Document>.
132  
133 Normally, this method is used to create an XML document, but we do not support XML, so this is basically an alias for L</createHTMLDocument>
134  
135 The namespaceURI argument is completely ignored, so do not bother and the qualified name can only be C<html>.
136  
137 Example:
138  
139 my $doc = $doc->implementation->createDocument( $namespaceURI, $qualifiedNameStr, $documentType_object );
140  
141 my $doc = $doc->implementation->createDocument( '', 'html' );
142 my $body = $doc->createElement( 'body' );
143 $body->setAttribute( 'id', 'abc' );
144 $doc->documentElement->appendChild( $body );
145 say( $doc->getElementById( 'abc') ); # [object HTMLBodyElement]
146  
147 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/DOMImplementation/createDocument>
148  
149 =head2 createDocumentType
150  
151 Provided with a qualified name, which should be C<html> and an optional public id and system id, both of which are ignored, and this creates and returns a L<document type object|HTML::Object::DOM::Declaration>.
152  
153 Example:
154  
155 my $doctype = $doc->implementation->createDocumentType( 'html', $publicId, $systemId );
156  
157 my $dt = $doc->implementation->createDocumentType( 'svg:svg', '-//W3C//DTD SVG 1.1//EN', 'http://www->w3.org/Graphics/SVG/1.1/DTD/svg11.dtd' );
158 my $d = $doc->implementation->createDocument( 'http://www->w3.org/2000/svg', 'svg:svg', $dt );
159 say( $d->doctype->publicId); # -//W3C//DTD SVG 1.1//EN
160  
161 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/DOMImplementation/createDocumentType>
162  
163 =head2 createHTMLDocument
164  
165 Creates and returns an HTML Document.
166  
167 Example:
168  
169 # Assuming $title is "Demo"
170 my $newDoc = $doc->implementation->createHTMLDocument( $title );
171  
172 Result:
173  
174 <!DOCTYPE html>
175 <html>
176 <head><title>Demo</title></head>
177 <body></body>
178 </html>
179  
180 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/DOMImplementation/createHTMLDocument>
181  
182 =head2 hasFeature
183  
184 This always returns true.
185  
186 Its original purpose is to return a boolean value indicating if a given feature is supported or not. However, this function has been unreliable and kept for compatibility purpose alone: except for SVG-related queries.
187  
188 Example:
189  
190 my $flag = $doc->implementation->hasFeature( $feature, $version );
191  
192 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/DOMImplementation/hasFeature>
193  
194 =head1 AUTHOR
195  
196 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
197  
198 =head1 SEE ALSO
199  
200 L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/DOMImplementation>
201  
202 =head1 COPYRIGHT & LICENSE
203  
204 Copyright(c) 2022 DEGUEST Pte. Ltd.
205  
206 All rights reserved
207  
208 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
209  
210 =cut
« lib/HTML/Object/XPath.pm ↑ Summary lib/HTML/Object/DOM/Document.pm »