Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure we can apply classes that are grouped with non-class selectors #6922

Merged
merged 2 commits into from Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- Allow use of falsy values in theme config ([#6917](https://github.com/tailwindlabs/tailwindcss/pull/6917))
- Ensure we can apply classes defined with non-"on-demandable" selectors ([#6922](https://github.com/tailwindlabs/tailwindcss/pull/6922))


## [3.0.11] - 2022-01-05
Expand Down
36 changes: 26 additions & 10 deletions src/lib/setupContextUtils.js
Expand Up @@ -89,39 +89,55 @@ function getClasses(selector) {
return parser.transformSync(selector)
}

function extractCandidates(node) {
function extractCandidates(node, state = { containsNonOnDemandable: false }, depth = 0) {
let classes = []

// Handle normal rules
if (node.type === 'rule') {
for (let selector of node.selectors) {
let classCandidates = getClasses(selector)
// At least one of the selectors contains non-"on-demandable" candidates.
if (classCandidates.length === 0) return []
if (classCandidates.length === 0) {
state.containsNonOnDemandable = true
}

classes = [...classes, ...classCandidates]
for (let classCandidate of classCandidates) {
classes.push(classCandidate)
}
}
return classes
}

if (node.type === 'atrule') {
// Handle at-rules (which contains nested rules)
else if (node.type === 'atrule') {
node.walkRules((rule) => {
classes = [...classes, ...rule.selectors.flatMap((selector) => getClasses(selector))]
for (let classCandidate of rule.selectors.flatMap((selector) =>
getClasses(selector, state, depth + 1)
)) {
classes.push(classCandidate)
}
})
}

if (depth === 0) {
return [state.containsNonOnDemandable || classes.length === 0, classes]
}

return classes
}

function withIdentifiers(styles) {
return parseStyles(styles).flatMap((node) => {
let nodeMap = new Map()
let candidates = extractCandidates(node)
let [containsNonOnDemandableSelectors, candidates] = extractCandidates(node)

// If this isn't "on-demandable", assign it a universal candidate.
if (candidates.length === 0) {
return [['*', node]]
// If this isn't "on-demandable", assign it a universal candidate to always include it.
if (containsNonOnDemandableSelectors) {
candidates.unshift('*')
}

// However, it could be that it also contains "on-demandable" candidates.
// E.g.: `span, .foo {}`, in that case it should still be possible to use
// `@apply foo` for example.
return candidates.map((c) => {
if (!nodeMap.has(node)) {
nodeMap.set(node, node)
Expand Down
98 changes: 98 additions & 0 deletions tests/apply.test.js
Expand Up @@ -812,6 +812,104 @@ it('should be possible to apply user css without tailwind directives', () => {
})
})

it('should be possible to apply a class from another rule with multiple selectors (2 classes)', () => {
let config = {
content: [{ raw: html`<div class="c"></div>` }],
plugins: [],
}

let input = css`
@tailwind utilities;
@layer utilities {
.a,
.b {
@apply underline;
}

.c {
@apply b;
}
}
`

return run(input, config).then((result) => {
return expect(result.css).toMatchFormattedCss(css`
.c {
text-decoration-line: underline;
}
`)
})
})

it('should be possible to apply a class from another rule with multiple selectors (1 class, 1 tag)', () => {
let config = {
content: [{ raw: html`<div class="c"></div>` }],
plugins: [],
}

let input = css`
@tailwind utilities;

@layer utilities {
span,
.b {
@apply underline;
}

.c {
@apply b;
}
}
`

return run(input, config).then((result) => {
return expect(result.css).toMatchFormattedCss(css`
span,
.b {
text-decoration-line: underline;
}

.c {
text-decoration-line: underline;
}
`)
})
})

it('should be possible to apply a class from another rule with multiple selectors (1 class, 1 id)', () => {
let config = {
content: [{ raw: html`<div class="c"></div>` }],
plugins: [],
}

let input = css`
@tailwind utilities;
@layer utilities {
#a,
.b {
@apply underline;
}

.c {
@apply b;
}
}
`

return run(input, config).then((result) => {
return expect(result.css).toMatchFormattedCss(css`
#a,
.b {
text-decoration-line: underline;
}

.c {
text-decoration-line: underline;
}
`)
})
})

/*
it('apply can emit defaults in isolated environments without @tailwind directives', () => {
let config = {
Expand Down