From ad850ac49128dfcc292f0025c8c42f26bc26c735 Mon Sep 17 00:00:00 2001 From: Adam Wathan Date: Fri, 8 May 2020 13:23:26 -0400 Subject: [PATCH 1/2] Throw if contains a class --- __tests__/processPlugins.test.js | 19 +++++++++++++++++++ src/util/processPlugins.js | 15 +++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/__tests__/processPlugins.test.js b/__tests__/processPlugins.test.js index 04ea56079e73..94d369ddec10 100644 --- a/__tests__/processPlugins.test.js +++ b/__tests__/processPlugins.test.js @@ -960,6 +960,25 @@ test('when important is a selector it is used to scope utilities instead of addi `) }) +test('when important contains a class an error is thrown', () => { + expect(() => { + processPlugins( + [ + function({ addUtilities }) { + addUtilities({ + '.rotate-90': { + transform: 'rotate(90deg)', + }, + }) + }, + ], + makeConfig({ + important: '#app .project', + }) + ) + }).toThrow() +}) + test('when important is a selector it scopes all selectors in a rule, even though defining utilities like this is stupid', () => { const { utilities } = processPlugins( [ diff --git a/src/util/processPlugins.js b/src/util/processPlugins.js index bdee55478cda..deb11b90e4cf 100644 --- a/src/util/processPlugins.js +++ b/src/util/processPlugins.js @@ -9,6 +9,7 @@ import parseObjectStyles from '../util/parseObjectStyles' import prefixSelector from '../util/prefixSelector' import wrapWithVariants from '../util/wrapWithVariants' import increaseSpecificity from '../util/increaseSpecificity' +import selectorParser from 'postcss-selector-parser' function parseStyles(styles) { if (!Array.isArray(styles)) { @@ -18,6 +19,14 @@ function parseStyles(styles) { return _.flatMap(styles, style => (style instanceof Node ? style : parseObjectStyles(style))) } +function containsClass(value) { + return selectorParser(selectors => { + let classFound = false + selectors.walkClasses(() => (classFound = true)) + return classFound + }).transformSync(value) +} + export default function(plugins, config) { const pluginBaseStyles = [] const pluginComponents = [] @@ -86,6 +95,12 @@ export default function(plugins, config) { if (config.important === true) { rule.walkDecls(decl => (decl.important = true)) } else if (typeof config.important === 'string') { + if (containsClass(config.important)) { + throw rule.error( + `Classes are not allowed when using the \`important\` option with a string argument. Please use an ID instead.` + ) + } + rule.selectors = rule.selectors.map(selector => { return increaseSpecificity(config.important, selector) }) From a6d434375be47d717ae90b506c7924f5d2089210 Mon Sep 17 00:00:00 2001 From: Adam Wathan Date: Fri, 8 May 2020 13:26:50 -0400 Subject: [PATCH 2/2] 1.4.6 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 321e7f7751f0..116fceb13e66 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "tailwindcss", - "version": "1.4.5", + "version": "1.4.6", "description": "A utility-first CSS framework for rapidly building custom user interfaces.", "license": "MIT", "main": "lib/index.js",