From 30fd4b59c981f806f862bcf8621c0b71da5890af Mon Sep 17 00:00:00 2001 From: BeiYuShuiGuoLvDeKongQi <958414905@qq.com> Date: Sun, 26 Apr 2020 21:38:53 +0800 Subject: [PATCH 1/2] fix(namespace): apply every namespace in slot when call applyNS (#11315) --- src/core/vdom/create-element.ts | 11 +++++ test/unit/modules/vdom/create-element.spec.ts | 43 +++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/src/core/vdom/create-element.ts b/src/core/vdom/create-element.ts index 62dd004c34a..bad54d97fa0 100644 --- a/src/core/vdom/create-element.ts +++ b/src/core/vdom/create-element.ts @@ -157,6 +157,17 @@ function applyNS(vnode, ns, force?: boolean) { } } } + + // #11315 + if (isObject(vnode.componentOptions) && isDef(vnode.componentOptions.children)) { + for (var i = 0, l = vnode.componentOptions.children.length; i < l; i++) { + var child = vnode.componentOptions.children[i] + if (isDef(child.tag) && ( + isUndef(child.ns) || (isTrue(force) && child.tag !== 'svg'))) { + applyNS(child, ns, force) + } + } + } } // ref #5318 diff --git a/test/unit/modules/vdom/create-element.spec.ts b/test/unit/modules/vdom/create-element.spec.ts index f97b4bc07cf..7ba8730c42c 100644 --- a/test/unit/modules/vdom/create-element.spec.ts +++ b/test/unit/modules/vdom/create-element.spec.ts @@ -147,6 +147,49 @@ describe('create-element', () => { expect(vnode.children[0].children[1].ns).toBe('svg') }) + // #11315 + it('render svg foreignObject nested component slot with correct namespace', () => { + const vm = new Vue({ + template: ` + + + + `, + components: { + 'box': { + template: ` + + +

+
+
+ `, + components: { + 'comp-with-slot': { + template: ` +
+ +
+ ` + } + } + } + } + }).$mount() + const box = vm.$children[0] + const compWithSlot = box.$children[0] + expect(box.$vnode.ns).toBe('svg') + expect(box._vnode.tag).toBe('foreignObject') + expect(box._vnode.ns).toBe('svg') + expect(compWithSlot.$vnode.ns).toBeUndefined() + expect(compWithSlot._vnode.tag).toBe('div') + expect(compWithSlot._vnode.ns).toBeUndefined() + expect(compWithSlot._vnode.children[0].tag).toBe('p') + expect(compWithSlot._vnode.children[0].ns).toBeUndefined() + expect(compWithSlot._vnode.children[1].tag).toBe('svg') + expect(compWithSlot._vnode.children[1].ns).toBe('svg') + }) + // #6642 it('render svg foreignObject component with correct namespace', () => { const vm = new Vue({ From 446b2ce9ec5ebff3bb03d59c66c6c154c79339a3 Mon Sep 17 00:00:00 2001 From: Rairn <958414905@qq.com> Date: Mon, 2 Jan 2023 15:34:24 +0800 Subject: [PATCH 2/2] refactor: extract variable --- src/core/vdom/create-element.ts | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/src/core/vdom/create-element.ts b/src/core/vdom/create-element.ts index bad54d97fa0..6abade56757 100644 --- a/src/core/vdom/create-element.ts +++ b/src/core/vdom/create-element.ts @@ -146,9 +146,14 @@ function applyNS(vnode, ns, force?: boolean) { ns = undefined force = true } - if (isDef(vnode.children)) { - for (let i = 0, l = vnode.children.length; i < l; i++) { - const child = vnode.children[i] + const children = + vnode.children || + // #11315 + (vnode.componentOptions && vnode.componentOptions.children) + + if (isDef(children)) { + for (let i = 0, l = children.length; i < l; i++) { + const child = children[i] if ( isDef(child.tag) && (isUndef(child.ns) || (isTrue(force) && child.tag !== 'svg')) @@ -157,17 +162,6 @@ function applyNS(vnode, ns, force?: boolean) { } } } - - // #11315 - if (isObject(vnode.componentOptions) && isDef(vnode.componentOptions.children)) { - for (var i = 0, l = vnode.componentOptions.children.length; i < l; i++) { - var child = vnode.componentOptions.children[i] - if (isDef(child.tag) && ( - isUndef(child.ns) || (isTrue(force) && child.tag !== 'svg'))) { - applyNS(child, ns, force) - } - } - } } // ref #5318