Skip to content

Commit 9265724

Browse files
defccyyx990803
authored andcommitted
Mark node with static props as static (#4662)
* fix special static attrs as dom prop * refactor
1 parent 38b30b4 commit 9265724

File tree

5 files changed

+20
-6
lines changed

5 files changed

+20
-6
lines changed

Diff for: flow/compiler.js

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ declare type ASTElement = {
8282
plain?: boolean;
8383
pre?: true;
8484
ns?: string;
85+
staticProps?: Array<string>;
8586

8687
component?: string;
8788
inlineTemplate?: true;

Diff for: src/compiler/helpers.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ export function pluckModuleFunction<F: Function> (
1515
: []
1616
}
1717

18-
export function addProp (el: ASTElement, name: string, value: string) {
18+
export function addProp (el: ASTElement, name: string, value: string, fromStaticAttr?: boolean) {
19+
if (fromStaticAttr) {
20+
(el.staticProps || (el.staticProps = [])).push(name)
21+
}
1922
(el.props || (el.props = [])).push({ name, value })
2023
}
2124

Diff for: src/compiler/optimizer.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* @flow */
22

3-
import { makeMap, isBuiltInTag, cached, no } from 'shared/util'
3+
import { makeMap, isBuiltInTag, cached, no, remove } from 'shared/util'
44

55
let isStaticKey
66
let isPlatformReservedTag
@@ -30,7 +30,7 @@ export function optimize (root: ?ASTElement, options: CompilerOptions) {
3030

3131
function genStaticKeys (keys: string): Function {
3232
return makeMap(
33-
'type,tag,attrsList,attrsMap,plain,parent,children,attrs' +
33+
'type,tag,attrsList,attrsMap,plain,parent,children,attrs,staticProps' +
3434
(keys ? ',' + keys : '')
3535
)
3636
}
@@ -99,13 +99,17 @@ function isStatic (node: ASTNode): boolean {
9999
if (node.type === 3) { // text
100100
return true
101101
}
102+
const nodeAttrs = Object.keys(node)
103+
if (node.staticProps && node.props && node.staticProps.length === node.props.length) {
104+
remove(nodeAttrs, 'props')
105+
}
102106
return !!(node.pre || (
103107
!node.hasBindings && // no dynamic bindings
104108
!node.if && !node.for && // not v-if or v-for or v-else
105109
!isBuiltInTag(node.tag) && // not a built-in
106110
isPlatformReservedTag(node.tag) && // not a component
107111
!isDirectChildOfTemplateFor(node) &&
108-
Object.keys(node).every(isStaticKey)
112+
nodeAttrs.every(isStaticKey)
109113
))
110114
}
111115

Diff for: src/compiler/parser/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -481,9 +481,9 @@ function processAttrs (el) {
481481
// so that patches between dynamic/static are consistent
482482
if (platformMustUseProp(el.tag, name)) {
483483
if (name === 'value') {
484-
addProp(el, name, JSON.stringify(value))
484+
addProp(el, name, JSON.stringify(value), true)
485485
} else {
486-
addProp(el, name, 'true')
486+
addProp(el, name, 'true', true)
487487
}
488488
}
489489
}

Diff for: test/unit/modules/compiler/optimizer.spec.js

+6
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,12 @@ describe('optimizer', () => {
191191
expect(ast.children[0].static).toBe(false)
192192
})
193193

194+
it('mark static with static dom property', () => {
195+
const ast = parse('<input type="text" value="1">', baseOptions)
196+
optimize(ast, baseOptions)
197+
expect(ast.static).toBe(true)
198+
})
199+
194200
it('not root ast', () => {
195201
const ast = null
196202
optimize(ast, baseOptions)

0 commit comments

Comments
 (0)