Skip to content

Commit 646061f

Browse files
committed
merge adjacent text nodes
1 parent c7b6183 commit 646061f

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/core/vdom/helpers.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,40 @@ export function normalizeChildren (children: any): Array<VNode> {
1111
children = children()
1212
}
1313
if (isPrimitive(children)) {
14-
return [new VNode(undefined, undefined, undefined, String(children))]
14+
return [createTextVNode(children)]
1515
}
1616
if (Array.isArray(children)) {
1717
const res = []
1818
for (let i = 0, l = children.length; i < l; i++) {
1919
const c = children[i]
20+
const last = res[res.length - 1]
2021
// nested
2122
if (Array.isArray(c)) {
2223
res.push.apply(res, normalizeChildren(c))
2324
} else if (isPrimitive(c)) {
24-
// convert primitive to vnode
25-
res.push(new VNode(undefined, undefined, undefined, String(c)))
25+
if (last && last.text) {
26+
last.text += String(c)
27+
} else {
28+
// convert primitive to vnode
29+
res.push(createTextVNode(c))
30+
}
2631
} else if (c instanceof VNode) {
27-
res.push(c)
32+
if (c.text && last && last.text) {
33+
last.text += c.text
34+
} else {
35+
res.push(c)
36+
}
2837
}
2938
}
3039
return res
3140
}
3241
return []
3342
}
3443

44+
function createTextVNode (val) {
45+
return new VNode(undefined, undefined, undefined, String(val))
46+
}
47+
3548
export function updateListeners (
3649
on: Object,
3750
oldOn: Object,

0 commit comments

Comments
 (0)