File Coverage

blib/lib/CSS/Object/Property.pm
Criterion Covered Total %
statement 53 69 76.8
branch 9 20 45.0
condition 2 9 22.2
subroutine 15 18 83.3
pod 9 9 100.0
total 88 125 70.4


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