Improve extractor for arbitrary values with quotes (#4817)

Fixes #4801.
This commit is contained in:
Adam Wathan 2021-06-26 10:20:49 -04:00 • committed by GitHub
parent 59c7e32a3b
commit a6e79f9e85
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 64 additions and 1 deletions

View file

@ -6,7 +6,13 @@ import cloneNodes from '../../util/cloneNodes'
let env = sharedState.env
let contentMatchCache = sharedState.contentMatchCache
const BROAD_MATCH_GLOBAL_REGEXP = /([^<>"'`\s]*\[[^<>\s]+\])|([^<>"'`\s]*[^<>"'`\s:])/g
const PATTERNS = [
"([^<>\"'`\\s]*\\['[^<>\"'`\\s]*'\\])", // `content-['hello']` but not `content-['hello']']`
'([^<>"\'`\\s]*\\["[^<>"\'`\\s]*"\\])', // `content-["hello"]` but not `content-["hello"]"]`
'([^<>"\'`\\s]*\\[[^<>"\'`\\s]+\\])', // `fill-[#bada55]`
'([^<>"\'`\\s]*[^<>"\'`\\s:])', // `px-1.5`, `uppercase` but not `uppercase:`
].join('|')
const BROAD_MATCH_GLOBAL_REGEXP = new RegExp(PATTERNS, 'g')
const INNER_MATCH_GLOBAL_REGEXP = /[^<>"'`\s.(){}[\]#=%]*[^<>"'`\s.(){}[\]#=%:]/g
const builtInExtractors = {

View file

@ -0,0 +1,57 @@
import postcss from 'postcss'
import path from 'path'
import tailwind from '../../src/jit/index.js'
function run(input, config = {}) {
const { currentTestName } = expect.getState()
return postcss(tailwind(config)).process(input, {
from: `${path.resolve(__filename)}?test=${currentTestName}`,
})
}
test('PHP arrays', async () => {
let config = {
mode: 'jit',
purge: [
{
raw: `<h1 class="<?php echo wrap(['class' => "max-w-[16rem]"]); ?>">Hello world</h1>`,
},
],
theme: {},
plugins: [],
}
let css = `@tailwind utilities`
return run(css, config).then((result) => {
expect(result.css).toMatchFormattedCss(`
.max-w-\\[16rem\\] {
max-width: 16rem;
}
`)
})
})
test('arbitrary values with quotes', async () => {
let config = {
mode: 'jit',
purge: [
{
raw: `<div class="content-['hello]']"></div>`,
},
],
theme: {},
plugins: [],
}
let css = `@tailwind utilities`
return run(css, config).then((result) => {
expect(result.css).toMatchFormattedCss(`
.content-\\[\\'hello\\]\\'\\] {
content: 'hello]';
}
`)
})
})