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

fix(dropdown): support closest for HTMLDocument (#3783) #3786

Merged
merged 1 commit into from
Jul 8, 2020

Conversation

Istanful
Copy link
Contributor

Support closest for HTMLDocument

When autoclosing a dropdown by clicking the scrollbar, an error would be
thrown in Edge 44.18362.449.0. This was due to the HTMLDocument not
supporting Element.prototype.closest.

By emulating Element.prototype.closest for this specific node, this
commit circumvents the issue.

@Istanful Istanful force-pushed the fix/3783/autoclose-in-edge branch 2 times, most recently from ed5fd75 to 6371956 Compare June 25, 2020 12:49
@Istanful Istanful marked this pull request as draft June 25, 2020 12:57
@Istanful Istanful force-pushed the fix/3783/autoclose-in-edge branch 3 times, most recently from e456fa7 to f9d23f6 Compare June 25, 2020 13:36
@Istanful Istanful marked this pull request as ready for review June 25, 2020 13:54
Copy link
Member

@benouat benouat left a comment

Choose a reason for hiding this comment

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

Nice to have added UT for that! This is great.
Nonetheless have a look at my comment below, and tell me what you think ?

if (!selector) {
return null;
}

/*
Copy link
Member

Choose a reason for hiding this comment

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

I think we should do thing a little bit differently here because we can't just access document like that.

Any angular app could rendered server-side, where basically document won't be defined. So in order to properly use it, we usually have to get it by injection so that DI can provide a special one in environment where there is no document. (Have a look at how we do it)

But here, as we are in an utility function, it would mean we need to pass it as an argument, to have access to DI from the caller standpoint.

I found this a bit heavy, for such a small use case. (It apparently only fails in EdgeHTML)

What about we simply

try {
  return element.closest(selector)
} catch(err) { 
  return null;
}

WDYT ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice catch. It slipped my mind. 😛

I'm a bit hesitant to use try-catch since any error would be caught. How about using a more exact check?

if (typeof element.closest === 'undefined') {
  return null;
}

Beware though; in modern browsers document.documentElement.closest('html') would return document. By returning null always, we get a differing behaviour in Edge. How would you go about that problem?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ping @benouat. Would you be ok with that behaviour and check? :)

Copy link
Member

Choose a reason for hiding this comment

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

Yes, go ahead with that!

Don't forget to comment out things (this is needed for old edgeHTML generation) because I am pretty sure in 3 months from now we'll have forgotten why we do have this test!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@benouat Cool. I've changed the commit and code according to discussion. :)

@benouat
Copy link
Member

benouat commented Jun 29, 2020

Could you please also rename the commit message when you'll push again with something starting with fix:
It's not build: related.

@Istanful
Copy link
Contributor Author

@benouat Sure! I'll rename the commit. I wasn't sure what category it would fit in but figured :build from the description. :)

build: ... → any change for the utility scripts, configurations, dependencies, etc.

@Istanful Istanful changed the title build: support closest for HTMLDocument (#3783) fix(dropdown): support closest for HTMLDocument (#3783) Jul 7, 2020
@Istanful Istanful force-pushed the fix/3783/autoclose-in-edge branch from f9d23f6 to 759ef73 Compare July 7, 2020 09:52
When autoclosing a dropdown by clicking the scrollbar, an error would be
thrown in Edge 44.18362.449.0. This was due to the HTMLDocument not
supporting `Element.prototype.closest`.

By emulating `Element.prototype.closest` for this specific node, this
commit circumvents the issue.

Fixes ng-bootstrap#3783.
@Istanful Istanful force-pushed the fix/3783/autoclose-in-edge branch from 759ef73 to 4b72fa1 Compare July 7, 2020 10:03
@Istanful Istanful requested a review from benouat July 7, 2020 10:19
Copy link
Member

@benouat benouat left a comment

Choose a reason for hiding this comment

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

LGTM!

@benouat benouat merged commit 3df904d into ng-bootstrap:master Jul 8, 2020
@Istanful Istanful deleted the fix/3783/autoclose-in-edge branch July 8, 2020 11:11
maxokorokov pushed a commit that referenced this pull request Jul 8, 2020
When autoclosing a dropdown by clicking the scrollbar, an error would be
thrown in Edge 44.18362.449.0. This was due to the HTMLDocument not
supporting `Element.prototype.closest`.

By emulating `Element.prototype.closest` for this specific node, this
commit circumvents the issue.

Fixes #3783.
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.

None yet

2 participants