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: fallback to typeof when toString is applied to incompatible object #16017

Merged
merged 1 commit into from Oct 10, 2023

Conversation

JLHwung
Copy link
Contributor

@JLHwung JLHwung commented Oct 5, 2023

Q                       A
Fixed Issues? Fixes #12609
Patch: Bug Fix? Yes
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

Firefox 31 will throw when toString is applied to an HTMLElement. Here we handle the error as suggested in #12609 (comment).

@JLHwung JLHwung added the PR: Bug Fix 🐛 A type of pull request used for our changelog categories label Oct 5, 2023
@babel-bot
Copy link
Collaborator

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/55610/

return Function.toString.call(fn).indexOf("[native code]") !== -1;
} catch (e) {
// Firefox 31 throws when "toString" is applied to an HTMLElement
return typeof fn === "function";
Copy link
Member

Choose a reason for hiding this comment

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

This is safe because on user-defined Function.toString never throws, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think so, Function.toString will throw a type error if the input is neither object nor function, unless there are more browser quirks we are unaware of.

According to this SO issue, Function.toString throws on HTMLElement because at that time they were not inherited from Object.

@nicolo-ribaudo nicolo-ribaudo merged commit d25f265 into babel:main Oct 10, 2023
46 of 47 checks passed
@nicolo-ribaudo nicolo-ribaudo deleted the fix-is-native-function-ff-31 branch October 10, 2023 03:03
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Jan 9, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 9, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: Bug Fix 🐛 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

_isNativeFunction(HTMLElement) throws an error in Firefox 31.
4 participants