@@ -748,7 +748,7 @@ impl ToCssWithContext for parcel_selectors::parser::Selector<Selectors> {
748748 // Iterate over everything so we serialize the namespace
749749 // too.
750750 let mut iter = compound. iter ( ) ;
751- let swap_nesting = has_leading_nesting && context. is_some ( ) && is_type_selector ( compound . get ( first_index ) ) ;
751+ let swap_nesting = has_leading_nesting && context. is_some ( ) ;
752752 if swap_nesting {
753753 // Swap nesting and type selector (e.g. &div -> div&).
754754 iter. next ( ) ;
@@ -780,15 +780,15 @@ impl ToCssWithContext for parcel_selectors::parser::Selector<Selectors> {
780780 // following code tries to match.
781781 if perform_step_2 {
782782 let mut iter = compound. iter ( ) ;
783- if has_leading_nesting && context. is_some ( ) && is_type_selector ( compound. get ( first_index ) ) {
783+ if has_leading_nesting && context. is_some ( ) && is_type_selector ( compound. get ( first_non_namespace ) ) {
784784 // Swap nesting and type selector (e.g. &div -> div&).
785785 // This ensures that the compiled selector is valid. e.g. (div.foo is valid, .foodiv is not).
786786 let nesting = iter. next ( ) . unwrap ( ) ;
787787 let local = iter. next ( ) . unwrap ( ) ;
788788 local. to_css_with_context ( dest, context) ?;
789789
790790 // Also check the next item in case of namespaces.
791- if is_type_selector ( compound . get ( first_index + 1 ) ) {
791+ if first_non_namespace > first_index {
792792 let local = iter. next ( ) . unwrap ( ) ;
793793 local. to_css_with_context ( dest, context) ?;
794794 }
@@ -918,11 +918,11 @@ impl ToCssWithContext for Component<Selectors> {
918918
919919fn serialize_nesting < W > ( dest : & mut Printer < W > , context : Option < & StyleContext > , first : bool ) -> Result < ( ) , PrinterError > where W : fmt:: Write {
920920 if let Some ( ctx) = context {
921- // If there's only one selector, just serialize it directly.
921+ // If there's only one simple selector, just serialize it directly.
922922 // Otherwise, use an :is() pseudo class.
923923 // Type selectors are only allowed at the start of a compound selector,
924924 // so use :is() if that is not the case.
925- if ctx. rule . selectors . 0 . len ( ) == 1 && ( first || !has_type_selector ( & ctx. rule . selectors . 0 [ 0 ] ) ) {
925+ if ctx. rule . selectors . 0 . len ( ) == 1 && ( first || ( !has_type_selector ( & ctx. rule . selectors . 0 [ 0 ] ) && is_simple ( & ctx . rule . selectors . 0 [ 0 ] ) ) ) {
926926 ctx. rule . selectors . 0 . first ( ) . unwrap ( ) . to_css_with_context ( dest, ctx. parent )
927927 } else {
928928 dest. write_str ( ":is(" ) ?;
@@ -934,21 +934,41 @@ fn serialize_nesting<W>(dest: &mut Printer<W>, context: Option<&StyleContext>, f
934934 }
935935}
936936
937+ #[ inline]
937938fn has_type_selector ( selector : & parcel_selectors:: parser:: Selector < Selectors > ) -> bool {
938939 let mut iter = selector. iter_raw_parse_order_from ( 0 ) ;
939940 let first = iter. next ( ) ;
940- is_type_selector ( first)
941+ if is_namespace ( first) {
942+ is_type_selector ( iter. next ( ) )
943+ } else {
944+ is_type_selector ( first)
945+ }
946+ }
947+
948+ #[ inline]
949+ fn is_simple ( selector : & parcel_selectors:: parser:: Selector < Selectors > ) -> bool {
950+ !selector
951+ . iter_raw_match_order ( )
952+ . any ( |component| component. is_combinator ( ) )
941953}
942954
955+ #[ inline]
943956fn is_type_selector ( component : Option < & Component < Selectors > > ) -> bool {
957+ matches ! (
958+ component,
959+ Some ( Component :: LocalName ( _) ) |
960+ Some ( Component :: ExplicitUniversalType )
961+ )
962+ }
963+
964+ #[ inline]
965+ fn is_namespace ( component : Option < & Component < Selectors > > ) -> bool {
944966 matches ! (
945967 component,
946968 Some ( Component :: ExplicitAnyNamespace ) |
947969 Some ( Component :: ExplicitNoNamespace ) |
948970 Some ( Component :: Namespace ( ..) ) |
949- Some ( Component :: DefaultNamespace ( _) ) |
950- Some ( Component :: LocalName ( _) ) |
951- Some ( Component :: ExplicitUniversalType )
971+ Some ( Component :: DefaultNamespace ( _) )
952972 )
953973}
954974
0 commit comments