Skip to content

Commit

Permalink
Fix localTareget when ShadyDOM.noPatch is in use
Browse files Browse the repository at this point in the history
Fixes #5526.

Return the "lowest" element in the same root as the node listening for the event (currentTarget).
  • Loading branch information
Steven Orvell committed Apr 22, 2019
1 parent 07f1e19 commit 7925254
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 7925254

Please sign in to comment.