Skip to content

Commit

Permalink
Merge pull request #1579 from Polymer/0.9-fix-targetfinding
Browse files Browse the repository at this point in the history
Use more resiliant shadowroot checking
  • Loading branch information
dfreedm committed May 21, 2015
2 parents 6bb5023 + 8f05e43 commit 269ef2f
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 14 deletions.
30 changes: 16 additions & 14 deletions src/standard/gestures.html
Original file line number Diff line number Diff line change
Expand Up @@ -97,24 +97,26 @@
return ta;
}

function deepTargetFind(x, y) {
var node = document.elementFromPoint(x, y);
// this code path is only taken when native ShadowDOM is used
var next = node.shadowRoot;
while(next) {
next = next.elementFromPoint(x, y);
if (next) {
node = next;
next = next.shadowRoot;
}
}
return node;
}

var Gestures = {
gestures: {},
recognizers: [],

deepTargetFind: function(x, y) {
var node = document.elementFromPoint(x, y);
var next = node;
// this code path is only taken when native ShadowDOM is used
// if there is a shadowroot, it may have a node at x/y
// if there is not a shadowroot, exit the loop
while (next && next.shadowRoot) {
// if there is a node at x/y in the shadowroot, look deeper
next = next.shadowRoot.elementFromPoint(x, y);
if (next) {
node = next;
}
}
return node;
},
handleNative: function(ev) {
var handled;
var type = ev.type;
Expand Down Expand Up @@ -396,7 +398,7 @@
ddx: ddx,
ddy: ddy,
hover: function() {
return deepTargetFind(touch.clientX, touch.clientY);
return Gestures.deepTargetFind(touch.clientX, touch.clientY);
}
});
}
Expand Down
60 changes: 60 additions & 0 deletions test/unit/gestures.html
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,66 @@
});
});

suite('target finding', function() {
var div, divLocation;

setup(function() {
div = document.createElement('div');
div.style.cssText = 'height: 50px; width: 50px; background: red;';
div.id = 'target';
document.body.appendChild(div);
divLocation = div.getBoundingClientRect();
});

test('target finding returns null outside the window', function() {
var actual = Polymer.Gestures.deepTargetFind(-1, -1);
assert.equal(actual, null);
})

test('find the div in document', function() {
var x = divLocation.left, y = divLocation.top;
var actual = Polymer.Gestures.deepTargetFind(x, y);
assert.equal(actual, div);
});

test('find the div with a shadowroot', function() {
if (!div.createShadowRoot) {
return;
}
div.createShadowRoot();
var x = divLocation.left, y = divLocation.top;
var actual = Polymer.Gestures.deepTargetFind(x, y);
assert.equal(actual, div);
});

test('find the div inside a shadowroot', function() {
if (!div.createShadowRoot) {
return;
}
var divOwner = document.createElement('span');
document.body.appendChild(divOwner);
divOwner.createShadowRoot().appendChild(div);
var bcr = divOwner.getBoundingClientRect();
var x = bcr.left, y = bcr.top;
var actual = Polymer.Gestures.deepTargetFind(x, y);
assert.equal(actual, div);
});

test('find the div with a shadowroot inside a shadowroot', function() {
if (!div.createShadowRoot) {
return;
}
div.createShadowRoot();
var divOwner = document.createElement('span');
document.body.appendChild(divOwner);
divOwner.createShadowRoot().appendChild(div);
var bcr = divOwner.getBoundingClientRect();
var x = bcr.left, y = bcr.top;
var actual = Polymer.Gestures.deepTargetFind(x, y);
assert.equal(actual, div);
});
});

// TODO(dfreedm): Add more gesture tests!

</script>
Expand Down

0 comments on commit 269ef2f

Please sign in to comment.