Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prev() function fails when non-element nodes are present #30117

Merged
merged 8 commits into from
Mar 9, 2020

Conversation

MartijnCuppens
Copy link
Member

@MartijnCuppens MartijnCuppens commented Feb 1, 2020

The prev() function doesn't take nodes other than elements into account. Also we could simplify things a lot using the previousElementSibling property. This property isn't fully supported in IE, it only works for elements, but since the element variable is an element, we can safely use it here.

I've also added an additional test.

I don't think we had this issue in v4, since we relied on jQuery back then.

Ref. https://developer.mozilla.org/en-US/docs/Web/API/NonDocumentTypeChildNode/nextElementSibling


@MartijnCuppens MartijnCuppens requested a review from a team as a code owner February 1, 2020 14:11
@MartijnCuppens MartijnCuppens changed the title Master mc fix prev function prev() function fails when non-element nodes are present Feb 1, 2020
@MartijnCuppens
Copy link
Member Author

This probably fixes a bug in scrollspy so I moved this back to the v5 board. @XhmikosR, could you have a look at this? The test basically illustrates what the problem was.

@XhmikosR
Copy link
Member

XhmikosR commented Mar 7, 2020

@MartijnCuppens is there any issue about this or failing snippets? Trying to understand why we need to make this change.

@MartijnCuppens
Copy link
Member Author

@XhmikosR
Copy link
Member

XhmikosR commented Mar 7, 2020

Actually this patch seems broken for me... Using Firefox 73.0.1

image

The master branch seems fine here.

@MartijnCuppens
Copy link
Member Author

It seems to be broken after the last merge, gonna check what happend there

@MartijnCuppens
Copy link
Member Author

It seems to be broken after the last merge

Lol, that was a lie, I just didn't scroll to the bottom to test this.

Anyway, I finally get why prev() returns an array. It's just easier to loop over the array which contains zero or one items. This way we avoid the check if the element is found.

@XhmikosR, can you recheck?

@XhmikosR
Copy link
Member

XhmikosR commented Mar 9, 2020

It seems to work now, but maybe we should keep siblings like the original code?

@MartijnCuppens
Copy link
Member Author

It seems to work now, but maybe we should keep siblings like the original code?

Why? previousSibling also targets comments and text elements which we do not need? (Also not sure what happens when we apply matches() to it)

@XhmikosR
Copy link
Member

XhmikosR commented Mar 9, 2020

I meant just keep the variable and return siblings like before.

@XhmikosR
Copy link
Member

XhmikosR commented Mar 9, 2020

But it's probably moot and the minifier might already drop this. I'll double check later.

Copy link
Member

@Johann-S Johann-S left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work @MartijnCuppens 👍

@XhmikosR XhmikosR merged commit 7d8c7c4 into master Mar 9, 2020
@XhmikosR XhmikosR deleted the master-mc-fix-prev-function branch March 9, 2020 15:21
olsza pushed a commit to olsza/bootstrap that referenced this pull request Oct 3, 2020
The `prev()` function doesn't take nodes other than elements into account. Also we could simplify things a lot using the `previousElementSibling` property. This property isn't fully supported in IE, it only works for elements, but since the `element` variable is an element, we can safely use it here.

I've also added an additional test.

I don't think we had this issue in v4, since we relied on jQuery back then.

Ref. https://developer.mozilla.org/en-US/docs/Web/API/NonDocumentTypeChildNode/nextElementSibling
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants