| 1 | ##---------------------------------------------------------------------------- | ||||||||||||||
| 2 | ## CSS Object Oriented - ~/lib/CSS/Object/Rule.pm | ||||||||||||||
| 3 | ## Version v0.2.0 | ||||||||||||||
| 4 | ## Copyright(c) 2021 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::Rule; | ||||||||||||||
| 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( CSS::Object::Element ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 19 | 6 | ▶ | use Wanted (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 20 | 6 | ▶ | use overload ( | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 21 | 6 | '""' => 'as_string', | |||||||||||||
| 22 | fallback => 1, | ||||||||||||||
| 23 | 6 | ); | |||||||||||||
| 24 | 6 | ▶ | our $VERSION = 'v0.2.0'; | ||||||||||||
|
Subroutine
BEGIN: not called CC=1 SCAR=0.0
| |||||||||||||||
| 25 | }; | ||||||||||||||
| 26 | |||||||||||||||
| 27 | sub init | ||||||||||||||
| 28 | 29 | ▶ | { | ||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 29 | 29 | my $self = shift( @_ ); | |||||||||||||
| 30 | 29 | $self->{format} = ''; | |||||||||||||
| 31 | 29 | $self->{properties} = []; | |||||||||||||
| 32 | 29 | $self->{selectors} = []; | |||||||||||||
| 33 | 29 | $self->{_init_strict_use_sub} = 1; | |||||||||||||
| 34 | 29 | $self->SUPER::init( @_ ); | |||||||||||||
| 35 | 29 | return( $self ); | |||||||||||||
| 36 | } | ||||||||||||||
| 37 | |||||||||||||||
| 38 | sub add_element | ||||||||||||||
| 39 | 5 | ▶ | { | ||||||||||||
|
Subroutine
add_element: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||
| 40 | 5 | my $self = shift( @_ ); | |||||||||||||
| 41 | 5 | ▶ | my $elem = shift( @_ ) || return( $self->error( "No element object was provided to add to this rule." ) ); | ||||||||||||
|
Condition50%1/2
shift @_ || (return $self->error('No element object was provided to add to this rule.'))
MC/DC0%0/1
shift @_ || (return $self->error('No element object was provided to add to this rule.'))
shift @_
Truth table50%1/2
A
A= shift @_
| |||||||||||||||
| 42 | 5 | ▶ | return( $self->error( "Element object provided ($elem) is not a CSS::Object::Element object." ) ) if( !$self->_is_a( $elem, 'CSS::Object::Element' ) ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 43 | # $elem->format( $self->format ); | ||||||||||||||
| 44 | 5 | $elem->debug( $self->debug ); | |||||||||||||
| 45 | # $self->properties->push( $prop ); | ||||||||||||||
| 46 | 5 | $self->elements->push( $elem ); | |||||||||||||
| 47 | 5 | return( $self ); | |||||||||||||
| 48 | } | ||||||||||||||
| 49 | |||||||||||||||
| 50 | sub add_property | ||||||||||||||
| 51 | 40 | ▶ | { | ||||||||||||
|
Subroutine
add_property: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||
| 52 | 40 | my $self = shift( @_ ); | |||||||||||||
| 53 | 40 | ▶ | my $prop = shift( @_ ) || return( $self->error( "No property object was provided to add to this rule." ) ); | ||||||||||||
|
Condition50%1/2
shift @_ || (return $self->error('No property object was provided to add to this rule.'))
MC/DC0%0/1
shift @_ || (return $self->error('No property object was provided to add to this rule.'))
shift @_
Truth table50%1/2
A
A= shift @_
| |||||||||||||||
| 54 | 40 | ▶ | return( $self->error( "Property object provided ($prop) is not a CSS::Object::Property object." ) ) if( !$self->_is_a( $prop, 'CSS::Object::Property' ) ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 55 | # $prop->format( $self->format ); | ||||||||||||||
| 56 | 40 | $prop->debug( $self->debug ); | |||||||||||||
| 57 | # $self->properties->push( $prop ); | ||||||||||||||
| 58 | 40 | $self->elements->push( $prop ); | |||||||||||||
| 59 | 40 | return( $self ); | |||||||||||||
| 60 | } | ||||||||||||||
| 61 | |||||||||||||||
| 62 | sub add_selector | ||||||||||||||
| 63 | 47 | ▶ | { | ||||||||||||
|
Subroutine
add_selector: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||
| 64 | 47 | my $self = shift( @_ ); | |||||||||||||
| 65 | 47 | ▶ | my $sel = shift( @_ ) || return( $self->error( "No selector object was provided to add to this rule." ) ); | ||||||||||||
|
Condition50%1/2
shift @_ || (return $self->error('No selector object was provided to add to this rule.'))
MC/DC0%0/1
shift @_ || (return $self->error('No selector object was provided to add to this rule.'))
shift @_
Truth table50%1/2
A
A= shift @_
| |||||||||||||||
| 66 | 47 | ▶ | return( $self->error( "Selector object provided is not a CSS::Object::Selector object." ) ) if( !$self->_is_a( $sel, 'CSS::Object::Selector' ) ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 67 | # $sel->format( $self->format ); | ||||||||||||||
| 68 | 47 | $sel->debug( $self->debug ); | |||||||||||||
| 69 | 47 | $self->selectors->push( $sel ); | |||||||||||||
| 70 | 47 | return( $self ); | |||||||||||||
| 71 | } | ||||||||||||||
| 72 | |||||||||||||||
| 73 | sub add_to | ||||||||||||||
| 74 | 9 | ▶ | { | ||||||||||||
|
Subroutine
add_to: called CC=4 SCAR=14.6
Pod
documented
| |||||||||||||||
| 75 | 9 | my $self = shift( @_ ); | |||||||||||||
| 76 | # no overloading; | ||||||||||||||
| 77 | 9 | ▶ | my $css = shift( @_ ) || return( $self->error( "No css object was provided to add our rule to it." ) ); | ||||||||||||
|
Condition50%1/2
shift @_ || (return $self->error('No css object was provided to add our rule to it.'))
MC/DC0%0/1
shift @_ || (return $self->error('No css object was provided to add our rule to it.'))
shift @_
Truth table50%1/2
A
A= shift @_
| |||||||||||||||
| 78 | # my $caller = ( split( /\::/, (caller(1))[3] ) )[-1]; | ||||||||||||||
| 79 | 9 | ▶ | return( $self->error( "CSS object provided (", overload::StrVal( $css ), ") is not actually a CSS::Object object." ) ) if( !$self->_is_a( $css, 'CSS::Object' ) ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 80 | 9 | ▶ | defined( $css->add_rule( $self ) ) || return( $self->error( "Unable to add our css rule object (", overload::StrVal( $self ), ") to main css object elements stack: ", $css->error ) ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 81 | 9 | return( $self ); | |||||||||||||
| 82 | } | ||||||||||||||
| 83 | |||||||||||||||
| 84 | sub as_string | ||||||||||||||
| 85 | 169 | ▶ | { | ||||||||||||
|
Subroutine
as_string: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||
| 86 | 169 | my $self = shift( @_ ); | |||||||||||||
| 87 | # my( $p, $f, $l ) = caller; | ||||||||||||||
| 88 | 169 | ▶ | my $format = $self->format || return( $self->error( "No formatter set to format this rule as string." ) ); | ||||||||||||
|
Condition50%1/2
$self->format || (return $self->error('No formatter set to format this rule as string.'))
MC/DC0%0/1
$self->format || (return $self->error('No formatter set to format this rule as string.'))
$self->format
Truth table50%1/2
A
A= $self->format
| |||||||||||||||
| 89 | 169 | return( $format->rule_as_string( $self ) ); | |||||||||||||
| 90 | } | ||||||||||||||
| 91 | |||||||||||||||
| 92 | sub comments | ||||||||||||||
| 93 | 1 | ▶ | { | ||||||||||||
|
Subroutine
comments: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 94 | 1 | my $self = shift( @_ ); | |||||||||||||
| 95 | 1 | ▶ | return( $self->elements->map(sub{ $_->isa( 'CSS::Object::Comment' ) ? $_ : () }) ); | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||
| 96 | } | ||||||||||||||
| 97 | 275 | ▶ | sub elements { return( shift->_set_get_object_array_object( 'elements', 'CSS::Object::Element', @_ ) ); } | ||||||||||||
|
Subroutine
elements: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 98 | |||||||||||||||
| 99 | sub elements_as_string | ||||||||||||||
| 100 | 169 | ▶ | { | ||||||||||||
|
Subroutine
elements_as_string: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||
| 101 | 169 | my $self = shift( @_ ); | |||||||||||||
| 102 | 169 | ▶ | my $format = $self->format || return( $self->error( "No format object set to format properties as string." ) ); | ||||||||||||
|
Condition50%1/2
$self->format || (return $self->error('No format object set to format properties as string.'))
MC/DC0%0/1
$self->format || (return $self->error('No format object set to format properties as string.'))
$self->format
Truth table50%1/2
A
A= $self->format
| |||||||||||||||
| 103 | 169 | return( $format->elements_as_string( $self->elements ) ); | |||||||||||||
| 104 | } | ||||||||||||||
| 105 | |||||||||||||||
| 106 | sub format | ||||||||||||||
| 107 | 770 | ▶ | { | ||||||||||||
|
Subroutine
format: called CC=3 SCAR=12.9
Pod
documented
| |||||||||||||||
| 108 | 770 | my $self = shift( @_ ); | |||||||||||||
| 109 | 770 | ▶ | if( @_ ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 110 | { | ||||||||||||||
| 111 | # my( $p, $f, $l ) = caller; | ||||||||||||||
| 112 | 29 | ▶ | my $format = $self->SUPER::format( @_ ) || return( $self->pass_error ); | ||||||||||||
|
Condition50%1/2
$self->SUPER::format(@_) || (return $self->pass_error)
MC/DC0%0/1
$self->SUPER::format(@_) || (return $self->pass_error)
$self->SUPER::format(@_)
Truth table50%1/2
A
A= $self->SUPER::format(@_)
| |||||||||||||||
| 113 | $self->selectors->foreach(sub | ||||||||||||||
| 114 | 0 | ▶ | { | ||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||
| 115 | 0 | ▶ | shift->format( $format ) || return; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 116 | 29 | }); | |||||||||||||
| 117 | $self->properties->foreach(sub | ||||||||||||||
| 118 | 0 | ▶ | { | ||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||
| 119 | 0 | ▶ | shift->format( $format ) || return; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 120 | 29 | }); | |||||||||||||
| 121 | 29 | return( $format ); | |||||||||||||
| 122 | } | ||||||||||||||
| 123 | 741 | return( $self->_set_get_object( 'format', 'CSS::Object::Format' ) ); | |||||||||||||
| 124 | } | ||||||||||||||
| 125 | |||||||||||||||
| 126 | sub get_property_by_name | ||||||||||||||
| 127 | 1 | ▶ | { | ||||||||||||
|
Subroutine
get_property_by_name: called CC=5 SCAR=18.7
Pod
documented
| |||||||||||||||
| 128 | 1 | my( $self, $prop_name ) = @_; | |||||||||||||
| 129 | # my $props = $self->properties; | ||||||||||||||
| 130 | 1 | my $arr = Module::Generic::Array->new; | |||||||||||||
| 131 | $self->elements->foreach(sub | ||||||||||||||
| 132 | 4 | ▶ | { | ||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||
| 133 | 4 | my $elem = shift( @_ ); | |||||||||||||
| 134 | 4 | ▶ | next if( !$elem->isa( 'CSS::Object::Property' ) ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 135 | 4 | ▶ | if( $elem->name eq $prop_name ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 136 | { | ||||||||||||||
| 137 | 1 | $arr->push( $elem ); | |||||||||||||
| 138 | } | ||||||||||||||
| 139 | 1 | }); | |||||||||||||
| 140 | 1 | ▶ | if( Wanted::want( 'OBJECT' ) ) | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 141 | { | ||||||||||||||
| 142 | 0 | ▶ | rreturn( $arr->length > 0 ? $arr->first : Module::Generic::Null->new ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 143 | } | ||||||||||||||
| 144 | elsif( Wanted::want( 'LIST' ) ) | ||||||||||||||
| 145 | { | ||||||||||||||
| 146 | 0 | ▶ | rreturn( $arr->length > 0 ? $arr->list : () ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 147 | } | ||||||||||||||
| 148 | else | ||||||||||||||
| 149 | { | ||||||||||||||
| 150 | 1 | return( $arr->first ); | |||||||||||||
| 151 | } | ||||||||||||||
| 152 | } | ||||||||||||||
| 153 | |||||||||||||||
| 154 | # Obsolete. See elements instead which has a wider scope including also comments | ||||||||||||||
| 155 | # sub properties { return( shift->_set_get_object_array_object( 'properties', 'CSS::Object::Property', @_ ) ); } | ||||||||||||||
| 156 | sub properties | ||||||||||||||
| 157 | 32 | ▶ | { | ||||||||||||
|
Subroutine
properties: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 158 | 32 | my $self = shift( @_ ); | |||||||||||||
| 159 | 32 | ▶ | return( $self->elements->map(sub{ $self->_is_a( $_, 'CSS::Object::Property' ) ? $_ : () }) ); | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||
| 160 | } | ||||||||||||||
| 161 | |||||||||||||||
| 162 | sub properties_as_string | ||||||||||||||
| 163 | 0 | ▶ | { | ||||||||||||
|
Subroutine
properties_as_string: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 164 | 0 | my $self = shift( @_ ); | |||||||||||||
| 165 | 0 | ▶ | my $format = $self->format || return( $self->error( "No formatter set to format properties as string." ) ); | ||||||||||||
|
Condition0%0/2
$self->format || (return $self->error('No formatter set to format properties as string.'))
MC/DC0%0/1
$self->format || (return $self->error('No formatter set to format properties as string.'))
$self->format
Truth table0%0/2
A
A= $self->format
| |||||||||||||||
| 166 | 0 | return( $format->properties_as_string( $self->properties ) ); | |||||||||||||
| 167 | } | ||||||||||||||
| 168 | |||||||||||||||
| 169 | sub remove_from | ||||||||||||||
| 170 | 0 | ▶ | { | ||||||||||||
|
Subroutine
remove_from: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||
| 171 | 0 | my $self = shift( @_ ); | |||||||||||||
| 172 | 0 | ▶ | my $css = shift( @_ ) || return( $self->error( "No css object was provided to remove our rule from it." ) ); | ||||||||||||
|
Condition0%0/2
shift @_ || (return $self->error('No css object was provided to remove our rule from it.'))
MC/DC0%0/1
shift @_ || (return $self->error('No css object was provided to remove our rule from it.'))
shift @_
Truth table0%0/2
A
A= shift @_
| |||||||||||||||
| 173 | # my $caller = ( split( /\::/, (caller(1))[3] ) )[-1]; | ||||||||||||||
| 174 | 0 | ▶ | return( $self->error( "CSS object provided (", overload::StrVal( $css ), ") is not actually a CSS::Object object." ) ) if( !$self->_is_a( $css, 'CSS::Object' ) ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 175 | 0 | $css->remove_rule( $self ); | |||||||||||||
| 176 | 0 | return( $self ); | |||||||||||||
| 177 | } | ||||||||||||||
| 178 | |||||||||||||||
| 179 | sub remove_property | ||||||||||||||
| 180 | 0 | ▶ | { | ||||||||||||
|
Subroutine
remove_property: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 181 | 0 | my $self = shift( @_ ); | |||||||||||||
| 182 | 0 | my $prop = shift( @_ ); | |||||||||||||
| 183 | 0 | ▶ | return( $self->error( "Property object provided ($prop) is not a CSS::Object::Property object." ) ) if( !$self->_is_a( $prop, 'CSS::Object::Property' ) ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 184 | 0 | $self->elements->remove( $prop ); | |||||||||||||
| 185 | 0 | return( $self ); | |||||||||||||
| 186 | } | ||||||||||||||
| 187 | |||||||||||||||
| 188 | # Array of CSS::Object::Selector objects | ||||||||||||||
| 189 | 256 | ▶ | sub selectors { return( shift->_set_get_object_array_object( 'selectors', 'CSS::Object::Property', @_ ) ); } | ||||||||||||
|
Subroutine
selectors: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 190 | |||||||||||||||
| 191 | sub selectors_as_string | ||||||||||||||
| 192 | 167 | ▶ | { | ||||||||||||
|
Subroutine
selectors_as_string: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||
| 193 | 167 | my $self = shift( @_ ); | |||||||||||||
| 194 | 167 | ▶ | my $format = $self->format || return( $self->error( "No formatter set to format selectors as string." ) ); | ||||||||||||
|
Condition50%1/2
$self->format || (return $self->error('No formatter set to format selectors as string.'))
MC/DC0%0/1
$self->format || (return $self->error('No formatter set to format selectors as string.'))
$self->format
Truth table50%1/2
A
A= $self->format
| |||||||||||||||
| 195 | 167 | return( $format->selectors_as_string( $self->selectors ) ); | |||||||||||||
| 196 | } | ||||||||||||||
| 197 | |||||||||||||||
| 198 | 6 | 1; | |||||||||||||
| 199 | # NOTE: POD | ||||||||||||||
| 200 | __END__ | ||||||||||||||