File Coverage

lib/HTML/Object/DOM/Implementation.pm
Criterion Covered Total %
statement 26 50 52.0
branch 1 16 6.2
condition 0 12 0.0
subroutine 9 13 69.2
pod 5 5 100.0
total 41 96 42.7


line stmt bran cond sub pod time code
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     2   1870 use strict;
  2         8  
  2         112  
18 2     2   14 use warnings;
  2         4  
  2         74  
19 2     2   13 use parent qw( Module::Generic );
  2         5  
  2         11  
20 2     2   146 use vars qw( $VERSION );
  2         4  
  2         89  
21 2     2   15 use HTML::Object::DOM::Declaration;
  2         4  
  2         30  
22 2     2   770 our $VERSION = 'v0.2.0';
23             };
24              
25 2     2   13 use strict;
  2         5  
  2         38  
26 2     2   8 use warnings;
  2         5  
  2         1017  
27              
28             sub init
29             {
30 1     1 1 289 my $self = shift( @_ );
31 1         42 $self->{_init_strict_use_sub} = 1;
32 1 50       8 $self->SUPER::init( @_ ) || return( $self->pass_error );
33 1         150 return( $self );
34             }
35              
36             sub createDocument
37             {
38 0     0 1   my $self = shift( @_ );
39 0 0         return( $self->error({
40             message => sprintf( "At least 2 arguments required, but only %d passed: \$document->implementation->createDocument( \$namespaceURI, \$qualifiedNameStr, \$documentType )", scalar( @_ ) ),
41             class => 'HTML::Object::TypeError',
42             }) ) if( scalar( @_ ) < 2 );
43 0           my( $namespaceURI, $qualifiedNameStr, $documentType ) = @_;
44 0 0         return( $self->error({
45             message => "The second argument, qualified name string, can only be \"html\".",
46             class => 'HTML::Object::TypeError',
47             }) ) if( lc( $qualifiedNameStr ) ne 'html' );
48 0 0 0       return( $self->error({
49             message => 'Document type argument provided, but it is not a HTML::Object::DOM::Declaration object.',
50             class => 'HTML::Object::TypeError',
51             }) ) if( scalar( @_ ) >= 3 && !$self->_is_a( $documentType => 'HTML::Object::DOM::Declaration' ) );
52 0   0       my $doc = $self->createHTMLDocument || return( $self->pass_error );
53 0 0         $doc->declaration( $documentType ) if( defined( $documentType ) );
54 0           return( $doc );
55             }
56              
57             sub createDocumentType
58             {
59 0     0 1   my $self = shift( @_ );
60             # return( $self->error({
61             # message => sprintf( "At least 3 arguments required, but only %d passed: \$document->implementation->createDocumentType( \$qualifiedNameStr, \$publicId, \$systemId )", scalar( @_ ) ),
62             # class => 'HTML::Object::TypeError',
63             # }) ) if( scalar( @_ ) < 3 );
64 0           my( $qualifiedNameStr, $publicId, $systemId ) = @_;
65 0 0 0       $qualifiedNameStr = 'html' if( !defined( $qualifiedNameStr ) || !CORE::length( "$qualifiedNameStr" ) );
66 0   0       my $dtd = HTML::Object::DOM::Declaration->new( name => $qualifiedNameStr ) ||
67             return( $self->pass_error( HTML::Object::DOM::Declaration->error ) );
68 0           return( $dtd );
69             }
70              
71             sub createHTMLDocument
72             {
73 0     0 1   my $self = shift( @_ );
74 0           my $title;
75 0 0         $title = shift( @_ ) if( @_ );
76 0           my $html;
77 0 0         if( defined( $title ) )
78             {
79 0           $html = <<EOT;
80             <!DOCTYPE html>
81             <html><head><title>${title}<title></head><body></body></html>
82             EOT
83             }
84             else
85             {
86 0           $html = <<EOT;
87             <!DOCTYPE html>
88             <html><head></head><body></body></html>
89             EOT
90             }
91 0           my $p = HTML::Object::DOM->new;
92 0   0       my $doc = $p->parse_data( $html ) || return( $self->pass_error( $p->error ) );
93 0           return( $doc );
94             }
95              
96 0     0 1   sub hasFeature { return(1); }
97              
98             1;
99             # NOTE: POD
100             __END__
101              
102             =encoding utf-8
103              
104             =head1 NAME
105              
106             HTML::Object::DOM::Implementation - HTML Object DOM Implementation
107              
108             =head1 SYNOPSIS
109              
110             use HTML::Object::DOM::Implementation;
111             my $impl = HTML::Object::DOM::Implementation->new ||
112             die( HTML::Object::DOM::Implementation->error, "\n" );
113              
114             =head1 VERSION
115              
116             v0.2.0
117              
118             =head1 DESCRIPTION
119              
120             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.
121              
122             =head1 PROPERTIES
123              
124             There are no properties
125              
126             =head1 METHODS
127              
128             =head2 createDocument
129              
130             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>.
131              
132             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>
133              
134             The namespaceURI argument is completely ignored, so do not bother and the qualified name can only be C<html>.
135              
136             Example:
137              
138             my $doc = $doc->implementation->createDocument( $namespaceURI, $qualifiedNameStr, $documentType_object );
139              
140             my $doc = $doc->implementation->createDocument( '', 'html' );
141             my $body = $doc->createElement( 'body' );
142             $body->setAttribute( 'id', 'abc' );
143             $doc->documentElement->appendChild( $body );
144             say( $doc->getElementById( 'abc') ); # [object HTMLBodyElement]
145              
146             See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/DOMImplementation/createDocument>
147              
148             =head2 createDocumentType
149              
150             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>.
151              
152             Example:
153              
154             my $doctype = $doc->implementation->createDocumentType( 'html', $publicId, $systemId );
155              
156             my $dt = $doc->implementation->createDocumentType( 'svg:svg', '-//W3C//DTD SVG 1.1//EN', 'http://www->w3.org/Graphics/SVG/1.1/DTD/svg11.dtd' );
157             my $d = $doc->implementation->createDocument( 'http://www->w3.org/2000/svg', 'svg:svg', $dt );
158             say( $d->doctype->publicId); # -//W3C//DTD SVG 1.1//EN
159              
160             See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/DOMImplementation/createDocumentType>
161              
162             =head2 createHTMLDocument
163              
164             Creates and returns an HTML Document.
165              
166             Example:
167              
168             # Assuming $title is "Demo"
169             my $newDoc = $doc->implementation->createHTMLDocument( $title );
170              
171             Result:
172              
173             <!DOCTYPE html>
174             <html>
175             <head><title>Demo</title></head>
176             <body></body>
177             </html>
178              
179             See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/DOMImplementation/createHTMLDocument>
180              
181             =head2 hasFeature
182              
183             This always returns true.
184              
185             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.
186              
187             Example:
188              
189             my $flag = $doc->implementation->hasFeature( $feature, $version );
190              
191             See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/DOMImplementation/hasFeature>
192              
193             =head1 AUTHOR
194              
195             Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
196              
197             =head1 SEE ALSO
198              
199             L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/DOMImplementation>
200              
201             =head1 COPYRIGHT & LICENSE
202              
203             Copyright(c) 2022 DEGUEST Pte. Ltd.
204              
205             All rights reserved
206              
207             This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
208              
209             =cut