]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-dom): avoid stringify option with null value (#12096)
authoredison <daiwei521@126.com>
Fri, 11 Oct 2024 02:41:55 +0000 (10:41 +0800)
committerGitHub <noreply@github.com>
Fri, 11 Oct 2024 02:41:55 +0000 (10:41 +0800)
close #12093

packages/compiler-dom/__tests__/transforms/__snapshots__/stringifyStatic.spec.ts.snap
packages/compiler-dom/__tests__/transforms/stringifyStatic.spec.ts
packages/compiler-dom/src/transforms/stringifyStatic.ts

index 78b576af5c7bca0710e6b07cdfb0f188eeea68e6..a863eb32e617be7a543ee563a4a596df467be574 100644 (file)
@@ -32,6 +32,23 @@ return function render(_ctx, _cache) {
 }"
 `;
 
+exports[`stringify static html > should bail for <option> elements with null values 1`] = `
+"const { createElementVNode: _createElementVNode, openBlock: _openBlock, createElementBlock: _createElementBlock } = Vue
+
+return function render(_ctx, _cache) {
+  return (_openBlock(), _createElementBlock("div", null, _cache[0] || (_cache[0] = [
+    _createElementVNode("select", null, [
+      _createElementVNode("option", { value: null }),
+      _createElementVNode("option", { value: "1" }),
+      _createElementVNode("option", { value: "1" }),
+      _createElementVNode("option", { value: "1" }),
+      _createElementVNode("option", { value: "1" }),
+      _createElementVNode("option", { value: "1" })
+    ], -1 /* HOISTED */)
+  ])))
+}"
+`;
+
 exports[`stringify static html > should bail for <option> elements with number values 1`] = `
 "const { createElementVNode: _createElementVNode, openBlock: _openBlock, createElementBlock: _createElementBlock } = Vue
 
index a35b5223198b1cfcaaca9c2ab783ded320fd8a1a..a460cfe68eb0e5a4f40c3b47e6748c3de879a582 100644 (file)
@@ -470,6 +470,17 @@ describe('stringify static html', () => {
     expect(code).toMatchSnapshot()
   })
 
+  test('should bail for <option> elements with null values', () => {
+    const { ast, code } = compileWithStringify(
+      `<div><select><option :value="null" />${repeat(
+        `<option value="1" />`,
+        StringifyThresholds.ELEMENT_WITH_BINDING_COUNT,
+      )}</select></div>`,
+    )
+    expect(ast.cached).toMatchObject([cachedArrayBailedMatcher()])
+    expect(code).toMatchSnapshot()
+  })
+
   test('eligible content (elements > 20) + non-eligible content', () => {
     const { code } = compileWithStringify(
       `<div>${repeat(
index a608ea3c4b34e2c783834a583076cb6f1f1b73de..cd8f1a9d1844f6003f9dec7cea922f69e71375eb 100644 (file)
@@ -261,8 +261,7 @@ function analyzeNode(node: StringifiableNode): [number, number] | false {
           isOptionTag &&
           isStaticArgOf(p.arg, 'value') &&
           p.exp &&
-          p.exp.ast &&
-          p.exp.ast.type !== 'StringLiteral'
+          !p.exp.isStatic
         ) {
           return bail()
         }