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   14121 use strict;
  2         5  
  2         60  
14 2     2   9 use warnings;
  2         3  
  2         61  
15 2     2   10 use parent qw( CSS::Object::Format );
  2         3  
  2         11  
16 2     2   117 use Devel::Confess;
  2         3  
  2         14  
17 2     2   391 our $VERSION = 'v0.1.0';
18             };
19              
20             sub init
21             {
22 1     1 1 78 my $self = shift( @_ );
23 1         6 $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         2 $self->{indent} = '';
28 1         1 $self->{property_separator} = ' ';
29 1         2 $self->{_init_strict_use_sub} = 1;
30 1         6 $self->SUPER::init( @_ );
31 1         5 return( $self );
32             }
33              
34             sub comment_as_string
35             {
36 5     5 1 88 my( $self, $elem ) = @_;
37 2     2   13 no overloading;
  2         4  
  2         333  
38 5 50       21 return( $self->error( "No comment object was provided." ) ) if( !$elem );
39 5 50       19 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         87 return( '/* ' . $elem->values->join( ' ' )->scalar . ' */' );
42             }
43              
44             sub copy_parameters_from
45             {
46 8     8 1 109 my $self = shift( @_ );
47 8   50     17 my $fmt = shift( @_ ) || return( $self->error( "No formatter object was provided to copy the parameters from." ) );
48 8 50       17 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         95 my @ok_params = qw(
53             property_separator
54             );
55 8         12 for( @ok_params )
56             {
57 8 50       37 $self->$_( $fmt->$_ ) if( $fmt->can( $_ ) );
58             }
59 8         364 return( $self );
60             }
61              
62             sub elements_as_string
63             {
64 3     3 1 61 my( $self, $elems ) = @_;
65 2     2   10 no overloading;
  2         4  
  2         323  
66             ## Make a backup of parameters and we'll restore them after
67 3         16 my $backup = $self->backup_parameters;
68             ## new_array() from Module::Generic
69 3         308 my $all = $backup->{elements} = $self->new_array;
70             $elems->foreach(sub
71             {
72 15     15   603 $all->push( $_->format->backup_parameters );
73 15         1132 $_->format->indent( '' );
74 15         707 $_->format->property_separator( ' ' );
75 3         57 });
76            
77 3         145 $self->property_separator( ' ' );
78 3         132 $self->indent( '' );
79 3         145 my $res = $self->SUPER::elements_as_string( $elems );
80 3         102 $self->restore_parameters( $backup );
81             $elems->for(sub
82             {
83 15     15   110 my( $i, $this ) = @_;
84 15         56 $this->format->restore_parameters( $all->get( $i ) );
85 3         31 });
86 3         82 return( $res );
87             }
88              
89             sub rule_as_string
90             {
91 3     3 1 9 my( $self, $rule ) = @_;
92 2     2   11 no overloading;
  2         3  
  2         176  
93             # $self->message( 3, "Stringifying rule for inline style" );
94 3 50       12 return( $self->error( "No rule object was provided." ) ) if( !$rule );
95 3 0       13 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         52 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