From e83c09eac58aa3ea2343aac3a648b3db3353afd6 Mon Sep 17 00:00:00 2001 From: Erik Arvidsson Date: Thu, 10 Oct 2013 15:59:37 -0400 Subject: [PATCH] Add work around for X-Tag issue related to relatedTarget --- src/wrappers/events.js | 20 +++++++++++++------- test/js/events.js | 13 +++++++++++++ 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/wrappers/events.js b/src/wrappers/events.js index d413e61..ba5df85 100644 --- a/src/wrappers/events.js +++ b/src/wrappers/events.js @@ -294,13 +294,19 @@ if ('relatedTarget' in event) { var originalEvent = unwrap(event); - var relatedTarget = wrap(originalEvent.relatedTarget); - - var adjusted = adjustRelatedTarget(currentTarget, relatedTarget); - if (adjusted === target) - return true; - - relatedTargetTable.set(event, adjusted); + // X-Tag sets relatedTarget on a CustomEvent. If they do that there is no + // way to have relatedTarget return the adjusted target but worse is that + // the originalEvent might not have a relatedTarget so we hit an assert + // when we try to wrap it. + if (originalEvent.relatedTarget) { + var relatedTarget = wrap(originalEvent.relatedTarget); + + var adjusted = adjustRelatedTarget(currentTarget, relatedTarget); + if (adjusted === target) + return true; + + relatedTargetTable.set(event, adjusted); + } } eventPhaseTable.set(event, phase); diff --git a/test/js/events.js b/test/js/events.js index 0114fcc..78da3fa 100644 --- a/test/js/events.js +++ b/test/js/events.js @@ -1277,4 +1277,17 @@ test('retarget order (multiple shadow roots)', function() { text.dispatchEvent(new Event('x')); }); + test('manual relatedTarget', function() { + var ce = new CustomEvent('x'); + ce.relatedTarget = 42; + var count = 0; + document.addEventListener('x', function f(e) { + count++; + assert.equal(e.relatedTarget, 42); + document.removeEventListener('x', f); + }); + document.dispatchEvent(ce); + assert.equal(count, 1); + }); + });