From 6773f86085bd50bc506a58dc7c7b80fd28cb53c6 Mon Sep 17 00:00:00 2001
From: linzhe141 <1572213544@qq.com>
Date: Tue, 10 Dec 2024 15:18:07 +0800
Subject: [PATCH 1/2] fix: v-html and v-text can only be used on elements
---
.../__tests__/transforms/vHtml.spec.ts | 23 ++++++++++++++++++-
.../__tests__/transforms/vText.spec.ts | 23 ++++++++++++++++++-
packages/compiler-dom/src/errors.ts | 4 ++++
packages/compiler-dom/src/transforms/vHtml.ts | 6 +++++
packages/compiler-dom/src/transforms/vText.ts | 6 +++++
packages/compiler-ssr/src/errors.ts | 2 +-
6 files changed, 61 insertions(+), 3 deletions(-)
diff --git a/packages/compiler-dom/__tests__/transforms/vHtml.spec.ts b/packages/compiler-dom/__tests__/transforms/vHtml.spec.ts
index bca2fdf95..bff4d57f6 100644
--- a/packages/compiler-dom/__tests__/transforms/vHtml.spec.ts
+++ b/packages/compiler-dom/__tests__/transforms/vHtml.spec.ts
@@ -6,6 +6,7 @@ import {
} from '@vue/compiler-core'
import { transformVHtml } from '../../src/transforms/vHtml'
import { transformElement } from '../../../compiler-core/src/transforms/transformElement'
+import { transformSlotOutlet } from '../../../compiler-core/src/transforms/transformSlotOutlet'
import { createObjectMatcher } from '../../../compiler-core/__tests__/testUtils'
import { PatchFlags } from '@vue/shared'
import { DOMErrorCodes } from '../../src/errors'
@@ -13,7 +14,7 @@ import { DOMErrorCodes } from '../../src/errors'
function transformWithVHtml(template: string, options: CompilerOptions = {}) {
const ast = parse(template)
transform(ast, {
- nodeTransforms: [transformElement],
+ nodeTransforms: [transformElement, transformSlotOutlet],
directiveTransforms: {
html: transformVHtml,
},
@@ -64,4 +65,24 @@ describe('compiler: v-html transform', () => {
[{ code: DOMErrorCodes.X_V_HTML_NO_EXPRESSION }],
])
})
+
+ it('should raise error if uses on component', () => {
+ const onError = vi.fn()
+ transformWithVHtml(``, {
+ onError,
+ })
+ expect(onError.mock.calls).toMatchObject([
+ [{ code: DOMErrorCodes.X_V_HTML_ON_INVALID_ELEMENT }],
+ ])
+ })
+
+ it('should raise error if uses on slot', () => {
+ const onError = vi.fn()
+ transformWithVHtml(``, {
+ onError,
+ })
+ expect(onError.mock.calls).toMatchObject([
+ [{ code: DOMErrorCodes.X_V_HTML_ON_INVALID_ELEMENT }],
+ ])
+ })
})
diff --git a/packages/compiler-dom/__tests__/transforms/vText.spec.ts b/packages/compiler-dom/__tests__/transforms/vText.spec.ts
index e96ab2972..0086d97ff 100644
--- a/packages/compiler-dom/__tests__/transforms/vText.spec.ts
+++ b/packages/compiler-dom/__tests__/transforms/vText.spec.ts
@@ -6,6 +6,7 @@ import {
} from '@vue/compiler-core'
import { transformVText } from '../../src/transforms/vText'
import { transformElement } from '../../../compiler-core/src/transforms/transformElement'
+import { transformSlotOutlet } from '../../../compiler-core/src/transforms/transformSlotOutlet'
import { createObjectMatcher } from '../../../compiler-core/__tests__/testUtils'
import { PatchFlags } from '@vue/shared'
import { DOMErrorCodes } from '../../src/errors'
@@ -13,7 +14,7 @@ import { DOMErrorCodes } from '../../src/errors'
function transformWithVText(template: string, options: CompilerOptions = {}) {
const ast = parse(template)
transform(ast, {
- nodeTransforms: [transformElement],
+ nodeTransforms: [transformElement, transformSlotOutlet],
directiveTransforms: {
text: transformVText,
},
@@ -68,4 +69,24 @@ describe('compiler: v-text transform', () => {
[{ code: DOMErrorCodes.X_V_TEXT_NO_EXPRESSION }],
])
})
+
+ it('should raise error if uses on component', () => {
+ const onError = vi.fn()
+ transformWithVText(``, {
+ onError,
+ })
+ expect(onError.mock.calls).toMatchObject([
+ [{ code: DOMErrorCodes.X_V_TEXT_ON_INVALID_ELEMENT }],
+ ])
+ })
+
+ it('should raise error if uses on slot', () => {
+ const onError = vi.fn()
+ transformWithVText(``, {
+ onError,
+ })
+ expect(onError.mock.calls).toMatchObject([
+ [{ code: DOMErrorCodes.X_V_TEXT_ON_INVALID_ELEMENT }],
+ ])
+ })
})
diff --git a/packages/compiler-dom/src/errors.ts b/packages/compiler-dom/src/errors.ts
index b47624840..2b5fe3edb 100644
--- a/packages/compiler-dom/src/errors.ts
+++ b/packages/compiler-dom/src/errors.ts
@@ -23,8 +23,10 @@ export function createDOMCompilerError(
export enum DOMErrorCodes {
X_V_HTML_NO_EXPRESSION = 53 /* ErrorCodes.__EXTEND_POINT__ */,
X_V_HTML_WITH_CHILDREN,
+ X_V_HTML_ON_INVALID_ELEMENT,
X_V_TEXT_NO_EXPRESSION,
X_V_TEXT_WITH_CHILDREN,
+ X_V_TEXT_ON_INVALID_ELEMENT,
X_V_MODEL_ON_INVALID_ELEMENT,
X_V_MODEL_ARG_ON_ELEMENT,
X_V_MODEL_ON_FILE_INPUT_ELEMENT,
@@ -51,8 +53,10 @@ if (__TEST__) {
export const DOMErrorMessages: { [code: number]: string } = {
[DOMErrorCodes.X_V_HTML_NO_EXPRESSION]: `v-html is missing expression.`,
[DOMErrorCodes.X_V_HTML_WITH_CHILDREN]: `v-html will override element children.`,
+ [DOMErrorCodes.X_V_HTML_ON_INVALID_ELEMENT]: `v-html can only be used on elements.`,
[DOMErrorCodes.X_V_TEXT_NO_EXPRESSION]: `v-text is missing expression.`,
[DOMErrorCodes.X_V_TEXT_WITH_CHILDREN]: `v-text will override element children.`,
+ [DOMErrorCodes.X_V_TEXT_ON_INVALID_ELEMENT]: `v-text can only be used on elements.`,
[DOMErrorCodes.X_V_MODEL_ON_INVALID_ELEMENT]: `v-model can only be used on ,