Skip to content

Commit

Permalink
do this check only for the wrapper-slot case
Browse files Browse the repository at this point in the history
  • Loading branch information
nolimits4web committed Jan 2, 2025
1 parent eec4594 commit db7d7f4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module.exports = {
ecmaFeatures: {
jsx: true,
},
ecmaVersion: 2018,
ecmaVersion: 2021,
sourceType: 'module',
},

Expand Down
21 changes: 16 additions & 5 deletions src/shared/utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -213,21 +213,32 @@ function elementChildren(element, selector = '') {
}
return children.filter((el) => el.matches(selector));
}
function elementIsChildOf(el, parent) {
function elementIsChildOfSlot(el, slot) {
// Breadth-first search through all parent's children and assigned elements
const elementsQueue = [parent];
const elementsQueue = [slot];
while (elementsQueue.length > 0) {
const elementToCheck = elementsQueue.shift();
if (el === elementToCheck) {
return true;
}
elementsQueue.push(
...elementToCheck.children,
...(elementToCheck.shadowRoot?.children ?? []),
...(elementToCheck.assignedElements?.() ?? []),
...(elementToCheck.shadowRoot?.children || []),
...(elementToCheck.assignedElements?.() || []),
);
}
return false;
}
function elementIsChildOf(el, parent) {
let isChild = parent.contains(el);
if (!isChild && parent instanceof HTMLSlotElement) {
const children = [...parent.assignedElements()];
isChild = children.includes(el);
if (!isChild) {
isChild = elementIsChildOfSlot(el, parent);
}
}

return isChild;
}
function showWarning(text) {
try {
Expand Down

0 comments on commit db7d7f4

Please sign in to comment.