File tree Expand file tree Collapse file tree 2 files changed +37
-0
lines changed
Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change @@ -27,6 +27,7 @@ import { createDocument, serializeDocument } from './dom'
2727import { createLogger , isSubpath } from './util'
2828
2929const removePseudoClassesAndElementsPattern = / (?< ! \\ ) : : ? [ a - z - ] + (?: \( .+ \) ) ? / gi
30+ const doubleNestingPattern = / > \s * (? = > | $ ) / g
3031const removeTrailingCommasPattern = / \( \s * , | , \s * \) / g
3132
3233export default class Beasties {
@@ -687,6 +688,8 @@ export default class Beasties {
687688 normalizedSelector = sel
688689 . replace ( removePseudoClassesAndElementsPattern , '' )
689690 . replace ( removeTrailingCommasPattern , match => ( match . includes ( '(' ) ? '(' : ')' ) )
691+ // in case an entire selector is a pseudo-class we need to preserve it
692+ . replace ( doubleNestingPattern , '> *' )
690693 . trim ( ) as string
691694
692695 this . #selectorCache. set ( sel , normalizedSelector )
Original file line number Diff line number Diff line change 1+ import { describe , expect , it , vi } from 'vitest'
2+ import Beasties from '../src/index'
3+
4+ describe ( 'selector normalisation' , ( ) => {
5+ it ( 'should handle complex selectors' , async ( ) => {
6+ vi . spyOn ( console , 'warn' )
7+ const beasties = new Beasties ( )
8+ const result = await beasties . process ( `
9+ <html>
10+ <body>
11+ <style> div > :not(.foo) > * { color:red; } </style>
12+ <style> div > :not(.foo) { color:red; } </style>
13+ <div>
14+ <div><div></div></div>
15+ </div>
16+ </body>
17+ </html>
18+ ` )
19+ expect ( result . replace ( / ^ { 4 } / gm, '' ) ) . toMatchInlineSnapshot ( `
20+ "
21+ <html data-beasties-container>
22+ <body>
23+ <style>div > :not(.foo) > *{color:red}div > :not(.foo){color:red}</style>
24+
25+ <div>
26+ <div><div></div></div>
27+ </div>
28+ </body>
29+ </html>
30+ "
31+ ` )
32+ expect ( console . warn ) . not . toBeCalled ( )
33+ } )
34+ } )
You can’t perform that action at this time.
0 commit comments