Skip to content

Commit 5d472fa

Browse files
authored
fix(transformer-attributify-jsx): improve attribute capturing (unocss#2334)
1 parent 4c49ff7 commit 5d472fa

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

packages/transformer-attributify-jsx/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export interface TransformerAttributifyJsxOptions {
3737
exclude?: FilterPattern
3838
}
3939

40-
const elementRE = /<([^>\s]*\s)((?:'.*?'|".*?"|`.*?`|\{.*?\}|[^>]*?)*)/g
40+
const elementRE = /(<\w[\w:\.$-]*\s)((?:'[^>]*?'|"[^>]*?"|`(?:[^>]|[\S])*?`|\{(?:[^>]|[\S])*?\}|[^>]*?)*)/g
4141
const attributeRE = /([a-zA-Z()#][\[?a-zA-Z0-9-_:()#%\]?]*)(?:\s*=\s*((?:'[^']*')|(?:"[^"]*")|\S+))?/g
4242
const valuedAttributeRE = /((?!\d|-{2}|-\d)[a-zA-Z0-9\u00A0-\uFFFF-_:!%-.~<]+)=(?:["]([^"]*)["]|[']([^']*)[']|[{]((?:[`(](?:[^`)]*)[`)]|[^}])+)[}])/gms
4343

@@ -85,7 +85,7 @@ export default function transformerAttributifyJsx(options: TransformerAttributif
8585
tasks.push(uno.parseToken(matchedRule).then((matched) => {
8686
if (matched) {
8787
const tag = item[1]
88-
const startIdx = (item.index || 0) + (attr.index || 0) + tag.length + 1
88+
const startIdx = (item.index || 0) + (attr.index || 0) + tag.length
8989
const endIdx = startIdx + matchedRule.length
9090
code.overwrite(startIdx, endIdx, `${matchedRule}=""`)
9191
}

test/transformer-attributify-jsx.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ describe('transformerAttributifyJs', () => {
3131
mr-10
3232
className={cn({ 'c-red': variable > 0 }, 'mr-10')}
3333
></section>
34+
<div
35+
label={
36+
<b>1</b>
37+
}
38+
></div>
3439
<div absolute bottom-5 right-0 left-0 text-center op30 fw300>
3540
on-demand · instant · fully customizable
3641
</div>
@@ -72,6 +77,11 @@ describe('transformerAttributifyJs', () => {
7277
mr-10=\\"\\"
7378
className={cn({ 'c-red': variable > 0 }, 'mr-10')}
7479
></section>
80+
<div
81+
label={
82+
<b>1</b>
83+
}
84+
></div>
7585
<div absolute=\\"\\" bottom-5=\\"\\" right-0=\\"\\" left-0=\\"\\" text-center=\\"\\" op30=\\"\\" fw300=\\"\\">
7686
on-demand · instant · fully customizable
7787
</div>"
@@ -111,6 +121,11 @@ describe('transformerAttributifyJs', () => {
111121
mr-10=\\"\\"
112122
className={cn({ 'c-red': variable > 0 }, 'mr-10')}
113123
></section>
124+
<div
125+
label={
126+
<b>1</b>
127+
}
128+
></div>
114129
<div absolute bottom-5=\\"\\" right-0=\\"\\" left-0=\\"\\" text-center=\\"\\" op30=\\"\\" fw300=\\"\\">
115130
on-demand · instant · fully customizable
116131
</div>"

0 commit comments

Comments
 (0)