Skip to content

Commit 19c74b1

Browse files
committed
Merge branch 'add-prefix-selector-support' of git://github.com/pxwee5/tailwindcss into pxwee5-add-prefix-selector-support
2 parents 5da2398 + 12ba0c8 commit 19c74b1

File tree

6 files changed

+100
-1
lines changed

6 files changed

+100
-1
lines changed

__tests__/applyAtRule.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,3 +242,25 @@ test('you can apply utility classes without using the given prefix when using a
242242
expect(result.warnings().length).toBe(0)
243243
})
244244
})
245+
246+
test('you can apply utility classes without specificity prefix even if important (selector) is used.', () => {
247+
const input = `
248+
.foo { @apply .mt-8 .mb-8; }
249+
`
250+
251+
const expected = `
252+
.foo { margin-top: 2rem; margin-bottom: 2rem; }
253+
`
254+
255+
const config = resolveConfig([
256+
{
257+
...defaultConfig,
258+
important: '#app',
259+
},
260+
])
261+
262+
return run(input, config, processPlugins(corePlugins(config), config).utilities).then(result => {
263+
expect(result.css).toEqual(expected)
264+
expect(result.warnings().length).toBe(0)
265+
})
266+
})

__tests__/processPlugins.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,32 @@ test('plugins respect prefix and important options by default when adding utilit
877877
`)
878878
})
879879

880+
test('plugins respect prefix and important (selector) options by default when adding utilities', () => {
881+
const { utilities } = processPlugins(
882+
[
883+
function({ addUtilities }) {
884+
addUtilities({
885+
'.rotate-90': {
886+
transform: 'rotate(90deg)',
887+
},
888+
})
889+
},
890+
],
891+
makeConfig({
892+
prefix: 'tw-',
893+
important: '#app',
894+
})
895+
)
896+
897+
expect(css(utilities)).toMatchCss(`
898+
@variants {
899+
#app .tw-rotate-90 {
900+
transform: rotate(90deg)
901+
}
902+
}
903+
`)
904+
})
905+
880906
test('important utilities are not made double important when important option is used', () => {
881907
const { utilities } = processPlugins(
882908
[

__tests__/resolveConfig.test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,46 @@ test('important key overrides default important', () => {
8080
})
8181
})
8282

83+
test('important (selector) key overrides default important', () => {
84+
const userConfig = {
85+
important: '#app',
86+
}
87+
88+
const defaultConfig = {
89+
prefix: '',
90+
important: false,
91+
separator: ':',
92+
theme: {
93+
screens: {
94+
mobile: '400px',
95+
},
96+
},
97+
variants: {
98+
appearance: ['responsive'],
99+
borderCollapse: [],
100+
borderColors: ['responsive', 'hover', 'focus'],
101+
},
102+
}
103+
104+
const result = resolveConfig([userConfig, defaultConfig])
105+
106+
expect(result).toEqual({
107+
prefix: '',
108+
important: '#app',
109+
separator: ':',
110+
theme: {
111+
screens: {
112+
mobile: '400px',
113+
},
114+
},
115+
variants: {
116+
appearance: ['responsive'],
117+
borderCollapse: [],
118+
borderColors: ['responsive', 'hover', 'focus'],
119+
},
120+
})
121+
})
122+
83123
test('separator key overrides default separator', () => {
84124
const userConfig = {
85125
separator: '__',

src/lib/substituteClassApplyAtRules.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import _ from 'lodash'
22
import postcss from 'postcss'
33
import escapeClassName from '../util/escapeClassName'
44
import prefixSelector from '../util/prefixSelector'
5+
import increaseSpecificity from '../util/increaseSpecificity'
56

67
function buildClassTable(css) {
78
const classTable = {}
@@ -85,6 +86,8 @@ export default function(config, generatedUtilities) {
8586
() => findClass(classToApply, classLookup, onError),
8687
() => findClass(classToApply, shadowLookup, onError),
8788
() => findClass(prefixSelector(config.prefix, classToApply), shadowLookup, onError),
89+
// prettier-ignore
90+
() => findClass(increaseSpecificity(config.important, classToApply), shadowLookup, onError),
8891
() => {
8992
// prettier-ignore
9093
throw onError(`\`@apply\` cannot be used with \`${classToApply}\` because \`${classToApply}\` either cannot be found, or its actual definition includes a pseudo-selector like :hover, :active, etc. If you're sure that \`${classToApply}\` exists, make sure that any \`@import\` statements are being properly processed *before* Tailwind CSS sees your CSS, as \`@apply\` can only be used for classes in the same CSS tree.`)

src/util/increaseSpecificity.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function(importantVal, selector) {
2+
return `${importantVal} ${selector}`
3+
}

src/util/processPlugins.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import generateVariantFunction from '../util/generateVariantFunction'
66
import parseObjectStyles from '../util/parseObjectStyles'
77
import prefixSelector from '../util/prefixSelector'
88
import wrapWithVariants from '../util/wrapWithVariants'
9+
import increaseSpecificity from '../util/increaseSpecificity'
910

1011
function parseStyles(styles) {
1112
if (!Array.isArray(styles)) {
@@ -55,7 +56,11 @@ export default function(plugins, config) {
5556
}
5657

5758
if (options.respectImportant && _.get(config, 'important')) {
58-
rule.walkDecls(decl => (decl.important = true))
59+
if (config.important === true) {
60+
rule.walkDecls(decl => (decl.important = true))
61+
} else if (typeof config.important === 'string') {
62+
rule.selector = increaseSpecificity(config.important, rule.selector)
63+
}
5964
}
6065
})
6166

0 commit comments

Comments
 (0)