Skip to content

Commit

Permalink
Eliminate recursion from candidatePermutations (#7331)
Browse files Browse the repository at this point in the history
  • Loading branch information
thecrypticace authored Feb 7, 2022
1 parent 8e38bbc commit dc6644e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
40 changes: 20 additions & 20 deletions src/lib/generateRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,33 @@ function getClassNameFromSelector(selector) {
// Example with dynamic classes:
// ['grid-cols', '[[linename],1fr,auto]']
// ['grid', 'cols-[[linename],1fr,auto]']
function* candidatePermutations(candidate, lastIndex = Infinity) {
if (lastIndex < 0) {
return
}
function* candidatePermutations(candidate) {
let lastIndex = Infinity

let dashIdx
while (lastIndex >= 0) {
let dashIdx

if (lastIndex === Infinity && candidate.endsWith(']')) {
let bracketIdx = candidate.indexOf('[')
if (lastIndex === Infinity && candidate.endsWith(']')) {
let bracketIdx = candidate.indexOf('[')

// If character before `[` isn't a dash or a slash, this isn't a dynamic class
// eg. string[]
dashIdx = ['-', '/'].includes(candidate[bracketIdx - 1]) ? bracketIdx - 1 : -1
} else {
dashIdx = candidate.lastIndexOf('-', lastIndex)
}
// If character before `[` isn't a dash or a slash, this isn't a dynamic class
// eg. string[]
dashIdx = ['-', '/'].includes(candidate[bracketIdx - 1]) ? bracketIdx - 1 : -1
} else {
dashIdx = candidate.lastIndexOf('-', lastIndex)
}

if (dashIdx < 0) {
return
}
if (dashIdx < 0) {
break
}

let prefix = candidate.slice(0, dashIdx)
let modifier = candidate.slice(dashIdx + 1)
let prefix = candidate.slice(0, dashIdx)
let modifier = candidate.slice(dashIdx + 1)

yield [prefix, modifier]
yield [prefix, modifier]

yield* candidatePermutations(candidate, dashIdx - 1)
lastIndex = dashIdx - 1
}
}

function applyPrefix(matches, context) {
Expand Down
16 changes: 16 additions & 0 deletions tests/basic-usage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,19 @@ it('shadows support values without a leading zero', () => {
`)
})
})

it('can scan extremely long classes without crashing', () => {
let val = 'cols-' + '-a'.repeat(65536)
let config = {
content: [{ raw: html`<div class="${val}"></div>` }],
corePlugins: { preflight: false },
}

let input = css`
@tailwind utilities;
`

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

0 comments on commit dc6644e

Please sign in to comment.