fix(compiler-core): fix loc.source for end tags with whitespace before >

close #10694
close #10695
This commit is contained in:
Evan You 2024-04-15 11:50:57 +08:00
parent f709238c30
commit 16174da21d
No known key found for this signature in database
GPG Key ID: B9D421896CA450FB
2 changed files with 17 additions and 1 deletions

View File

@ -2070,6 +2070,16 @@ describe('compiler: parse', () => {
baseParse(`<Foo>`, { parseMode: 'sfc', onError() {} }) baseParse(`<Foo>`, { parseMode: 'sfc', onError() {} })
expect(() => baseParse(`{ foo }`)).not.toThrow() 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', () => { describe('decodeEntities option', () => {

View File

@ -613,7 +613,7 @@ function onCloseTag(el: ElementNode, end: number, isImplied = false) {
// implied close, end should be backtracked to close // implied close, end should be backtracked to close
setLocEnd(el.loc, backTrack(end, CharCodes.Lt)) setLocEnd(el.loc, backTrack(end, CharCodes.Lt))
} else { } else {
setLocEnd(el.loc, end + 1) setLocEnd(el.loc, lookAhead(end, CharCodes.Gt) + 1)
} }
if (tokenizer.inSFCRoot) { if (tokenizer.inSFCRoot) {
@ -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) { function backTrack(index: number, c: number) {
let i = index let i = index
while (currentInput.charCodeAt(i) !== c && i >= 0) i-- while (currentInput.charCodeAt(i) !== c && i >= 0) i--