@@ -13,11 +13,30 @@ class Processor
13
13
* @param string $rulesString
14
14
* @return array
15
15
*/
16
- public function splitIntoSeparateRules ($ rulesString )
16
+ public function splitIntoSeparateMediaQueries ($ rulesString )
17
17
{
18
18
$ rulesString = $ this ->cleanup ($ rulesString );
19
19
20
- return (array )explode ('} ' , $ rulesString );
20
+ // Intelligently break up rules, preserving mediaquery context and such
21
+ $ queryParts = explode ('@media ' , $ rulesString );
22
+
23
+ $ indexedRules = [];
24
+
25
+ $ first = true ;
26
+ foreach ($ queryParts as $ part ) {
27
+ if ($ first ) {
28
+ $ first = false ;
29
+ $ indexedRules ['' ] = (array )explode ('} ' , $ part );
30
+ continue ;
31
+ }
32
+
33
+ $ mediaQueryString = "@media " . substr ($ part , 0 , strpos ($ part , '{ ' ));
34
+ $ mediaQueryRules = substr ($ part , strpos ($ part , '{ ' ) + 1 );
35
+ $ mediaQueryRules = substr ($ mediaQueryRules , 0 , -1 );
36
+ $ indexedRules [$ mediaQueryString ] = (array )explode ('} ' , $ mediaQueryRules );
37
+ }
38
+
39
+ return $ indexedRules ;
21
40
}
22
41
23
42
/**
@@ -33,7 +52,6 @@ private function cleanup($string)
33
52
$ string = preg_replace ('/\s\s+/ ' , ' ' , $ string );
34
53
35
54
$ string = trim ($ string );
36
- $ string = rtrim ($ string , '} ' );
37
55
38
56
return $ string ;
39
57
}
@@ -45,27 +63,20 @@ private function cleanup($string)
45
63
* @param int $originalOrder
46
64
* @return array
47
65
*/
48
- public function convertToObjects ($ rule , $ originalOrder )
66
+ public function convertToObjects ($ media , $ rule , $ originalOrder )
49
67
{
50
68
$ rule = $ this ->cleanup ($ rule );
51
69
52
70
$ chunks = explode ('{ ' , $ rule );
53
71
54
72
$ selectorIdentifier = 0 ;
55
73
$ ruleIdentifier = 1 ;
56
- $ media = '' ;
57
-
58
- if (count ($ chunks ) == 3 ) {
59
- $ selectorIdentifier ++;
60
- $ ruleIdentifier ++;
61
- $ media = $ chunks [0 ];
62
- }
63
74
64
75
if (!isset ($ chunks [$ ruleIdentifier ])) {
65
- return array () ;
76
+ return [] ;
66
77
}
67
78
$ propertiesProcessor = new PropertyProcessor ();
68
- $ rules = array () ;
79
+ $ rules = [] ;
69
80
$ selectors = (array )explode (', ' , trim ($ chunks [$ selectorIdentifier ]));
70
81
$ properties = $ propertiesProcessor ->splitIntoSeparateProperties ($ chunks [$ ruleIdentifier ]);
71
82
@@ -133,12 +144,15 @@ public function calculateSpecificityBasedOnASelector($selector)
133
144
* @param array $rules
134
145
* @return Rule[]
135
146
*/
136
- public function convertArrayToObjects (array $ rules , array $ objects = array ())
147
+ public function convertArrayToObjects (array $ mediaQueryRules , array $ objects = array ())
137
148
{
138
- $ order = 1 ;
139
- foreach ($ rules as $ rule ) {
140
- $ objects = array_merge ($ objects , $ this ->convertToObjects ($ rule , $ order ));
141
- $ order ++;
149
+
150
+ foreach ($ mediaQueryRules as $ media => $ rules ) {
151
+ $ order = 1 ;
152
+ foreach ($ rules as $ rule ) {
153
+ $ objects = array_merge ($ objects , $ this ->convertToObjects ($ media , $ rule , $ order ));
154
+ $ order ++;
155
+ }
142
156
}
143
157
144
158
return $ objects ;
0 commit comments