-
Notifications
You must be signed in to change notification settings - Fork 791
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add preload configuration option for media files (#1958)
- Loading branch information
Showing
9 changed files
with
134 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* Given a rootNode | ||
* -> get all HTMLMediaElement's and ensure their metadata is loaded | ||
* | ||
* @method preloadMedia | ||
* @memberof axe.utils | ||
* @property {Object} options.treeRoot (optional) the DOM tree to be inspected | ||
*/ | ||
axe.utils.preloadMedia = function preloadMedia({ treeRoot = axe._tree[0] }) { | ||
const mediaVirtualNodes = axe.utils.querySelectorAll( | ||
treeRoot, | ||
'video, audio' | ||
); | ||
|
||
return Promise.all( | ||
mediaVirtualNodes.map(({ actualNode }) => isMediaElementReady(actualNode)) | ||
); | ||
}; | ||
|
||
/** | ||
* Ensures a media element's metadata is loaded | ||
* @param {HTMLMediaElement} elm elm | ||
* @returns {Promise} | ||
*/ | ||
function isMediaElementReady(elm) { | ||
return new Promise(resolve => { | ||
/** | ||
* See - https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/readyState | ||
*/ | ||
if (elm.readyState > 0) { | ||
resolve(elm); | ||
} | ||
|
||
function onMediaReady() { | ||
elm.removeEventListener('loadedmetadata', onMediaReady); | ||
resolve(elm); | ||
} | ||
|
||
/** | ||
* Given media is not ready, wire up listener for `loadedmetadata` | ||
* See - https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/loadedmetadata_event | ||
*/ | ||
elm.addEventListener('loadedmetadata', onMediaReady); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* global Promise */ | ||
|
||
describe('axe.utils.preloadMedia', function() { | ||
'use strict'; | ||
|
||
var origFn = axe.utils.preloadMedia; | ||
var fixture = document.getElementById('fixture'); | ||
var fixtureSetup = axe.testUtils.fixtureSetup; | ||
var isIE11 = axe.testUtils.isIE11; | ||
|
||
afterEach(function() { | ||
axe.utils.preloadMedia = origFn; | ||
fixture.innerHTML = ''; | ||
}); | ||
|
||
it('invokes utils.preloadMedia and passes the treeRoot property', function(done) { | ||
var isCalled = false; | ||
axe.utils.preloadMedia = function(options) { | ||
assert.isDefined(options.treeRoot); | ||
isCalled = true; | ||
return Promise.resolve(); | ||
}; | ||
|
||
axe._tree = axe.utils.getFlattenedTree(document); | ||
|
||
axe.utils.preloadMedia({ treeRoot: axe._tree[0] }).then(function() { | ||
assert.ok(isCalled); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('returns empty array when there are no media nodes to be preloaded', function(done) { | ||
axe._tree = axe.utils.getFlattenedTree(document); | ||
|
||
axe.utils.preloadMedia({ treeRoot: axe._tree[0] }).then(function(result) { | ||
assert.equal(result.length, 0); | ||
done(); | ||
}); | ||
}); | ||
|
||
(isIE11 ? it.skip : it)( | ||
'returns media node (audio) after their metadata has been preloaded', | ||
function(done) { | ||
fixtureSetup( | ||
'<audio src="/test/assets/moon-speech.mp3" autoplay="true" controls></audio>' | ||
); | ||
|
||
axe.utils.preloadMedia({ treeRoot: axe._tree[0] }).then(function(result) { | ||
assert.equal(result.length, 1); | ||
assert.isTrue(result[0].readyState > 0); | ||
assert.equal(Math.round(result[0].duration), 27); | ||
|
||
done(); | ||
}); | ||
} | ||
); | ||
|
||
(isIE11 ? it.skip : it)( | ||
'returns media nodes (audio, video) after their metadata has been preloaded', | ||
function(done) { | ||
fixtureSetup( | ||
// 1 audio elm | ||
'<audio src="/test/assets/moon-speech.mp3"></audio>' + | ||
// 1 video elm | ||
'<video>' + | ||
'<source src="/test/assets/video.mp4" type="video/mp4" />' + | ||
'<source src="/test/assets/video.webm" type="video/webm" />' + | ||
'</video>' | ||
); | ||
|
||
axe.utils.preloadMedia({ treeRoot: axe._tree[0] }).then(function(result) { | ||
assert.equal(result.length, 2); | ||
assert.isTrue(result[0].readyState > 0); | ||
assert.equal(Math.round(result[0].duration), 27); | ||
|
||
assert.isTrue(result[1].readyState > 0); | ||
assert.equal(Math.round(result[1].duration), 14); | ||
|
||
done(); | ||
}); | ||
} | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters