HTMLUtilities.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* *
  2. *
  3. * (c) 2009-2020 Øystein Moseng
  4. *
  5. * Utility functions for accessibility module.
  6. *
  7. * License: www.highcharts.com/license
  8. *
  9. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  10. *
  11. * */
  12. import H from '../../Core/Globals.js';
  13. var doc = H.doc, win = H.win;
  14. import U from '../../Core/Utilities.js';
  15. var merge = U.merge;
  16. /* eslint-disable valid-jsdoc */
  17. /**
  18. * @private
  19. * @param {Highcharts.HTMLDOMElement} el
  20. * @param {string} className
  21. * @return {void}
  22. */
  23. function addClass(el, className) {
  24. if (el.classList) {
  25. el.classList.add(className);
  26. }
  27. else if (el.className.indexOf(className) < 0) {
  28. // Note: Dumb check for class name exists, should be fine for practical
  29. // use cases, but will return false positives if the element has a class
  30. // that contains the className.
  31. el.className += className;
  32. }
  33. }
  34. /**
  35. * @private
  36. * @param {string} str
  37. * @return {string}
  38. */
  39. function escapeStringForHTML(str) {
  40. return str
  41. .replace(/&/g, '&amp;')
  42. .replace(/</g, '&lt;')
  43. .replace(/>/g, '&gt;')
  44. .replace(/"/g, '&quot;')
  45. .replace(/'/g, '&#x27;')
  46. .replace(/\//g, '&#x2F;');
  47. }
  48. /**
  49. * Get an element by ID
  50. * @param {string} id
  51. * @private
  52. * @return {Highcharts.HTMLDOMElement|Highcharts.SVGDOMElement|null}
  53. */
  54. function getElement(id) {
  55. return doc.getElementById(id);
  56. }
  57. /**
  58. * Get a fake mouse event of a given type
  59. * @param {string} type
  60. * @private
  61. * @return {global.MouseEvent}
  62. */
  63. function getFakeMouseEvent(type) {
  64. if (typeof win.MouseEvent === 'function') {
  65. return new win.MouseEvent(type);
  66. }
  67. // No MouseEvent support, try using initMouseEvent
  68. if (doc.createEvent) {
  69. var evt = doc.createEvent('MouseEvent');
  70. if (evt.initMouseEvent) {
  71. evt.initMouseEvent(type, true, // Bubble
  72. true, // Cancel
  73. win, // View
  74. type === 'click' ? 1 : 0, // Detail
  75. // Coords
  76. 0, 0, 0, 0,
  77. // Pressed keys
  78. false, false, false, false, 0, // button
  79. null // related target
  80. );
  81. return evt;
  82. }
  83. }
  84. return { type: type };
  85. }
  86. /**
  87. * Remove an element from the DOM.
  88. * @private
  89. * @param {Highcharts.HTMLDOMElement|Highcharts.SVGDOMElement} [element]
  90. * @return {void}
  91. */
  92. function removeElement(element) {
  93. if (element && element.parentNode) {
  94. element.parentNode.removeChild(element);
  95. }
  96. }
  97. /**
  98. * Utility function. Reverses child nodes of a DOM element.
  99. * @private
  100. * @param {Highcharts.HTMLDOMElement|Highcharts.SVGDOMElement} node
  101. * @return {void}
  102. */
  103. function reverseChildNodes(node) {
  104. var i = node.childNodes.length;
  105. while (i--) {
  106. node.appendChild(node.childNodes[i]);
  107. }
  108. }
  109. /**
  110. * Set attributes on element. Set to null to remove attribute.
  111. * @private
  112. * @param {Highcharts.HTMLDOMElement|Highcharts.SVGDOMElement} el
  113. * @param {Highcharts.HTMLAttributes|Highcharts.SVGAttributes} attrs
  114. * @return {void}
  115. */
  116. function setElAttrs(el, attrs) {
  117. Object.keys(attrs).forEach(function (attr) {
  118. var val = attrs[attr];
  119. if (val === null) {
  120. el.removeAttribute(attr);
  121. }
  122. else {
  123. var cleanedVal = escapeStringForHTML('' + val);
  124. el.setAttribute(attr, cleanedVal);
  125. }
  126. });
  127. }
  128. /**
  129. * Used for aria-label attributes, painting on a canvas will fail if the
  130. * text contains tags.
  131. * @private
  132. * @param {string} str
  133. * @return {string}
  134. */
  135. function stripHTMLTagsFromString(str) {
  136. return typeof str === 'string' ?
  137. str.replace(/<\/?[^>]+(>|$)/g, '') : str;
  138. }
  139. /**
  140. * Utility function for hiding an element visually, but still keeping it
  141. * available to screen reader users.
  142. * @private
  143. * @param {Highcharts.HTMLDOMElement} element
  144. * @return {void}
  145. */
  146. function visuallyHideElement(element) {
  147. var hiddenStyle = {
  148. position: 'absolute',
  149. width: '1px',
  150. height: '1px',
  151. overflow: 'hidden',
  152. whiteSpace: 'nowrap',
  153. clip: 'rect(1px, 1px, 1px, 1px)',
  154. marginTop: '-3px',
  155. '-ms-filter': 'progid:DXImageTransform.Microsoft.Alpha(Opacity=1)',
  156. filter: 'alpha(opacity=1)',
  157. opacity: '0.01'
  158. };
  159. merge(true, element.style, hiddenStyle);
  160. }
  161. var HTMLUtilities = {
  162. addClass: addClass,
  163. escapeStringForHTML: escapeStringForHTML,
  164. getElement: getElement,
  165. getFakeMouseEvent: getFakeMouseEvent,
  166. removeElement: removeElement,
  167. reverseChildNodes: reverseChildNodes,
  168. setElAttrs: setElAttrs,
  169. stripHTMLTagsFromString: stripHTMLTagsFromString,
  170. visuallyHideElement: visuallyHideElement
  171. };
  172. export default HTMLUtilities;