Przeglądaj źródła

Mark node with static props as static (#4662)

* fix special static attrs as dom prop

* refactor
chengchao 9 lat temu
rodzic
commit
92657249dd

+ 1 - 0
flow/compiler.js

@@ -82,6 +82,7 @@ declare type ASTElement = {
   plain?: boolean;
   pre?: true;
   ns?: string;
+  staticProps?: Array<string>;
 
   component?: string;
   inlineTemplate?: true;

+ 4 - 1
src/compiler/helpers.js

@@ -15,7 +15,10 @@ export function pluckModuleFunction<F: Function> (
     : []
 }
 
-export function addProp (el: ASTElement, name: string, value: string) {
+export function addProp (el: ASTElement, name: string, value: string, fromStaticAttr?: boolean) {
+  if (fromStaticAttr) {
+    (el.staticProps || (el.staticProps = [])).push(name)
+  }
   (el.props || (el.props = [])).push({ name, value })
 }
 

+ 7 - 3
src/compiler/optimizer.js

@@ -1,6 +1,6 @@
 /* @flow */
 
-import { makeMap, isBuiltInTag, cached, no } from 'shared/util'
+import { makeMap, isBuiltInTag, cached, no, remove } from 'shared/util'
 
 let isStaticKey
 let isPlatformReservedTag
@@ -30,7 +30,7 @@ export function optimize (root: ?ASTElement, options: CompilerOptions) {
 
 function genStaticKeys (keys: string): Function {
   return makeMap(
-    'type,tag,attrsList,attrsMap,plain,parent,children,attrs' +
+    'type,tag,attrsList,attrsMap,plain,parent,children,attrs,staticProps' +
     (keys ? ',' + keys : '')
   )
 }
@@ -99,13 +99,17 @@ function isStatic (node: ASTNode): boolean {
   if (node.type === 3) { // text
     return true
   }
+  const nodeAttrs = Object.keys(node)
+  if (node.staticProps && node.props && node.staticProps.length === node.props.length) {
+    remove(nodeAttrs, 'props')
+  }
   return !!(node.pre || (
     !node.hasBindings && // no dynamic bindings
     !node.if && !node.for && // not v-if or v-for or v-else
     !isBuiltInTag(node.tag) && // not a built-in
     isPlatformReservedTag(node.tag) && // not a component
     !isDirectChildOfTemplateFor(node) &&
-    Object.keys(node).every(isStaticKey)
+    nodeAttrs.every(isStaticKey)
   ))
 }
 

+ 2 - 2
src/compiler/parser/index.js

@@ -481,9 +481,9 @@ function processAttrs (el) {
       // so that patches between dynamic/static are consistent
       if (platformMustUseProp(el.tag, name)) {
         if (name === 'value') {
-          addProp(el, name, JSON.stringify(value))
+          addProp(el, name, JSON.stringify(value), true)
         } else {
-          addProp(el, name, 'true')
+          addProp(el, name, 'true', true)
         }
       }
     }

+ 6 - 0
test/unit/modules/compiler/optimizer.spec.js

@@ -191,6 +191,12 @@ describe('optimizer', () => {
     expect(ast.children[0].static).toBe(false)
   })
 
+  it('mark static with static dom property', () => {
+    const ast = parse('<input type="text" value="1">', baseOptions)
+    optimize(ast, baseOptions)
+    expect(ast.static).toBe(true)
+  })
+
   it('not root ast', () => {
     const ast = null
     optimize(ast, baseOptions)