Skip to content

Commit

Permalink
Ensure invalid typehints are not generated (#5590)
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinMalfait committed Sep 24, 2021
1 parent abcd9ac commit 79b37a8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/util/pluginUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,12 @@ let typeMap = {
lookup: asLookupValue,
}

let supportedTypes = Object.keys(typeMap)

function splitAtFirst(input, delim) {
return (([first, ...rest]) => [first, rest.join(delim)])(input.split(delim))
let idx = input.indexOf(delim)
if (idx === -1) return [undefined, input]
return [input.slice(0, idx), input.slice(idx + 1)]
}

export function coerceValue(type, modifier, values, tailwindConfig) {
Expand All @@ -294,7 +298,11 @@ export function coerceValue(type, modifier, values, tailwindConfig) {
if (isArbitraryValue(modifier)) {
let [explicitType, value] = splitAtFirst(modifier.slice(1, -1), ':')

if (value.length > 0 && Object.keys(typeMap).includes(explicitType)) {
if (explicitType !== undefined && !supportedTypes.includes(explicitType)) {
return []
}

if (value.length > 0 && supportedTypes.includes(explicitType)) {
return [asValue(`[${value}]`, values, tailwindConfig), explicitType]
}

Expand Down
14 changes: 14 additions & 0 deletions tests/arbitrary-values.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ test('arbitrary values', () => {
})
})

it('should not generate any css if an unknown typehint is used', () => {
let config = {
content: [
{
raw: html`<div class="inset-[hmm:12px]"></div>`,
},
],
}

return run('@tailwind utilities', config).then((result) => {
return expect(result.css).toMatchFormattedCss(css``)
})
})

it('should convert _ to spaces', () => {
let config = {
content: [
Expand Down

0 comments on commit 79b37a8

Please sign in to comment.