dom.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. var domUtils = (function (exports) {
  2. 'use strict';
  3. const range = document.createRange();
  4. function parseHTML(html) {
  5. return range.createContextualFragment(html);
  6. }
  7. // equivalent to jQuery's :visble
  8. function isVisible(el) {
  9. return !!(el.offsetWidth || el.offsetHeight || el.getClientRects().length);
  10. }
  11. function hideElement(el) {
  12. if (el.style.display === 'none') {
  13. return;
  14. }
  15. // back up the existing display setting in data-style-display
  16. if (el.style.display) {
  17. el.dataset.styleDisplay = el.style.display;
  18. }
  19. el.style.display = 'none';
  20. }
  21. function showElement(el) {
  22. if (el.style.display !== 'none') {
  23. return;
  24. }
  25. if (el.dataset.styleDisplay) {
  26. // restore backed-up dispay property
  27. el.style.display = el.dataset.styleDisplay;
  28. delete el.dataset.styleDisplay;
  29. } else {
  30. el.style.display = '';
  31. }
  32. }
  33. function emptyChildNodes(el) {
  34. if (el.firstChild) {
  35. el.removeChild(el.firstChild);
  36. emptyChildNodes(el);
  37. }
  38. }
  39. function replaceChildNodes(el, newChildNodes) {
  40. emptyChildNodes(el);
  41. if (newChildNodes instanceof DocumentFragment) {
  42. el.appendChild(newChildNodes);
  43. } else if (typeof newChildNodes === 'string') {
  44. el.appendChild(parseHTML(newChildNodes));
  45. } else if (typeof newChildNodes.forEach === 'function') {
  46. newChildNodes.forEach((node) => {
  47. el.appendChild(node);
  48. });
  49. }
  50. }
  51. exports.emptyChildNodes = emptyChildNodes;
  52. exports.hideElement = hideElement;
  53. exports.isVisible = isVisible;
  54. exports.parseHTML = parseHTML;
  55. exports.replaceChildNodes = replaceChildNodes;
  56. exports.showElement = showElement;
  57. Object.defineProperty(exports, '__esModule', { value: true });
  58. return exports;
  59. }({}));