@@ -13,11 +13,30 @@ class Processor
1313 * @param string $rulesString
1414 * @return array
1515 */
16- public function splitIntoSeparateRules ($ rulesString )
16+ public function splitIntoSeparateMediaQueries ($ rulesString )
1717 {
1818 $ rulesString = $ this ->cleanup ($ rulesString );
1919
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 ;
2140 }
2241
2342 /**
@@ -33,7 +52,6 @@ private function cleanup($string)
3352 $ string = preg_replace ('/\s\s+/ ' , ' ' , $ string );
3453
3554 $ string = trim ($ string );
36- $ string = rtrim ($ string , '} ' );
3755
3856 return $ string ;
3957 }
@@ -45,27 +63,20 @@ private function cleanup($string)
4563 * @param int $originalOrder
4664 * @return array
4765 */
48- public function convertToObjects ($ rule , $ originalOrder )
66+ public function convertToObjects ($ media , $ rule , $ originalOrder )
4967 {
5068 $ rule = $ this ->cleanup ($ rule );
5169
5270 $ chunks = explode ('{ ' , $ rule );
5371
5472 $ selectorIdentifier = 0 ;
5573 $ ruleIdentifier = 1 ;
56- $ media = '' ;
57-
58- if (count ($ chunks ) == 3 ) {
59- $ selectorIdentifier ++;
60- $ ruleIdentifier ++;
61- $ media = $ chunks [0 ];
62- }
6374
6475 if (!isset ($ chunks [$ ruleIdentifier ])) {
65- return array () ;
76+ return [] ;
6677 }
6778 $ propertiesProcessor = new PropertyProcessor ();
68- $ rules = array () ;
79+ $ rules = [] ;
6980 $ selectors = (array )explode (', ' , trim ($ chunks [$ selectorIdentifier ]));
7081 $ properties = $ propertiesProcessor ->splitIntoSeparateProperties ($ chunks [$ ruleIdentifier ]);
7182
@@ -133,12 +144,15 @@ public function calculateSpecificityBasedOnASelector($selector)
133144 * @param array $rules
134145 * @return Rule[]
135146 */
136- public function convertArrayToObjects (array $ rules , array $ objects = array ())
147+ public function convertArrayToObjects (array $ mediaQueryRules , array $ objects = array ())
137148 {
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+ }
142156 }
143157
144158 return $ objects ;
0 commit comments