| 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 | use strict; | ||
| 18 | use warnings; | ||
| 19 | warnings::register_categories( 'HTML::Object' ); | ||
| 20 | use parent qw( Module::Generic ); | ||
| 21 | use vars qw( $VERSION ); | ||
| 22 | use HTML::Object::DOM::Declaration; | ||
| 23 | our $VERSION = 'v0.2.0'; | ||
| 24 | }; | ||
| 25 | |||
| 26 | use strict; | ||
| 27 | use warnings; | ||
| 28 | |||
| 29 | sub init | ||
| 30 | { | ||
| 31 | my $self = shift( @_ ); | ||
| 32 | $self->{_init_strict_use_sub} = 1; | ||
| 33 | $self->SUPER::init( @_ ) || return( $self->pass_error ); | ||
| 34 | return( $self ); | ||
| 35 | } | ||
| 36 | |||
| 37 | sub createDocument | ||
| 38 | { | ||
| 39 | my $self = shift( @_ ); | ||
| 40 | return( $self->error({ | ||
| 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 | my( $namespaceURI, $qualifiedNameStr, $documentType ) = @_; | ||
| 45 | return( $self->error({ | ||
| 46 | message => "The second argument, qualified name string, can only be \"html\".", | ||
| 47 | class => 'HTML::Object::TypeError', | ||
| 48 | }) ) if( lc( $qualifiedNameStr ) ne 'html' ); | ||
| 49 | return( $self->error({ | ||
| 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 | my $doc = $self->createHTMLDocument || return( $self->pass_error ); | ||
| 54 | $doc->declaration( $documentType ) if( defined( $documentType ) ); | ||
| 55 | return( $doc ); | ||
| 56 | } | ||
| 57 | |||
| 58 | sub createDocumentType | ||
| 59 | { | ||
| 60 | my $self = shift( @_ ); | ||
| 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 | my( $qualifiedNameStr, $publicId, $systemId ) = @_; | ||
| 66 | $qualifiedNameStr = 'html' if( !defined( $qualifiedNameStr ) || !CORE::length( "$qualifiedNameStr" ) ); | ||
| 67 | my $dtd = HTML::Object::DOM::Declaration->new( name => $qualifiedNameStr ) || | ||
| 68 | return( $self->pass_error( HTML::Object::DOM::Declaration->error ) ); | ||
| 69 | return( $dtd ); | ||
| 70 | } | ||
| 71 | |||
| 72 | sub createHTMLDocument | ||
| 73 | { | ||
| 74 | my $self = shift( @_ ); | ||
| 75 | my $title; | ||
| 76 | $title = shift( @_ ) if( @_ ); | ||
| 77 | my $html; | ||
| 78 | if( defined( $title ) ) | ||
| 79 | { | ||
| 80 | $html = <<EOT; | ||
| 81 | <!DOCTYPE html> | ||
| 82 | <html><head><title>${title}<title></head><body></body></html> | ||
| 83 | EOT | ||
| 84 | } | ||
| 85 | else | ||
| 86 | { | ||
| 87 | $html = <<EOT; | ||
| 88 | <!DOCTYPE html> | ||
| 89 | <html><head></head><body></body></html> | ||
| 90 | EOT | ||
| 91 | } | ||
| 92 | my $p = HTML::Object::DOM->new; | ||
| 93 | my $doc = $p->parse_data( $html ) || return( $self->pass_error( $p->error ) ); | ||
| 94 | return( $doc ); | ||
| 95 | } | ||
| 96 | |||
| 97 | sub hasFeature { return(1); } | ||
| 98 | |||
| 99 | 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 |