Skip to content

Commit 5ff5569

Browse files
misteroneillgkatsev
authored andcommitted
fix: ensure the default ID of the first player is 'vjs_video_3' as some people have relied on this (#6216)
When a player is created without an id on the embed code, Video.js automatically assigns it one based on an auto-incrementing number (a.k.a. a GUID). For the longest time, this has happened to result in the default id of the first player being vjs_video_3. It was never intended for users to rely on this value being consistent, but users do strange and inadvisable things. PR #6103 had an unintended side effect in that it changed the default id to vjs_video_2, which we worry could affect some users of Video.js.
1 parent 6636d78 commit 5ff5569

File tree

3 files changed

+53
-13
lines changed

3 files changed

+53
-13
lines changed

src/js/setup.js

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
* @module setup
66
*/
77
import * as Dom from './utils/dom';
8-
import * as Events from './utils/events.js';
98
import document from 'global/document';
109
import window from 'global/window';
1110

@@ -79,21 +78,34 @@ function autoSetupTimeout(wait, vjs) {
7978
window.setTimeout(autoSetup, wait);
8079
}
8180

82-
if (Dom.isReal() && document.readyState === 'complete') {
81+
/**
82+
* Used to set the internal tracking of window loaded state to true.
83+
*
84+
* @private
85+
*/
86+
function setWindowLoaded() {
8387
_windowLoaded = true;
84-
} else {
85-
/**
86-
* Listen for the load event on window, and set _windowLoaded to true.
87-
*
88-
* @listens load
89-
*/
90-
Events.one(window, 'load', function() {
91-
_windowLoaded = true;
92-
});
88+
window.removeEventListener('load', setWindowLoaded);
89+
}
90+
91+
if (Dom.isReal()) {
92+
if (document.readyState === 'complete') {
93+
setWindowLoaded();
94+
} else {
95+
/**
96+
* Listen for the load event on window, and set _windowLoaded to true.
97+
*
98+
* We use a standard event listener here to avoid incrementing the GUID
99+
* before any players are created.
100+
*
101+
* @listens load
102+
*/
103+
window.addEventListener('load', setWindowLoaded);
104+
}
93105
}
94106

95107
/**
96-
* check if the document has been loaded
108+
* check if the window has been loaded
97109
*/
98110
const hasLoaded = function() {
99111
return _windowLoaded;

src/js/utils/guid.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,20 @@
33
* @module guid
44
*/
55

6+
// Default value for GUIDs. This allows us to reset the GUID counter in tests.
7+
//
8+
// The initial GUID is 3 because some users have come to rely on the first
9+
// default player ID ending up as `vjs_video_3`.
10+
//
11+
// See: https://github.com/videojs/video.js/pull/6216
12+
const _initialGuid = 3;
13+
614
/**
715
* Unique ID for an element or function
16+
*
817
* @type {Number}
918
*/
10-
let _guid = 1;
19+
let _guid = _initialGuid;
1120

1221
/**
1322
* Get a unique auto-incrementing ID by number that has not been returned before.
@@ -18,3 +27,10 @@ let _guid = 1;
1827
export function newGUID() {
1928
return _guid++;
2029
}
30+
31+
/**
32+
* Reset the unique auto-incrementing ID for testing only.
33+
*/
34+
export function resetGuidInTestsOnly() {
35+
_guid = _initialGuid;
36+
}

test/unit/player.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import sinon from 'sinon';
1414
import window from 'global/window';
1515
import * as middleware from '../../src/js/tech/middleware.js';
1616
import * as Events from '../../src/js/utils/events.js';
17+
import * as Guid from '../../src/js/utils/guid.js';
1718

1819
QUnit.module('Player', {
1920
beforeEach() {
@@ -31,6 +32,17 @@ QUnit.module('Player', {
3132
}
3233
});
3334

35+
QUnit.test('the default ID of the first player remains "vjs_video_3"', function(assert) {
36+
Guid.resetGuidInTestsOnly();
37+
const tag = document.createElement('video');
38+
39+
tag.className = 'video-js';
40+
41+
const player = TestHelpers.makePlayer({}, tag);
42+
43+
assert.strictEqual(player.id(), 'vjs_video_3', 'id is correct');
44+
});
45+
3446
QUnit.test('should create player instance that inherits from component and dispose it', function(assert) {
3547
const player = TestHelpers.makePlayer();
3648

0 commit comments

Comments
 (0)