]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-core): fix loc.source for end tags with whitespace before >
authorEvan You <yyx990803@gmail.com>
Mon, 15 Apr 2024 03:50:57 +0000 (11:50 +0800)
committerEvan You <yyx990803@gmail.com>
Mon, 15 Apr 2024 03:50:57 +0000 (11:50 +0800)
close #10694
close #10695

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

index 882133a53872ed740a4b23e2c06bfe9fb19999f6..429a796861698ffc0a0a72f574343e36c93084b3 100644 (file)
@@ -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', () => {
index 202fba81b1870e707d645fa186e4f375b369ef1c..da8861b92375574fed03ca5abee619707e8eb7ac 100644 (file)
@@ -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) {
@@ -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--