Skip to content

Commit 5d6d84f

Browse files
authored
Ensure full node stays, if only a partial is used (#5617)
1 parent 6c0b8e8 commit 5d6d84f

File tree

2 files changed

+92
-2
lines changed

2 files changed

+92
-2
lines changed

src/lib/setupContextUtils.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,22 @@ function getClasses(selector) {
5959
}
6060

6161
function extractCandidates(node) {
62-
let classes = node.type === 'rule' ? getClasses(node.selector) : []
62+
let classes = []
63+
64+
if (node.type === 'rule') {
65+
for (let selector of node.selectors) {
66+
let classCandidates = getClasses(selector)
67+
// At least one of the selectors contains non-"on-demandable" candidates.
68+
if (classCandidates.length === 0) return []
69+
70+
classes = [...classes, ...classCandidates]
71+
}
72+
return classes
73+
}
6374

6475
if (node.type === 'atrule') {
6576
node.walkRules((rule) => {
66-
classes = [...classes, ...getClasses(rule.selector)]
77+
classes = [...classes, ...rule.selectors.flatMap((selector) => getClasses(selector))]
6778
})
6879
}
6980

tests/combined-selectors.test.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { run, html, css } from './util/run'
2+
3+
it('should generate the partial selector, if only a partial is used (base layer)', () => {
4+
let config = {
5+
content: [{ raw: html`<div></div>` }],
6+
corePlugins: { preflight: false },
7+
}
8+
9+
let input = css`
10+
@tailwind base;
11+
12+
@layer base {
13+
:root {
14+
font-weight: bold;
15+
}
16+
17+
/* --- */
18+
19+
:root,
20+
.a {
21+
color: black;
22+
}
23+
}
24+
`
25+
26+
return run(input, config).then((result) => {
27+
return expect(result.css).toMatchFormattedCss(css`
28+
:root {
29+
font-weight: bold;
30+
}
31+
32+
/* --- */
33+
34+
:root,
35+
.a {
36+
color: black;
37+
}
38+
`)
39+
})
40+
})
41+
42+
it('should generate the partial selector, if only a partial is used (utilities layer)', () => {
43+
let config = {
44+
content: [{ raw: html`<div class="a"></div>` }],
45+
corePlugins: { preflight: false },
46+
}
47+
48+
let input = css`
49+
@tailwind utilities;
50+
51+
@layer utilities {
52+
:root {
53+
font-weight: bold;
54+
}
55+
56+
/* --- */
57+
58+
:root,
59+
.a {
60+
color: black;
61+
}
62+
}
63+
`
64+
65+
return run(input, config).then((result) => {
66+
return expect(result.css).toMatchFormattedCss(css`
67+
:root {
68+
font-weight: bold;
69+
}
70+
71+
/* --- */
72+
73+
:root,
74+
.a {
75+
color: black;
76+
}
77+
`)
78+
})
79+
})

0 commit comments

Comments
 (0)