Skip to content
This repository was archived by the owner on Apr 6, 2021. It is now read-only.

Commit 4579fad

Browse files
committed
WIP
1 parent 98ae69d commit 4579fad

File tree

5 files changed

+154
-5
lines changed

5 files changed

+154
-5
lines changed

src/lib/expandApplyAtRules.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ function buildApplyCache(applyCandidates, context) {
2727
return context.applyClassCache
2828
}
2929

30-
// TODO: Apply `!important` stuff correctly instead of just skipping it
3130
function extractApplyCandidates(params) {
3231
let candidates = params.split(/[\s\t\n]+/g)
3332

src/lib/generateRules.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,19 @@ function generateRules(candidates, context) {
217217
}
218218

219219
return allRules.flat(1).map(([{ sort, layer, options }, rule]) => {
220-
if (context.tailwindConfig.important === true && options.respectImportant) {
221-
rule.walkDecls((d) => {
222-
d.important = true
223-
})
220+
if (options.respectImportant) {
221+
if (context.tailwindConfig.important === true && options.respectImportant) {
222+
rule.walkDecls((d) => {
223+
d.important = true
224+
})
225+
} else if (typeof context.tailwindConfig.important === 'string') {
226+
if (rule.type === 'rule') {
227+
rule.selectors = rule.selectors.map((s) => `${context.tailwindConfig.important} ${s}`)
228+
}
229+
rule.walkRules((r) => {
230+
r.selectors = r.selectors.map((s) => `${context.tailwindConfig.important} ${s}`)
231+
})
232+
}
224233
}
225234
return [sort | context.layerOrder[layer], rule]
226235
})

tests/04-important-selector.test.css

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
* {
2+
--tw-shadow: 0 0 #0000;
3+
--tw-ring-inset: var(--tw-empty, /*!*/ /*!*/);
4+
--tw-ring-offset-width: 0px;
5+
--tw-ring-offset-color: #fff;
6+
--tw-ring-color: rgba(59, 130, 246, 0.5);
7+
--tw-ring-offset-shadow: 0 0 #0000;
8+
--tw-ring-shadow: 0 0 #0000;
9+
}
10+
.container {
11+
width: 100%;
12+
}
13+
@media (min-width: 640px) {
14+
.container {
15+
max-width: 640px;
16+
}
17+
}
18+
@media (min-width: 768px) {
19+
.container {
20+
max-width: 768px;
21+
}
22+
}
23+
@media (min-width: 1024px) {
24+
.container {
25+
max-width: 1024px;
26+
}
27+
}
28+
@media (min-width: 1280px) {
29+
.container {
30+
max-width: 1280px;
31+
}
32+
}
33+
@media (min-width: 1536px) {
34+
.container {
35+
max-width: 1536px;
36+
}
37+
}
38+
#app .btn {
39+
button: yes;
40+
}
41+
.custom-component {
42+
font-weight: 700;
43+
}
44+
#app .custom-important-component {
45+
text-align: center;
46+
}
47+
#app .font-bold {
48+
font-weight: 700;
49+
}
50+
.custom-util {
51+
button: no;
52+
}
53+
#app .group:hover .group-hover\:focus-within\:text-left:focus-within {
54+
text-align: left;
55+
}
56+
#app [dir='rtl'] .rtl\:active\:text-center:active {
57+
text-align: center;
58+
}
59+
@media (prefers-reduced-motion: no-preference) {
60+
#app .motion-safe\:hover\:text-center:hover {
61+
text-align: center;
62+
}
63+
}
64+
#app .dark .dark\:focus\:text-left:focus {
65+
text-align: left;
66+
}
67+
@media (min-width: 768px) {
68+
#app .md\:hover\:text-right:hover {
69+
text-align: right;
70+
}
71+
}

tests/04-important-selector.test.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<div class="container"></div>
2+
<div class="btn"></div>
3+
<div class="custom-util"></div>
4+
<div class="custom-component"></div>
5+
<div class="custom-important-component"></div>
6+
<div class="font-bold"></div>
7+
<div class="md:hover:text-right"></div>
8+
<div class="motion-safe:hover:text-center"></div>
9+
<div class="dark:focus:text-left"></div>
10+
<div class="group-hover:focus-within:text-left"></div>
11+
<div class="rtl:active:text-center"></div>

tests/04-important-selector.test.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const postcss = require('postcss')
2+
const tailwind = require('../src/index.js')
3+
const fs = require('fs')
4+
const path = require('path')
5+
6+
function run(input, config = {}) {
7+
return postcss([tailwind(config)]).process(input, { from: path.resolve(__filename) })
8+
}
9+
10+
test('important boolean', () => {
11+
let config = {
12+
important: '#app',
13+
darkMode: 'class',
14+
purge: [path.resolve(__dirname, './04-important-selector.test.html')],
15+
corePlugins: { preflight: false },
16+
theme: {},
17+
plugins: [
18+
function ({ addComponents, addUtilities }) {
19+
addComponents(
20+
{
21+
'.btn': {
22+
button: 'yes',
23+
},
24+
},
25+
{ respectImportant: true }
26+
)
27+
addUtilities(
28+
{
29+
'.custom-util': {
30+
button: 'no',
31+
},
32+
},
33+
{ respectImportant: false }
34+
)
35+
},
36+
],
37+
}
38+
39+
let css = `
40+
@tailwind base;
41+
@tailwind components;
42+
@layer components {
43+
.custom-component {
44+
@apply font-bold;
45+
}
46+
.custom-important-component {
47+
@apply text-center !important;
48+
}
49+
}
50+
@tailwind utilities;
51+
`
52+
53+
return run(css, config).then((result) => {
54+
let expectedPath = path.resolve(__dirname, './04-important-selector.test.css')
55+
let expected = fs.readFileSync(expectedPath, 'utf8')
56+
57+
expect(result.css).toMatchCss(expected)
58+
})
59+
})

0 commit comments

Comments
 (0)