| 1 | ##---------------------------------------------------------------------------- | ||||||||||||||
| 2 | ## CSS Object Oriented - ~/lib/CSS/Object/Builder.pm | ||||||||||||||
| 3 | ## Version v0.1.1 | ||||||||||||||
| 4 | ## Copyright(c) 2020 DEGUEST Pte. Ltd. | ||||||||||||||
| 5 | ## Author: Jacques Deguest <@sitael.local> | ||||||||||||||
| 6 | ## Created 2020/06/21 | ||||||||||||||
| 7 | ## Modified 2020/06/24 | ||||||||||||||
| 8 | ## | ||||||||||||||
| 9 | ##---------------------------------------------------------------------------- | ||||||||||||||
| 10 | package CSS::Object::Builder; | ||||||||||||||
| 11 | BEGIN | ||||||||||||||
| 12 | { | ||||||||||||||
| 13 | 6 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | 6 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | 6 | ▶ | use parent qw( Module::Generic ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | 6 | ▶ | use Devel::Confess; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | 6 | ▶ | our $VERSION = 'v0.1.0'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 18 | }; | ||||||||||||||
| 19 | |||||||||||||||
| 20 | sub init | ||||||||||||||
| 21 | { | ||||||||||||||
| 22 | 2 | ▶ | my $self = shift( @_ ); | ||||||||||||
|
Subroutine
init: called CC=5 SCAR=17.0
Pod
documented
| |||||||||||||||
| 23 | 2 | ▶ | my $css = shift( @_ ) || return( $self->error( "No css object was provided." ) ); | ||||||||||||
|
Condition50%1/2
shift @_ || (return $self->error('No css object was provided.'))
MC/DC0%0/1
shift @_ || (return $self->error('No css object was provided.'))
shift @_
Truth table50%1/2
A
A= shift @_
| |||||||||||||||
| 24 | 2 | ▶ | return( $self->error( "CSS object provided is not a CSS::Object object." ) ) if( !$self->_is_a( $css, 'CSS::Object' ) ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 25 | 2 | $self->{_init_strict_use_sub} = 1; | |||||||||||||
| 26 | 2 | ▶ | $self->SUPER::init( @_ ) || return( $self->pass_error ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 27 | 2 | ▶ | $self->css( $css ) || return( $self->pass_error ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 28 | 2 | return( $self ); | |||||||||||||
| 29 | } | ||||||||||||||
| 30 | |||||||||||||||
| 31 | 0 | ▶ | sub as_string { return( shift->css->as_string ); } | ||||||||||||
|
Subroutine
as_string: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 32 | |||||||||||||||
| 33 | sub at | ||||||||||||||
| 34 | { | ||||||||||||||
| 35 | 1 | ▶ | my $self = shift( @_ ); | ||||||||||||
|
Subroutine
at: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||
| 36 | 1 | my( $name, $value ) = @_; | |||||||||||||
| 37 | 1 | ▶ | if( $name =~ /^([\-\_][a-zA-Z]+[\-\_])?keyframes/ ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 38 | { | ||||||||||||||
| 39 | 1 | my( $type, $name ) = @_; | |||||||||||||
| 40 | 1 | return( $self->css->new_keyframes_rule( type => $type, name => $name )->add_to( $self->css ) ); | |||||||||||||
| 41 | } | ||||||||||||||
| 42 | else | ||||||||||||||
| 43 | { | ||||||||||||||
| 44 | # return( $self->error( "I do not know what kind of rule is \"$type\" for rule name \"$name\"." ) ); | ||||||||||||||
| 45 | 0 | return( $self->css->new_at_rule( name => $name, value => $value )->add_to( $self->css ) ); | |||||||||||||
| 46 | } | ||||||||||||||
| 47 | } | ||||||||||||||
| 48 | |||||||||||||||
| 49 | 1 | ▶ | sub charset { return( shift->css->charset( @_ ) ); } | ||||||||||||
|
Subroutine
charset: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 50 | |||||||||||||||
| 51 | 1 | ▶ | sub comment { return( shift->css->add_element( CSS::Object::Comment->new( @_ ) ) ); } | ||||||||||||
|
Subroutine
comment: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 52 | |||||||||||||||
| 53 | 8 | ▶ | sub css { return( shift->_set_get_object( '__css', 'CSS::Object', @_ ) ); } | ||||||||||||
|
Subroutine
css: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 54 | |||||||||||||||
| 55 | 0 | ▶ | sub current_rule { return( shift->css->elements->last ); } | ||||||||||||
|
Subroutine
current_rule: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 56 | |||||||||||||||
| 57 | 0 | ▶ | sub elements { return( shift->_set_get_object_array_object( 'elements', 'CSS::Object::Element', @_ ) ); } | ||||||||||||
|
Subroutine
elements: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 58 | |||||||||||||||
| 59 | sub new_rule | ||||||||||||||
| 60 | { | ||||||||||||||
| 61 | 1 | ▶ | my $self = shift( @_ ); | ||||||||||||
|
Subroutine
new_rule: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||
| 62 | 1 | ▶ | my $css = $self->css || return( $self->error( "Our main css object is gone!" ) ); | ||||||||||||
|
Condition50%1/2
$self->css || (return $self->error('Our main css object is gone!'))
MC/DC0%0/1
$self->css || (return $self->error('Our main css object is gone!'))
$self->css
Truth table50%1/2
A
A= $self->css
| |||||||||||||||
| 63 | 1 | $self->message( 3, "Creating new CSS::Object::Builder::Rule object with css object '$css' and formatter set to '", $css->format, "'." ); | |||||||||||||
| 64 | # return( CSS::Object::Builder::Rule->new( @_, | ||||||||||||||
| 65 | # format => $css->format, | ||||||||||||||
| 66 | # debug => $self->debug, | ||||||||||||||
| 67 | # css => $css, | ||||||||||||||
| 68 | # ) ); | ||||||||||||||
| 69 | 1 | my $rule = CSS::Object::Builder::Rule->new( @_, | |||||||||||||
| 70 | format => $css->format, | ||||||||||||||
| 71 | debug => $self->debug, | ||||||||||||||
| 72 | css => $css, | ||||||||||||||
| 73 | ); | ||||||||||||||
| 74 | # $self->message( 3, "Returning rule object '", overload::StrVal( $rule ), "'." ); | ||||||||||||||
| 75 | 1 | return( $rule ); | |||||||||||||
| 76 | } | ||||||||||||||
| 77 | |||||||||||||||
| 78 | sub select | ||||||||||||||
| 79 | { | ||||||||||||||
| 80 | 1 | ▶ | my $self = shift( @_ ); | ||||||||||||
|
Subroutine
select: called CC=8 SCAR=28.6
Pod
documented
| |||||||||||||||
| 81 | 1 | ▶ | my $this = shift( @_ ) || return( $self->error( "No css selector or rule object was provided" ) ); | ||||||||||||
|
Condition50%1/2
shift @_ || (return $self->error('No css selector or rule object was provided'))
MC/DC0%0/1
shift @_ || (return $self->error('No css selector or rule object was provided'))
shift @_
Truth table50%1/2
A
A= shift @_
| |||||||||||||||
| 82 | 1 | ▶ | my $css = $self->css || return( $self->error( "Our main css object is gone!" ) ); | ||||||||||||
|
Condition50%1/2
$self->css || (return $self->error('Our main css object is gone!'))
MC/DC0%0/1
$self->css || (return $self->error('Our main css object is gone!'))
$self->css
Truth table50%1/2
A
A= $self->css
| |||||||||||||||
| 83 | 1 | my $rule; | |||||||||||||
| 84 | 1 | ▶ | if( $self->_is_a( $this, 'CSS::Object::Rule' ) ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 85 | { | ||||||||||||||
| 86 | 0 | my $found = 0; | |||||||||||||
| 87 | $css->elements->foreach(sub | ||||||||||||||
| 88 | { | ||||||||||||||
| 89 | 0 | ▶ | if( overload::StrVal( $_ ) eq overload::StrVal( $rule ) ) | ||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||
| 90 | { | ||||||||||||||
| 91 | 0 | $found++; | |||||||||||||
| 92 | 0 | return; | |||||||||||||
| 93 | } | ||||||||||||||
| 94 | 0 | }); | |||||||||||||
| 95 | 0 | $rule = bless( $this, 'CSS::Object::Builder::Rule' ); | |||||||||||||
| 96 | 0 | $rule->css( $css ); | |||||||||||||
| 97 | # Unless this object is already added to the CSS::Object we add it now | ||||||||||||||
| 98 | 0 | ▶ | unless( $found ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 99 | { | ||||||||||||||
| 100 | 0 | $rule->add_to( $css ); | |||||||||||||
| 101 | } | ||||||||||||||
| 102 | 0 | return( $rule ); | |||||||||||||
| 103 | } | ||||||||||||||
| 104 | else | ||||||||||||||
| 105 | { | ||||||||||||||
| 106 | 1 | $self->message( 3, "Creating new CSS::Object::Builder::Rule object." ); | |||||||||||||
| 107 | 1 | $rule = $self->new_rule->add_to( $css ); | |||||||||||||
| 108 | 1 | ▶ | defined( $rule ) || return( $self->error( "Cannot create CSS::Object::Builder::Rule object: ", CSS::Object::Builder::Rule->error ) ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 109 | } | ||||||||||||||
| 110 | # $self->message( 3, "Rule object is '", overload::StrVal( $rule ), "'." ); | ||||||||||||||
| 111 | 1 | ▶ | if( $self->_is_array( $this ) ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 112 | { | ||||||||||||||
| 113 | 1 | foreach my $s ( @$this ) | |||||||||||||
| 114 | { | ||||||||||||||
| 115 | 2 | $css->new_selector( name => $s )->add_to( $rule ); | |||||||||||||
| 116 | } | ||||||||||||||
| 117 | } | ||||||||||||||
| 118 | # further calls will be made in the context of the CSS::Object::Builder::Rule package with dynamic method name | ||||||||||||||
| 119 | 1 | return( $rule ); | |||||||||||||
| 120 | } | ||||||||||||||
| 121 | |||||||||||||||
| 122 | # XXX CSS::Object::Builder::Rule class | ||||||||||||||
| 123 | # Dynamic css property name pakcage | ||||||||||||||
| 124 | package CSS::Object::Builder::Rule; | ||||||||||||||
| 125 | BEGIN | ||||||||||||||
| 126 | { | ||||||||||||||
| 127 | 6 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 128 | 6 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 129 | 6 | ▶ | use parent qw( CSS::Object::Rule ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 130 | 6 | ▶ | use Devel::Confess; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: not called CC=1 SCAR=0.0
| |||||||||||||||
| 131 | }; | ||||||||||||||
| 132 | |||||||||||||||
| 133 | sub init | ||||||||||||||
| 134 | { | ||||||||||||||
| 135 | 1 | ▶ | my $self = shift( @_ ); | ||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
| |||||||||||||||
| 136 | 1 | $self->{_init_strict_use_sub} = 1; | |||||||||||||
| 137 | 1 | $self->SUPER::init( @_ ); | |||||||||||||
| 138 | 1 | return( $self ); | |||||||||||||
| 139 | } | ||||||||||||||
| 140 | |||||||||||||||
| 141 | sub comment | ||||||||||||||
| 142 | { | ||||||||||||||
| 143 | 2 | ▶ | my $self = shift( @_ ); | ||||||||||||
|
Subroutine
comment: called CC=2 SCAR=7.0
| |||||||||||||||
| 144 | 2 | my $cmt = $self->css->new_comment( @_ ); | |||||||||||||
| 145 | 2 | ▶ | $cmt->format->indent( $self->elements->length > 0 ? $self->elements->first->format->indent : ' ' ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 146 | 2 | return( $self->add_element( $cmt ) ); | |||||||||||||
| 147 | } | ||||||||||||||
| 148 | |||||||||||||||
| 149 | 7 | ▶ | sub css { return( shift->_set_get_object( 'css', 'CSS::Object', @_ ) ); } | ||||||||||||
|
Subroutine
css: called CC=1 SCAR=0.0
| |||||||||||||||
| 150 | |||||||||||||||
| 151 | AUTOLOAD | ||||||||||||||
| 152 | { | ||||||||||||||
| 153 | 4 | ▶ | my( $prop_name ) = our $AUTOLOAD =~ /([^:]+)$/; | ||||||||||||
|
Subroutine
AUTOLOAD: called CC=5 SCAR=16.6
| |||||||||||||||
| 154 | 4 | my $self = shift( @_ ); | |||||||||||||
| 155 | 4 | ▶ | die( "No method \"$prop_name\" exists in this package \"", __PACKAGE__, "\".\n" ) if( !defined( $self ) ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 156 | 4 | ▶ | my $css = $self->css || return( $self->error( "Our main css object is gone!" ) ); | ||||||||||||
|
Condition50%1/2
$self->css || (return $self->error('Our main css object is gone!'))
MC/DC0%0/1
$self->css || (return $self->error('Our main css object is gone!'))
$self->css
Truth table50%1/2
A
A= $self->css
| |||||||||||||||
| 157 | 4 | $prop_name =~ tr/_/-/; | |||||||||||||
| 158 | 4 | ▶ | my $prop_val = $self->_is_array( $_[0] ) | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 159 | ? shift( @_ ) | ||||||||||||||
| 160 | : scalar( @_ ) > 1 | ||||||||||||||
| 161 | ? [ @_ ] | ||||||||||||||
| 162 | : shift( @_ ); | ||||||||||||||
| 163 | 4 | my $prop = $css->new_property( | |||||||||||||
| 164 | name => $prop_name, | ||||||||||||||
| 165 | value => $prop_val, | ||||||||||||||
| 166 | debug => $self->debug, | ||||||||||||||
| 167 | ); | ||||||||||||||
| 168 | 4 | $prop->format->indent( ' ' ); | |||||||||||||
| 169 | 4 | $self->elements->push( $prop ); | |||||||||||||
| 170 | 4 | return( $self ); | |||||||||||||
| 171 | }; | ||||||||||||||
| 172 | |||||||||||||||
| 173 | 6 | 1; | |||||||||||||
| 174 | |||||||||||||||
| 175 | __END__ | ||||||||||||||