diff --git a/src/js/setup.js b/src/js/setup.js index 7b6f47efee..ebd9bc7027 100644 --- a/src/js/setup.js +++ b/src/js/setup.js @@ -5,7 +5,6 @@ * @module setup */ import * as Dom from './utils/dom'; -import * as Events from './utils/events.js'; import document from 'global/document'; import window from 'global/window'; @@ -79,21 +78,34 @@ function autoSetupTimeout(wait, vjs) { window.setTimeout(autoSetup, wait); } -if (Dom.isReal() && document.readyState === 'complete') { +/** + * Used to set the internal tracking of window loaded state to true. + * + * @private + */ +function setWindowLoaded() { _windowLoaded = true; -} else { - /** - * Listen for the load event on window, and set _windowLoaded to true. - * - * @listens load - */ - Events.one(window, 'load', function() { - _windowLoaded = true; - }); + window.removeEventListener('load', setWindowLoaded); +} + +if (Dom.isReal()) { + if (document.readyState === 'complete') { + setWindowLoaded(); + } else { + /** + * Listen for the load event on window, and set _windowLoaded to true. + * + * We use a standard event listener here to avoid incrementing the GUID + * before any players are created. + * + * @listens load + */ + window.addEventListener('load', setWindowLoaded); + } } /** - * check if the document has been loaded + * check if the window has been loaded */ const hasLoaded = function() { return _windowLoaded; diff --git a/src/js/utils/guid.js b/src/js/utils/guid.js index 59e05554ec..7801251791 100644 --- a/src/js/utils/guid.js +++ b/src/js/utils/guid.js @@ -3,11 +3,20 @@ * @module guid */ +// Default value for GUIDs. This allows us to reset the GUID counter in tests. +// +// The initial GUID is 3 because some users have come to rely on the first +// default player ID ending up as `vjs_video_3`. +// +// See: https://github.com/videojs/video.js/pull/6216 +const _initialGuid = 3; + /** * Unique ID for an element or function + * * @type {Number} */ -let _guid = 1; +let _guid = _initialGuid; /** * Get a unique auto-incrementing ID by number that has not been returned before. @@ -18,3 +27,10 @@ let _guid = 1; export function newGUID() { return _guid++; } + +/** + * Reset the unique auto-incrementing ID for testing only. + */ +export function resetGuidInTestsOnly() { + _guid = _initialGuid; +} diff --git a/test/unit/player.test.js b/test/unit/player.test.js index e66a00224a..a1eef9a238 100644 --- a/test/unit/player.test.js +++ b/test/unit/player.test.js @@ -14,6 +14,7 @@ import sinon from 'sinon'; import window from 'global/window'; import * as middleware from '../../src/js/tech/middleware.js'; import * as Events from '../../src/js/utils/events.js'; +import * as Guid from '../../src/js/utils/guid.js'; QUnit.module('Player', { beforeEach() { @@ -31,6 +32,17 @@ QUnit.module('Player', { } }); +QUnit.test('the default ID of the first player remains "vjs_video_3"', function(assert) { + Guid.resetGuidInTestsOnly(); + const tag = document.createElement('video'); + + tag.className = 'video-js'; + + const player = TestHelpers.makePlayer({}, tag); + + assert.strictEqual(player.id(), 'vjs_video_3', 'id is correct'); +}); + QUnit.test('should create player instance that inherits from component and dispose it', function(assert) { const player = TestHelpers.makePlayer();