Skip to content

Commit

Permalink
fix(preset-mini): bracket curly match, close #1300
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jul 19, 2022
1 parent aa7b86f commit 54d0e32
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
29 changes: 22 additions & 7 deletions packages/preset-mini/src/utils/handlers/handlers.ts
Expand Up @@ -102,14 +102,29 @@ 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 ')
})
}
}

Expand Down
8 changes: 8 additions & 0 deletions test/handler.test.ts
Expand Up @@ -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('[][[[]]]')
})
})

0 comments on commit 54d0e32

Please sign in to comment.