Processor.php
4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
namespace TijsVerkoyen\CssToInlineStyles\Css\Rule;
use Symfony\Component\CssSelector\Node\Specificity;
use \TijsVerkoyen\CssToInlineStyles\Css\Property\Processor as PropertyProcessor;
class Processor
{
/**
* Split a string into seperate rules
*
* @param string $rulesString
* @return array
*/
public function splitIntoSeparateRules($rulesString)
{
$rulesString = $this->cleanup($rulesString);
return (array) explode('}', $rulesString);
}
/**
* @param string $string
* @return string
*/
private function cleanup($string)
{
$string = str_replace(array("\r", "\n"), '', $string);
$string = str_replace(array("\t"), ' ', $string);
$string = str_replace('"', '\'', $string);
$string = preg_replace('|/\*.*?\*/|', '', $string);
$string = preg_replace('/\s\s+/', ' ', $string);
$string = trim($string);
$string = rtrim($string, '}');
return $string;
}
/**
* Convert a rule-string into an object
*
* @param string $rule
* @param int $originalOrder
* @return array
*/
public function convertToObjects($rule, $originalOrder)
{
$rule = $this->cleanup($rule);
$chunks = explode('{', $rule);
if (!isset($chunks[1])) {
return array();
}
$propertiesProcessor = new PropertyProcessor();
$rules = array();
$selectors = (array) explode(',', trim($chunks[0]));
$properties = $propertiesProcessor->splitIntoSeparateProperties($chunks[1]);
foreach ($selectors as $selector) {
$selector = trim($selector);
$specificity = $this->calculateSpecificityBasedOnASelector($selector);
$rules[] = new Rule(
$selector,
$propertiesProcessor->convertArrayToObjects($properties, $specificity),
$specificity,
$originalOrder
);
}
return $rules;
}
/**
* Calculate the specificity based on a CSS Selector string,
* Based on the patterns from premailer/css_parser by Alex Dunae
*
* @see https://github.com/premailer/css_parser/blob/master/lib/css_parser/regexps.rb
* @param string $selector
* @return Specificity
*/
public function calculateSpecificityBasedOnASelector($selector)
{
$idSelectorsPattern = " \#";
$classAttributesPseudoClassesSelectorsPattern = " (\.[\w]+) # classes
|
\[(\w+) # attributes
|
(\:( # pseudo classes
link|visited|active
|hover|focus
|lang
|target
|enabled|disabled|checked|indeterminate
|root
|nth-child|nth-last-child|nth-of-type|nth-last-of-type
|first-child|last-child|first-of-type|last-of-type
|only-child|only-of-type
|empty|contains
))";
$typePseudoElementsSelectorPattern = " ((^|[\s\+\>\~]+)[\w]+ # elements
|
\:{1,2}( # pseudo-elements
after|before
|first-letter|first-line
|selection
)
)";
return new Specificity(
preg_match_all("/{$idSelectorsPattern}/ix", $selector, $matches),
preg_match_all("/{$classAttributesPseudoClassesSelectorsPattern}/ix", $selector, $matches),
preg_match_all("/{$typePseudoElementsSelectorPattern}/ix", $selector, $matches)
);
}
/**
* @param array $rules
* @return Rule[]
*/
public function convertArrayToObjects(array $rules, array $objects = array())
{
$order = 1;
foreach ($rules as $rule) {
$objects = array_merge($objects, $this->convertToObjects($rule, $order));
$order++;
}
return $objects;
}
/**
* Sort an array on the specificity element in an ascending way
* Lower specificity will be sorted to the beginning of the array
*
* @return int
* @param Rule $e1 The first element.
* @param Rule $e2 The second element.
*/
public static function sortOnSpecificity(Rule $e1, Rule $e2)
{
$e1Specificity = $e1->getSpecificity();
$value = $e1Specificity->compareTo($e2->getSpecificity());
// if the specificity is the same, use the order in which the element appeared
if ($value === 0) {
$value = $e1->getOrder() - $e2->getOrder();
}
return $value;
}
}