blib/lib/HTML/Object/Document.pm untested

filter: Statementstmt 0.0% 0 / 74 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 19 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 133 CC 9 SCAR 26.1
CC
9
Cov
0%
append
42.0
as_string
12.0
as_xml
6.0
CRAP 13.6
1 ##----------------------------------------------------------------------------
2 ## HTML Object - ~/lib/HTML/Object/Document.pm
3 ## Version v0.2.0
4 ## Copyright(c) 2021 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2021/04/19
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::Document;
15 BEGIN
16 {
17 use strict;
18 use warnings;
19 warnings::register_categories( 'HTML::Object' );
20 use parent qw( HTML::Object::Element );
21 use vars qw( $VERSION );
22 use Scalar::Util ();
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->{is_empty} = 0;
33 $self->{tag} = '_document';
34 $self->{declaration} = undef;
35 $self->{referrer} = undef;
36 $self->{uri} = undef;
37 $self->{_init_strict_use_sub} = 1;
38 $self->{_exception_class} = 'HTML::Object::Exception';
39 $self->{_last_modified} = undef;
40 $self->SUPER::init( @_ ) || return( $self->pass_error );
41 return( $self );
42 }
43  
44 sub append
45 {
46 my $self = shift( @_ );
47 my $this = shift( @_ );
48 return( $self->error( "Nothing to append was provided." ) ) if( !defined( $this ) || !CORE::length( $this ) );
49 if( !ref( $this ) || overload::Method( $this, '""' ) )
50 {
51 my $p = HTML::Object->new;
52 my $doc = $p->parse( "$this" ) || return( $self->pass_error( $p->error ) );
53 return( $self->error( "No element could be found from parsing html text provided." ) ) if( !$doc->children->length );
54 $this = $doc->children->first;
55 }
56 if( !$self->_is_a( $this, 'HTML::Object::Element' ) || $self->_is_a( $this, 'HTML::Object::Collection' ) )
57 {
58 return( $self->error( "Element object provided is not an HTML::Object::Element object." ) );
59 }
60 if( $this->tag eq 'html' && $self->children->length && $self->children->first->tag eq 'html' )
61 {
62 require HTML::Object::Exception;
63 return( $self->error({
64 class => 'HTML::Object::HierarchyRequestError',
65 code => 403,
66 message => "You are atttempting to add an html tag, but there is already one.",
67 }) );
68 }
69 $this->parent( $self );
70 $this->children->push( $this );
71 return( $this );
72 }
73  
74 sub as_string
75 {
76 my $self = shift( @_ );
77 # if( $self->isa( 'HTML::Object::Collection' ) )
78 # {
79 # return( '' ) if( !$self->children->length );
80 # my $first = $self->children->first;
81 # return( '' ) if( !$self->_is_a( $first, 'HTML::Object::Element' ) );
82 # return( $self->error( "as_string() called on a Collection object, but its first children element is also a collection. Stopping before starting an infinite recursion." ) ) if( $self->_is_a( $first, 'HTML::Object::Collection' ) );
83 # return( $first->as_string );
84 # }
85 my $a = $self->new_array;
86 $a->push( $self->declaration->as_string ) if( $self->declaration );
87 $self->children->foreach(sub
88 {
89 my $e = shift( @_ );
90 my $v = $e->as_string;
91 $a->push( defined( $v ) ? $v->scalar : $v );
92 });
93 return( $a->join( '' ) );
94 }
95  
96 sub as_xml
97 {
98 my $self = shift( @_ );
99 my $a = $self->new_array;
100 $self->children->foreach(sub
101 {
102 my $e = shift( @_ );
103 my $v = $e->as_xml;
104 $a->push( defined( $v ) ? $v->scalar : $v );
105 });
106 return( $a->join( '' ) );
107 }
108  
109 sub declaration { return( shift->_set_get_object_without_init( 'declaration', 'HTML::Object::Declaration', @_ ) ); }
110  
111 sub uri { return( shift->_set_get_uri( 'uri', @_ ) ); }
112  
113 sub _last_modified { return( shift->_set_get_datetime( '_last_modified', @_ ) ); }
114  
115 1;
116 # NOTE: POD
117 __END__