| 1 | ##---------------------------------------------------------------------------- | ||||||||||||||
| 2 | ## CSS Object Oriented - ~/lib/CSS/Object/Builder.pm | ||||||||||||||
| 3 | ## Version v0.2.0 | ||||||||||||||
| 4 | ## Copyright(c) 2020 DEGUEST Pte. Ltd. | ||||||||||||||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||||||||||||||
| 6 | ## Created 2020/06/21 | ||||||||||||||
| 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::Builder; | ||||||||||||||
| 14 | BEGIN | ||||||||||||||
| 15 | 6 | ▶ | { | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | 6 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | 6 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 18 | 6 | ▶ | use parent qw( Module::Generic ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 19 | 6 | our $VERSION = 'v0.2.0'; | |||||||||||||
| 20 | }; | ||||||||||||||
| 21 | |||||||||||||||
| 22 | sub init | ||||||||||||||
| 23 | 2 | ▶ | { | ||||||||||||
|
Subroutine
init: called CC=5 SCAR=17.0
Pod
documented
| |||||||||||||||
| 24 | 2 | my $self = shift( @_ ); | |||||||||||||
| 25 | 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 @_
| |||||||||||||||
| 26 | 2 | ▶ | return( $self->error( "CSS object provided is not a CSS::Object object." ) ) if( !$self->_is_a( $css, 'CSS::Object' ) ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 27 | 2 | $self->{_init_strict_use_sub} = 1; | |||||||||||||
| 28 | 2 | ▶ | $self->SUPER::init( @_ ) || return( $self->pass_error ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 29 | 2 | ▶ | $self->css( $css ) || return( $self->pass_error ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 30 | 2 | return( $self ); | |||||||||||||
| 31 | } | ||||||||||||||
| 32 | |||||||||||||||
| 33 | 0 | ▶ | sub as_string { return( shift->css->as_string ); } | ||||||||||||
|
Subroutine
as_string: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 34 | |||||||||||||||
| 35 | sub at | ||||||||||||||
| 36 | 1 | ▶ | { | ||||||||||||
|
Subroutine
at: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||
| 37 | 1 | my $self = shift( @_ ); | |||||||||||||
| 38 | 1 | my( $name, $value ) = @_; | |||||||||||||
| 39 | 1 | ▶ | if( $name =~ /^([\-\_][a-zA-Z]+[\-\_])?keyframes/ ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 40 | { | ||||||||||||||
| 41 | 1 | my( $type, $name ) = @_; | |||||||||||||
| 42 | 1 | return( $self->css->new_keyframes_rule( type => $type, name => $name )->add_to( $self->css ) ); | |||||||||||||
| 43 | } | ||||||||||||||
| 44 | else | ||||||||||||||
| 45 | { | ||||||||||||||
| 46 | # return( $self->error( "I do not know what kind of rule is \"$type\" for rule name \"$name\"." ) ); | ||||||||||||||
| 47 | 0 | return( $self->css->new_at_rule( name => $name, value => $value )->add_to( $self->css ) ); | |||||||||||||
| 48 | } | ||||||||||||||
| 49 | } | ||||||||||||||
| 50 | |||||||||||||||
| 51 | 1 | ▶ | sub charset { return( shift->css->charset( @_ ) ); } | ||||||||||||
|
Subroutine
charset: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 52 | |||||||||||||||
| 53 | 1 | ▶ | sub comment { return( shift->css->add_element( CSS::Object::Comment->new( @_ ) ) ); } | ||||||||||||
|
Subroutine
comment: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 54 | |||||||||||||||
| 55 | 8 | ▶ | sub css { return( shift->_set_get_object( '__css', 'CSS::Object', @_ ) ); } | ||||||||||||
|
Subroutine
css: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 56 | |||||||||||||||
| 57 | 0 | ▶ | sub current_rule { return( shift->css->elements->last ); } | ||||||||||||
|
Subroutine
current_rule: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 58 | |||||||||||||||
| 59 | 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
| |||||||||||||||
| 60 | |||||||||||||||
| 61 | sub new_rule | ||||||||||||||
| 62 | 1 | ▶ | { | ||||||||||||
|
Subroutine
new_rule: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||
| 63 | 1 | my $self = shift( @_ ); | |||||||||||||
| 64 | 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
| |||||||||||||||
| 65 | # return( CSS::Object::Builder::Rule->new( @_, | ||||||||||||||
| 66 | # format => $css->format, | ||||||||||||||
| 67 | # debug => $self->debug, | ||||||||||||||
| 68 | # css => $css, | ||||||||||||||
| 69 | # ) ); | ||||||||||||||
| 70 | 1 | my $rule = CSS::Object::Builder::Rule->new( @_, | |||||||||||||
| 71 | format => $css->format, | ||||||||||||||
| 72 | debug => $self->debug, | ||||||||||||||
| 73 | css => $css, | ||||||||||||||
| 74 | ); | ||||||||||||||
| 75 | 1 | return( $rule ); | |||||||||||||
| 76 | } | ||||||||||||||
| 77 | |||||||||||||||
| 78 | sub select | ||||||||||||||
| 79 | 1 | ▶ | { | ||||||||||||
|
Subroutine
select: called CC=8 SCAR=29.0
Pod
documented
| |||||||||||||||
| 80 | 1 | my $self = shift( @_ ); | |||||||||||||
| 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 | 0 | ▶ | { | ||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||
| 89 | 0 | ▶ | if( overload::StrVal( $_ ) eq overload::StrVal( $rule ) ) | ||||||||||||
|
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 | $rule = $self->new_rule->add_to( $css ); | |||||||||||||
| 107 | 1 | ▶ | defined( $rule ) || return( $self->error( "Cannot create CSS::Object::Builder::Rule object: ", CSS::Object::Builder::Rule->error ) ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 108 | } | ||||||||||||||
| 109 | 1 | ▶ | if( $self->_is_array( $this ) ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 110 | { | ||||||||||||||
| 111 | 1 | foreach my $s ( @$this ) | |||||||||||||
| 112 | { | ||||||||||||||
| 113 | 2 | $css->new_selector( name => $s )->add_to( $rule ); | |||||||||||||
| 114 | } | ||||||||||||||
| 115 | } | ||||||||||||||
| 116 | # further calls will be made in the context of the CSS::Object::Builder::Rule package with dynamic method name | ||||||||||||||
| 117 | 1 | return( $rule ); | |||||||||||||
| 118 | } | ||||||||||||||
| 119 | |||||||||||||||
| 120 | # NOTE: CSS::Object::Builder::Rule class | ||||||||||||||
| 121 | # Dynamic css property name pakcage | ||||||||||||||
| 122 | package CSS::Object::Builder::Rule; | ||||||||||||||
| 123 | BEGIN | ||||||||||||||
| 124 | { | ||||||||||||||
| 125 | 6 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 126 | 6 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 127 | 6 | ▶ | use parent qw( CSS::Object::Rule ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 128 | }; | ||||||||||||||
| 129 | |||||||||||||||
| 130 | sub init | ||||||||||||||
| 131 | 1 | ▶ | { | ||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
| |||||||||||||||
| 132 | 1 | my $self = shift( @_ ); | |||||||||||||
| 133 | 1 | $self->{_init_strict_use_sub} = 1; | |||||||||||||
| 134 | 1 | $self->SUPER::init( @_ ); | |||||||||||||
| 135 | 1 | return( $self ); | |||||||||||||
| 136 | } | ||||||||||||||
| 137 | |||||||||||||||
| 138 | sub comment | ||||||||||||||
| 139 | 2 | ▶ | { | ||||||||||||
|
Subroutine
comment: called CC=2 SCAR=7.0
| |||||||||||||||
| 140 | 2 | my $self = shift( @_ ); | |||||||||||||
| 141 | 2 | my $cmt = $self->css->new_comment( @_ ); | |||||||||||||
| 142 | 2 | ▶ | $cmt->format->indent( $self->elements->length > 0 ? $self->elements->first->format->indent : ' ' ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 143 | 2 | return( $self->add_element( $cmt ) ); | |||||||||||||
| 144 | } | ||||||||||||||
| 145 | |||||||||||||||
| 146 | 7 | ▶ | sub css { return( shift->_set_get_object( 'css', 'CSS::Object', @_ ) ); } | ||||||||||||
|
Subroutine
css: called CC=1 SCAR=0.0
| |||||||||||||||
| 147 | |||||||||||||||
| 148 | AUTOLOAD | ||||||||||||||
| 149 | 4 | ▶ | { | ||||||||||||
|
Subroutine
AUTOLOAD: called CC=5 SCAR=16.6
| |||||||||||||||
| 150 | 4 | my( $prop_name ) = our $AUTOLOAD =~ /([^:]+)$/; | |||||||||||||
| 151 | 4 | my $self = shift( @_ ); | |||||||||||||
| 152 | 4 | ▶ | die( "No method \"$prop_name\" exists in this package \"", __PACKAGE__, "\".\n" ) if( !defined( $self ) ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 153 | 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
| |||||||||||||||
| 154 | 4 | $prop_name =~ tr/_/-/; | |||||||||||||
| 155 | 4 | ▶ | my $prop_val = $self->_is_array( $_[0] ) | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 156 | ? shift( @_ ) | ||||||||||||||
| 157 | : scalar( @_ ) > 1 | ||||||||||||||
| 158 | ? [ @_ ] | ||||||||||||||
| 159 | : shift( @_ ); | ||||||||||||||
| 160 | 4 | my $prop = $css->new_property( | |||||||||||||
| 161 | name => $prop_name, | ||||||||||||||
| 162 | value => $prop_val, | ||||||||||||||
| 163 | debug => $self->debug, | ||||||||||||||
| 164 | ); | ||||||||||||||
| 165 | 4 | $prop->format->indent( ' ' ); | |||||||||||||
| 166 | 4 | $self->elements->push( $prop ); | |||||||||||||
| 167 | 4 | return( $self ); | |||||||||||||
| 168 | }; | ||||||||||||||
| 169 | |||||||||||||||
| 170 | 6 | 1; | |||||||||||||
| 171 | # NOTE: POD | ||||||||||||||
| 172 | __END__ | ||||||||||||||