@@ -60,8 +60,21 @@ impl Ord for StyleSelector {
6060
6161impl From < & str > for StyleSelector {
6262 fn from ( value : & str ) -> Self {
63- if let Some ( s) = value. strip_prefix ( "group" ) {
64- Dual ( "*[role=group]" . to_string ( ) , to_kebab_case ( s) )
63+ if value. contains ( ":" ) {
64+ let t: Vec < _ > = value. split ( ":" ) . collect ( ) ;
65+ if let Prefix ( v) = t[ 0 ] . into ( ) {
66+ Dual ( v, t[ 1 ] . to_string ( ) )
67+ } else {
68+ Postfix ( t[ 1 ] . to_string ( ) )
69+ }
70+ } else if let Some ( s) = value. strip_prefix ( "group" ) {
71+ let post = to_kebab_case ( s) ;
72+ Prefix ( format ! (
73+ "{}{}{}" ,
74+ "*[role=group]" ,
75+ get_selector_separator( & post) ,
76+ post
77+ ) )
6578 } else if let Some ( s) = value. strip_prefix ( "theme" ) {
6679 // first character should lower case
6780 Prefix ( format ! (
@@ -95,15 +108,20 @@ impl Display for StyleSelector {
95108pub fn merge_selector ( class_name : & str , selector : Option < & StyleSelector > ) -> String {
96109 if let Some ( selector) = selector {
97110 match selector {
98- Postfix ( postfix) => match get_selector_separator ( postfix) {
99- SelectorSeparator :: Single => format ! ( ".{}:{}" , class_name, postfix) ,
100- SelectorSeparator :: Double => format ! ( ".{}::{}" , class_name, postfix) ,
101- } ,
111+ Postfix ( postfix) => format ! (
112+ ".{}{}{}" ,
113+ class_name,
114+ get_selector_separator( postfix) ,
115+ postfix
116+ ) ,
102117 Prefix ( prefix) => format ! ( "{} .{}" , prefix, class_name) ,
103- Dual ( prefix, postfix) => match get_selector_separator ( postfix) {
104- SelectorSeparator :: Single => format ! ( "{}:{} .{}" , prefix, postfix, class_name) ,
105- SelectorSeparator :: Double => format ! ( "{}::{} .{}" , prefix, postfix, class_name) ,
106- } ,
118+ Dual ( prefix, postfix) => format ! (
119+ "{} .{}{}{}" ,
120+ prefix,
121+ class_name,
122+ get_selector_separator( postfix) ,
123+ postfix
124+ ) ,
107125 Media ( _) => format ! ( ".{}" , class_name) ,
108126 }
109127 } else {
@@ -115,6 +133,18 @@ pub enum SelectorSeparator {
115133 Single ,
116134 Double ,
117135}
136+ impl Display for SelectorSeparator {
137+ fn fmt ( & self , f : & mut Formatter ) -> fmt:: Result {
138+ write ! (
139+ f,
140+ "{}" ,
141+ match self {
142+ SelectorSeparator :: Single => ":" ,
143+ SelectorSeparator :: Double => "::" ,
144+ }
145+ )
146+ }
147+ }
118148
119149static DOUBLE_SEPARATOR : Lazy < HashSet < & str > > = Lazy :: new ( || {
120150 let mut set = HashSet :: new ( ) ;
@@ -627,15 +657,23 @@ mod tests {
627657 ) ;
628658 assert_eq ! (
629659 StyleSelector :: from( "groupHover" ) ,
630- Dual ( "*[role=group]" . to_string ( ) , " hover". to_string( ) )
660+ Prefix ( "*[role=group]: hover" . to_string( ) )
631661 ) ;
632662 assert_eq ! (
633663 StyleSelector :: from( "groupFocusVisible" ) ,
634- Dual ( "*[role=group]" . to_string ( ) , " focus-visible". to_string( ) )
664+ Prefix ( "*[role=group]: focus-visible" . to_string( ) )
635665 ) ;
636666 assert_eq ! (
637667 StyleSelector :: from( "group1" ) ,
638- Dual ( "*[role=group]" . to_string( ) , "1" . to_string( ) )
668+ Prefix ( "*[role=group]:1" . to_string( ) )
669+ ) ;
670+
671+ assert_eq ! (
672+ StyleSelector :: from( "themeDark:placeholder" ) ,
673+ Dual (
674+ ":root[data-theme=dark]" . to_string( ) ,
675+ "placeholder" . to_string( )
676+ )
639677 ) ;
640678
641679 assert_eq ! ( Prefix ( ".cls" . to_string( ) ) . to_string( ) , "-.cls-" ) ;
@@ -661,22 +699,21 @@ mod tests {
661699 assert_eq ! (
662700 merge_selector(
663701 "cls" ,
664- Some ( & Dual (
665- ":root[data-theme=dark]" . to_string( ) ,
666- "hover" . to_string( )
667- ) ) ,
702+ Some ( & Prefix ( ":root[data-theme=dark]:hover" . to_string( ) , ) ) ,
668703 ) ,
669704 ":root[data-theme=dark]:hover .cls"
670705 ) ;
671706 assert_eq ! (
672707 merge_selector(
673708 "cls" ,
674- Some ( & Dual (
675- ":root[data-theme=dark]" . to_string( ) ,
676- "placeholder" . to_string( )
677- ) ) ,
709+ Some ( & Prefix ( ":root[data-theme=dark]::placeholder" . to_string( ) ) ) ,
678710 ) ,
679711 ":root[data-theme=dark]::placeholder .cls"
680712 ) ;
713+
714+ assert_eq ! (
715+ merge_selector( "cls" , Some ( & "themeDark:hover" . into( ) ) , ) ,
716+ ":root[data-theme=dark] .cls:hover"
717+ ) ;
681718 }
682719}
0 commit comments