Skip to content

<content> select should only match children. #787

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/compiler/transclude.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,14 @@ function transcludeContent (el, raw) {
select = outlet.getAttribute('select')
if (select) { // select content
selected = raw.querySelectorAll(select)
outlet.content = _.toArray(
selected.length
? selected
: outlet.childNodes
)
if (selected.length) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One possible issue here: if all nodes in selected are nested ones (thus should be filtered out), we end up outputting empty content, but in that case we should actually use the fallback content (outlet.childNodes).

According to http://w3c.github.io/webcomponents/spec/shadow/#dfn-pool-distribution-algorithm, the algorithm here should be:

  • For each direct child node of raw, check if it matches select.
  • If yes, push it into selected.
  • If selected ends up empty, use outlet.childNodes instead.

This also means we need a matches() helper in util/dom.js:

var p = Element.prototype
var matches = p.matches || p.webkitMatchesSelector || p.mozMatchesSelector || p.msMatchesSelector
exports.matches = function (el, selector) {
  return matches.call(el, selector)
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to avoid element.matches because its browser support is slightly worse than querySelectorAll (http://caniuse.com/#feat=matchesselector vs http://caniuse.com/#search=queryselectorall). In practice it probably doesn't matter (opera mini 8 is the only browser that's still used of those, and I don't know if vue even supports it), but I decided to play it safe.

selected = _.toArray(selected).filter(function(node) {
return node.parentNode === raw
})
}
outlet.content = selected.length
? selected
: _.toArray(outlet.childNodes)
} else { // default content
main = outlet
}
Expand Down
10 changes: 10 additions & 0 deletions test/unit/specs/compiler/transclude_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,15 @@ if (_.inBrowser) {
expect(res.childNodes[3].tagName).toBe('SPAN')
})

it('select should only match children', function () {
el.innerHTML = '<p class="b">select b</p><span><p class="b">nested b</p></span><span><p class="c">nested c</p></span>'
options.template = '<content select=".a"><p>fallback a</p></content><content select=".b">fallback b</content><content select=".c">fallback c</content>'
var res = transclude(el, options)
expect(res.childNodes.length).toBe(3)
expect(res.firstChild.textContent).toBe('fallback a')
expect(res.childNodes[1].textContent).toBe('select b')
expect(res.lastChild.textContent).toBe('fallback c')
})

})
}