UNPKG

devextreme

Version:

HTML5 JavaScript Component Suite for Responsive Web Development

105 lines (97 loc) 4.3 kB
/** * DevExtreme (cjs/__internal/core/utils/m_shadow_dom.js) * Version: 24.2.6 * Build date: Mon Mar 17 2025 * * Copyright (c) 2012 - 2025 Developer Express Inc. ALL RIGHTS RESERVED * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/ */ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.addShadowDomStyles = addShadowDomStyles; exports.getShadowElementsFromPoint = getShadowElementsFromPoint; const DX_RULE_PREFIX = "dx-"; let ownerDocumentStyleSheet = null; function createConstructedStyleSheet(rootNode) { try { return new CSSStyleSheet } catch (err) { const styleElement = rootNode.ownerDocument.createElement("style"); rootNode.appendChild(styleElement); return styleElement.sheet } } function processRules(targetStyleSheet, styleSheets, needApplyAllStyles) { for (let i = 0; i < styleSheets.length; i++) { const sheet = styleSheets[i]; try { for (let j = 0; j < sheet.cssRules.length; j++) { insertRule(targetStyleSheet, sheet.cssRules[j], needApplyAllStyles) } } catch (err) {} } } function insertRule(targetStyleSheet, rule, needApplyAllStyles) { var _rule$selectorText, _rule$cssRules, _rule$name, _rule$style; const isDxRule = needApplyAllStyles || (null === (_rule$selectorText = rule.selectorText) || void 0 === _rule$selectorText ? void 0 : _rule$selectorText.includes("dx-")) || (null === (_rule$cssRules = rule.cssRules) || void 0 === _rule$cssRules || null === (_rule$cssRules = _rule$cssRules[0]) || void 0 === _rule$cssRules || null === (_rule$cssRules = _rule$cssRules.selectorText) || void 0 === _rule$cssRules ? void 0 : _rule$cssRules.includes("dx-")) || (null === (_rule$name = rule.name) || void 0 === _rule$name ? void 0 : _rule$name.startsWith("dx-")) || "DXIcons" === (null === (_rule$style = rule.style) || void 0 === _rule$style ? void 0 : _rule$style.fontFamily); if (isDxRule) { targetStyleSheet.insertRule(rule.cssText, targetStyleSheet.cssRules.length) } } function addShadowDomStyles($element) { var _el$getRootNode; const el = $element.get(0); const root = null === (_el$getRootNode = el.getRootNode) || void 0 === _el$getRootNode ? void 0 : _el$getRootNode.call(el); if (!(null !== root && void 0 !== root && root.host)) { return } if (!ownerDocumentStyleSheet) { ownerDocumentStyleSheet = createConstructedStyleSheet(root); processRules(ownerDocumentStyleSheet, el.ownerDocument.styleSheets, false) } const currentShadowDomStyleSheet = createConstructedStyleSheet(root); processRules(currentShadowDomStyleSheet, root.styleSheets, true); root.adoptedStyleSheets = [ownerDocumentStyleSheet, currentShadowDomStyleSheet] } function isPositionInElementRectangle(element, x, y) { var _element$getBoundingC; const rect = null === (_element$getBoundingC = element.getBoundingClientRect) || void 0 === _element$getBoundingC ? void 0 : _element$getBoundingC.call(element); return rect && x >= rect.left && x < rect.right && y >= rect.top && y < rect.bottom } function createQueue() { let shiftIndex = 0; const items = []; return { push(item) { items.push(item); return this }, shift() { shiftIndex++; return items[shiftIndex - 1] }, get length() { return items.length - shiftIndex }, get items() { return items } } } function getShadowElementsFromPoint(x, y, root) { const elementQueue = createQueue().push(root); while (elementQueue.length) { const el = elementQueue.shift(); for (let i = 0; i < el.childNodes.length; i++) { const childNode = el.childNodes[i]; if (childNode.nodeType === Node.ELEMENT_NODE && isPositionInElementRectangle(childNode, x, y) && "none" !== getComputedStyle(childNode).pointerEvents) { elementQueue.push(childNode) } } } const result = elementQueue.items.reverse(); result.pop(); return result }