Source: lib/util/dom_utils.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.util.Dom');
  7. goog.require('goog.asserts');
  8. // TODO: revisit this when Closure Compiler supports partially-exported classes.
  9. /** @export */
  10. shaka.util.Dom = class {
  11. /**
  12. * Creates an element, and cast the type from Element to HTMLElement.
  13. *
  14. * @param {string} tagName
  15. * @return {!HTMLElement}
  16. */
  17. static createHTMLElement(tagName) {
  18. const element =
  19. /** @type {!HTMLElement} */ (document.createElement(tagName));
  20. return element;
  21. }
  22. /**
  23. * Create a "button" element with the correct type.
  24. *
  25. * The compiler is very picky about the use of the "disabled" property on
  26. * HTMLElement, since it is only defined on certain subclasses of that. This
  27. * method merely creates a button and casts it to the correct type.
  28. *
  29. * @return {!HTMLButtonElement}
  30. */
  31. static createButton() {
  32. const button = document.createElement('button');
  33. button.setAttribute('type', 'button');
  34. return /** @type {!HTMLButtonElement} */ (button);
  35. }
  36. /**
  37. * @param {string} url
  38. * @param {string=} mimeType
  39. * @return {!HTMLSourceElement}
  40. */
  41. static createSourceElement(url, mimeType = '') {
  42. const source =
  43. /** @type {HTMLSourceElement} */ (document.createElement('source'));
  44. source.src = url;
  45. source.type = mimeType;
  46. return source;
  47. }
  48. /**
  49. * Cast a Node/Element to an HTMLElement
  50. *
  51. * @param {!Node|!Element} original
  52. * @return {!HTMLElement}
  53. */
  54. static asHTMLElement(original) {
  55. return /** @type {!HTMLElement}*/ (original);
  56. }
  57. /**
  58. * Cast a Node/Element to an HTMLCanvasElement
  59. *
  60. * @param {!Node|!Element} original
  61. * @return {!HTMLCanvasElement}
  62. */
  63. static asHTMLCanvasElement(original) {
  64. return /** @type {!HTMLCanvasElement}*/ (original);
  65. }
  66. /**
  67. * Cast a Node/Element to an HTMLMediaElement
  68. *
  69. * @param {!Node|!Element} original
  70. * @return {!HTMLMediaElement}
  71. */
  72. static asHTMLMediaElement(original) {
  73. return /** @type {!HTMLMediaElement}*/ (original);
  74. }
  75. /**
  76. * Returns the element with a given class name.
  77. * Assumes the class name to be unique for a given parent.
  78. *
  79. * @param {string} className
  80. * @param {!HTMLElement} parent
  81. * @return {!HTMLElement}
  82. */
  83. static getElementByClassName(className, parent) {
  84. const elements = parent.getElementsByClassName(className);
  85. goog.asserts.assert(elements.length == 1,
  86. 'Should only be one element with class name ' + className);
  87. return shaka.util.Dom.asHTMLElement(elements[0]);
  88. }
  89. /**
  90. * Remove all of the child nodes of an element.
  91. * @param {!Element} element
  92. * @export
  93. */
  94. static removeAllChildren(element) {
  95. while (element.firstChild) {
  96. element.removeChild(element.firstChild);
  97. }
  98. }
  99. };