Skip to content

Commit 0ea17e2

Browse files
fix(compiler-core): avoid crash on CDATA at the document root (#14916)
1 parent db3e117 commit 0ea17e2

3 files changed

Lines changed: 40 additions & 1 deletion

File tree

‎packages/compiler-core/__tests__/__snapshots__/parse.spec.ts.snap‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,35 @@ exports[`compiler: parse > Edge Cases > valid html 1`] = `
496496
}
497497
`;
498498
499+
exports[`compiler: parse > Errors > CDATA_IN_HTML_CONTENT > <![CDATA[cdata]]> 1`] = `
500+
{
501+
"cached": [],
502+
"children": [],
503+
"codegenNode": undefined,
504+
"components": [],
505+
"directives": [],
506+
"helpers": Set {},
507+
"hoists": [],
508+
"imports": [],
509+
"loc": {
510+
"end": {
511+
"column": 18,
512+
"line": 1,
513+
"offset": 17,
514+
},
515+
"source": "<![CDATA[cdata]]>",
516+
"start": {
517+
"column": 1,
518+
"line": 1,
519+
"offset": 0,
520+
},
521+
},
522+
"source": "<![CDATA[cdata]]>",
523+
"temps": 0,
524+
"type": 0,
525+
}
526+
`;
527+
499528
exports[`compiler: parse > Errors > CDATA_IN_HTML_CONTENT > <template><![CDATA[cdata]]></template> 1`] = `
500529
{
501530
"cached": [],

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2597,6 +2597,16 @@ describe('compiler: parse', () => {
25972597
code: '<template><svg><![CDATA[cdata]]></svg></template>',
25982598
errors: [],
25992599
},
2600+
{
2601+
// invalid root-level CDATA should report a parser error
2602+
code: '<![CDATA[cdata]]>',
2603+
errors: [
2604+
{
2605+
type: ErrorCodes.CDATA_IN_HTML_CONTENT,
2606+
loc: { offset: 0, line: 1, column: 1 },
2607+
},
2608+
],
2609+
},
26002610
],
26012611
DUPLICATE_ATTRIBUTE: [
26022612
{

‎packages/compiler-core/src/parser.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ const tokenizer = new Tokenizer(stack, {
470470
},
471471

472472
oncdata(start, end) {
473-
if (stack[0].ns !== Namespaces.HTML) {
473+
if ((stack[0] ? stack[0].ns : currentOptions.ns) !== Namespaces.HTML) {
474474
onText(getSlice(start, end), start, end)
475475
} else {
476476
emitError(ErrorCodes.CDATA_IN_HTML_CONTENT, start - 9)

0 commit comments

Comments
 (0)