From f57c2f90d8d084d121098d27f1da0dd1f86efb8a Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Mon, 2 Oct 2023 16:19:23 +0200 Subject: [PATCH] Improve RegEx parser, reduce possibilities as the key for arbitrary properties (#12121) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * optimize handling of RegEx parser results Previous: - Copy `results`, for every subsequent result of other `patterns` - Loop over results to filter out `undefined` values - Loop over results to map to `clipAtBalancedParens` Current: - For each candidate, push the `clipAtBalancedParens(candidate)` into the `results` This way we are not copying existing results, and we are also avoiding additional loops over the entire array to filter out `undefined` values and map to `clipAtBalancedParens`. * do not allow `]` in the first part of arbitrary properties ``` [foo:bar] ─┬─ └── This part cannot contain `]` ``` This is also a very targeted fix for when the arbitrary properties seem to match a large piece of text, but shouldn't * add real world tests for parsing candidate strings * sync package-lock.json * update changelog --- CHANGELOG.md | 1 + src/lib/defaultExtractor.js | 8 +++++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ef780e40e86..a637d0352c1e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Update types to work with `Node16` module resolution ([#12097](https://github.com/tailwindlabs/tailwindcss/pull/12097)) - Don’t crash when important and parent selectors are equal in `@apply` ([#12112](https://github.com/tailwindlabs/tailwindcss/pull/12112)) - Eliminate irrelevant rules when applying variants ([#12113](https://github.com/tailwindlabs/tailwindcss/pull/12113)) +- Improve RegEx parser, reduce possibilities as the key for arbitrary properties ([#12121](https://github.com/tailwindlabs/tailwindcss/pull/12121)) ## [3.3.3] - 2023-07-13 diff --git a/src/lib/defaultExtractor.js b/src/lib/defaultExtractor.js index c56597e92e9e..5d63ef653101 100644 --- a/src/lib/defaultExtractor.js +++ b/src/lib/defaultExtractor.js @@ -12,10 +12,12 @@ export function defaultExtractor(context) { let results = [] for (let pattern of patterns) { - results = [...results, ...(content.match(pattern) ?? [])] + for (let result of content.match(pattern) ?? []) { + results.push(clipAtBalancedParens(result)) + } } - return results.filter((v) => v !== undefined).map(clipAtBalancedParens) + return results } } @@ -34,7 +36,7 @@ function* buildRegExps(context) { // This is a targeted fix to continue to allow theme() // with square brackets to work in arbitrary properties // while fixing a problem with the regex matching too much - /\[[^\s:'"`]+:[^\s]+?\[[^\s]+\][^\s]+?\]/, + /\[[^\s:'"`\]]+:[^\s]+?\[[^\s]+\][^\s]+?\]/, // Utilities regex.pattern([