Skip to content

Commit e774ce2

Browse files
committed
refactor: split vdom helpers into separate files
1 parent ceab0b7 commit e774ce2

File tree

14 files changed

+166
-157
lines changed

14 files changed

+166
-157
lines changed

src/core/components/keep-alive.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { callHook } from 'core/instance/lifecycle'
2-
import { getFirstComponentChild } from 'core/vdom/helpers'
2+
import { getFirstComponentChild } from 'core/vdom/helpers/index'
33

44
export default {
55
name: 'keep-alive',

src/core/instance/events.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* @flow */
22

33
import { bind, toArray } from '../util/index'
4-
import { updateListeners } from '../vdom/helpers'
4+
import { updateListeners } from '../vdom/helpers/index'
55

66
export function initEvents (vm: Component) {
77
vm._events = Object.create(null)

src/core/instance/render.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import config from '../config'
44
import VNode, { emptyVNode, cloneVNode, cloneVNodes } from '../vdom/vnode'
5-
import { normalizeChildren } from '../vdom/helpers'
5+
import { normalizeChildren } from '../vdom/helpers/index'
66
import {
77
warn, formatComponentName, bind, isObject, toObject,
88
nextTick, resolveAsset, _toString, toNumber, looseEqual, looseIndexOf

src/core/vdom/create-component.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import Vue from '../instance/index'
44
import VNode from './vnode'
5-
import { normalizeChildren } from './helpers'
5+
import { normalizeChildren } from './helpers/index'
66
import { activeInstance, callHook } from '../instance/lifecycle'
77
import { resolveSlots } from '../instance/render'
88
import { createElement } from './create-element'

src/core/vdom/create-element.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import VNode, { emptyVNode } from './vnode'
44
import config from '../config'
55
import { createComponent } from './create-component'
6-
import { normalizeChildren } from './helpers'
6+
import { normalizeChildren } from './helpers/index'
77
import { warn, resolveAsset } from '../util/index'
88

99
// wrapper function for providing a more flexible interface

src/core/vdom/helpers.js

Lines changed: 0 additions & 148 deletions
This file was deleted.

src/core/vdom/helpers/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* @flow */
2+
3+
export * from './merge-hook'
4+
export * from './update-listeners'
5+
export * from './normalize-children'
6+
7+
export function getFirstComponentChild (children: ?Array<any>): ?VNodeWithData {
8+
return children && children.filter(c => c && c.componentOptions)[0]
9+
}

src/core/vdom/helpers/merge-hook.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* @flow */
2+
3+
export function mergeVNodeHook (def: Object, hookKey: string, hook: Function, key: string) {
4+
key = key + hookKey
5+
const injectedHash = def.__injected || (def.__injected = {})
6+
if (!injectedHash[key]) {
7+
injectedHash[key] = true
8+
const oldHook = def[hookKey]
9+
if (oldHook) {
10+
def[hookKey] = function () {
11+
oldHook.apply(this, arguments)
12+
hook.apply(this, arguments)
13+
}
14+
} else {
15+
def[hookKey] = hook
16+
}
17+
}
18+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/* @flow */
2+
3+
import { isPrimitive } from 'core/util/index'
4+
import VNode from 'core/vdom/vnode'
5+
6+
export function normalizeChildren (
7+
children: any,
8+
ns: string | void,
9+
nestedIndex: number | void
10+
): Array<VNode> | void {
11+
if (isPrimitive(children)) {
12+
return [createTextVNode(children)]
13+
}
14+
if (Array.isArray(children)) {
15+
const res = []
16+
for (let i = 0, l = children.length; i < l; i++) {
17+
const c = children[i]
18+
const last = res[res.length - 1]
19+
// nested
20+
if (Array.isArray(c)) {
21+
res.push.apply(res, normalizeChildren(c, ns, i))
22+
} else if (isPrimitive(c)) {
23+
if (last && last.text) {
24+
last.text += String(c)
25+
} else if (c !== '') {
26+
// convert primitive to vnode
27+
res.push(createTextVNode(c))
28+
}
29+
} else if (c instanceof VNode) {
30+
if (c.text && last && last.text) {
31+
last.text += c.text
32+
} else {
33+
// inherit parent namespace
34+
if (ns) {
35+
applyNS(c, ns)
36+
}
37+
// default key for nested array children (likely generated by v-for)
38+
if (c.tag && c.key == null && nestedIndex != null) {
39+
c.key = `__vlist_${nestedIndex}_${i}__`
40+
}
41+
res.push(c)
42+
}
43+
}
44+
}
45+
return res
46+
}
47+
}
48+
49+
function createTextVNode (val) {
50+
return new VNode(undefined, undefined, undefined, String(val))
51+
}
52+
53+
function applyNS (vnode, ns) {
54+
if (vnode.tag && !vnode.ns) {
55+
vnode.ns = ns
56+
if (vnode.children) {
57+
for (let i = 0, l = vnode.children.length; i < l; i++) {
58+
applyNS(vnode.children[i], ns)
59+
}
60+
}
61+
}
62+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/* @flow */
2+
3+
import { warn } from 'core/util/index'
4+
5+
export function updateListeners (
6+
on: Object,
7+
oldOn: Object,
8+
add: Function,
9+
remove: Function,
10+
vm: Component
11+
) {
12+
let name, cur, old, fn, event, capture
13+
for (name in on) {
14+
cur = on[name]
15+
old = oldOn[name]
16+
if (!cur) {
17+
process.env.NODE_ENV !== 'production' && warn(
18+
`Invalid handler for event "${name}": got ` + String(cur),
19+
vm
20+
)
21+
} else if (!old) {
22+
capture = name.charAt(0) === '!'
23+
event = capture ? name.slice(1) : name
24+
if (Array.isArray(cur)) {
25+
add(event, (cur.invoker = arrInvoker(cur)), capture)
26+
} else {
27+
if (!cur.invoker) {
28+
fn = cur
29+
cur = on[name] = {}
30+
cur.fn = fn
31+
cur.invoker = fnInvoker(cur)
32+
}
33+
add(event, cur.invoker, capture)
34+
}
35+
} else if (cur !== old) {
36+
if (Array.isArray(old)) {
37+
old.length = cur.length
38+
for (let i = 0; i < old.length; i++) old[i] = cur[i]
39+
on[name] = old
40+
} else {
41+
old.fn = cur
42+
on[name] = old
43+
}
44+
}
45+
}
46+
for (name in oldOn) {
47+
if (!on[name]) {
48+
event = name.charAt(0) === '!' ? name.slice(1) : name
49+
remove(event, oldOn[name].invoker)
50+
}
51+
}
52+
}
53+
54+
function arrInvoker (arr: Array<Function>): Function {
55+
return function (ev) {
56+
const single = arguments.length === 1
57+
for (let i = 0; i < arr.length; i++) {
58+
single ? arr[i](ev) : arr[i].apply(null, arguments)
59+
}
60+
}
61+
}
62+
63+
function fnInvoker (o: { fn: Function }): Function {
64+
return function (ev) {
65+
const single = arguments.length === 1
66+
single ? o.fn(ev) : o.fn.apply(null, arguments)
67+
}
68+
}

0 commit comments

Comments
 (0)