Skip to content

Commit

Permalink
Recursively collapse adjacent rules (#7565)
Browse files Browse the repository at this point in the history
* Recursively collapse adjacent rules

* Update changelog
  • Loading branch information
thecrypticace authored Feb 21, 2022
1 parent af64d71 commit 3b8ca9d
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

- Prevent nesting plugin from breaking other plugins ([#7563](https://github.com/tailwindlabs/tailwindcss/pull/7563))
- Recursively collapse adjacent rules ([#7565](https://github.com/tailwindlabs/tailwindcss/pull/7565))

## [3.0.23] - 2022-02-16

Expand Down
17 changes: 16 additions & 1 deletion src/lib/collapseAdjacentRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ let comparisonMap = {
let types = new Set(Object.keys(comparisonMap))

export default function collapseAdjacentRules() {
return (root) => {
function collapseRulesIn(root) {
let currentRule = null
root.each((node) => {
if (!types.has(node.type)) {
Expand Down Expand Up @@ -35,5 +35,20 @@ export default function collapseAdjacentRules() {
currentRule = node
}
})

// After we've collapsed adjacent rules & at-rules, we need to collapse
// adjacent rules & at-rules that are children of at-rules.
// We do not care about nesting rules because Tailwind CSS
// explicitly does not handle rule nesting on its own as
// the user is expected to use a nesting plugin
root.each((node) => {
if (node.type === 'atrule') {
collapseRulesIn(node)
}
})
}

return (root) => {
collapseRulesIn(root)
}
}
3 changes: 0 additions & 3 deletions tests/apply.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1289,9 +1289,6 @@ it('apply partitioning works with media queries', async () => {
body {
--tw-text-opacity: 1;
color: rgb(220 38 38 / var(--tw-text-opacity));
}
html,
body {
font-size: 2rem;
}
}
Expand Down
23 changes: 23 additions & 0 deletions tests/collapse-adjacent-rules.test.css
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,29 @@
color: black;
font-weight: 700;
}
@supports (foo: bar) {
.some-apply-thing {
font-weight: 700;
--tw-text-opacity: 1;
color: rgb(0 0 0 / var(--tw-text-opacity));
}
}
@media (min-width: 768px) {
.some-apply-thing {
font-weight: 700;
--tw-text-opacity: 1;
color: rgb(0 0 0 / var(--tw-text-opacity));
}
}
@supports (foo: bar) {
@media (min-width: 768px) {
.some-apply-thing {
font-weight: 700;
--tw-text-opacity: 1;
color: rgb(0 0 0 / var(--tw-text-opacity));
}
}
}
@media (min-width: 640px) {
.sm\:text-center {
text-align: center;
Expand Down
1 change: 1 addition & 0 deletions tests/collapse-adjacent-rules.test.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@
<div class="md:text-center"></div>
<div class="lg:font-bold"></div>
<div class="lg:text-center"></div>
<div class="some-apply-thing"></div>
</body>
</html>
9 changes: 8 additions & 1 deletion tests/collapse-adjacent-rules.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ test('collapse adjacent rules', () => {
content: [path.resolve(__dirname, './collapse-adjacent-rules.test.html')],
corePlugins: { preflight: false },
theme: {},
plugins: [],
plugins: [
function ({ addVariant }) {
addVariant('foo-bar', '@supports (foo: bar)')
},
],
}

let input = css`
Expand Down Expand Up @@ -45,6 +49,9 @@ test('collapse adjacent rules', () => {
.bar {
font-weight: 700;
}
.some-apply-thing {
@apply foo-bar:md:text-black foo-bar:md:font-bold foo-bar:text-black foo-bar:font-bold md:font-bold md:text-black;
}
`

return run(input, config).then((result) => {
Expand Down
2 changes: 0 additions & 2 deletions tests/kitchen-sink.test.css
Original file line number Diff line number Diff line change
Expand Up @@ -498,8 +498,6 @@ div {
transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
transition-duration: 150ms;
}
}
@media (prefers-reduced-motion: no-preference) {
.dark .md\:dark\:motion-safe\:foo\:active\:custom-util:active {
background: #abcdef !important;
}
Expand Down

0 comments on commit 3b8ca9d

Please sign in to comment.