Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Validate that css values are actually parsable
  • Loading branch information
thecrypticace committed Dec 13, 2021
commit 772b399dbd269565de4c1e5aeb53f6877586f017
23 changes: 23 additions & 0 deletions src/lib/generateRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,19 +248,42 @@ function parseRules(rule, cache, options = {}) {
return [cache.get(rule), options]
}

const IS_VALID_PROPERTY_NAME = /^[a-z_-]/

function isValidPropName(name) {
return IS_VALID_PROPERTY_NAME.test(name)
}

function isParsableCssValue(property, value) {
try {
postcss.parse(`a{${property}:${value}}`).toResult()
return true
} catch (err) {
return false
}
}

function extractArbitraryProperty(classCandidate, context) {
let [, property, value] = classCandidate.match(/^\[([a-zA-Z0-9-_]+):(\S+)\]$/) ?? []

if (value === undefined) {
return null
}

if (!isValidPropName(property)) {
return null
}

if (!isValidArbitraryValue(value)) {
return null
}

let normalized = normalize(value)

if (!isParsableCssValue(property, normalized)) {
return null
}

return [
[
{ sort: context.arbitraryPropertiesSort, layer: 'utilities' },
Expand Down
42 changes: 42 additions & 0 deletions tests/arbitrary-properties.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,45 @@ test('invalid class', () => {
expect(result.css).toMatchFormattedCss(css``)
})
})

test('invalid arbitrary property', () => {
let config = {
content: [
{
raw: html`<div class="[autoplay:\${autoplay}]"></div>`,
},
],
corePlugins: { preflight: false },
}

let input = css`
@tailwind base;
@tailwind components;
@tailwind utilities;
`

return run(input, config).then((result) => {
expect(result.css).toMatchFormattedCss(css``)
})
})

test('invalid arbitrary property 2', () => {
let config = {
content: [
{
raw: html`[0:02]`,
},
],
corePlugins: { preflight: false },
}

let input = css`
@tailwind base;
@tailwind components;
@tailwind utilities;
`

return run(input, config).then((result) => {
expect(result.css).toMatchFormattedCss(css``)
})
})