Skip to content

Commit 1d75830

Browse files
committed
fix(beasties): handle empty nested pseudo-selectors
resolves #79
1 parent ebfbef2 commit 1d75830

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

packages/beasties/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { createDocument, serializeDocument } from './dom'
2727
import { createLogger, isSubpath } from './util'
2828

2929
const removePseudoClassesAndElementsPattern = /(?<!\\)::?[a-z-]+(?:\(.+\))?/gi
30+
const doubleNestingPattern = />\s*(?=>|$)/g
3031
const removeTrailingCommasPattern = /\(\s*,|,\s*\)/g
3132

3233
export 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)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
})

0 commit comments

Comments
 (0)