Skip to content

Commit ad48de9

Browse files
authored
[0.3] Escape separator string (tailwindlabs#270)
1 parent 92011af commit ad48de9

File tree

5 files changed

+27
-23
lines changed

5 files changed

+27
-23
lines changed

__tests__/variantsAtRule.test.js

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import postcss from 'postcss'
22
import plugin from '../src/lib/substituteVariantsAtRules'
33
import config from '../defaultConfig.stub.js'
44

5-
const separator = config.options.separator
6-
75
function run(input, opts = () => config) {
86
return postcss([plugin(opts)]).process(input)
97
}
@@ -19,8 +17,8 @@ test('it can generate hover variants', () => {
1917
const output = `
2018
.banana { color: yellow; }
2119
.chocolate { color: brown; }
22-
.hover${separator}banana:hover { color: yellow; }
23-
.hover${separator}chocolate:hover { color: brown; }
20+
.hover\\:banana:hover { color: yellow; }
21+
.hover\\:chocolate:hover { color: brown; }
2422
`
2523

2624
return run(input).then(result => {
@@ -40,8 +38,8 @@ test('it can generate focus variants', () => {
4038
const output = `
4139
.banana { color: yellow; }
4240
.chocolate { color: brown; }
43-
.focus${separator}banana:focus { color: yellow; }
44-
.focus${separator}chocolate:focus { color: brown; }
41+
.focus\\:banana:focus { color: yellow; }
42+
.focus\\:chocolate:focus { color: brown; }
4543
`
4644

4745
return run(input).then(result => {
@@ -61,8 +59,8 @@ test('it can generate parent-hover variants', () => {
6159
const output = `
6260
.banana { color: yellow; }
6361
.chocolate { color: brown; }
64-
.parent:hover .parent-hover${separator}banana { color: yellow; }
65-
.parent:hover .parent-hover${separator}chocolate { color: brown; }
62+
.parent:hover .parent-hover\\:banana { color: yellow; }
63+
.parent:hover .parent-hover\\:chocolate { color: brown; }
6664
`
6765

6866
return run(input).then(result => {
@@ -82,10 +80,10 @@ test('it can generate hover and focus variants', () => {
8280
const output = `
8381
.banana { color: yellow; }
8482
.chocolate { color: brown; }
85-
.focus${separator}banana:focus { color: yellow; }
86-
.focus${separator}chocolate:focus { color: brown; }
87-
.hover${separator}banana:hover { color: yellow; }
88-
.hover${separator}chocolate:hover { color: brown; }
83+
.focus\\:banana:focus { color: yellow; }
84+
.focus\\:chocolate:focus { color: brown; }
85+
.hover\\:banana:hover { color: yellow; }
86+
.hover\\:chocolate:hover { color: brown; }
8987
`
9088

9189
return run(input).then(result => {
@@ -106,10 +104,10 @@ test('it wraps the output in a responsive at-rule if responsive is included as a
106104
@responsive {
107105
.banana { color: yellow; }
108106
.chocolate { color: brown; }
109-
.focus${separator}banana:focus { color: yellow; }
110-
.focus${separator}chocolate:focus { color: brown; }
111-
.hover${separator}banana:hover { color: yellow; }
112-
.hover${separator}chocolate:hover { color: brown; }
107+
.focus\\:banana:focus { color: yellow; }
108+
.focus\\:chocolate:focus { color: brown; }
109+
.hover\\:banana:hover { color: yellow; }
110+
.hover\\:chocolate:hover { color: brown; }
113111
}
114112
`
115113

defaultConfig.stub.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,7 @@ module.exports = {
871871
options: {
872872
prefix: '',
873873
important: false,
874-
separator: '\\:',
874+
separator: ':',
875875
},
876876

877877
}

src/lib/substituteResponsiveAtRules.js

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

67
export default function(config) {
78
return function(css) {
@@ -26,9 +27,8 @@ export default function(config) {
2627
mediaQuery.append(
2728
responsiveRules.map(rule => {
2829
const cloned = rule.clone()
29-
cloned.selectors = _.map(
30-
rule.selectors,
31-
selector => `.${screen}${separator}${selector.slice(1)}`
30+
cloned.selectors = _.map(rule.selectors, selector =>
31+
buildClassVariant(selector, screen, separator)
3232
)
3333
return cloned
3434
})

src/lib/substituteVariantsAtRules.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import _ from 'lodash'
22
import postcss from 'postcss'
3+
import buildClassVariant from '../util/buildClassVariant'
34

45
const variantGenerators = {
56
hover: (container, config) => {
67
const cloned = container.clone()
78

89
cloned.walkRules(rule => {
9-
rule.selector = `.hover${config.options.separator}${rule.selector.slice(1)}:hover`
10+
rule.selector = `${buildClassVariant(rule.selector, 'hover', config.options.separator)}:hover`
1011
})
1112

1213
container.before(cloned.nodes)
@@ -15,7 +16,7 @@ const variantGenerators = {
1516
const cloned = container.clone()
1617

1718
cloned.walkRules(rule => {
18-
rule.selector = `.focus${config.options.separator}${rule.selector.slice(1)}:focus`
19+
rule.selector = `${buildClassVariant(rule.selector, 'focus', config.options.separator)}:focus`
1920
})
2021

2122
container.before(cloned.nodes)
@@ -25,7 +26,7 @@ const variantGenerators = {
2526

2627
cloned.walkRules(rule => {
2728
// prettier-ignore
28-
rule.selector = `.parent:hover .parent-hover${config.options.separator}${rule.selector.slice(1)}`
29+
rule.selector = `.parent:hover ${buildClassVariant(rule.selector, 'parent-hover', config.options.separator)}`
2930
})
3031

3132
container.before(cloned.nodes)

src/util/buildClassVariant.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import escapeClassName from './escapeClassName'
2+
3+
export default function buildClassVariant(className, variantName, separator) {
4+
return `.${variantName}${escapeClassName(separator)}${className.slice(1)}`
5+
}

0 commit comments

Comments
 (0)