File Coverage

blib/lib/OCBNET/CSS3/Styles/Font.pm
Criterion Covered Total %
statement 15 15 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 20 20 100.0


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             # the font shorthand is one of the most complex ones
6             # I think I could parse them in any possible order, but
7             # current browsers seem to ignore the line when font-family
8             # or font-size are not given. They also must be after any
9             # other properties (like font-weight). Means they are pretty
10             # strict (maybe for a good reason) but not too strict anyway.
11             # This makes it tricky for us to mimic the same behavior here.
12             # IMO it's not possible to mimic every browser, so we try to
13             # get pretty close. Otherwise it's up to the css developers to
14             # write standard compliant code that will be parsed correctly.
15             ####################################################################################################
16             # tests done with firefox v26.0
17             # to "understand" a font shorthand:
18             # both the size and the family must be given
19             # family must be last, which means that
20             # size/line-height must be second to last
21             # no value may contain the keyword inherit
22             # but the complete value may be set to inherit
23             # size fallowed by a slash mush have line-height
24             ####################################################################################################
25             package OCBNET::CSS3::Styles::Font;
26             ####################################################################################################
27             our $VERSION = '0.2.5';
28             ####################################################################################################
29              
30 1     1   3859 use strict;
  1         3  
  1         43  
31 1     1   6 use warnings;
  1         2  
  1         30  
32              
33             ####################################################################################################
34              
35 1     1   6 use OCBNET::CSS3::Regex::Base;
  1         3  
  1         134  
36 1     1   5 use OCBNET::CSS3::Regex::Colors;
  1         3  
  1         72  
37 1     1   5 use OCBNET::CSS3::Regex::Numbers;
  1         3  
  1         439  
38              
39             ####################################################################################################
40             # register longhand properties for fonts
41             ####################################################################################################
42              
43             OCBNET::CSS3::Styles::register('font-style', qr/(?:normal|italic|oblique)/is, 'normal');
44             OCBNET::CSS3::Styles::register('font-variant', qr/(?:normal|small-caps)/is, 'normal');
45             OCBNET::CSS3::Styles::register('font-weight', qr/(?:normal|bold|bolder|lighter|[1-9]00)/is, 'normal');
46             OCBNET::CSS3::Styles::register('font-size', qr/(?:$re_length|$re_percent|smaller|larger|(?:xx?-)?(?:small|large))/is, 'inherit');
47             OCBNET::CSS3::Styles::register('line-height', qr/(?:$re_length|$re_percent)/is, 'normal');
48             OCBNET::CSS3::Styles::register('font-family', qr/(?:$re_string)(?:\s*,\s*$re_string)*/is, 'inherit');
49              
50             ####################################################################################################
51              
52             # register shorthand property for font
53             OCBNET::CSS3::Styles::register('font',
54             {
55             # optional styes
56             # prefered order
57             'prefix' =>
58             [
59             'font-style',
60             'font-variant',
61             'font-weight'
62             ],
63             # needed in order
64             'ordered' =>
65             [
66             # always needed
67             [ 'font-size' ],
68             # next bit is optional, but
69             # must have a forward slash
70             [
71             # attention, logic order here is reversed
72             # this way we have the css name always first in the array
73             # but this actually means that the regex must match first
74             'line-height',
75             # value gets mandatory if we find a forwards slash
76             qr/\s*\/\s*/
77             ],
78             # always needed
79             [ 'font-family' ]
80             ]
81             });
82              
83             ####################################################################################################
84             ####################################################################################################
85             1;