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