From e3d7a03617d28e40ca7f9edfb52fee95a41ddfa2 Mon Sep 17 00:00:00 2001 From: defcc Date: Fri, 6 Jan 2017 00:42:01 +0800 Subject: [PATCH 1/2] fix special static attrs as dom prop --- flow/compiler.js | 1 + src/compiler/optimizer.js | 8 ++++++-- src/compiler/parser/index.js | 1 + test/unit/modules/compiler/optimizer.spec.js | 6 ++++++ 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/flow/compiler.js b/flow/compiler.js index debdbe04f50..cac6129c37c 100644 --- a/flow/compiler.js +++ b/flow/compiler.js @@ -75,6 +75,7 @@ declare type ASTElement = { staticInFor?: boolean; staticProcessed?: boolean; hasBindings?: boolean; + hasNormalProp?: true; text?: string; attrs?: Array<{ name: string; value: string }>; diff --git a/src/compiler/optimizer.js b/src/compiler/optimizer.js index af083f0c5f8..e10593c474a 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 @@ -99,13 +99,17 @@ function isStatic (node: ASTNode): boolean { if (node.type === 3) { // text return true } + const nodeAttrs = Object.keys(node) + if (!node.hasNormalProp && node.props && 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..d0dfd841c35 100644 --- a/src/compiler/parser/index.js +++ b/src/compiler/parser/index.js @@ -444,6 +444,7 @@ function processAttrs (el) { } } if (isProp || platformMustUseProp(el.tag, name)) { + el.hasNormalProp = true addProp(el, name, value) } else { addAttr(el, name, value) 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) From d3ddf2136384f0dddd2ed1de285bfeee1c8c5d44 Mon Sep 17 00:00:00 2001 From: defcc Date: Fri, 6 Jan 2017 01:02:46 +0800 Subject: [PATCH 2/2] refactor --- flow/compiler.js | 2 +- src/compiler/helpers.js | 5 ++++- src/compiler/optimizer.js | 4 ++-- src/compiler/parser/index.js | 5 ++--- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/flow/compiler.js b/flow/compiler.js index cac6129c37c..a31a5721e1f 100644 --- a/flow/compiler.js +++ b/flow/compiler.js @@ -75,7 +75,6 @@ declare type ASTElement = { staticInFor?: boolean; staticProcessed?: boolean; hasBindings?: boolean; - hasNormalProp?: true; text?: string; attrs?: Array<{ name: string; value: string }>; @@ -83,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 e10593c474a..f98092cc297 100644 --- a/src/compiler/optimizer.js +++ b/src/compiler/optimizer.js @@ -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 : '') ) } @@ -100,7 +100,7 @@ function isStatic (node: ASTNode): boolean { return true } const nodeAttrs = Object.keys(node) - if (!node.hasNormalProp && node.props && node.props.length) { + if (node.staticProps && node.props && node.staticProps.length === node.props.length) { remove(nodeAttrs, 'props') } return !!(node.pre || ( diff --git a/src/compiler/parser/index.js b/src/compiler/parser/index.js index d0dfd841c35..7f6158731fa 100644 --- a/src/compiler/parser/index.js +++ b/src/compiler/parser/index.js @@ -444,7 +444,6 @@ function processAttrs (el) { } } if (isProp || platformMustUseProp(el.tag, name)) { - el.hasNormalProp = true addProp(el, name, value) } else { addAttr(el, name, value) @@ -482,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) } } }