forked from shaka-project/shaka-player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aria.js
72 lines (64 loc) · 2.13 KB
/
aria.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
goog.provide('shaka.polyfill.Aria');
goog.require('shaka.log');
goog.require('shaka.polyfill');
/**
* @summary A polyfill to add support for the ARIAMixin interface mixin, for
* browsers that do not implement it (e.g. Firefox).
* Note that IE also does not support ARIAMixin, but this polyfill does not work
* for that platform, as it relies on getters and setters.
* @see https://w3c.github.io/aria/#ARIAMixin
* @see https://developer.mozilla.org/en-US/docs/Web/API/Element
*/
shaka.polyfill.Aria = class {
/** Install the polyfill if needed. */
static install() {
// eslint-disable-next-line no-restricted-syntax
if (Object.getOwnPropertyDescriptor(Element.prototype, 'ariaHidden')) {
shaka.log.info('Using native ARIAMixin interface.');
return;
}
shaka.log.info('ARIAMixin interface not detected. Installing polyfill.');
// Define a list of all of the ARIAMixin properties that we have externs
// for.
const attributes = [
'ariaHidden',
'ariaLabel',
'ariaPressed',
'ariaSelected',
];
// Add each attribute, one by one.
for (const attribute of attributes) {
shaka.polyfill.Aria.addARIAMixinAttribute_(attribute);
}
}
/**
* Adds an attribute with the given name.
* @param {string} name The name of the attribute, in camelCase.
* @private
*/
static addARIAMixinAttribute_(name) {
const snakeCaseName = name.toLowerCase().replace('aria', 'aria-');
/* eslint-disable no-restricted-syntax */
Object.defineProperty(Element.prototype, name, {
get() {
const element = /** @type {!Element} */ (this);
return element.getAttribute(snakeCaseName);
},
set(value) {
const element = /** @type {!Element} */ (this);
if (value == null || value == undefined) {
element.removeAttribute(snakeCaseName);
} else {
element.setAttribute(snakeCaseName, value);
}
},
});
/* eslint-enable no-restricted-syntax */
}
};
shaka.polyfill.register(shaka.polyfill.Aria.install);