Files
shaka-player/lib/util/dom_utils.js
T
Joey Parrish a65ef9983b Remove partial exports, broken in new compiler
The old compiler would let us export a static method on a class
without exporting the whole class and its constructor.  The new
compiler silently ignores `@export` for any methods of a non-exported
class.

This means that for the latest Closure Compiler, we must export any
class with exported static methods.  In some cases, these are
non-utility classes with constructors we'd rather not export, but the
constructor is implicitly exported by exporting the class itself.

This was initially caught by the integration tests.  The error wasn't
especially helpful, so I added a try/catch to loadShaka that makes the
error more apparent:
  TypeError: Cannot read property 'registerParserByMime' of undefined

To make sure we do not make this mistake again, I've added a check to
the extern generator, which was already able to detect these types of
classes.  I don't know a compiler-based way to do it, since the
compiler silently ignores the export annotations in these cases.

Issue #2528

Change-Id: I797c75a8098b0bb3cf837588569f878253dec2cf
2020-04-30 17:30:36 +00:00

93 lines
2.2 KiB
JavaScript

/** @license
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
goog.provide('shaka.util.Dom');
goog.require('goog.asserts');
// TODO: revisit this when Closure Compiler supports partially-exported classes.
/** @export */
shaka.util.Dom = class {
/**
* Creates an element, and cast the type from Element to HTMLElement.
*
* @param {string} tagName
* @return {!HTMLElement}
*/
static createHTMLElement(tagName) {
const element =
/** @type {!HTMLElement} */ (document.createElement(tagName));
return element;
}
/**
* Create a "button" element with the correct type.
*
* The compiler is very picky about the use of the "disabled" property on
* HTMLElement, since it is only defined on certain subclasses of that. This
* method merely creates a button and casts it to the correct type.
*
* @return {!HTMLButtonElement}
*/
static createButton() {
return /** @type {!HTMLButtonElement} */(document.createElement('button'));
}
/**
* Cast a Node/Element to an HTMLElement
*
* @param {!Node|!Element} original
* @return {!HTMLElement}
*/
static asHTMLElement(original) {
return /** @type {!HTMLElement}*/ (original);
}
/**
* Cast a Node/Element to an HTMLMediaElement
*
* @param {!Node|!Element} original
* @return {!HTMLMediaElement}
*/
static asHTMLMediaElement(original) {
return /** @type {!HTMLMediaElement}*/ (original);
}
/**
* Returns the element with a given class name.
* Assumes the class name to be unique for a given parent.
*
* @param {string} className
* @param {!HTMLElement} parent
* @return {!HTMLElement}
*/
static getElementByClassName(className, parent) {
const elements = parent.getElementsByClassName(className);
goog.asserts.assert(elements.length == 1,
'Should only be one element with class name ' + className);
return shaka.util.Dom.asHTMLElement(elements[0]);
}
/**
* Remove all of the child nodes of an element.
* @param {!Element} element
* @export
*/
static removeAllChildren(element) {
while (element.firstChild) {
element.removeChild(element.firstChild);
}
}
};