Skip to content

Commit

Permalink
fix: newline in variant group
Browse files Browse the repository at this point in the history
  • Loading branch information
lvjiaxuan committed Apr 23, 2023
1 parent c08c2e1 commit 33589b1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
10 changes: 8 additions & 2 deletions index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ export default {

const attributeValuesGroupRegex = new RegExp(`(&\\[${ nameRegexStr }=)\\((${ valueRegexStr })\\)`, 'gm')
const dataAttributeValuesGroupRegex = new RegExp(`(data-\\[${ nameRegexStr }=)\\((${ valueRegexStr })\\)\\]:(${ nameRegexStr }|\\(${ valueRegexStr }\\))`, 'gm')
// Remove the newline in parentheses
const removeRegex = new RegExp(`\\]:\\((${ valueRegexStr })\\)`, 'gm')

const str = code.toString().replace(
const str = code.toString()
.replace(
removeRegex,
(from, variant) => `]:(${ variant.replace(/[\n\r]?/g, '').replace(/ {2,}/g, ' ') })`
).replace(
attributeValuesGroupRegex,
(from, pre, values) => values
.split(/\s/g)
Expand All @@ -20,7 +26,7 @@ export default {
(from, pre, values, variant) => values
.split(/\s/g)
.filter(Boolean)
.map(i => `${ pre }${i}]:${variant}`)
.map(i => `${ pre }${ i }]:${ variant.replace(/[\n\r]?/g, '').replace(/ {2,}/g, ' ') }`)
.join(' ')
)

Expand Down
10 changes: 10 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ describe('group attribute values', () => {
expect(multi).toMatchInlineSnapshot('"[&[type=number],&[type=text],&[aa=bb],&[aa=cc]]:(c-red m-1)"')
})

it('with newline', () => {
const newline = main('[&[type=(number\n text)],&[aa=(bb cc)]]:(\n c-red \n m-1\n p-1\n )')
expect(newline).toMatchInlineSnapshot('"[&[type=number],&[type=text],&[aa=bb],&[aa=cc]]:( c-red m-1 p-1 )"')
})

it('empty group', () => {
const empty = main('[&[type=()]]:p-1')
expect(empty).toMatchInlineSnapshot('"[&[type=()]]:p-1"')
Expand All @@ -47,6 +52,11 @@ describe('group data-attributes values', () => {
expect(multi).toMatchInlineSnapshot('"data-[xxx=foo]:(p-1 m-1) data-[xxx=bar]:(p-1 m-1)"')
})

it('with newline', () => {
const newline = main('data-[xxx=(foo\n bar)]:(\n c-red \n m-1\n p-1\n )')
expect(newline).toMatchInlineSnapshot('"data-[xxx=foo]:( c-red m-1 p-1 ) data-[xxx=bar]:( c-red m-1 p-1 )"')
})

it('empty', () => {
const empty = main('data-[xxx=()]:p-1')
expect(empty).toMatchInlineSnapshot('"data-[xxx=()]:p-1"')
Expand Down

0 comments on commit 33589b1

Please sign in to comment.