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