Skip to content

Commit

Permalink
fix(core): Fixed elementIsChildOf returning false for nested web comp…
Browse files Browse the repository at this point in the history
…onents (#7762)

* 7761: Fixed elementIsChildOf returning false for nested web components; added .mjs files to prettier job

* do this check only for the wrapper-slot case

fixes #7761

---------

Co-authored-by: Vladimir Kharlampidi <[email protected]>
  • Loading branch information
andreyoganesyan and nolimits4web authored Jan 2, 2025
1 parent e5ad9fd commit 8136607
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 12 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 .",
Expand Down
2 changes: 1 addition & 1 deletion src/modules/a11y/a11y.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default function A11y({ swiper, extendParams, on }) {
itemRoleDescriptionMessage: null,
slideRole: 'group',
id: null,
scrollOnFocus: true
scrollOnFocus: true,
},
});

Expand Down
23 changes: 21 additions & 2 deletions src/shared/utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,31 @@ function elementChildren(element, selector = '') {
}
return children.filter((el) => el.matches(selector));
}
function elementIsChildOfSlot(el, slot) {
// Breadth-first search through all parent's children and assigned elements
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?.() || []),
);
}
}
function elementIsChildOf(el, parent) {
const isChild = parent.contains(el);
let isChild = parent.contains(el);
if (!isChild && parent instanceof HTMLSlotElement) {
const children = [...parent.assignedElements()];
return children.includes(el);
isChild = children.includes(el);
if (!isChild) {
isChild = elementIsChildOfSlot(el, parent);
}
}

return isChild;
}
function showWarning(text) {
Expand Down
10 changes: 3 additions & 7 deletions src/vue/get-children.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand Down

0 comments on commit 8136607

Please sign in to comment.