Skip to content

Commit 7eab6e2

Browse files
authored
fix(transformer-attributify-jsx): properly extract non-valued attributes (unocss#2291)
1 parent 9e38551 commit 7eab6e2

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ export interface TransformerAttributifyJsxOptions {
3939

4040
const elementRE = /<([^>\s]*\s)((?:'.*?'|".*?"|`.*?`|\{.*?\}|[^>]*?)*)/g
4141
const attributeRE = /([a-zA-Z()#][\[?a-zA-Z0-9-_:()#%\]?]*)(?:\s*=\s*((?:'[^']*')|(?:"[^"]*")|\S+))?/g
42-
const classFilterRE = /(className|class)\s*=\s*\{[^\}]*\}/i
43-
const curlybraceRE = /\w+\s?=\s?\{.+?\}/g
42+
const valuedAttributeRE = /((?!\d|-{2}|-\d)[a-zA-Z0-9\u00A0-\uFFFF-_:!%-.~<]+)=(?:["]([^"]*)["]|[']([^']*)[']|[{]((?:[`(](?:[^`)]*)[`)]|[^}])+)[}])/gms
43+
4444
export default function transformerAttributifyJsx(options: TransformerAttributifyJsxOptions = {}): SourceCodeTransformer {
4545
const {
4646
blocklist = [],
@@ -74,12 +74,9 @@ export default function transformerAttributifyJsx(options: TransformerAttributif
7474

7575
for (const item of Array.from(code.original.matchAll(elementRE))) {
7676
// Get the length of the className part, and replace it with the equal length of empty string
77-
const classNamePart = item[2].match(classFilterRE)
7877
let attributifyPart = item[2]
79-
if (classNamePart)
80-
attributifyPart = item[2].replace(classFilterRE, ' '.repeat(classNamePart[0].length))
81-
if (curlybraceRE.test(attributifyPart))
82-
attributifyPart = attributifyPart.replace(curlybraceRE, match => ' '.repeat(match.length))
78+
if (valuedAttributeRE.test(attributifyPart))
79+
attributifyPart = attributifyPart.replace(valuedAttributeRE, match => ' '.repeat(match.length))
8380
for (const attr of attributifyPart.matchAll(attributeRE)) {
8481
const matchedRule = attr[0].replace(/\:/i, '-')
8582
if (matchedRule.includes('=') || isBlocked(matchedRule))

test/transformer-attributify-jsx.test.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@ describe('transformerAttributifyJs', () => {
2626
</div>
2727
</div>
2828
</div>
29-
<section className={cn({ 'c-red': variable > 0 })}>
30-
</section>
29+
<section
30+
className={cn({ 'c-red': variable > 0 }, 'mr-10')}
31+
mr-10
32+
className={cn({ 'c-red': variable > 0 }, 'mr-10')}
33+
></section>
3134
<div absolute bottom-5 right-0 left-0 text-center op30 fw300>
3235
on-demand · instant · fully customizable
3336
</div>
@@ -64,8 +67,11 @@ describe('transformerAttributifyJs', () => {
6467
</div>
6568
</div>
6669
</div>
67-
<section className={cn({ 'c-red': variable > 0 })}>
68-
</section>
70+
<section
71+
className={cn({ 'c-red': variable > 0 }, 'mr-10')}
72+
mr-10=\\"\\"
73+
className={cn({ 'c-red': variable > 0 }, 'mr-10')}
74+
></section>
6975
<div absolute=\\"\\" bottom-5=\\"\\" right-0=\\"\\" left-0=\\"\\" text-center=\\"\\" op30=\\"\\" fw300=\\"\\">
7076
on-demand · instant · fully customizable
7177
</div>"
@@ -100,8 +106,11 @@ describe('transformerAttributifyJs', () => {
100106
</div>
101107
</div>
102108
</div>
103-
<section className={cn({ 'c-red': variable > 0 })}>
104-
</section>
109+
<section
110+
className={cn({ 'c-red': variable > 0 }, 'mr-10')}
111+
mr-10=\\"\\"
112+
className={cn({ 'c-red': variable > 0 }, 'mr-10')}
113+
></section>
105114
<div absolute bottom-5=\\"\\" right-0=\\"\\" left-0=\\"\\" text-center=\\"\\" op30=\\"\\" fw300=\\"\\">
106115
on-demand · instant · fully customizable
107116
</div>"

0 commit comments

Comments
 (0)