File Coverage

blib/lib/CSS/Object/Format/Inline.pm
Criterion Covered Total %
statement 59 59 100.0
branch 6 14 42.8
condition 1 2 50.0
subroutine 14 14 100.0
pod 5 5 100.0
total 85 94 90.4


line stmt bran cond sub pod time code
1             ##----------------------------------------------------------------------------
2             ## CSS Object Oriented - ~/lib/CSS/Object/Format/Inline.pm
3             ## Version v0.2.0
4             ## Copyright(c) 2020 DEGUEST Pte. Ltd.
5             ## Author: Jacques Deguest <jack@deguest.jp>
6             ## Created 2020/08/09
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::Format::Inline;
14             BEGIN
15             {
16 2     2   5713 use strict;
  2         4  
  2         165  
17 2     2   12 use warnings;
  2         4  
  2         134  
18 2     2   13 use parent qw( CSS::Object::Format );
  2         3  
  2         14  
19 2     2   553 our $VERSION = 'v0.2.0';
20             };
21              
22             sub init
23             {
24 1     1 1 163 my $self = shift( @_ );
25 1         68 $self->{new_line} = "\n";
26 1         3 $self->{open_brace_on_new_line} = 0;
27 1         2 $self->{close_brace_on_new_line} = 0;
28 1         3 $self->{open_brace_and_new_line} = 0;
29 1         2 $self->{indent} = '';
30 1         4 $self->{property_separator} = ' ';
31 1         2 $self->{_init_strict_use_sub} = 1;
32 1         9 $self->SUPER::init( @_ );
33 1         4 return( $self );
34             }
35              
36             sub comment_as_string
37             {
38 5     5 1 161 my( $self, $elem ) = @_;
39 2     2   17 no overloading;
  2         5  
  2         569  
40 5 50       22 return( $self->error( "No comment object was provided." ) ) if( !$elem );
41 5 50       18 return( $self->error( "Comment object provied is not a CSS::Object::Comment object." ) ) if( !$self->_is_a( $elem, 'CSS::Object::Comment' ) );
42             ## Because this is inline, there is no carriage returns
43 5         180 return( '/* ' . $elem->values->join( ' ' )->scalar . ' */' );
44             }
45              
46             sub copy_parameters_from
47             {
48 8     8 1 232 my $self = shift( @_ );
49 8   50     62 my $fmt = shift( @_ ) || return( $self->error( "No formatter object was provided to copy the parameters from." ) );
50 8 50       32 return( $self->error( "Formatter object provided is actually not a formatter object." ) ) if( !$self->_is_a( $fmt, 'CSS::Object::Format' ) );
51             # my( $p, $f, $l ) = caller();
52             ## We only copy the property separator, and ignore all other possible parameters
53 8         296 my @ok_params = qw(
54             property_separator
55             );
56 8         47 for( @ok_params )
57             {
58 8 50       57 $self->$_( $fmt->$_ ) if( $fmt->can( $_ ) );
59             }
60 8         12096 return( $self );
61             }
62              
63             sub elements_as_string
64             {
65 3     3 1 2345 my( $self, $elems ) = @_;
66 2     2   14 no overloading;
  2         4  
  2         521  
67             ## Make a backup of parameters and we'll restore them after
68 3         18 my $backup = $self->backup_parameters;
69             ## new_array() from Module::Generic
70 3         306 my $all = $backup->{elements} = $self->new_array;
71             $elems->foreach(sub
72             {
73 15     15   16498 $all->push( $_->format->backup_parameters );
74 15         1456 $_->format->indent( '' );
75 15         16525 $_->format->property_separator( ' ' );
76 3         2026 });
77            
78 3         4522 $self->property_separator( ' ' );
79 3         3889 $self->indent( '' );
80 3         3785 my $res = $self->SUPER::elements_as_string( $elems );
81 3         716 $self->restore_parameters( $backup );
82             $elems->for(sub
83             {
84 15     15   2429 my( $i, $this ) = @_;
85 15         69 $this->format->restore_parameters( $all->get( $i ) );
86 3         28 });
87 3         1133 return( $res );
88             }
89              
90             sub rule_as_string
91             {
92 3     3 1 9 my( $self, $rule ) = @_;
93 2     2   16 no overloading;
  2         5  
  2         292  
94 3 50       13 return( $self->error( "No rule object was provided." ) ) if( !$rule );
95 3 0       14 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         146 return( $rule->elements_as_string );
97             }
98              
99             1;
100             # NOTE: POD
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.2.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