File Coverage

blib/lib/OCBNET/CSS3/DOM/Block.pm
Criterion Covered Total %
statement 42 59 71.1
branch 15 26 57.6
condition 4 7 57.1
subroutine 12 14 85.7
pod 0 10 0.0
total 73 116 62.9


line stmt bran cond sub pod time code
1             ###################################################################################################
2             # Copyright 2013/2014 by Marcel Greter
3             # This file is part of OCBNET-CSS3 (GPL3)
4             ####################################################################################################
5             # a css3 object with styles and options
6             ####################################################################################################
7             package OCBNET::CSS3::DOM::Block;
8             ####################################################################################################
9             our $VERSION = '0.2.5';
10             ####################################################################################################
11              
12 11     11   60 use strict;
  11         19  
  11         444  
13 11     11   51 use warnings;
  11         17  
  11         265  
14              
15             ####################################################################################################
16 11     11   50 use base 'OCBNET::CSS3';
  11         17  
  11         1283  
17 11     11   6558 use OCBNET::CSS3::Styles;
  11         27  
  11         8153  
18             ####################################################################################################
19              
20             # create a new object
21             # ***************************************************************************************
22             sub new
23             {
24              
25             # package name
26 105     105 0 4952 my ($pckg) = shift;
27              
28             # create a new instance
29 105         464 my $self = $pckg->SUPER::new;
30              
31             # store only longhands
32 105         416 $self->{'style'} = OCBNET::CSS3::Styles->new($self);
33 105         328 $self->{'option'} = OCBNET::CSS3::Styles->new($self);
34              
35             # instance
36 105         253 return $self;
37              
38             }
39             # EO constructor
40              
41             ####################################################################################################
42              
43             # static getter
44             # always overwrite this
45             #**************************************************************************************************
46 1     1 0 15 sub type { die 'not implemented' }
47              
48             # static getters
49             #**************************************************************************************************
50 110     110 0 429 sub styles { $_[0]->{'style'} }
51 210     210 0 829 sub options { $_[0]->{'option'} }
52              
53             ####################################################################################################
54              
55             # getter with recursive logic
56             # can reference ids in options
57             # try to load styles from there
58             #**************************************************************************************************
59             sub get
60             {
61              
62             # get input arguments
63 264     264 0 502 my ($self, $type, $key, $idx) = @_;
64              
65             # try to get/call registered getter function for key
66 264         373 my $getter = $OCBNET::CSS3::Styles::getter{$key};
67 264 100       596 return $getter->($self, $type, $key, $idx) if defined $getter;
68              
69             # check if found in current styles
70 258 100 100     1427 if (exists $self->{$type}->{$key}->[$idx || 0])
71 197   100     1576 { return $self->{$type}->{$key}->[$idx || 0]; }
72              
73             # do not go recursive on certain keys
74 61 50       151 return undef if $key eq 'css-ref';
75 61 50       127 return undef if $key eq 'css-id';
76              
77             # find the node that has the key
78 61         181 my $node = $self->find($type, $key);
79              
80             # return if nothing found
81 61 100       168 return undef unless $node;
82              
83             # return results from getter
84 54         151 $node->get($type, $key, $idx);
85              
86             }
87             # EO sub get
88              
89             ####################################################################################################
90              
91             # getters for styles and options
92             #**************************************************************************************************
93 192     192 0 447 sub style { get(shift, 'style', @_) }
94 12     12 0 31 sub option { get(shift, 'option', @_) }
95              
96             ####################################################################################################
97              
98             # getter with recursive logic
99             # can reference ids in options
100             # try to load options from there
101             #**************************************************************************************************
102             sub find
103             {
104              
105             # get input arguments
106 126     126 0 178 my ($self, $type, $key) = @_;
107              
108             # check if found in current styles
109 126 100       345 if (exists $self->{$type}->{$key})
110 123 100       123 { return $self if scalar(@{$self->{$type}->{$key}}) }
  123         546  
111              
112             # do not go recursive on certain keys
113             # return undef if $key eq 'css-ref';
114             # return undef if $key eq 'css-id';
115              
116             # check each css references for given key
117 72         186 foreach my $id ($self->options->list('css-ref'))
118             {
119             # get the actual referenced dom node
120 65         190 my $ref = $self->root->{'ids'}->{$id};
121             # give error message if reference was not found
122 65 50       171 die "referenced id <$id> not found" unless $ref;
123             # resolve value on referenced block
124             # will itself try to resolve further
125 65         225 my $result = $ref->find($type, $key);
126             # only return if result is defined
127 65 100       192 return $result if defined $result;
128             }
129              
130             # nothing found
131 10         20 return undef;
132              
133             }
134             # EO sub option
135              
136             ####################################################################################################
137              
138             # helper to check if we implement a certain class
139             #**************************************************************************************************
140 0     0 0   sub isa { shift->SUPER::isa(map { 'OCBNET::CSS3::' . $_ } @_) }
  0            
141              
142             ####################################################################################################
143              
144             # remove certain styles
145             #**************************************************************************************************
146             sub clean
147             {
148              
149             # get selector and regex
150             # remove styles found by regex
151 0     0 0   my ($selector, $regexp) = @_;
152              
153             # define default expression to clean all
154 0 0         $regexp = qr// unless defined $regexp;
155              
156             # remove options
157 0           foreach my $key (keys %{$selector->{'option'}})
  0            
158             {
159 0 0         next unless $key =~ m/^\s*$regexp/is;
160 0           delete $selector->{'option'}->{$key};
161             }
162              
163             # remove styles
164 0           foreach my $key (keys %{$selector->{'style'}})
  0            
165             {
166 0 0         next unless $key =~ m/^\s*$regexp/is;
167 0           delete $selector->{'style'}->{$key};
168             }
169              
170             # define default expression to clean all
171 0 0         $regexp = qr// unless defined $regexp;
172              
173             # remove all background declarations now
174 0   0       @{$selector->{'children'}} = grep {
  0            
175 0           not ($_->{'key'} && $_->{'key'} =~ m/^\s*$regexp/is)
176 0           } @{$selector->{'children'}};
177              
178              
179             }
180             # EO sub clean
181              
182             ####################################################################################################
183             ####################################################################################################
184             1;