@@ -9,7 +9,10 @@ use crate::{constant::SELECTOR_ORDER_MAP, selector_separator::SelectorSeparator,
99
1010#[ derive( Debug , PartialEq , Clone , Hash , Eq , Serialize , Deserialize ) ]
1111pub enum StyleSelector {
12- Media ( String ) ,
12+ Media {
13+ query : String ,
14+ selector : Option < String > ,
15+ } ,
1316 Selector ( String ) ,
1417 // selector, file
1518 Global ( String , String ) ,
@@ -23,12 +26,36 @@ impl PartialOrd for StyleSelector {
2326impl Ord for StyleSelector {
2427 fn cmp ( & self , other : & Self ) -> Ordering {
2528 match ( self , other) {
26- ( StyleSelector :: Media ( a) , StyleSelector :: Media ( b) ) => a. cmp ( b) ,
29+ (
30+ StyleSelector :: Media {
31+ query : a,
32+ selector : aa,
33+ } ,
34+ StyleSelector :: Media {
35+ query : b,
36+ selector : bb,
37+ } ,
38+ ) => {
39+ let c = a. cmp ( b) ;
40+ if c == Ordering :: Equal { aa. cmp ( bb) } else { c }
41+ }
2742 ( StyleSelector :: Selector ( a) , StyleSelector :: Selector ( b) ) => {
2843 get_selector_order ( a) . cmp ( & get_selector_order ( b) )
2944 }
30- ( StyleSelector :: Media ( _) , StyleSelector :: Selector ( _) ) => Ordering :: Greater ,
31- ( StyleSelector :: Selector ( _) , StyleSelector :: Media ( _) ) => Ordering :: Less ,
45+ (
46+ StyleSelector :: Media {
47+ selector : _,
48+ query : _,
49+ } ,
50+ StyleSelector :: Selector ( _) ,
51+ ) => Ordering :: Greater ,
52+ (
53+ StyleSelector :: Selector ( _) ,
54+ StyleSelector :: Media {
55+ selector : _,
56+ query : _,
57+ } ,
58+ ) => Ordering :: Less ,
3259 ( StyleSelector :: Global ( a, _) , StyleSelector :: Global ( b, _) ) => {
3360 if a == b {
3461 return Ordering :: Equal ;
@@ -84,7 +111,10 @@ impl From<&str> for StyleSelector {
84111 & s[ 1 ..]
85112 ) )
86113 } else if value == "print" {
87- StyleSelector :: Media ( "print" . to_string ( ) )
114+ StyleSelector :: Media {
115+ query : "print" . to_string ( ) ,
116+ selector : None ,
117+ }
88118 } else {
89119 let post = to_kebab_case ( value) ;
90120
@@ -134,7 +164,13 @@ impl Display for StyleSelector {
134164 "{}" ,
135165 match self {
136166 StyleSelector :: Selector ( value) => value. to_string( ) ,
137- StyleSelector :: Media ( value) => format!( "@{value}" ) ,
167+ StyleSelector :: Media { query, selector } => {
168+ if let Some ( selector) = selector {
169+ format!( "@{query} {selector}" )
170+ } else {
171+ format!( "@{query}" )
172+ }
173+ }
138174 StyleSelector :: Global ( value, _) => format!( "{value}" ) ,
139175 }
140176 )
@@ -182,7 +218,12 @@ mod tests {
182218
183219 #[ rstest]
184220 #[ case( StyleSelector :: Selector ( "&:hover" . to_string( ) ) , "&:hover" ) ]
185- #[ case( StyleSelector :: Media ( "screen and (max-width: 600px)" . to_string( ) ) , "@screen and (max-width: 600px)" ) ]
221+ #[ case( StyleSelector :: Media {
222+ query: "screen and (max-width: 600px)" . to_string( ) ,
223+ selector: None ,
224+ } ,
225+ "@screen and (max-width: 600px)"
226+ ) ]
186227 #[ case( StyleSelector :: Global ( ":root[data-theme=dark]" . to_string( ) , "file.rs" . to_string( ) ) , ":root[data-theme=dark]" ) ]
187228 fn test_style_selector_display ( #[ case] selector : StyleSelector , #[ case] expected : & str ) {
188229 let output = format ! ( "{selector}" ) ;
@@ -191,7 +232,10 @@ mod tests {
191232
192233 #[ rstest]
193234 #[ case(
194- StyleSelector :: Media ( "screen" . to_string( ) ) ,
235+ StyleSelector :: Media {
236+ query: "screen" . to_string( ) ,
237+ selector: None ,
238+ } ,
195239 StyleSelector :: Selector ( "&:hover" . to_string( ) ) ,
196240 std:: cmp:: Ordering :: Greater
197241 ) ]
@@ -201,8 +245,14 @@ mod tests {
201245 std:: cmp:: Ordering :: Less
202246 ) ]
203247 #[ case(
204- StyleSelector :: Media ( "a" . to_string( ) ) ,
205- StyleSelector :: Media ( "b" . to_string( ) ) ,
248+ StyleSelector :: Media {
249+ query: "a" . to_string( ) ,
250+ selector: None ,
251+ } ,
252+ StyleSelector :: Media {
253+ query: "b" . to_string( ) ,
254+ selector: None ,
255+ } ,
206256 std:: cmp:: Ordering :: Less
207257 ) ]
208258 #[ case(
@@ -217,7 +267,10 @@ mod tests {
217267 ) ]
218268 #[ case(
219269 StyleSelector :: Selector ( "&:hover" . to_string( ) ) ,
220- StyleSelector :: Media ( "screen" . to_string( ) ) ,
270+ StyleSelector :: Media {
271+ query: "screen" . to_string( ) ,
272+ selector: None ,
273+ } ,
221274 std:: cmp:: Ordering :: Less
222275 ) ]
223276 #[ case(
0 commit comments