]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
test: add transform tests to `v-html` / `v-text` tests (#49)
authorRizumu Ayaka <rizumu@ayaka.moe>
Sun, 10 Dec 2023 19:05:11 +0000 (03:05 +0800)
committerGitHub <noreply@github.com>
Sun, 10 Dec 2023 19:05:11 +0000 (03:05 +0800)
Co-authored-by: 三咲智子 Kevin Deng <sxzz@sxzz.moe>
packages/compiler-vapor/__tests__/transforms/__snapshots__/vHtml.spec.ts.snap
packages/compiler-vapor/__tests__/transforms/__snapshots__/vText.spec.ts.snap
packages/compiler-vapor/__tests__/transforms/vHtml.spec.ts
packages/compiler-vapor/__tests__/transforms/vText.spec.ts
packages/compiler-vapor/src/transforms/vText.ts

index 459741e9acb0c69cf7b6a7805437782d6b5a0315..d3abf8903d7c1530590f42f1fddddec751d08037 100644 (file)
@@ -1,6 +1,6 @@
 // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
 
-exports[`v-html > should raise error and ignore children when v-html is present 1`] = `
+exports[`v-html > should convert v-html to innerHTML 1`] = `
 "import { template as _template, children as _children, effect as _effect, setHtml as _setHtml } from 'vue/vapor';
 
 export function render(_ctx) {
@@ -8,34 +8,34 @@ export function render(_ctx) {
   const n0 = t0()
   const { 0: [n1],} = _children(n0)
   _effect(() => {
-    _setHtml(n1, undefined, _ctx.test)
+    _setHtml(n1, undefined, _ctx.code)
   })
   return n0
 }"
 `;
 
-exports[`v-html > should raise error if has no expression 1`] = `
-"import { template as _template, children as _children, setHtml as _setHtml } from 'vue/vapor';
+exports[`v-html > should raise error and ignore children when v-html is present 1`] = `
+"import { template as _template, children as _children, effect as _effect, setHtml as _setHtml } from 'vue/vapor';
 
 export function render(_ctx) {
   const t0 = _template("<div></div>")
   const n0 = t0()
   const { 0: [n1],} = _children(n0)
-  _setHtml(n1, undefined, "")
+  _effect(() => {
+    _setHtml(n1, undefined, _ctx.test)
+  })
   return n0
 }"
 `;
 
-exports[`v-html > simple expression 1`] = `
-"import { template as _template, children as _children, effect as _effect, setHtml as _setHtml } from 'vue/vapor';
+exports[`v-html > should raise error if has no expression 1`] = `
+"import { template as _template, children as _children, setHtml as _setHtml } from 'vue/vapor';
 
 export function render(_ctx) {
   const t0 = _template("<div></div>")
   const n0 = t0()
   const { 0: [n1],} = _children(n0)
-  _effect(() => {
-    _setHtml(n1, undefined, _ctx.code)
-  })
+  _setHtml(n1, undefined, "")
   return n0
 }"
 `;
index f2b62b2eeceb93e9dfc0a587c09aea43fd09f208..d18e398b11a0795920a01cbc5752a0978a56629d 100644 (file)
@@ -1,18 +1,20 @@
 // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
 
-exports[`v-text > no expression 1`] = `
-"import { template as _template, children as _children, setText as _setText } from 'vue/vapor';
+exports[`v-text > should convert v-text to textContent 1`] = `
+"import { template as _template, children as _children, effect as _effect, setText as _setText } from 'vue/vapor';
 
 export function render(_ctx) {
   const t0 = _template("<div></div>")
   const n0 = t0()
   const { 0: [n1],} = _children(n0)
-  _setText(n1, undefined, "")
+  _effect(() => {
+    _setText(n1, undefined, _ctx.str)
+  })
   return n0
 }"
 `;
 
-exports[`v-text > simple expression 1`] = `
+exports[`v-text > should raise error and ignore children when v-text is present 1`] = `
 "import { template as _template, children as _children, effect as _effect, setText as _setText } from 'vue/vapor';
 
 export function render(_ctx) {
@@ -20,8 +22,20 @@ export function render(_ctx) {
   const n0 = t0()
   const { 0: [n1],} = _children(n0)
   _effect(() => {
-    _setText(n1, undefined, _ctx.str)
+    _setText(n1, undefined, _ctx.test)
   })
   return n0
 }"
 `;
+
+exports[`v-text > should raise error if has no expression 1`] = `
+"import { template as _template, children as _children, setText as _setText } from 'vue/vapor';
+
+export function render(_ctx) {
+  const t0 = _template("<div></div>")
+  const n0 = t0()
+  const { 0: [n1],} = _children(n0)
+  _setText(n1, undefined, "")
+  return n0
+}"
+`;
index 6d0492fa67c70c413a2ed28603516b30addd292c..0e38f9e452b2bcb268e2722f71c53fa2d4fcba31 100644 (file)
-import { type RootNode, BindingTypes, DOMErrorCodes } from '@vue/compiler-dom'
-import { type CompilerOptions, compile as _compile } from '../../src'
+import {
+  BindingTypes,
+  DOMErrorCodes,
+  NodeTypes,
+  parse,
+} from '@vue/compiler-dom'
+import {
+  type CompilerOptions,
+  compile as _compile,
+  RootIRNode,
+  transform,
+  generate,
+  IRNodeTypes,
+} from '../../src'
+import { getBaseTransformPreset } from '../../src/compile'
 
-function compile(template: string | RootNode, options: CompilerOptions = {}) {
-  let { code } = _compile(template, {
-    ...options,
-    mode: 'module',
+function compileWithVHtml(
+  template: string,
+  options: CompilerOptions = {},
+): {
+  ir: RootIRNode
+  code: string
+} {
+  const ast = parse(template, { prefixIdentifiers: true, ...options })
+  const [nodeTransforms, directiveTransforms] = getBaseTransformPreset(true)
+  const ir = transform(ast, {
+    nodeTransforms,
+    directiveTransforms,
     prefixIdentifiers: true,
+    ...options,
   })
-  return code
+  const { code } = generate(ir, { prefixIdentifiers: true, ...options })
+  return { ir, code }
 }
 
 describe('v-html', () => {
-  test('simple expression', () => {
-    const code = compile(`<div v-html="code"></div>`, {
+  test('should convert v-html to innerHTML', () => {
+    const { code, ir } = compileWithVHtml(`<div v-html="code"></div>`, {
       bindingMetadata: {
         code: BindingTypes.SETUP_REF,
       },
     })
+
+    expect(ir.vaporHelpers).contains('setHtml')
+    expect(ir.helpers.size).toBe(0)
+
+    expect(ir.operation).toEqual([])
+    expect(ir.effect).toMatchObject([
+      {
+        expressions: [
+          {
+            type: NodeTypes.SIMPLE_EXPRESSION,
+            content: 'code',
+            isStatic: false,
+          },
+        ],
+        operations: [
+          {
+            type: IRNodeTypes.SET_HTML,
+            element: 1,
+            value: {
+              type: NodeTypes.SIMPLE_EXPRESSION,
+              content: 'code',
+              isStatic: false,
+            },
+          },
+        ],
+      },
+    ])
+
     expect(code).matchSnapshot()
   })
 
   test('should raise error and ignore children when v-html is present', () => {
     const onError = vi.fn()
-    const code = compile(`<div v-html="test">hello</div>`, {
+    const { code, ir } = compileWithVHtml(`<div v-html="test">hello</div>`, {
       onError,
     })
-    expect(code).matchSnapshot()
+
+    expect(ir.vaporHelpers).contains('setHtml')
+    expect(ir.helpers.size).toBe(0)
+
+    expect(ir.operation).toEqual([])
+    expect(ir.effect).toMatchObject([
+      {
+        expressions: [
+          {
+            type: NodeTypes.SIMPLE_EXPRESSION,
+            content: 'test',
+            isStatic: false,
+          },
+        ],
+        operations: [
+          {
+            type: IRNodeTypes.SET_HTML,
+            element: 1,
+            value: {
+              type: NodeTypes.SIMPLE_EXPRESSION,
+              content: 'test',
+              isStatic: false,
+            },
+          },
+        ],
+      },
+    ])
+
     expect(onError.mock.calls).toMatchObject([
       [{ code: DOMErrorCodes.X_V_HTML_WITH_CHILDREN }],
     ])
+
+    expect(code).matchSnapshot()
   })
 
   test('should raise error if has no expression', () => {
     const onError = vi.fn()
-    const code = compile(`<div v-html></div>`, {
+    const { code } = compileWithVHtml(`<div v-html></div>`, {
       onError,
     })
     expect(code).matchSnapshot()
index 0989ef19f23e3d45bdf114e121f2dfe5c198ef5f..bae5c91663a9467adef9ca7022176b410d21d428 100644 (file)
-import { type RootNode, BindingTypes, DOMErrorCodes } from '@vue/compiler-dom'
-import { type CompilerOptions, compile as _compile } from '../../src'
+import {
+  BindingTypes,
+  DOMErrorCodes,
+  NodeTypes,
+  parse,
+} from '@vue/compiler-dom'
+import {
+  type CompilerOptions,
+  compile as _compile,
+  RootIRNode,
+  transform,
+  generate,
+  IRNodeTypes,
+} from '../../src'
+import { getBaseTransformPreset } from '../../src/compile'
 
-function compile(template: string | RootNode, options: CompilerOptions = {}) {
-  let { code } = _compile(template, {
-    ...options,
-    mode: 'module',
+function compileWithVText(
+  template: string,
+  options: CompilerOptions = {},
+): {
+  ir: RootIRNode
+  code: string
+} {
+  const ast = parse(template, { prefixIdentifiers: true, ...options })
+  const [nodeTransforms, directiveTransforms] = getBaseTransformPreset(true)
+  const ir = transform(ast, {
+    nodeTransforms,
+    directiveTransforms,
     prefixIdentifiers: true,
+    ...options,
   })
-  return code
+  const { code } = generate(ir, { prefixIdentifiers: true, ...options })
+  return { ir, code }
 }
 
 describe('v-text', () => {
-  test('simple expression', () => {
-    const code = compile(`<div v-text="str"></div>`, {
+  test('should convert v-text to textContent', () => {
+    const { code, ir } = compileWithVText(`<div v-text="str"></div>`, {
       bindingMetadata: {
         str: BindingTypes.SETUP_REF,
       },
     })
+
+    expect(ir.vaporHelpers).contains('setText')
+    expect(ir.helpers.size).toBe(0)
+
+    expect(ir.operation).toEqual([])
+
+    expect(ir.effect).toMatchObject([
+      {
+        expressions: [
+          {
+            type: NodeTypes.SIMPLE_EXPRESSION,
+            content: 'str',
+            isStatic: false,
+          },
+        ],
+        operations: [
+          {
+            type: IRNodeTypes.SET_TEXT,
+            element: 1,
+            value: {
+              type: NodeTypes.SIMPLE_EXPRESSION,
+              content: 'str',
+              isStatic: false,
+            },
+          },
+        ],
+      },
+    ])
+
+    expect(code).matchSnapshot()
+  })
+
+  test('should raise error and ignore children when v-text is present', () => {
+    const onError = vi.fn()
+    const { code, ir } = compileWithVText(`<div v-text="test">hello</div>`, {
+      onError,
+    })
+    expect(onError.mock.calls).toMatchObject([
+      [{ code: DOMErrorCodes.X_V_TEXT_WITH_CHILDREN }],
+    ])
+
+    // children should have been removed
+    expect(ir.template).toMatchObject([{ template: '<div></div>' }])
+
+    expect(ir.effect).toMatchObject([
+      {
+        expressions: [
+          {
+            type: NodeTypes.SIMPLE_EXPRESSION,
+            content: 'test',
+            isStatic: false,
+          },
+        ],
+        operations: [
+          {
+            type: IRNodeTypes.SET_TEXT,
+            element: 1,
+            value: {
+              type: NodeTypes.SIMPLE_EXPRESSION,
+              content: 'test',
+              isStatic: false,
+            },
+          },
+        ],
+      },
+    ])
+
     expect(code).matchSnapshot()
+    // children should have been removed
+    expect(code).contains('template("<div></div>")')
   })
 
-  test('no expression', () => {
+  test('should raise error if has no expression', () => {
     const onError = vi.fn()
-    const code = compile(`<div v-text></div>`, { onError })
+    const { code } = compileWithVText(`<div v-text></div>`, { onError })
     expect(code).matchSnapshot()
     expect(onError.mock.calls).toMatchObject([
       [{ code: DOMErrorCodes.X_V_TEXT_NO_EXPRESSION }],
index 7a235a7939f086cf42e3e1c3b7d8b4a04de30944..729c859aa2837ac219512ce60fa0f382f834e3e8 100644 (file)
@@ -13,7 +13,7 @@ export const transformVText: DirectiveTransform = (dir, node, context) => {
     context.options.onError(
       createDOMCompilerError(DOMErrorCodes.X_V_TEXT_WITH_CHILDREN, loc),
     )
-    node.children.length = 0
+    context.childrenTemplate.length = 0
   }
 
   context.registerEffect(