Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prevent default action for simple html5 media events. fixes #573, fixes #620 (duplicate bug) #630

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/js/media/html5.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ vjs.Html5.prototype.setupTriggers = function(){
// Triggers removed using this.off when disposed

vjs.Html5.prototype.eventHandler = function(e){
// Prevent default action from being called on the target (the video element),
// as all events handled here originated from it. Fixes #620.
e.preventDefault();

this.trigger(e);

// No need for media events to bubble up.
Expand Down
19 changes: 19 additions & 0 deletions test/unit/media.html5.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,23 @@ test('should re-link the player if the tech is moved', function(){
tech.createEl();

strictEqual(player, tech.el()['player']);
});

test('should not call default action on media event', function() {
expect(2);
var player = {
id: function() { return 'id'; },
el: function() { return document.createElement('div'); },
options_: {},
trigger: function(event) {
ok(event.type === 'play', 'non-play media event fired');
ok(event.isDefaultPrevented(), 'default action not prevented');
},
ready: function() {}
};
var tech = new vjs.Html5(player, { el: vjs.TEST_VID });
// Mediafaker doesn't support play/pause, so dispatch an event manually.
var event = document.createEvent('CustomEvent');
event.initCustomEvent('play', false /*bubbles*/, true /*cancelable*/, null);
tech.el_.dispatchEvent(event);
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for writing a test!

There's a PlayerTest.makePlayer() function that could make this simpler. https://github.com/videojs/video.js/blob/master/test/unit/test-helpers.js#L8

createEvent is apparently deprecated.
https://developer.mozilla.org/en-US/docs/Web/API/document.createEvent
Also not supported in ie8, but I guess that's not really an issue in the HTML5 tests. Probably better to use a constructor like MDN says.

It seems like at the core of this you're just testing that preventDefault works, does that sound right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay - I've incorporated PlayerTest.makePlayer().

I should have mentioned this before submitting, but I did try to use event constructors and ran into an issue in phantomjs - ariya/phantomjs#11289. Okay to keep createElement for now?

And yeah - the point is just a regression test to verify that events bubbled from the html5 tech have preventDefault called on them before trigger is called.