Skip to content

Commit 2eca7d1

Browse files
Merge pull request #65 from cloud-walker/escape-values
Properly escape custom props
2 parents b1a9215 + 435096d commit 2eca7d1

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

src/index.spec.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { Theme } from './config'
2-
import multiThemePlugin from '.'
3-
import { MultiThemePluginOptions } from './utils/optionsUtils'
4-
import { defaultThemeName } from './utils/optionsUtils'
5-
import { PluginAPI } from 'tailwindcss/types/config'
61
import { mock } from 'jest-mock-extended'
2+
import { PluginAPI } from 'tailwindcss/types/config'
3+
import multiThemePlugin from '.'
4+
import { Theme } from './config'
5+
import { MultiThemePluginOptions, defaultThemeName } from './utils/optionsUtils'
76

87
describe('multiThemePlugin', () => {
98
describe('handler', () => {
@@ -25,6 +24,9 @@ describe('multiThemePlugin', () => {
2524
colors: {
2625
primary: 'another',
2726
secondary: 'something'
27+
},
28+
spacing: {
29+
'0.5': '10px'
2830
}
2931
}
3032
}
@@ -65,7 +67,8 @@ describe('multiThemePlugin', () => {
6567
expect(api.addBase).toHaveBeenCalledWith({
6668
[`.escaped-${theme.name}`]: {
6769
'--colors-primary': 'another',
68-
'--colors-secondary': 'something'
70+
'--colors-secondary': 'something',
71+
'--spacing-0\\.5': '10px'
6972
}
7073
})
7174
}

src/utils/customPropUtils.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
import { isColor, toRgb, withOpacity } from './colorUtils'
22

3+
/**
4+
* Code copied from the tailwind official codebase
5+
* @link https://github.com/tailwindlabs/tailwindcss/blob/fbbba6f67f73c3a4f9571649c3fc27006446d8f4/src/lib/regex.js#L70
6+
*/
7+
const REGEX_SPECIAL = /[\\^$.*+?()[\]{}|]/g
8+
const REGEX_HAS_SPECIAL = RegExp(REGEX_SPECIAL.source)
9+
export const escape = (string?: string): string => {
10+
return string && REGEX_HAS_SPECIAL.test(string)
11+
? string.replace(REGEX_SPECIAL, '\\$&')
12+
: string || ''
13+
}
14+
315
/**
416
* @param value - a custom prop value
517
* @return the value converted to a string of its rgb components comma separated if it is a color else it returns the value unaltered
@@ -27,9 +39,9 @@ export const toCustomPropName = (valuePath: string[]): string => {
2739
)}"`
2840
)
2941
}
30-
return `--${valuePath
31-
.filter(step => step.toLowerCase() !== 'default')
32-
.join('-')}`
42+
return escape(
43+
`--${valuePath.filter(step => step.toLowerCase() !== 'default').join('-')}`
44+
)
3345
}
3446

3547
/**

src/utils/themeUtils.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
import { mock } from 'jest-mock-extended'
2+
import { PluginAPI } from 'tailwindcss/types/config'
3+
import { PluginUtils, TailwindExtension, Theme } from '../config'
14
import {
25
resolveThemeExtensionAsCustomProps,
36
resolveThemeExtensionsAsTailwindExtension
47
} from './themeUtils'
5-
import { PluginUtils, TailwindExtension, Theme } from '../config'
6-
import { PluginAPI } from 'tailwindcss/types/config'
7-
import { mock } from 'jest-mock-extended'
88

99
describe('themeUtils', () => {
1010
let pluginUtils: PluginUtils

0 commit comments

Comments
 (0)