Skip to content

Commit 16174da

Browse files
committed
fix(compiler-core): fix loc.source for end tags with whitespace before >
close #10694 close #10695
1 parent f709238 commit 16174da

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

packages/compiler-core/__tests__/parse.spec.ts

+10
Original file line numberDiff line numberDiff line change
@@ -2070,6 +2070,16 @@ describe('compiler: parse', () => {
20702070
baseParse(`<Foo>`, { parseMode: 'sfc', onError() {} })
20712071
expect(() => baseParse(`{ foo }`)).not.toThrow()
20722072
})
2073+
2074+
test('correct loc when the closing > is foarmatted', () => {
2075+
const [span] = baseParse(`<span></span
2076+
2077+
>`).children
2078+
2079+
expect(span.loc.source).toBe('<span></span\n \n >')
2080+
expect(span.loc.start.offset).toBe(0)
2081+
expect(span.loc.end.offset).toBe(27)
2082+
})
20732083
})
20742084

20752085
describe('decodeEntities option', () => {

packages/compiler-core/src/parser.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ function onCloseTag(el: ElementNode, end: number, isImplied = false) {
613613
// implied close, end should be backtracked to close
614614
setLocEnd(el.loc, backTrack(end, CharCodes.Lt))
615615
} else {
616-
setLocEnd(el.loc, end + 1)
616+
setLocEnd(el.loc, lookAhead(end, CharCodes.Gt) + 1)
617617
}
618618

619619
if (tokenizer.inSFCRoot) {
@@ -736,6 +736,12 @@ function onCloseTag(el: ElementNode, end: number, isImplied = false) {
736736
}
737737
}
738738

739+
function lookAhead(index: number, c: number) {
740+
let i = index
741+
while (currentInput.charCodeAt(i) !== c && i < currentInput.length - 1) i++
742+
return i
743+
}
744+
739745
function backTrack(index: number, c: number) {
740746
let i = index
741747
while (currentInput.charCodeAt(i) !== c && i >= 0) i--

0 commit comments

Comments
 (0)