Skip to content

Commit

Permalink
fix(compiler-core): fix loc.source for end tags with whitespace before >
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 authored and lynxlangya committed May 30, 2024
1 parent 9e10ef9 commit cf501b6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
10 changes: 10 additions & 0 deletions packages/compiler-core/__tests__/parse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2070,6 +2070,16 @@ describe('compiler: parse', () => {
baseParse(`<Foo>`, { parseMode: 'sfc', onError() {} })
expect(() => baseParse(`{ foo }`)).not.toThrow()
})

test('correct loc when the closing > is foarmatted', () => {
const [span] = baseParse(`<span></span
>`).children

expect(span.loc.source).toBe('<span></span\n \n >')
expect(span.loc.start.offset).toBe(0)
expect(span.loc.end.offset).toBe(27)
})
})

describe('decodeEntities option', () => {
Expand Down
8 changes: 7 additions & 1 deletion packages/compiler-core/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ function onCloseTag(el: ElementNode, end: number, isImplied = false) {
// implied close, end should be backtracked to close
setLocEnd(el.loc, backTrack(end, CharCodes.Lt))
} else {
setLocEnd(el.loc, end + 1)
setLocEnd(el.loc, lookAhead(end, CharCodes.Gt) + 1)
}

if (tokenizer.inSFCRoot) {
Expand Down Expand Up @@ -736,6 +736,12 @@ function onCloseTag(el: ElementNode, end: number, isImplied = false) {
}
}

function lookAhead(index: number, c: number) {
let i = index
while (currentInput.charCodeAt(i) !== c && i < currentInput.length - 1) i++
return i
}

function backTrack(index: number, c: number) {
let i = index
while (currentInput.charCodeAt(i) !== c && i >= 0) i--
Expand Down

0 comments on commit cf501b6

Please sign in to comment.