Skip to content

Commit 7503f8e

Browse files
committed
support _scopeId
1 parent 5eca12f commit 7503f8e

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

src/core/vdom/patch.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ export function createPatchFunction (backend) {
7878
// in that case we can just return the element and be done.
7979
if (isDef(i = vnode.child)) {
8080
invokeCreateHooks(vnode, insertedVnodeQueue)
81+
setScope(vnode)
8182
return vnode.elm
8283
}
8384
}
@@ -87,6 +88,7 @@ export function createPatchFunction (backend) {
8788
elm = vnode.elm = vnode.ns
8889
? nodeOps.createElementNS(vnode.ns, tag)
8990
: nodeOps.createElement(tag)
91+
setScope(vnode)
9092
if (Array.isArray(children)) {
9193
for (i = 0; i < children.length; ++i) {
9294
nodeOps.appendChild(elm, createElm(children[i], insertedVnodeQueue))
@@ -114,6 +116,16 @@ export function createPatchFunction (backend) {
114116
}
115117
}
116118

119+
// set scope id attribute for scoped CSS.
120+
// this is implemented as a special case to avoid the overhead
121+
// of going through the normal attribute patching process.
122+
function setScope (vnode) {
123+
let i
124+
if (isDef(i = vnode.context) && isDef(i = i.$options._scopeId)) {
125+
nodeOps.setAttribute(vnode.elm, i, '')
126+
}
127+
}
128+
117129
function addVnodes (parentElm, before, vnodes, startIdx, endIdx, insertedVnodeQueue) {
118130
for (; startIdx <= endIdx; ++startIdx) {
119131
nodeOps.insertBefore(parentElm, createElm(vnodes[startIdx], insertedVnodeQueue), before)

src/platforms/web/runtime/node-ops.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,7 @@ export function setTextContent (node: Node, text: string) {
4545
export function childNodes (node: Node): NodeList {
4646
return node.childNodes
4747
}
48+
49+
export function setAttribute (node: Element, key: string, val: string) {
50+
node.setAttribute(key, val)
51+
}

test/unit/modules/vdom/patch/element.spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import Vue from 'vue'
12
import { patch } from 'web/runtime/patch'
23
import VNode from 'core/vdom/vnode'
34

@@ -30,4 +31,11 @@ describe('element', () => {
3031
expect(elm.childNodes[0].tagName).toBe('SPAN')
3132
expect(elm.childNodes[1].textContent).toBe('hello world')
3233
})
34+
35+
it('should create element with scope attribute', () => {
36+
const vnode = new VNode('div')
37+
vnode.context = new Vue({ _scopeId: 'foo' })
38+
const elm = patch(null, vnode)
39+
expect(elm.hasAttribute('foo')).toBe(true)
40+
})
3341
})

0 commit comments

Comments
 (0)