File Coverage

blib/lib/OCBNET/CSS3/Regex/Numbers.pm
Criterion Covered Total %
statement 16 16 100.0
branch 5 6 83.3
condition n/a
subroutine 7 7 100.0
pod 0 2 0.0
total 28 31 90.3


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             package OCBNET::CSS3::Regex::Numbers;
6             ####################################################################################################
7             our $VERSION = '0.2.5';
8             ####################################################################################################
9              
10 12     12   5594 use strict;
  12         23  
  12         356  
11 12     12   54 use warnings;
  12         19  
  12         445  
12              
13             ####################################################################################################
14              
15             # load exporter and inherit from it
16 12     12   63 BEGIN { use Exporter qw(); our @ISA = qw(Exporter); }
  12     12   20  
  12         298  
  12         577  
17              
18             # define our functions that will be exported
19 12     12   3684 BEGIN { our @EXPORT = qw($re_number $re_percent $re_size $re_length $re_byte fromPx toPx); }
20              
21             ####################################################################################################
22             # base regular expressions
23             ####################################################################################################
24              
25             # match (floating point) numbers
26             #**************************************************************************************************
27             our $re_number = qr/[\-\+]?[0-9]*\.?[0-9]+/s;
28             # our $re_number_neg = qr/\-[0-9]*\.?[0-9]+/s;
29             # our $re_number_pos = qr/\+?[0-9]*\.?[0-9]+/s;
30              
31             # regular expression to match a percent property
32             #**************************************************************************************************
33             our $re_percent = qr/$re_number(?:\%)/i;
34              
35             # regular expression to match a size property
36             #**************************************************************************************************
37             our $re_size = qr/$re_number(?:em|ex|px|in|cm|mm|pt|pc)/i;
38              
39             # regular expression to match any length property
40             #**************************************************************************************************
41             our $re_length = qr/$re_number(?:em|ex|px|\%|in|cm|mm|pt|pc)?/i;
42              
43             # match a octal number from 0 to 255 (strict match)
44             #**************************************************************************************************
45             our $re_byte = qr/(?:0|[1-9]\d?|1\d{2}|2(?:[0-4]\d|5[0-5]))/s;
46              
47             ####################################################################################################
48              
49             # parse dimension from pixel
50             #**************************************************************************************************
51             sub fromPx
52             {
53             # return undef if nothing passed
54 13 50   13 0 52 return unless defined $_[0];
55             # parse number via regular expression (pretty strict)
56 13 100       265 $_[0] =~ m/\A\s*($re_number)(?:px)?\s*\z/i ? $1 : undef;
57             }
58              
59             # adds pixel unit
60             #**************************************************************************************************
61             sub toPx
62             {
63             # parse via fromPx
64 5     5 0 18 my $px = fromPx($_[0]);
65             # check if input was valid
66 5 100       20 return undef unless defined $px;
67             # format correctly
68 4         65 sprintf "%gpx", $px
69             }
70              
71             ####################################################################################################
72             ####################################################################################################
73             1;