File Coverage

blib/lib/CSS/Object/Element.pm
Criterion Covered Total %
statement 34 46 73.9
branch 7 14 50.0
condition 1 9 11.1
subroutine 7 10 70.0
pod 5 5 100.0
total 54 84 64.2


line stmt bran cond sub pod time code
1             ##----------------------------------------------------------------------------
2             ## CSS Object Oriented - ~/lib/CSS/Object/Element.pm
3             ## Version v0.2.0
4             ## Copyright(c) 2020 DEGUEST Pte. Ltd.
5             ## Author: Jacques Deguest <jack@deguest.jp>
6             ## Created 2020/06/21
7             ## Modified 2024/09/05
8             ## All rights reserved
9             ##
10             ## This program is free software; you can redistribute it and/or modify it
11             ## under the same terms as Perl itself.
12             ##----------------------------------------------------------------------------
13             package CSS::Object::Element;
14             BEGIN
15             {
16 6     6   3116 use strict;
  6         22  
  6         211  
17 6     6   31 use warnings;
  6         13  
  6         372  
18 6     6   77 use parent qw( Module::Generic );
  6         12  
  6         55  
19 6     6   3753 use CSS::Object::Format;
  6         24  
  6         78  
20 6     6   5307 our $VERSION = 'v0.2.0';
21             };
22              
23             sub init
24             {
25 175     175 1 411 my $self = shift( @_ );
26 175         522 $self->{format} = '';
27 175         402 $self->{_init_strict_use_sub} = 1;
28 175         911 $self->SUPER::init( @_ );
29 175 100       31536 unless( $self->_is_a( $self->{format}, 'CSS::Object::Format' ) )
30             {
31 7         93 my $format = CSS::Object::Format->new(
32             debug => $self->debug
33             );
34 7         65 $self->format( $format );
35             }
36 175         11733 return( $self );
37             }
38              
39             sub add_to
40             {
41 0     0 1 0 my $self = shift( @_ );
42 0   0     0 my $css = shift( @_ ) || return( $self->error( "No css object was provided to add our element to it." ) );
43 0 0       0 return( $self->error( "CSS object provided (", overload::StrVal( $css ), ") is not actually a CSS::Object object." ) ) if( !$self->_is_a( $css, 'CSS::Object' ) );
44 0         0 $self->format->indent( $css->format->indent );
45 0         0 $css->add_element( $self );
46 0         0 return( $self );
47             }
48              
49 0     0 1 0 sub as_string { return( shift->error( "This method has not been implemented in this class." ) ); }
50              
51 0     0 1 0 sub class { return( ref( $_[0] ) ); }
52              
53             sub format
54             {
55 902     902 1 104892 my $self = shift( @_ );
56 902         1445 my $format;
57 902 100       2583 if( @_ )
58             {
59             # $format = $self->_set_get_object( 'format', 'CSS::Object::Format', @_ ) || return;
60 161         319 my $val = shift( @_ );
61 161         312 my $format;
62 161 50 0     747 if( $self->_is_a( $val, 'CSS::Object::Format' ) )
    0          
63             {
64 161         11276 my $clone = $val->clone;
65             ## Make a copy for ourself
66 161 100       19475 if( $self->format )
67             {
68 30         895 my( $p, $f, $l ) = caller();
69 30         85 $clone->copy_parameters_from( $self->format );
70             }
71 161   50     15059 $format = $self->_set_get_object( 'format', 'CSS::Object::Format', $clone ) ||
72             return( $self->pass_error );
73             # die( "Provided value (", overload::StrVal( $val ), " and stored value (", overload::StrVal( $format ), ") are the same. It should have been cloned.\n" ) if( overload::StrVal( $val ) eq overload::StrVal( $format ) );
74             }
75             ## format as a class name
76             elsif( !ref( $val ) && CORE::index( $val, '::' ) != -1 )
77             {
78 0 0       0 $self->_load_class( $val ) || return( $self->pass_error );
79 0   0     0 $format = $val->new( debug => $self->debug ) || return( $self->pass_error( $val->error ) );
80 0         0 $self->_set_get_object( 'format', 'CSS::Object::Format', $format );
81             }
82             else
83             {
84 0         0 return( $self->error( "Unknown format \"$val\". I do not know what to do with it." ) );
85             }
86 161         72612 return( $format );
87             }
88 741         2661 return( $self->_set_get_object( 'format', 'CSS::Object::Format' ) );
89             }
90              
91             1;
92             # NOTE: POD
93             __END__
94              
95             =encoding utf-8
96              
97             =head1 NAME
98              
99             CSS::Object::Element - CSS Object Oriented Element
100              
101             =head1 SYNOPSIS
102              
103             package CSS::Object::Comment;
104             use parent qw( CSS::Object::Element );
105             my $cmt = CSS::Object::Comment->new( "No comment" ) ||
106             die( CSS::Object::Comment->error );
107              
108             =head1 VERSION
109              
110             v0.2.0
111              
112             =head1 DESCRIPTION
113              
114             L<CSS::Object::Element> is a base class for all L<CSS::Object> elements that get added to the style sheet.
115              
116             =head1 CONSTRUCTOR
117              
118             =head2 new
119              
120             This is the base method to instantiate object. It takes by default the following arguments and instantiate an L<CSS::Object::Format> object if none was provided.
121              
122             =over 4
123              
124             =item I<debug>
125              
126             This is an integer. The bigger it is and the more verbose is the output.
127              
128             =item I<format>
129              
130             This is a L<CSS::Object::Format> object or one of its child modules.
131              
132             =back
133              
134             =head1 METHODS
135              
136             =head2 add_to
137              
138             Provided with a L<CSS::Object> and this add our object to the css object array of elements by calling L<CSS::Object/add_element>. Elements here are top level elements which are css rules.
139              
140             =head2 as_string
141              
142             This method must be overridden by the child class.
143              
144             =head2 format
145              
146             This is a L<CSS::Object::Format> object or one of its child modules.
147              
148             head1 AUTHOR
149              
150             Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
151              
152             =head1 SEE ALSO
153              
154             L<CSS::Object>
155              
156             =head1 COPYRIGHT & LICENSE
157              
158             Copyright (c) 2020 DEGUEST Pte. Ltd.
159              
160             You can use, copy, modify and redistribute this package and associated
161             files under the same terms as Perl itself.
162              
163             =cut