Skip to content

Commit 9fbed43

Browse files
committed
content selector should select all matching elements (fix part of #936)
1 parent 942e2bb commit 9fbed43

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

src/compiler/content.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,18 @@ module.exports = {
3636
} else {
3737
// select content
3838
selector = vm.$interpolate(selector)
39-
content = raw.querySelector(selector)
40-
// only allow top-level select
41-
if (content && content.parentNode === raw) {
42-
// same deal, clone the node for v-if
43-
content = content.cloneNode(true)
44-
this.compile(content, parent, vm)
39+
var nodes = raw.querySelectorAll(selector)
40+
if (nodes.length) {
41+
content = document.createDocumentFragment()
42+
for (var i = 0, l = nodes.length; i < l; i++) {
43+
// only allow top-level select
44+
if (nodes[i].parentNode === raw) {
45+
content.appendChild(nodes[i].cloneNode(true))
46+
}
47+
}
48+
if (content.hasChildNodes()) {
49+
this.compile(content, parent, vm)
50+
}
4551
}
4652
}
4753
},

test/unit/specs/compiler/content_spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ describe('Content Transclusion', function () {
5454
expect(el.lastChild.textContent).toBe('select b')
5555
})
5656

57+
it('selector matching multiple elements', function () {
58+
el.innerHTML = '<p class="t">1</p><div></div><p class="t">2</p>'
59+
options.template = '<content select=".t"></content>'
60+
mount()
61+
expect(el.innerHTML).toBe('<p class="t">1</p><p class="t">2</p>')
62+
})
63+
5764
it('content transclusion with replace', function () {
5865
el.innerHTML = '<p>hi</p>'
5966
options.template = '<div><div><content></content></div></div>'

0 commit comments

Comments
 (0)