File Coverage

blib/lib/CSS/Object/Format/Inline.pm
Criterion Covered Total %
statement 62 62 100.0
branch 6 14 42.8
condition 1 2 50.0
subroutine 15 15 100.0
pod 5 5 100.0
total 89 98 90.8


line stmt bran cond sub pod time code
1             ##----------------------------------------------------------------------------
2             ## CSS Object Oriented - ~/lib/CSS/Object/Format/Inline.pm
3             ## Version v0.1.0
4             ## Copyright(c) 2020 DEGUEST Pte. Ltd.
5             ## Author: Jacques Deguest <@sitael.tokyo.deguest.jp>
6             ## Created 2020/08/09
7             ## Modified 2020/08/09
8             ##
9             ##----------------------------------------------------------------------------
10             package CSS::Object::Format::Inline;
11             BEGIN
12             {
13 2     2   4184 use strict;
  2         5  
  2         79  
14 2     2   11 use warnings;
  2         5  
  2         80  
15 2     2   11 use parent qw( CSS::Object::Format );
  2         6  
  2         10  
16 2     2   199 use Devel::Confess;
  2         5  
  2         15  
17 2     2   387 our $VERSION = 'v0.1.0';
18             };
19              
20             sub init
21             {
22 1     1 1 57 my $self = shift( @_ );
23 1         7 $self->{new_line} = "\n";
24 1         2 $self->{open_brace_on_new_line} = 0;
25 1         2 $self->{close_brace_on_new_line} = 0;
26 1         2 $self->{open_brace_and_new_line} = 0;
27 1         3 $self->{indent} = '';
28 1         2 $self->{property_separator} = ' ';
29 1         2 $self->{_init_strict_use_sub} = 1;
30 1         7 $self->SUPER::init( @_ );
31 1         6 return( $self );
32             }
33              
34             sub comment_as_string
35             {
36 5     5 1 106 my( $self, $elem ) = @_;
37 2     2   14 no overloading;
  2         4  
  2         435  
38 5 50       24 return( $self->error( "No comment object was provided." ) ) if( !$elem );
39 5 50       21 return( $self->error( "Comment object provied is not a CSS::Object::Comment object." ) ) if( !$self->_is_a( $elem, 'CSS::Object::Comment' ) );
40             ## Because this is inline, there is no carriage returns
41 5         111 return( '/* ' . $elem->values->join( ' ' )->scalar . ' */' );
42             }
43              
44             sub copy_parameters_from
45             {
46 8     8 1 183 my $self = shift( @_ );
47 8   50     24 my $fmt = shift( @_ ) || return( $self->error( "No formatter object was provided to copy the parameters from." ) );
48 8 50       63 return( $self->error( "Formatter object provided is actually not a formatter object." ) ) if( !$self->_is_a( $fmt, 'CSS::Object::Format' ) );
49             # my( $p, $f, $l ) = caller();
50             # $self->message( 3, "copy_parameters_from called from package $p at line $l in file $f to set indent from '", $self->indent->scalar, "' to '", $fmt->indent->scalar, "'." );
51             ## We only copy the property separator, and ignore all other possible parameters
52 8         138 my @ok_params = qw(
53             property_separator
54             );
55 8         18 for( @ok_params )
56             {
57 8 50       40 $self->$_( $fmt->$_ ) if( $fmt->can( $_ ) );
58             }
59 8         510 return( $self );
60             }
61              
62             sub elements_as_string
63             {
64 3     3 1 68 my( $self, $elems ) = @_;
65 2     2   14 no overloading;
  2         3  
  2         469  
66             ## Make a backup of parameters and we'll restore them after
67 3         18 my $backup = $self->backup_parameters;
68             ## new_array() from Module::Generic
69 3         373 my $all = $backup->{elements} = $self->new_array;
70             $elems->foreach(sub
71             {
72 15     15   752 $all->push( $_->format->backup_parameters );
73 15         1430 $_->format->indent( '' );
74 15         848 $_->format->property_separator( ' ' );
75 3         70 });
76            
77 3         181 $self->property_separator( ' ' );
78 3         162 $self->indent( '' );
79 3         162 my $res = $self->SUPER::elements_as_string( $elems );
80 3         135 $self->restore_parameters( $backup );
81             $elems->for(sub
82             {
83 15     15   131 my( $i, $this ) = @_;
84 15         82 $this->format->restore_parameters( $all->get( $i ) );
85 3         34 });
86 3         100 return( $res );
87             }
88              
89             sub rule_as_string
90             {
91 3     3 1 8 my( $self, $rule ) = @_;
92 2     2   15 no overloading;
  2         4  
  2         211  
93             # $self->message( 3, "Stringifying rule for inline style" );
94 3 50       16 return( $self->error( "No rule object was provided." ) ) if( !$rule );
95 3 0       17 return( $self->error( "Rule object provided (", overload::Overloaded( $rule ) ? overload::StrVal( $rule ) : $rule ,") is not an actual rule object." ) ) if( !$self->_is_a( $rule, 'CSS::Object::Rule' ) );
    50          
96 3         69 return( $rule->elements_as_string );
97             }
98              
99             1;
100              
101             __END__
102              
103             =encoding utf-8
104              
105             =head1 NAME
106              
107             CSS::Object::Format - CSS Object Oriented Stringificator for Inline CSS
108              
109             =head1 SYNOPSIS
110              
111             use CSS::Object::Format::Inline;
112             my $format = CSS::Object::Format::Inline->new( debug => 3 ) ||
113             die( CSS::Object::Format::Inline->error );
114             my $prop = CSS::Object::Property->new(
115             format => $format,
116             debug => 3,
117             name => 'display',
118             value => 'inline-block',
119             ) || die( CSS::Object::Property->error );
120             print( $prop->as_string );
121              
122             =head1 VERSION
123              
124             v0.1.0
125              
126             =head1 DESCRIPTION
127              
128             L<CSS::Object::Format> is a object oriented CSS parser and manipulation interface to write properties suitable to be added inline, i.e. inside an html element attribute C<style>. This package inherits from L<CSS::Object::Format>
129              
130             Because it is designed to be inline, there cannot be multiple rules. There is only rule and is implicit and used solely to hold all the properties.
131              
132             =head1 CONSTRUCTOR
133              
134             =head2 new
135              
136             To instantiate a new L<CSS::Object::Format> object, pass an hash reference of following parameters:
137              
138             =over 4
139              
140             =item I<debug>
141              
142             This is an integer. The bigger it is and the more verbose is the output.
143              
144             =back
145              
146             =head1 METHODS
147              
148             =head2 rule_as_string
149              
150             Provided with a L<CSS::Object::Rule> object and this will format it and return its string representation suitable for inline use in an HTML element.
151              
152             my $css = CSS::Object->new(
153             debug => 3,
154             format => CSS::Object::Format::Inline->new( debug => 3 )
155             );
156             my $rule = $css->add_rule( $css->new_rule );
157             $rule->add_property( $css->new_property(
158             name => 'display',
159             value => 'none',
160             ));
161             $rule->add_property( $css->new_property(
162             name => 'font-size',
163             value => '1.2rem',
164             ));
165             $rule->add_property( $css->new_property(
166             name => 'text-align',
167             value => 'center',
168             ));
169             print( '<div style="%s"></div>', $rule->as_string );
170              
171             =head2 comment_as_string
172              
173             This returns a formatted comment string.
174              
175             =head2 rule_as_string
176              
177             This returns a css rule as string.
178              
179             =head1 AUTHOR
180              
181             Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
182              
183             =head1 SEE ALSO
184              
185             L<CSS::Object>
186              
187             =head1 COPYRIGHT & LICENSE
188              
189             Copyright (c) 2020 DEGUEST Pte. Ltd.
190              
191             You can use, copy, modify and redistribute this package and associated
192             files under the same terms as Perl itself.
193              
194             =cut