From eb816e7078d373c36f8d159f0ea9d08f9f0b8990 Mon Sep 17 00:00:00 2001 From: MellowCo <799478052@qq.com> Date: Sat, 23 Jul 2022 10:30:27 +0800 Subject: [PATCH] fix: bracket curly match https://github.com/unocss/unocss/issues/1300 --- src/utils/handlers/handlers.ts | 30 +++++++++++++++++++++++------- test/handler.test.ts | 8 ++++++++ 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/utils/handlers/handlers.ts b/src/utils/handlers/handlers.ts index a561799..c09a983 100644 --- a/src/utils/handlers/handlers.ts +++ b/src/utils/handlers/handlers.ts @@ -129,14 +129,30 @@ function bracketWithType(str: string, type?: string) { else if (type && match[1] === type) base = str.slice(match[0].length, -1) - if (base !== undefined) { - return base - .replace(/(url\(.*?\))/g, v => v.replace(/_/g, '\\_')) - .replace(/([^\\])_/g, '$1 ') - .replace(/(?:calc|clamp|max|min)\((.*)/g, (v) => { - return v.replace(/(-?\d*\.?\d(?!\b-.+[,)](?![^+\-/*])\D)(?:%|[a-z]+)?|\))([+\-/*])/g, '$1 $2 ') - }) + if (!base) + return + + let curly = 0 + for (const i of base) { + if (i === '[') { + curly += 1 + } + else if (i === ']') { + curly -= 1 + if (curly < 0) + return + } } + + if (curly) + return + + return base + .replace(/(url\(.*?\))/g, v => v.replace(/_/g, '\\_')) + .replace(/([^\\])_/g, '$1 ') + .replace(/(?:calc|clamp|max|min)\((.*)/g, (v) => { + return v.replace(/(-?\d*\.?\d(?!\b-.+[,)](?![^+\-/*])\D)(?:%|[a-z]+)?|\))([+\-/*])/g, '$1 $2 ') + }) } } diff --git a/test/handler.test.ts b/test/handler.test.ts index 85f786b..4c9d2f7 100644 --- a/test/handler.test.ts +++ b/test/handler.test.ts @@ -12,4 +12,12 @@ describe('value handler', () => { expect(h.bracket('[calc(1/2)]')).eql('calc(1 / 2)') expect(h.bracket('[calc(1*2)]')).eql('calc(1 * 2)') }) + + it('bracket curly', () => { + expect(h.bracket('[foo][bar]')).eql(undefined) + expect(h.bracket('[[]]')).eql('[]') + expect(h.bracket('[]]')).eql(undefined) + expect(h.bracket('[]][[]]]]')).eql(undefined) + expect(h.bracket('[[][[[]]]]')).eql('[][[[]]]') + }) })