Skip to content

Commit

Permalink
Find non distributed children with deepContains
Browse files Browse the repository at this point in the history
Walk logical tree when necessary
  • Loading branch information
dfreedm committed Oct 29, 2015
1 parent 279bf63 commit 8e6f55a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/lib/dom-api.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,15 @@
if (this.node.contains(node)) {
return true;
}

var n = node;
// wrap document for SD polyfill
var wrappedDocument = wrap(document);

// walk from node to `this` or `document`
while (n && n !== document && n !== this.node) {
n = n.parentNode || n.host;
while (n && n !== wrappedDocument && n !== this.node) {
// use logical parentnode, or native ShadowRoot host
n = Polymer.dom(n).parentNode || n.host;
}
return n === this.node;
},
Expand Down
5 changes: 4 additions & 1 deletion test/unit/polymer-dom-elements.html
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@
<dom-module id="x-deep-contains">
<template>
<div id="shadowed"></div>
<content></content>
<content select="#light"></content>
</template>
<script>
Polymer({
Expand All @@ -342,6 +342,9 @@
var e = document.createElement('div');
e.id = 'light';
Polymer.dom(this).appendChild(e);
e = document.createElement('div');
e.id = 'notdistributed';
Polymer.dom(this).appendChild(e);
}
});
</script>
Expand Down
4 changes: 3 additions & 1 deletion test/unit/polymer-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -921,14 +921,16 @@ suite('Polymer.dom non-distributed elements', function() {
test('Deep Contains', function() {
var el = document.querySelector('x-deep-contains');
var shadow = el.$.shadowed;
var light = el.querySelector('#light');
var light = Polymer.dom(el).querySelector('#light');
var notdistributed = Polymer.dom(el).querySelector('#notdistributed');
var disconnected = document.createElement('div');
var separate = document.createElement('div');
document.body.appendChild(separate);

assert.equal(Polymer.dom(el).deepContains(el), true, 'Element should deepContain itself');
assert.equal(Polymer.dom(el).deepContains(shadow), true, 'Shadowed Child element should be found');
assert.equal(Polymer.dom(el).deepContains(light), true, 'Light Child element should be found');
assert.equal(Polymer.dom(el).deepContains(notdistributed), true, 'Non-distributed child element should be found');
assert.equal(Polymer.dom(el).deepContains(disconnected), false, 'Disconnected element should not be found');
assert.equal(Polymer.dom(el).deepContains(separate), false, 'Unassociated, attached element should not be found');

Expand Down

0 comments on commit 8e6f55a

Please sign in to comment.