From 4654395f8d554a2b51d4789e900fa1442cdba4cb Mon Sep 17 00:00:00 2001 From: Neil Date: Tue, 30 Aug 2022 20:38:15 +0800 Subject: [PATCH] fix: '/' in tags, resolve #5 --- packages/transformer-attributify/src/index.ts | 5 +++-- playground/src/pages/index/index.vue | 3 ++- test/fixtures/attributify.vue | 9 +++++++++ test/transformer-attributify.test.ts | 18 +++++++++++++++++- 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/packages/transformer-attributify/src/index.ts b/packages/transformer-attributify/src/index.ts index adf1b00..e0eb9b2 100644 --- a/packages/transformer-attributify/src/index.ts +++ b/packages/transformer-attributify/src/index.ts @@ -76,12 +76,13 @@ export default function transformerAttributify(options: TransformerAttributifyOp } if (attrSelectors.length) { if (!existsClass) { - code.overwrite(start, start + eleMatch[0].length, `${matchStrTemp.slice(0, -1)} class="${attrSelectors.join(' ')}"${matchStrTemp.slice(-1)}`) + const sliceNum = matchStrTemp.endsWith('/>') ? -2 : -1 + matchStrTemp = `${matchStrTemp.slice(0, sliceNum)} class="${attrSelectors.join(' ')}"${matchStrTemp.slice(sliceNum)}` } else { matchStrTemp = matchStrTemp.replace(existsClass, `${existsClass} ${attrSelectors.join(' ')}`) - code.overwrite(start, start + eleMatch[0].length, matchStrTemp) } + code.overwrite(start, start + eleMatch[0].length, matchStrTemp) } } diff --git a/playground/src/pages/index/index.vue b/playground/src/pages/index/index.vue index 9cce6b9..2d308d1 100644 --- a/playground/src/pages/index/index.vue +++ b/playground/src/pages/index/index.vue @@ -18,7 +18,8 @@ const bool = true
{{ `index${index + 1}` }} -
+ +
1 diff --git a/test/fixtures/attributify.vue b/test/fixtures/attributify.vue index 375852e..0c7eace 100644 --- a/test/fixtures/attributify.vue +++ b/test/fixtures/attributify.vue @@ -1,17 +1,26 @@