Skip to content

Commit

Permalink
fix(element): fix whitespace trim, fix koishijs/koishi#1297
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Dec 6, 2023
1 parent a3f0999 commit f951d4b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/element/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
16 changes: 10 additions & 6 deletions packages/element/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit f951d4b

Please sign in to comment.