Просмотр исходного кода

fix(compiler-core): avoid crash on CDATA at the document root (#14916)

Sarath Francis 5 дней назад
Родитель
Сommit
0ea17e232f

+ 29 - 0
packages/compiler-core/__tests__/__snapshots__/parse.spec.ts.snap

@@ -496,6 +496,35 @@ exports[`compiler: parse > Edge Cases > valid html 1`] = `
 }
 `;
 
+exports[`compiler: parse > Errors > CDATA_IN_HTML_CONTENT > <![CDATA[cdata]]> 1`] = `
+{
+  "cached": [],
+  "children": [],
+  "codegenNode": undefined,
+  "components": [],
+  "directives": [],
+  "helpers": Set {},
+  "hoists": [],
+  "imports": [],
+  "loc": {
+    "end": {
+      "column": 18,
+      "line": 1,
+      "offset": 17,
+    },
+    "source": "<![CDATA[cdata]]>",
+    "start": {
+      "column": 1,
+      "line": 1,
+      "offset": 0,
+    },
+  },
+  "source": "<![CDATA[cdata]]>",
+  "temps": 0,
+  "type": 0,
+}
+`;
+
 exports[`compiler: parse > Errors > CDATA_IN_HTML_CONTENT > <template><![CDATA[cdata]]></template> 1`] = `
 {
   "cached": [],

+ 10 - 0
packages/compiler-core/__tests__/parse.spec.ts

@@ -2597,6 +2597,16 @@ describe('compiler: parse', () => {
           code: '<template><svg><![CDATA[cdata]]></svg></template>',
           errors: [],
         },
+        {
+          // invalid root-level CDATA should report a parser error
+          code: '<![CDATA[cdata]]>',
+          errors: [
+            {
+              type: ErrorCodes.CDATA_IN_HTML_CONTENT,
+              loc: { offset: 0, line: 1, column: 1 },
+            },
+          ],
+        },
       ],
       DUPLICATE_ATTRIBUTE: [
         {

+ 1 - 1
packages/compiler-core/src/parser.ts

@@ -470,7 +470,7 @@ const tokenizer = new Tokenizer(stack, {
   },
 
   oncdata(start, end) {
-    if (stack[0].ns !== Namespaces.HTML) {
+    if ((stack[0] ? stack[0].ns : currentOptions.ns) !== Namespaces.HTML) {
       onText(getSlice(start, end), start, end)
     } else {
       emitError(ErrorCodes.CDATA_IN_HTML_CONTENT, start - 9)