Skip to content

Commit 37b06c8

Browse files
committed
Generate focus variants as separate declarations
1 parent 7746f64 commit 37b06c8

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

__tests__/focusableAtRule.test.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ test('it adds a focusable variant to each nested class definition', () => {
1414
`
1515

1616
const output = `
17-
.banana, .focus\\:banana:focus { color: yellow; }
18-
.chocolate, .focus\\:chocolate:focus { color: brown; }
17+
.banana { color: yellow; }
18+
.chocolate { color: brown; }
19+
.focus\\:banana:focus { color: yellow; }
20+
.focus\\:chocolate:focus { color: brown; }
1921
`
2022

2123
return run(input).then(result => {
22-
expect(result.css).toEqual(output)
24+
expect(result.css).toMatchCss(output)
2325
expect(result.warnings().length).toBe(0)
2426
})
2527
})

jest/customMatchers.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
expect.extend({
2+
// Compare two CSS strings with all whitespace removed
3+
// This is probably naive but it's fast and works well enough.
4+
toMatchCss(received, argument) {
5+
function stripped(str) {
6+
return str.replace(/\s/g, '')
7+
}
8+
9+
if (stripped(received) == stripped(argument)) {
10+
return {
11+
message: () =>
12+
`expected ${received} not to match CSS ${argument}`,
13+
pass: true,
14+
};
15+
} else {
16+
return {
17+
message: () => `expected ${received} to match CSS ${argument}`,
18+
pass: false,
19+
};
20+
}
21+
}
22+
})

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@
6868
"react"
6969
]
7070
},
71+
"jest": {
72+
"setupTestFrameworkScriptFile": "<rootDir>/jest/customMatchers.js"
73+
},
7174
"engines": {
7275
"node": ">=6.9.0"
7376
}

src/lib/substituteFocusableAtRules.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
import cloneNodes from '../util/cloneNodes'
2-
31
export default function() {
42
return function(css) {
53
css.walkAtRules('focusable', atRule => {
6-
atRule.walkRules(rule => {
4+
const clonedRule = atRule.clone()
5+
6+
clonedRule.walkRules(rule => {
77
// Might be wise to error if the rule has multiple selectors,
88
// or weird compound selectors like .bg-blue>p>h1
9-
rule.selectors = [rule.selector, `.focus\\:${rule.selector.slice(1)}:focus`]
9+
rule.selector = `.focus\\:${rule.selector.slice(1)}:focus`
1010
})
1111

12-
atRule.before(cloneNodes(atRule.nodes))
12+
atRule.before(atRule.clone().nodes)
13+
atRule.after(clonedRule.nodes)
1314

1415
atRule.remove()
1516
})

0 commit comments

Comments
 (0)