From f951d4bb93dc07f6882bc98a4f1e92b2ccff8292 Mon Sep 17 00:00:00 2001 From: Shigma Date: Wed, 6 Dec 2023 23:57:08 +0800 Subject: [PATCH] fix(element): fix whitespace trim, fix koishijs/koishi#1297 --- packages/element/package.json | 2 +- packages/element/src/index.ts | 16 ++++++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/packages/element/package.json b/packages/element/package.json index 6ecfd3fa..0f55f322 100644 --- a/packages/element/package.json +++ b/packages/element/package.json @@ -1,7 +1,7 @@ { "name": "@satorijs/element", "description": "Element Manipulation", - "version": "2.6.0", + "version": "2.6.1", "main": "lib/index.cjs", "module": "lib/index.mjs", "types": "lib/index.d.ts", diff --git a/packages/element/src/index.ts b/packages/element/src/index.ts index 34e5b7f3..e2e02a7f 100644 --- a/packages/element/src/index.ts +++ b/packages/element/src/index.ts @@ -270,8 +270,11 @@ namespace Element { const tagRegExp = context ? tagRegExp2 : tagRegExp1 let tagCap: RegExpExecArray + let trimStart = true while ((tagCap = tagRegExp.exec(source))) { - parseContent(source.slice(0, tagCap.index)) + const trimEnd = !tagCap.groups.curly + parseContent(source.slice(0, tagCap.index), trimStart, trimEnd) + trimStart = trimEnd source = source.slice(tagCap.index + tagCap[0].length) const [_, , , close, type, extra, empty] = tagCap if (tagCap.groups.comment) continue @@ -304,11 +307,12 @@ namespace Element { }) } - parseContent(source) - function parseContent(source: string) { - pushText(unescape(source - .replace(/^\s*\n\s*/, '') - .replace(/\s*\n\s*$/, ''))) + parseContent(source, trimStart, true) + function parseContent(source: string, trimStart: boolean, trimEnd: boolean) { + source = unescape(source) + if (trimStart) source = source.replace(/^\s*\n\s*/, '') + if (trimEnd) source = source.replace(/\s*\n\s*$/, '') + pushText(source) } return parseTokens(foldTokens(tokens), context)