Skip to content

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

Merged
edison1105 merged 2 commits into
vuejs:mainfrom
sarathfrancis90:fix/cdata-root-crash
Jun 11, 2026
Merged

fix(compiler-core): avoid crash on CDATA at the document root#14916
edison1105 merged 2 commits into
vuejs:mainfrom
sarathfrancis90:fix/cdata-root-crash

Conversation

@sarathfrancis90

@sarathfrancis90 sarathfrancis90 commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

What kind of change does this PR introduce?

Bug fix.

Description

A CDATA section with no open element on the stack — e.g. at the document root — crashes the parser:

import { compile } from '@vue/compiler-dom'
compile('<![CDATA[x]]>')
// TypeError: Cannot read properties of undefined (reading 'ns')

oncdata reads stack[0].ns unconditionally, but stack[0] is undefined when there's no enclosing element. The same handler works inside an element (<div><![CDATA[x]]></div>), so this only affects top-level CDATA.

This falls back to the root namespace (currentOptions.ns) when the stack is empty — mirroring the existing onprocessinginstruction handler right below it — so top-level CDATA now reports CDATA_IN_HTML_CONTENT instead of throwing. Behavior inside elements and in SVG/foreign content is unchanged. Added a parser error-test case for the root position.

Summary by CodeRabbit

  • Bug Fixes

    • Prevented a crash when encountering CDATA in certain parsing scenarios while preserving correct CDATA-in-HTML error reporting.
  • Tests

    • Added coverage for root-level CDATA parsing to ensure the parser reports the expected error and remains stable.

oncdata read stack[0].ns unconditionally, but when a CDATA section
appears with no open element on the stack (for example at the document
root), stack[0] is undefined and parsing threw "Cannot read properties
of undefined (reading 'ns')".

Fall back to the root namespace (currentOptions.ns) when the stack is
empty, mirroring onprocessinginstruction, so a top-level CDATA section
reports CDATA_IN_HTML_CONTENT instead of crashing.
@coderabbitai

coderabbitai Bot commented Jun 4, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: b7acb90b-8007-48ae-b8cd-d1ef16916cfd

📥 Commits

Reviewing files that changed from the base of the PR and between 7586305 and 3ab4554.

📒 Files selected for processing (1)
  • packages/compiler-core/__tests__/parse.spec.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/compiler-core/tests/parse.spec.ts

📝 Walkthrough

Walkthrough

Fixes a null-access in the compiler's CDATA handler by resolving namespace from the top of the element stack or falling back to options; adds a test asserting a root-level <![CDATA[cdata]]> reports CDATA_IN_HTML_CONTENT at document start.

Changes

CDATA root-level handling

Layer / File(s) Summary
Parser null-safety fix
packages/compiler-core/src/parser.ts
oncdata now reads namespace via a null-safe expression that uses stack[0].ns when present, otherwise currentOptions.ns.
Root-level CDATA test
packages/compiler-core/__tests__/parse.spec.ts
Adds a CDATA_IN_HTML_CONTENT pattern for root-level <![CDATA[cdata]]>, asserting the error at offset 0 (line 1, column 1).

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

🐰 I nibbled a bug in the parser's root,
A CDATA alone with no enclosing boot,
A safe little fallback now keeps it fine,
No crash, just a test and a tidy line. 🌙

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title accurately describes the main fix: preventing a crash when CDATA appears at the document root by handling the empty stack case.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@edison1105 edison1105 added 🍰 p2-nice-to-have Priority 2: this is not breaking anything but nice to have it addressed. ready to merge The PR is ready to be merged. labels Jun 4, 2026
@github-actions

github-actions Bot commented Jun 4, 2026

Copy link
Copy Markdown

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 106 kB 40.1 kB 36 kB
vue.global.prod.js 164 kB (+14 B) 60.1 kB (-3 B) 53.4 kB (-75 B)

Usages

Name Size Gzip Brotli
createApp (CAPI only) 48.8 kB 19 kB 17.4 kB
createApp 56.9 kB 22 kB 20.1 kB
createSSRApp 61.2 kB 23.8 kB 21.7 kB
defineCustomElement 63.1 kB 23.9 kB 21.8 kB
overall 71.7 kB 27.4 kB 25 kB

@pkg-pr-new

pkg-pr-new Bot commented Jun 4, 2026

Copy link
Copy Markdown

Open in StackBlitz

@vue/compiler-core

pnpm add https://pkg.pr.new/@vue/compiler-core@14916
npm i https://pkg.pr.new/@vue/compiler-core@14916
yarn add https://pkg.pr.new/@vue/compiler-core@14916.tgz

@vue/compiler-dom

pnpm add https://pkg.pr.new/@vue/compiler-dom@14916
npm i https://pkg.pr.new/@vue/compiler-dom@14916
yarn add https://pkg.pr.new/@vue/compiler-dom@14916.tgz

@vue/compiler-sfc

pnpm add https://pkg.pr.new/@vue/compiler-sfc@14916
npm i https://pkg.pr.new/@vue/compiler-sfc@14916
yarn add https://pkg.pr.new/@vue/compiler-sfc@14916.tgz

@vue/compiler-ssr

pnpm add https://pkg.pr.new/@vue/compiler-ssr@14916
npm i https://pkg.pr.new/@vue/compiler-ssr@14916
yarn add https://pkg.pr.new/@vue/compiler-ssr@14916.tgz

@vue/reactivity

pnpm add https://pkg.pr.new/@vue/reactivity@14916
npm i https://pkg.pr.new/@vue/reactivity@14916
yarn add https://pkg.pr.new/@vue/reactivity@14916.tgz

@vue/runtime-core

pnpm add https://pkg.pr.new/@vue/runtime-core@14916
npm i https://pkg.pr.new/@vue/runtime-core@14916
yarn add https://pkg.pr.new/@vue/runtime-core@14916.tgz

@vue/runtime-dom

pnpm add https://pkg.pr.new/@vue/runtime-dom@14916
npm i https://pkg.pr.new/@vue/runtime-dom@14916
yarn add https://pkg.pr.new/@vue/runtime-dom@14916.tgz

@vue/server-renderer

pnpm add https://pkg.pr.new/@vue/server-renderer@14916
npm i https://pkg.pr.new/@vue/server-renderer@14916
yarn add https://pkg.pr.new/@vue/server-renderer@14916.tgz

@vue/shared

pnpm add https://pkg.pr.new/@vue/shared@14916
npm i https://pkg.pr.new/@vue/shared@14916
yarn add https://pkg.pr.new/@vue/shared@14916.tgz

vue

pnpm add https://pkg.pr.new/vue@14916
npm i https://pkg.pr.new/vue@14916
yarn add https://pkg.pr.new/vue@14916.tgz

@vue/compat

pnpm add https://pkg.pr.new/@vue/compat@14916
npm i https://pkg.pr.new/@vue/compat@14916
yarn add https://pkg.pr.new/@vue/compat@14916.tgz

commit: 3ab4554

@edison1105

Copy link
Copy Markdown
Member

/ecosystem-ci run

@vue-bot

vue-bot commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

📝 Ran ecosystem CI: Open

suite result latest scheduled
vant success success
quasar success success
pinia success success
router success failure
language-tools success success
primevue success success
test-utils success success
nuxt success success
vite-plugin-vue success success
radix-vue failure failure
vue-simple-compiler success failure
vue-macros success failure
vuetify success failure
vueuse success failure
vitepress success success
vue-i18n success failure

@edison1105 edison1105 merged commit 0ea17e2 into vuejs:main Jun 11, 2026
13 of 14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🍰 p2-nice-to-have Priority 2: this is not breaking anything but nice to have it addressed. ready to merge The PR is ready to be merged.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants