Skip to content
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

Use more resiliant shadowroot checking #1579

Merged
merged 1 commit into from
May 21, 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
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