Skip to content

Commit

Permalink
use destination insertion points when calculating the path
Browse files Browse the repository at this point in the history
  • Loading branch information
valdrinkoshi committed Dec 15, 2015
1 parent a5b7b85 commit 3f8b6ee
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
32 changes: 20 additions & 12 deletions src/lib/dom-api-event.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
var DomApi = Polymer.DomApi.ctor;
var Settings = Polymer.Settings;


/**
* DomApi.Event allows maniuplation of events compatible with
* the scoping concepts in Shadow DOM and compatible with both Shady DOM
* and Shadow DOM. The general usage is
* `Polymer.dom(event).property`. The `path` property returns `event.path`
* matching Shadow DOM. The `rootTarget` property returns the first node
* DomApi.Event allows maniuplation of events compatible with
* the scoping concepts in Shadow DOM and compatible with both Shady DOM
* and Shadow DOM. The general usage is
* `Polymer.dom(event).property`. The `path` property returns `event.path`
* matching Shadow DOM. The `rootTarget` property returns the first node
* in the `path` and is the original event target. The `localTarget` property
* matches event.target under Shadow DOM and is the scoped event target.
*/
Expand All @@ -32,7 +32,7 @@
if (Settings.useShadow) {

DomApi.Event.prototype = {

get rootTarget() {
return this.event.path[0];
},
Expand All @@ -50,7 +50,7 @@
} else {

DomApi.Event.prototype = {

get rootTarget() {
return this.event.target;
},
Expand All @@ -71,10 +71,18 @@
get path() {
if (!this.event._path) {
var path = [];
var o = this.rootTarget;
while (o) {
path.push(o);
o = Polymer.dom(o).parentNode || o.host;
var current = this.rootTarget;
while (current) {
path.push(current);
var insertionPoints = Polymer.dom(current).getDestinationInsertionPoints();
if (insertionPoints.length) {
for (var i = 0; i < insertionPoints.length - 1; i++) {
path.push(insertionPoints[i]);
}
current = insertionPoints[insertionPoints.length - 1];
} else {
current = Polymer.dom(current).parentNode || current.host;
}
}
// event path includes window in most recent native implementations
path.push(window);
Expand Down
19 changes: 19 additions & 0 deletions test/unit/polymer-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,25 @@ suite('Polymer.dom', function() {
assert.sameMembers(order, [cb1, cbReentrant, cb2, cb3, cb4]);
});

test('path correctly calculated for elements with destination insertion points', function(done) {
var re = document.createElement('x-reproject');
var p = Polymer.dom(re.root).querySelector('x-project');
var child = document.createElement('p');
child.innerHTML = "hello";
// child will be inserted into p after distributeContent is performed.
Polymer.dom(re).appendChild(child);
Polymer.dom(document.body).appendChild(re);
Polymer.dom.flush();
child.addEventListener('child-event', function(e){
var path = Polymer.dom(e).path;
assert.isTrue(path.indexOf(p) !== -1, 'path contains p');
assert.isTrue(path.indexOf(re) !== -1, 'path contains re');
done();
});
var evt = new CustomEvent('child-event');
child.dispatchEvent(evt);
});

});

suite('Polymer.dom accessors', function() {
Expand Down

0 comments on commit 3f8b6ee

Please sign in to comment.