Skip to content
This repository was archived by the owner on Apr 6, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add basic experimental support for !font-bold-style important modif…
…iers
  • Loading branch information
adamwathan committed Mar 27, 2021
commit eae1d1310e82a1b61fbf3e0e13caf3422e5a8e77
37 changes: 35 additions & 2 deletions src/lib/generateRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const parseObjectStyles = require('tailwindcss/lib/util/parseObjectStyles').defa
const { isPlainObject, bigSign } = require('./utils')
const selectorParser = require('postcss-selector-parser')
const prefixSelector = require('tailwindcss/lib/util/prefixSelector').default
const { updateAllClasses } = require('../pluginUtils')

let classNameParser = selectorParser((selectors) => {
return selectors.first.filter(({ type }) => type === 'class').pop().value
Expand Down Expand Up @@ -64,6 +65,27 @@ function applyPrefix(matches, context) {
return matches
}

function applyImportant(matches) {
if (matches.length === 0) {
return matches
}
let result = []

for (let [{ sort, layer, options }, rule] of matches) {
let container = postcss.root({ nodes: [rule] })
container.walkRules((r) => {
r.selector = updateAllClasses(r.selector, (className) => {
return `!${className}`
})
r.walkDecls((d) => (d.important = true))
})
let withOffset = [{ sort: sort, layer, options }, container.nodes[0]]
result.push(withOffset)
}

return result
}

// Takes a list of rule tuples and applies a variant like `hover`, sm`,
// whatever to it. We used to do some extra caching here to avoid generating
// a variant of the same rule more than once, but this was never hit because
Expand Down Expand Up @@ -155,8 +177,9 @@ function* resolveMatchedPlugins(classCandidate, context) {
let candidatePrefix = classCandidate
let negative = false

const twConfigPrefix = context.tailwindConfig.prefix || ''
const twConfigPrefixLen = twConfigPrefix.length
let twConfigPrefix = context.tailwindConfig.prefix || ''
let twConfigPrefixLen = twConfigPrefix.length

if (candidatePrefix[twConfigPrefixLen] === '-') {
negative = true
candidatePrefix = twConfigPrefix + candidatePrefix.slice(twConfigPrefixLen + 1)
Expand All @@ -179,6 +202,12 @@ function sortAgainst(toSort, against) {
function* resolveMatches(candidate, context) {
let separator = context.tailwindConfig.separator
let [classCandidate, ...variants] = candidate.split(separator).reverse()
let important = false

if (classCandidate.startsWith('!')) {
important = true
classCandidate = classCandidate.slice(1)
}

// Strip prefix
// md:hover:tw-bg-black
Expand Down Expand Up @@ -220,6 +249,10 @@ function* resolveMatches(candidate, context) {

matches = applyPrefix(matches, context)

if (important) {
matches = applyImportant(matches, context)
}

for (let variant of variants) {
matches = applyVariant(variant, matches, context)
}
Expand Down
53 changes: 53 additions & 0 deletions tests/14-important-modifier.test.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
* {
--tw-shadow: 0 0 #0000;
--tw-ring-inset: var(--tw-empty, /*!*/ /*!*/);
--tw-ring-offset-width: 0px;
--tw-ring-offset-color: #fff;
--tw-ring-color: rgba(59, 130, 246, 0.5);
--tw-ring-offset-shadow: 0 0 #0000;
--tw-ring-shadow: 0 0 #0000;
}
.\!container {
width: 100% !important;
}
@media (min-width: 640px) {
.\!container {
max-width: 640px !important;
}
}
@media (min-width: 768px) {
.\!container {
max-width: 768px !important;
}
}
@media (min-width: 1024px) {
.\!container {
max-width: 1024px !important;
}
}
@media (min-width: 1280px) {
.\!container {
max-width: 1280px !important;
}
}
@media (min-width: 1536px) {
.\!container {
max-width: 1536px !important;
}
}
.\!font-bold {
font-weight: 700 !important;
}
.hover\:\!text-center:hover {
text-align: center !important;
}
@media (min-width: 1024px) {
.lg\:\!opacity-50 {
opacity: 0.5 !important;
}
}
@media (min-width: 1280px) {
.xl\:focus\:disabled\:\!float-right:focus:disabled {
float: right !important;
}
}
5 changes: 5 additions & 0 deletions tests/14-important-modifier.test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div class="!container"></div>
<div class="!font-bold"></div>
<div class="hover:!text-center"></div>
<div class="lg:!opacity-50"></div>
<div class="xl:focus:disabled:!float-right"></div>
32 changes: 32 additions & 0 deletions tests/14-important-modifier.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const postcss = require('postcss')
const tailwind = require('../src/index.js')
const fs = require('fs')
const path = require('path')

function run(input, config = {}) {
return postcss([tailwind(config)]).process(input, { from: path.resolve(__filename) })
}

test('important boolean', () => {
let config = {
important: false,
darkMode: 'class',
purge: [path.resolve(__dirname, './14-important-modifier.test.html')],
corePlugins: { preflight: false },
theme: {},
plugins: [],
}

let css = `
@tailwind base;
@tailwind components;
@tailwind utilities;
`

return run(css, config).then((result) => {
let expectedPath = path.resolve(__dirname, './14-important-modifier.test.css')
let expected = fs.readFileSync(expectedPath, 'utf8')

expect(result.css).toMatchCss(expected)
})
})