Skip to content

Commit

Permalink
fix: fallback $selector to $wrapperEl if no children exist (#5173)
Browse files Browse the repository at this point in the history
  • Loading branch information
istaiti authored Nov 10, 2021
1 parent 815e837 commit d8b4ca1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
25 changes: 25 additions & 0 deletions cypress/integration/modules/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -468,4 +468,29 @@ context('Core', () => {
cy.get(`.swiper-scrollbar`).should('exist');
cy.get(`.swiper-pagination`).should('exist');
});

it('allow init empty Swiper', () => {
cy.window().then(async (_window) => {
_window.document.body.innerHTML = `
<div class="swiper-container">
<div class="swiper-wrapper"></div>
<div class="swiper-slide"></div>
</div>
`;
_window.swiperRef = new _window.Swiper('.swiper-container', {
loop: true,
loopedSlides: 0,
loopPreventsSlide: false,
on: {
init: () => {
cy.get('.swiper-slide').then((slide) => {
_window.swiperRef.addSlide(0, slide);
});
},
},
});
return _window.swiperRef;
});
cy.getActiveSlide();
});
});
3 changes: 2 additions & 1 deletion src/core/loop/loopCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export default function loopCreate() {
const document = getDocument();
const { params, $wrapperEl } = swiper;
// Remove duplicated slides
const $selector = $($wrapperEl.children()[0].parentNode);
const $selector =
$wrapperEl.children().length > 0 ? $($wrapperEl.children()[0].parentNode) : $wrapperEl;
$selector.children(`.${params.slideClass}.${params.slideDuplicateClass}`).remove();

let slides = $selector.children(`.${params.slideClass}`);
Expand Down

0 comments on commit d8b4ca1

Please sign in to comment.