Skip to content

Commit

Permalink
Merge pull request #5527 from Polymer/localTarget-noPatch
Browse files Browse the repository at this point in the history
Fix `localTarget` when `ShadyDOM.noPatch` is in use
  • Loading branch information
dfreedm authored Apr 23, 2019
2 parents 8f7119a + 7925254 commit 03b2c66
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
13 changes: 12 additions & 1 deletion lib/legacy/polymer.dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,9 +408,20 @@ if (window['ShadyDOM'] && window['ShadyDOM']['inUse'] && window['ShadyDOM']['noP

Object.defineProperties(EventApi.prototype, {

// Returns the "lowest" node in the same root as the event's currentTarget.
// When in `noPatch` mode, this must be calculated by walking the event's
// path.
localTarget: {
get() {
return this.event.currentTarget;
const current = this.event.currentTarget;
const currentRoot = current && dom(current).getOwnerRoot();
const p$ = this.path;
for (let i = 0; i < p$.length; i++) {
const e = p$[i];
if (dom(e).getOwnerRoot() === currentRoot) {
return e;
}
}
},
configurable: true
},
Expand Down
21 changes: 18 additions & 3 deletions test/unit/polymer-dom-nopatch.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@

<dom-module id="x-event-scoped">
<template>
<div id="scoped"></div>
<div id="container">
<div id="scoped"></div>
</div>
</template>
<script type="module">
import { Polymer } from '../../polymer-legacy.js';
Expand Down Expand Up @@ -189,7 +191,7 @@

suite('events', function() {

test('localTarget, rootTarget, path', function(done) {
test('localTarget, rootTarget, path', function() {
// skip if noPatch is not available
if (!window.ShadyDOM.wrap) {
this.skip();
Expand All @@ -207,7 +209,20 @@
nodes.push(window);
const path = dom(e).path;
assert.deepEqual(path, nodes);
done();
});
ShadyDOM.flush();
el.fireComposed();
});

test('localTarget when target node and event listener node are distinct', function() {
// skip if noPatch is not available
if (!window.ShadyDOM.wrap) {
this.skip();
}
var el = fixture('scoped');
el.$.container.addEventListener('composed', function(e) {
assert.equal(dom(e).rootTarget, el.$.scoped);
assert.equal(dom(e).localTarget, el.$.scoped);
});
ShadyDOM.flush();
el.fireComposed();
Expand Down

0 comments on commit 03b2c66

Please sign in to comment.