Skip to content

Commit

Permalink
Checking on the event the relatedTarget exists before setting its pro…
Browse files Browse the repository at this point in the history
…perty

Fixes a bug where relatedTarget was undefined in Firefox

fixes #2024
closes #2025
  • Loading branch information
Michelle Anderson authored and heff committed Jul 9, 2015
1 parent 004b1e8 commit 3479a54
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ CHANGELOG
* @mmcc added the vjs-big-play-centered class ([view](https://github.com/videojs/video.js/pull/2293))
* @thijstriemstra added a logged error when a plugin is missing ([view](https://github.com/videojs/video.js/pull/1931))
* @gkatsev fixed the texttrackchange event and text track display for non-native tracks ([view](https://github.com/videojs/video.js/pull/2215))
* @mischizzle fixed event.relatedTarget in Firefox ([view](https://github.com/videojs/video.js/pull/2025))

--------------------

Expand Down
8 changes: 5 additions & 3 deletions src/js/utils/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,11 @@ export function fixEvent(event) {
}

// Handle which other element the event is related to
event.relatedTarget = event.fromElement === event.target ?
event.toElement :
event.fromElement;
if (!event.relatedTarget) {
event.relatedTarget = event.fromElement === event.target ?
event.toElement :
event.fromElement;
}

// Stop the default browser action
event.preventDefault = function () {
Expand Down
20 changes: 20 additions & 0 deletions test/unit/events.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,23 @@ test('should have a defaultPrevented property on an event that was prevent from

Events.trigger(el, 'test');
});

test('should have relatedTarget correctly set on the event', function() {
expect(2);

var el1 = document.createElement('div'),
el2 = document.createElement('div'),
relatedEl = document.createElement('div');

Events.on(el1, 'click', function(e){
equal(e.relatedTarget, relatedEl, 'relatedTarget is set for all browsers when related element is set on the event');
});

Events.trigger(el1, { type:'click', relatedTarget:relatedEl });

Events.on(el2, 'click', function(e) {
equal(e.relatedTarget, null, 'relatedTarget is null when none is provided');
});

Events.trigger(el2, { type:'click', relatedTarget:undefined });
});

0 comments on commit 3479a54

Please sign in to comment.