File Coverage

blib/lib/CSS/Object/Property.pm
Criterion Covered Total %
statement 50 66 75.7
branch 9 20 45.0
condition 2 9 22.2
subroutine 14 17 82.3
pod 9 9 100.0
total 84 121 69.4


line stmt bran cond sub pod time code
1             ##----------------------------------------------------------------------------
2             ## CSS Object Oriented - ~/lib/CSS/Object/Property.pm
3             ## Version v0.2.0
4             ## Copyright(c) 2021 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::Property;
14             BEGIN
15             {
16 6     6   38 use strict;
  6         12  
  6         250  
17 6     6   30 use warnings;
  6         11  
  6         308  
18 6     6   29 use parent qw( CSS::Object::Element );
  6         11  
  6         31  
19 6     6   574 use CSS::Object::Format;
  6         11  
  6         38  
20 6     6   5042 use CSS::Object::Value;
  6         47  
  6         110  
21 6     6   8356 our $VERSION = 'v0.2.0';
22             };
23              
24             sub init
25             {
26 45     45 1 6908 my $self = shift( @_ );
27             ## print( STDERR ref( $self ), "::init() Args received are: ", $self->dump( @_ ), "\n" );
28             ## $self->{format} = '';
29 45         631 $self->{name} = '';
30 45         155 $self->{value} = '';
31 45         166 $self->{values} = [];
32             ## $self->{_init_strict_use_sub} = 1;
33 45         334 $self->SUPER::init( @_ );
34 45         160 $self->format->indent( ' ' );
35 45         60461 return( $self );
36             }
37              
38             sub add_to
39             {
40 7     7 1 22 my $self = shift( @_ );
41 7   50     57 my $rule = shift( @_ ) || return( $self->error( "No rule object was provided to add our property to it." ) );
42 7 50       5422 return( $self->error( "Rule object provided (", overload::StrVal( $rule ), ") is not actually a CSS::Object::Rule object." ) ) if( !$self->_is_a( $rule, 'CSS::Object::Rule' ) );
43 7         266 $self->format->indent( $rule->format->indent );
44 7         6922 $rule->add_property( $self );
45 7         45 return( $self );
46             }
47              
48 250     250 1 203887 sub as_string { return( $_[0]->format->property_as_string( $_[0] ) ); }
49              
50             sub format
51             {
52 958     958 1 7591 my $self = shift( @_ );
53 958 100       2809 if( @_ )
54             {
55             # my( $p, $f, $l ) = caller;
56 16   50     122 my $format = $self->SUPER::format( @_ ) || return( $self->pass_error );
57             $self->values->foreach(sub
58             {
59 15 50   15   10691 shift->format( $format ) || return;
60 16         80 });
61 16         6811 $format->indent( ' ' );
62 16         76406 return( $format );
63             }
64 942         3198 return( $self->_set_get_object( 'format', 'CSS::Object::Format' ) );
65             }
66              
67 298     298 1 8915 sub name { return( shift->_set_get_scalar_as_object( 'name', @_ ) ); }
68              
69             sub remove_from
70             {
71 0     0 1 0 my $self = shift( @_ );
72 0   0     0 my $rule = shift( @_ ) || return( $self->error( "No rule object was provided to remove our property from it." ) );
73 0 0       0 return( $self->error( "Rule object provided (", overload::StrVal( $rule ), ") is not actually a CSS::Object::Rule object." ) ) if( !$self->_is_a( $rule, 'CSS::Object::Rule' ) );
74 0         0 $rule->elements->remove( $self );
75 0         0 return( $self );
76             }
77              
78             sub value
79             {
80 48     48 1 119081 my $self = shift( @_ );
81 48 100       191 if( @_ )
82             {
83 44         113 my $val = shift( @_ );
84 44         90 my $valObj;
85 44 50       181 if( $self->_is_a( $val, 'CSS::Object::Value' ) )
    50          
86             {
87 0         0 $valObj = $val;
88 0         0 $valObj->format( $self->format );
89 0         0 $valObj->debug( $self->debug );
90             }
91             ## Array of values, which could be
92             elsif( $self->_is_array( $val ) )
93             {
94             ## Make sure this is a Module::Generic::Array
95 0         0 $val = $self->new_array( $val );
96             $val->foreach(sub
97             {
98 0 0 0 0   0 if( $self->_is_a( $_, 'CSS::Object::Value' ) )
    0          
99             {
100 0         0 $self->values->push( $_ );
101             }
102             elsif( !ref( $_ ) || $self->_is_a( $_, 'Module::Generic::Scalar' ) )
103             {
104 0         0 $valObj = CSS::Object::Value->new( "$_",
105             debug => $self->debug,
106             format => $self->format,
107             );
108 0         0 $self->values->push( $_ );
109             }
110             else
111             {
112 0         0 CORE::warn( "Got value \"$_\" in an array of values provided for this property, but I just do not know what to do with it.\n" );
113             }
114 0         0 });
115             }
116             else
117             {
118 44         1266 $valObj = CSS::Object::Value->new( $val,
119             debug => $self->debug,
120             format => $self->format,
121             );
122 44 50       400 defined( $valObj ) || return( $self->error( "Unable to initialise CSS::Object::Value object for value \"$val\"." ) );
123             }
124 44         228 $self->values->set( $valObj );
125 44         48634 return( $valObj );
126             }
127             # my $last = $self->values->last;
128 4         29 return( $self->values->last );
129             }
130              
131             # Array of CSS::Object::Value objects
132 314     314 1 199175 sub values { return( shift->_set_get_object_array_object( 'values', 'CSS::Object::Value', @_ ) ); }
133              
134 0     0 1   sub values_as_string { return( $_[0]->format->values_as_string( $_[0]->values ) ); }
135              
136             1;
137             # NOTE: POD
138             __END__
139              
140             =encoding utf-8
141              
142             =head1 NAME
143              
144             CSS::Object::Property - CSS Object Oriented Property
145              
146             =head1 SYNOPSIS
147              
148             use CSS::Object::Property;
149             my $prop = CSS::Object::Property->new(
150             name => 'display',
151             value => 'inline-block',
152             format => $format_object,
153             debug => 3,
154             ) || die( CSS::Object::Property->error );
155              
156             =head1 VERSION
157              
158             v0.2.0
159              
160             =head1 DESCRIPTION
161              
162             L<CSS::Object::Property> is a class to represent a CSS property.
163              
164             =head1 CONSTRUCTOR
165              
166             =head2 new
167              
168             To instantiate a new L<CSS::Object::Property> object, pass an hash reference of following parameters:
169              
170             =over 4
171              
172             =item I<debug>
173              
174             This is an integer. The bigger it is and the more verbose is the output.
175              
176             =item I<format>
177              
178             This is a L<CSS::Object::Format> object or one of its child modules.
179              
180             =item I<name>
181              
182             This is the property's name. When provided, this calls the method L</name> to store the value.
183              
184             =item I<value>
185              
186             This is the property's name. When provided, this calls the method L</value> to store the value.
187              
188             =back
189              
190             =head1 METHODS
191              
192             =head2 format
193              
194             This is a L<CSS::Object::Format> object or one of its child modules.
195              
196             =head2 as_string
197              
198             This calls the L</format> and its method L<CSS::Object::Format/property_as_string>
199              
200             It returns the css string produced or undef and sets an L<Module::Generic::Exception> upon error.
201              
202             =head2 format
203              
204             This sets or gets the L<CSS::Object::Format> object. When set, this will share the formatter with all its L<CSS::Object::Value> objects.
205              
206             =head2 name
207              
208             Sets or gets the property's name. The name stored here becomes a L<Module::Generic::Scalar> and thus all its object methods can be used
209              
210             =head2 remove_from
211              
212             This takes an L<CSS::Object::Rule> object as single argument and remove this property object from its list of elements.
213              
214             It basically does:
215              
216             $rule->elements->remove( $self );
217              
218             It returns the current property object.
219              
220             =head2 value
221              
222             Sets the value or get the last value for this property.
223              
224             It takes either a string or a L<CSS::Object::Value> object and add it to the list of stored values for this property using the L</properties> method.
225              
226             It returns the last property value object in the L</values> array.
227              
228             =head2 values
229              
230             This sets or gets the list of L<CSS::Object::Value> objects. It uses a L<Module::Generic::Array> object to store those value objects.
231              
232             It returns the array object.
233              
234             =head2 values_as_string
235              
236             This returns a string representation of all the L<CSS::Object::Value> objects currently stored. It does this by calling the format's method L<CSS::Object::Format/values_as_string>
237              
238             =head1 AUTHOR
239              
240             Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
241              
242             =head1 SEE ALSO
243              
244             L<CSS::Object>
245              
246             =head1 COPYRIGHT & LICENSE
247              
248             Copyright (c) 2020 DEGUEST Pte. Ltd.
249              
250             You can use, copy, modify and redistribute this package and associated
251             files under the same terms as Perl itself.
252              
253             =cut