Generate focus variants as separate declarations

This commit is contained in:
Adam Wathan 2017-11-22 15:52:32 -05:00
parent 7746f64806
commit 37b06c8dc9
4 changed files with 36 additions and 8 deletions

View File

@ -14,12 +14,14 @@ test('it adds a focusable variant to each nested class definition', () => {
`
const output = `
.banana, .focus\\:banana:focus { color: yellow; }
.chocolate, .focus\\:chocolate:focus { color: brown; }
.banana { color: yellow; }
.chocolate { color: brown; }
.focus\\:banana:focus { color: yellow; }
.focus\\:chocolate:focus { color: brown; }
`
return run(input).then(result => {
expect(result.css).toEqual(output)
expect(result.css).toMatchCss(output)
expect(result.warnings().length).toBe(0)
})
})

22
jest/customMatchers.js Normal file
View File

@ -0,0 +1,22 @@
expect.extend({
// Compare two CSS strings with all whitespace removed
// This is probably naive but it's fast and works well enough.
toMatchCss(received, argument) {
function stripped(str) {
return str.replace(/\s/g, '')
}
if (stripped(received) == stripped(argument)) {
return {
message: () =>
`expected ${received} not to match CSS ${argument}`,
pass: true,
};
} else {
return {
message: () => `expected ${received} to match CSS ${argument}`,
pass: false,
};
}
}
})

View File

@ -68,6 +68,9 @@
"react"
]
},
"jest": {
"setupTestFrameworkScriptFile": "<rootDir>/jest/customMatchers.js"
},
"engines": {
"node": ">=6.9.0"
}

View File

@ -1,15 +1,16 @@
import cloneNodes from '../util/cloneNodes'
export default function() {
return function(css) {
css.walkAtRules('focusable', atRule => {
atRule.walkRules(rule => {
const clonedRule = atRule.clone()
clonedRule.walkRules(rule => {
// Might be wise to error if the rule has multiple selectors,
// or weird compound selectors like .bg-blue>p>h1
rule.selectors = [rule.selector, `.focus\\:${rule.selector.slice(1)}:focus`]
rule.selector = `.focus\\:${rule.selector.slice(1)}:focus`
})
atRule.before(cloneNodes(atRule.nodes))
atRule.before(atRule.clone().nodes)
atRule.after(clonedRule.nodes)
atRule.remove()
})