From db7d7f407d975733cf7383c0532b89ddd97f5066 Mon Sep 17 00:00:00 2001 From: Vladimir Kharlampidi Date: Thu, 2 Jan 2025 14:11:08 -0500 Subject: [PATCH] do this check only for the wrapper-slot case --- .eslintrc.cjs | 2 +- src/shared/utils.mjs | 21 ++++++++++++++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 2dceb4ec4..2bb5bef4d 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -36,7 +36,7 @@ module.exports = { ecmaFeatures: { jsx: true, }, - ecmaVersion: 2018, + ecmaVersion: 2021, sourceType: 'module', }, diff --git a/src/shared/utils.mjs b/src/shared/utils.mjs index 6e7a957d9..c69ec5750 100644 --- a/src/shared/utils.mjs +++ b/src/shared/utils.mjs @@ -213,9 +213,9 @@ 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) { @@ -223,11 +223,22 @@ function elementIsChildOf(el, parent) { } 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 {