Skip to content

Commit 03b2c66

Browse files
authored
Merge pull request #5527 from Polymer/localTarget-noPatch
Fix `localTarget` when `ShadyDOM.noPatch` is in use
2 parents 8f7119a + 7925254 commit 03b2c66

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

lib/legacy/polymer.dom.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -408,9 +408,20 @@ if (window['ShadyDOM'] && window['ShadyDOM']['inUse'] && window['ShadyDOM']['noP
408408

409409
Object.defineProperties(EventApi.prototype, {
410410

411+
// Returns the "lowest" node in the same root as the event's currentTarget.
412+
// When in `noPatch` mode, this must be calculated by walking the event's
413+
// path.
411414
localTarget: {
412415
get() {
413-
return this.event.currentTarget;
416+
const current = this.event.currentTarget;
417+
const currentRoot = current && dom(current).getOwnerRoot();
418+
const p$ = this.path;
419+
for (let i = 0; i < p$.length; i++) {
420+
const e = p$[i];
421+
if (dom(e).getOwnerRoot() === currentRoot) {
422+
return e;
423+
}
424+
}
414425
},
415426
configurable: true
416427
},

test/unit/polymer-dom-nopatch.html

+18-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@
4747

4848
<dom-module id="x-event-scoped">
4949
<template>
50-
<div id="scoped"></div>
50+
<div id="container">
51+
<div id="scoped"></div>
52+
</div>
5153
</template>
5254
<script type="module">
5355
import { Polymer } from '../../polymer-legacy.js';
@@ -189,7 +191,7 @@
189191

190192
suite('events', function() {
191193

192-
test('localTarget, rootTarget, path', function(done) {
194+
test('localTarget, rootTarget, path', function() {
193195
// skip if noPatch is not available
194196
if (!window.ShadyDOM.wrap) {
195197
this.skip();
@@ -207,7 +209,20 @@
207209
nodes.push(window);
208210
const path = dom(e).path;
209211
assert.deepEqual(path, nodes);
210-
done();
212+
});
213+
ShadyDOM.flush();
214+
el.fireComposed();
215+
});
216+
217+
test('localTarget when target node and event listener node are distinct', function() {
218+
// skip if noPatch is not available
219+
if (!window.ShadyDOM.wrap) {
220+
this.skip();
221+
}
222+
var el = fixture('scoped');
223+
el.$.container.addEventListener('composed', function(e) {
224+
assert.equal(dom(e).rootTarget, el.$.scoped);
225+
assert.equal(dom(e).localTarget, el.$.scoped);
211226
});
212227
ShadyDOM.flush();
213228
el.fireComposed();

0 commit comments

Comments
 (0)