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 videojs#2024
  • Loading branch information
Michelle Anderson committed Apr 9, 2015
1 parent 7f70f09 commit 3e6001e
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ sandbox/*

test/coverage/*
.coveralls.yml

.idea/
5 changes: 2 additions & 3 deletions .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@
"_V_",
"goog",
"console",

"require",
"define",
"module",
"exports",
"process",

"q",
"asyncTest",
"deepEqual",
Expand All @@ -39,6 +37,7 @@
"stop",
"strictEqual",
"test",
"sinon"
"sinon",
"MouseEvent"
]
}
10 changes: 5 additions & 5 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ module.exports = function(grunt) {
novtt: { src: './build/temp/video.js', dest: './build/temp/alt/video.novtt.js' },
dist: { expand: true, cwd: 'build/temp/', src: ['**/**'], dest: 'dist/', filter: 'isFile' },
examples: { expand: true, cwd: 'build/examples/', src: ['**/**'], dest: 'dist/examples/', filter: 'isFile' },
cdn: { expand: true, cwd: 'dist/', src: ['**/**'], dest: 'dist/cdn/', filter: 'isFile' },
cdn: { expand: true, cwd: 'dist/', src: ['**/**'], dest: 'dist/cdn/', filter: 'isFile' }
},
aws_s3: {
options: {
Expand Down Expand Up @@ -281,7 +281,7 @@ module.exports = function(grunt) {
build: {
options: {},
files: {
'build/temp/video.js.map': ['build/temp/video.js'],
'build/temp/video.js.map': ['build/temp/video.js']
}
}
},
Expand All @@ -293,11 +293,11 @@ module.exports = function(grunt) {
concat: {
vtt: {
options: {
separator: '\n',
separator: '\n'
},
src: ['build/temp/video.js', 'node_modules/vtt.js/dist/vtt.js'],
dest: 'build/temp/video.js',
},
dest: 'build/temp/video.js'
}
}
});

Expand Down
8 changes: 5 additions & 3 deletions src/js/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ var fixEvent = function(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
21 changes: 21 additions & 0 deletions test/unit/events.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as Events from '../../src/js/events.js';
import document from 'global/document';
import TestHelpers from './test-helpers.js';

q.module('Events');

Expand Down Expand Up @@ -201,3 +202,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 referring element is set on the event');
});

Events.trigger(el1, TestHelpers.makeMouseEvent('click', relatedEl));

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

Events.trigger(el2, TestHelpers.makeMouseEvent('click'));
});
19 changes: 18 additions & 1 deletion test/unit/test-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@ var TestHelpers = {
return player = new Player(videoTag, playerOptions);
},

makeMouseEvent: function(type, relatedTarget){
var event;

if(MouseEvent) {
return new MouseEvent(type, {relatedTarget: relatedTarget || null});
}

event = document.createEvent('MouseEvents');

if(event.initMouseEvent) {
event.initMouseEvent(type, true, true, window, 0, 0, 0, 80, 20, false, false, false, false, 0, relatedTarget || null);
} else {
event.initEvent(type, true, false);
}
return event;
},

getComputedStyle: function(el, rule){
var val;

Expand All @@ -39,4 +56,4 @@ var TestHelpers = {
}
};

export default TestHelpers;
export default TestHelpers;

0 comments on commit 3e6001e

Please sign in to comment.