diff --git a/flow/compiler.js b/flow/compiler.js index debdbe04f50..a31a5721e1f 100644 --- a/flow/compiler.js +++ b/flow/compiler.js @@ -82,6 +82,7 @@ declare type ASTElement = { plain?: boolean; pre?: true; ns?: string; + staticProps?: Array; component?: string; inlineTemplate?: true; diff --git a/src/compiler/helpers.js b/src/compiler/helpers.js index 29469333003..44ac3853eba 100644 --- a/src/compiler/helpers.js +++ b/src/compiler/helpers.js @@ -15,7 +15,10 @@ export function pluckModuleFunction ( : [] } -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 }) } diff --git a/src/compiler/optimizer.js b/src/compiler/optimizer.js index af083f0c5f8..f98092cc297 100644 --- a/src/compiler/optimizer.js +++ b/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) )) } diff --git a/src/compiler/parser/index.js b/src/compiler/parser/index.js index 69d0eac67cf..7f6158731fa 100644 --- a/src/compiler/parser/index.js +++ b/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) } } } diff --git a/test/unit/modules/compiler/optimizer.spec.js b/test/unit/modules/compiler/optimizer.spec.js index d7ed5542123..9db2860d9b4 100644 --- a/test/unit/modules/compiler/optimizer.spec.js +++ b/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('', baseOptions) + optimize(ast, baseOptions) + expect(ast.static).toBe(true) + }) + it('not root ast', () => { const ast = null optimize(ast, baseOptions)