-
Notifications
You must be signed in to change notification settings - Fork 20.5k
Core: Return empty array instead of null for parseHTML("") #1998
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd even support a followup to drop this
ifentirely. If non-string input really causes a problem, it should be visible as an exception.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't support that. parseHTML via createHTMLDocument does not throw an error with parseHTML(1) or parseHTML({}).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
However, maybe we could just return an empty array here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The docs say the first arg is a String and that it returns Array. I don't think we should ever be returning
null, despite the unit test. 😄 If the first arg is not a string it seems either all bets are off or we should turn it into a string for cases we feel are possible (numbers,null,undefined)? I would be fine with removing this entirely but have a feeling it would be like ourjQuery.parseJSON(null)fiasco.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the intent of my suggestion. Just dropping the
ifand letting values propagate through gives good (and mostly expected) array results (http://jsfiddle.net/evse952q/), so we need not check for any special input or construct any special output (and it's worth noting that our internal uses always provide string input as documented).Edit: And whatever weirdness exists in the output stems from
buildFragment, which could perhaps also use a similar treatment atjquery/src/manipulation.js
Line 203 in a3779bc
elem != nullinstead ofelem || elem === 0).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The results for numbers and
trueare inconsistent. I'd rather they return empty array.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dunno @timmywil , looking through the whole execution path (which I should have done originally) it seems like dropping the check only affects really strange inputs and not in a catastrophic way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, it's not terrible. i just think if we're going to change it, why not match native behavior?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Native behavior always casts to string except
null(which is inconsistent between browsers): http://jsfiddle.net/evse952q/1/Achieving that in jQuery would mean adopting my above
buildFragmentsuggestion and casting (some) nullish values to string inparseHTML.