Quellcode durchsuchen

fix(compiler-dom): avoid stringify option with null value (#12096)

close #12093
edison vor 1 Jahr
Ursprung
Commit
f6d9926236

+ 17 - 0
packages/compiler-dom/__tests__/transforms/__snapshots__/stringifyStatic.spec.ts.snap

@@ -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`] = `
 exports[`stringify static html > should bail for <option> elements with number values 1`] = `
 "const { createElementVNode: _createElementVNode, openBlock: _openBlock, createElementBlock: _createElementBlock } = Vue
 "const { createElementVNode: _createElementVNode, openBlock: _openBlock, createElementBlock: _createElementBlock } = Vue
 
 

+ 11 - 0
packages/compiler-dom/__tests__/transforms/stringifyStatic.spec.ts

@@ -470,6 +470,17 @@ describe('stringify static html', () => {
     expect(code).toMatchSnapshot()
     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', () => {
   test('eligible content (elements > 20) + non-eligible content', () => {
     const { code } = compileWithStringify(
     const { code } = compileWithStringify(
       `<div>${repeat(
       `<div>${repeat(

+ 1 - 2
packages/compiler-dom/src/transforms/stringifyStatic.ts

@@ -261,8 +261,7 @@ function analyzeNode(node: StringifiableNode): [number, number] | false {
           isOptionTag &&
           isOptionTag &&
           isStaticArgOf(p.arg, 'value') &&
           isStaticArgOf(p.arg, 'value') &&
           p.exp &&
           p.exp &&
-          p.exp.ast &&
-          p.exp.ast.type !== 'StringLiteral'
+          !p.exp.isStatic
         ) {
         ) {
           return bail()
           return bail()
         }
         }