Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

isElementDisplayed.js 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. function isElementDisplayed(element) {
  4. function nodeIsElement(node) {
  5. if (!node) {
  6. return false;
  7. }
  8. switch (node.nodeType) {
  9. case Node.ELEMENT_NODE:
  10. case Node.DOCUMENT_NODE:
  11. case Node.DOCUMENT_FRAGMENT_NODE:
  12. return true;
  13. default:
  14. return false;
  15. }
  16. }
  17. function parentElementForElement(element) {
  18. if (!element) {
  19. return null;
  20. }
  21. return enclosingNodeOrSelfMatchingPredicate(element.parentNode, nodeIsElement);
  22. }
  23. function enclosingNodeOrSelfMatchingPredicate(targetNode, predicate) {
  24. for (let node = targetNode; node && node !== targetNode.ownerDocument; node = node.parentNode)
  25. if (predicate(node)) {
  26. return node;
  27. }
  28. return null;
  29. }
  30. function enclosingElementOrSelfMatchingPredicate(targetElement, predicate) {
  31. for (let element = targetElement; element && element !== targetElement.ownerDocument; element = parentElementForElement(element))
  32. if (predicate(element)) {
  33. return element;
  34. }
  35. return null;
  36. }
  37. function cascadedStylePropertyForElement(element, property) {
  38. if (!element || !property) {
  39. return null;
  40. }
  41. if (element instanceof DocumentFragment) {
  42. element = element.host;
  43. }
  44. let computedStyle = window.getComputedStyle(element);
  45. let computedStyleProperty = computedStyle.getPropertyValue(property);
  46. if (computedStyleProperty && computedStyleProperty !== 'inherit') {
  47. return computedStyleProperty;
  48. }
  49. let parentElement = parentElementForElement(element);
  50. return cascadedStylePropertyForElement(parentElement, property);
  51. }
  52. function elementSubtreeHasNonZeroDimensions(element) {
  53. let boundingBox = element.getBoundingClientRect();
  54. if (boundingBox.width > 0 && boundingBox.height > 0) {
  55. return true;
  56. }
  57. if (element.tagName.toUpperCase() === 'PATH' && boundingBox.width + boundingBox.height > 0) {
  58. let strokeWidth = cascadedStylePropertyForElement(element, 'stroke-width');
  59. return !!strokeWidth && (parseInt(strokeWidth, 10) > 0);
  60. }
  61. let cascadedOverflow = cascadedStylePropertyForElement(element, 'overflow');
  62. if (cascadedOverflow === 'hidden') {
  63. return false;
  64. }
  65. return Array.from(element.childNodes).some((childNode) => {
  66. if (childNode.nodeType === Node.TEXT_NODE) {
  67. return true;
  68. }
  69. if (nodeIsElement(childNode)) {
  70. return elementSubtreeHasNonZeroDimensions(childNode);
  71. }
  72. return false;
  73. });
  74. }
  75. function elementOverflowsContainer(element) {
  76. let cascadedOverflow = cascadedStylePropertyForElement(element, 'overflow');
  77. if (cascadedOverflow !== 'hidden') {
  78. return false;
  79. }
  80. return true;
  81. }
  82. function isElementSubtreeHiddenByOverflow(element) {
  83. if (!element) {
  84. return false;
  85. }
  86. if (!elementOverflowsContainer(element)) {
  87. return false;
  88. }
  89. if (!element.childNodes.length) {
  90. return false;
  91. }
  92. return Array.from(element.childNodes).every((childNode) => {
  93. if (childNode.nodeType === Node.TEXT_NODE) {
  94. return false;
  95. }
  96. if (!nodeIsElement(childNode)) {
  97. return true;
  98. }
  99. if (!elementSubtreeHasNonZeroDimensions(childNode)) {
  100. return true;
  101. }
  102. return isElementSubtreeHiddenByOverflow(childNode);
  103. });
  104. }
  105. function isElementInsideShadowRoot(element) {
  106. if (!element) {
  107. return false;
  108. }
  109. if (element.parentNode && element.parentNode.host) {
  110. return true;
  111. }
  112. return isElementInsideShadowRoot(element.parentNode);
  113. }
  114. if (!isElementInsideShadowRoot(element) && !document.contains(element)) {
  115. return false;
  116. }
  117. switch (element.tagName.toUpperCase()) {
  118. case 'BODY':
  119. return true;
  120. case 'SCRIPT':
  121. case 'NOSCRIPT':
  122. return false;
  123. case 'OPTGROUP':
  124. case 'OPTION': {
  125. let enclosingSelectElement = enclosingNodeOrSelfMatchingPredicate(element, (e) => e.tagName.toUpperCase() === 'SELECT');
  126. return isElementDisplayed(enclosingSelectElement);
  127. }
  128. case 'INPUT':
  129. if (element.type === 'hidden') {
  130. return false;
  131. }
  132. break;
  133. default:
  134. break;
  135. }
  136. if (cascadedStylePropertyForElement(element, 'visibility') !== 'visible') {
  137. return false;
  138. }
  139. let hasAncestorWithZeroOpacity = !!enclosingElementOrSelfMatchingPredicate(element, (e) => {
  140. return Number(cascadedStylePropertyForElement(e, 'opacity')) === 0;
  141. });
  142. let hasAncestorWithDisplayNone = !!enclosingElementOrSelfMatchingPredicate(element, (e) => {
  143. return cascadedStylePropertyForElement(e, 'display') === 'none';
  144. });
  145. if (hasAncestorWithZeroOpacity || hasAncestorWithDisplayNone) {
  146. return false;
  147. }
  148. if (!elementSubtreeHasNonZeroDimensions(element)) {
  149. return false;
  150. }
  151. if (isElementSubtreeHiddenByOverflow(element)) {
  152. return false;
  153. }
  154. return true;
  155. }
  156. exports.default = isElementDisplayed;