From eec4594b41a948d98a7eb3a3c7ce54da8109bd04 Mon Sep 17 00:00:00 2001 From: Andrey Oganesyan Date: Thu, 10 Oct 2024 19:42:31 +0400 Subject: [PATCH] 7761: Fixed elementIsChildOf returning false for nested web components; added .mjs files to prettier job --- package.json | 2 +- src/modules/a11y/a11y.mjs | 2 +- src/shared/utils.mjs | 18 +++++++++++++----- src/vue/get-children.mjs | 10 +++------- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index 001c89d29..a7bfc087b 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "element": "npm run build && concurrently --kill-others \"vite ./playground/element\" \"npm run watch\" ", "react": "npm run build && concurrently --kill-others \"vite ./playground/react\" \"npm run watch\"", "vue": "npm run build && concurrently --kill-others \"vite ./playground/vue\" \"npm run watch\"", - "prettier": "prettier \"**/*.+(js|json|scss|css|less|ts|jsx)\"", + "prettier": "prettier \"**/*.+(js|json|scss|css|less|ts|jsx|mjs)\"", "format": "npm run prettier -- --write", "check-format": "npm run prettier -- --list-different", "lint": "eslint --ext .js,.jsx .", diff --git a/src/modules/a11y/a11y.mjs b/src/modules/a11y/a11y.mjs index f76177d83..384aa4a80 100644 --- a/src/modules/a11y/a11y.mjs +++ b/src/modules/a11y/a11y.mjs @@ -19,7 +19,7 @@ export default function A11y({ swiper, extendParams, on }) { itemRoleDescriptionMessage: null, slideRole: 'group', id: null, - scrollOnFocus: true + scrollOnFocus: true, }, }); diff --git a/src/shared/utils.mjs b/src/shared/utils.mjs index 0638f70e8..6e7a957d9 100644 --- a/src/shared/utils.mjs +++ b/src/shared/utils.mjs @@ -214,12 +214,20 @@ function elementChildren(element, selector = '') { return children.filter((el) => el.matches(selector)); } function elementIsChildOf(el, parent) { - const isChild = parent.contains(el); - if (!isChild && parent instanceof HTMLSlotElement) { - const children = [...parent.assignedElements()]; - return children.includes(el); + // Breadth-first search through all parent's children and assigned elements + const elementsQueue = [parent]; + while (elementsQueue.length > 0) { + const elementToCheck = elementsQueue.shift(); + if (el === elementToCheck) { + return true; + } + elementsQueue.push( + ...elementToCheck.children, + ...(elementToCheck.shadowRoot?.children ?? []), + ...(elementToCheck.assignedElements?.() ?? []), + ); } - return isChild; + return false; } function showWarning(text) { try { diff --git a/src/vue/get-children.mjs b/src/vue/get-children.mjs index 53f2fec17..a12b9d274 100644 --- a/src/vue/get-children.mjs +++ b/src/vue/get-children.mjs @@ -18,13 +18,9 @@ function getChildren(originalSlots = {}, slidesRef, oldSlidesRef) { if (isFragment && vnode.children) { getSlidesFromElements(vnode.children, slotName); } else if ( - ( - vnode.type && - (vnode.type.name === 'SwiperSlide' || vnode.type.name === 'AsyncComponentWrapper') - ) || - ( - vnode.componentOptions && (vnode.componentOptions.tag === 'SwiperSlide') - ) + (vnode.type && + (vnode.type.name === 'SwiperSlide' || vnode.type.name === 'AsyncComponentWrapper')) || + (vnode.componentOptions && vnode.componentOptions.tag === 'SwiperSlide') ) { slides.push(vnode); } else if (slots[slotName]) {