@@ -3,7 +3,7 @@ import splitSelectors from "./split-selector";
33import { SelectorWithStyles } from "./types" ;
44import { getDefaultStyles } from "./default-styles" ;
55const pseudoElementRegex =
6- / : : ? ( b e f o r e | a f t e r | f i r s t - l e t t e r | f i r s t - l i n e | s e l e c t i o n | b a c k d r o p | p l a c e h o l d e r | m a r k e r | s p e l l i n g - e r r o r | g r a m m a r - e r r o r ) / gi;
6+ / ( [ > ~ | + \s ] ) ? \s * : : ? ( b e f o r e | a f t e r | f i r s t - l e t t e r | f i r s t - l i n e | s e l e c t i o n | b a c k d r o p | p l a c e h o l d e r | m a r k e r | s p e l l i n g - e r r o r | g r a m m a r - e r r o r | t a r g e t (?: - t e x t ) ? ) / gi;
77
88/**
99 * Given a document, reads all style sheets returns extracts all CSSRules
@@ -52,8 +52,9 @@ export function getPseudoElementStyles(
5252 let match : RegExpExecArray | null = null ;
5353 let seenPseudos : string [ ] | null = null ;
5454
55- while ( ( match = pseudoElementRegex . exec ( selectorText ) ) ) {
56- const name = `::${ match [ 1 ] } ` ;
55+ while ( ( match = pseudoElementRegex . exec ( baseSelector ) ) ) {
56+ const name = `::${ match [ 2 ] } ` ;
57+ const childCombinator = match [ 1 ] ;
5758
5859 if ( seenPseudos ) {
5960 if ( ! seenPseudos . includes ( name ) ) {
@@ -64,8 +65,8 @@ export function getPseudoElementStyles(
6465 }
6566
6667 baseSelector =
67- selectorText . slice ( 0 , match . index ) +
68- selectorText . slice ( match . index + match [ 0 ] . length ) ;
68+ baseSelector . slice ( 0 , match . index ) + ( childCombinator || "" ) ;
69+ baseSelector . slice ( match . index + match [ 0 ] . length ) ;
6970 }
7071
7172 if ( seenPseudos && el . matches ( baseSelector || "*" ) ) {
@@ -79,20 +80,23 @@ export function getPseudoElementStyles(
7980 return rulesByPseudoElement ;
8081 } , { } ) ;
8182
82- const foundPseudoElements = Object . keys ( stylesByPseudoElement ) ;
83+ let appliedPseudoElementStyles : null | {
84+ [ name : string ] : { [ property : string ] : string } ;
85+ } = null ;
8386
84- if ( ! foundPseudoElements . length ) {
85- return null ;
86- }
87-
88- return foundPseudoElements . reduce ( ( styleByPseudoElement , name ) => {
89- styleByPseudoElement [ name ] = getAppliedStylesForElement (
87+ for ( const name in stylesByPseudoElement ) {
88+ const styles = getAppliedStylesForElement (
9089 el ,
9190 name ,
9291 stylesByPseudoElement [ name ]
93- ) ! ;
94- return styleByPseudoElement ;
95- } , { } as { [ x : string ] : { [ x : string ] : string } } ) ;
92+ ) ;
93+ if ( styles ) {
94+ appliedPseudoElementStyles ||= { } ;
95+ appliedPseudoElementStyles [ name ] = styles ;
96+ }
97+ }
98+
99+ return appliedPseudoElementStyles ;
96100}
97101
98102/**
0 commit comments