Skip to content

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
wants to merge 1 commit into from
Closed

Core: Return empty array instead of null for parseHTML("") #1998

wants to merge 1 commit into from

Conversation

Krinkle
Copy link
Member

@Krinkle Krinkle commented Jan 7, 2015

Fixes #1997

@@ -15,7 +15,7 @@ define([
// defaults to document
// keepScripts (optional): If true, will include scripts passed in the html string
jQuery.parseHTML = function( data, context, keepScripts ) {
if ( !data || typeof data !== "string" ) {
if ( typeof data !== "string" ) {
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'd even support a followup to drop this if entirely. If non-string input really causes a problem, it should be visible as an exception.

Copy link
Member

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({}).

Copy link
Member

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.

Copy link
Member

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 our jQuery.parseJSON(null) fiasco.

Copy link
Member

Choose a reason for hiding this comment

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

I don't think we should ever be returning null, despite the unit test.

This is the intent of my suggestion. Just dropping the if and 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 at

if ( elem || elem === 0 ) {
(e.g., check for elem != null instead of elem || elem === 0).

Copy link
Member

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 true are inconsistent. I'd rather they return empty array.

Copy link
Member

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.

Copy link
Member

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?

Copy link
Member

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 buildFragment suggestion and casting (some) nullish values to string in parseHTML.

@gibson042
Copy link
Member

Are you willing to update or re-PR for the more comprehensive changes, @Krinkle?

@Krinkle Krinkle changed the title core: Return empty array instead of null for parseHTML("") Core: Return empty array instead of null for parseHTML("") Jan 13, 2015
@Krinkle
Copy link
Member Author

Krinkle commented Jan 14, 2015

@gibson042 Done.

@timmywil timmywil closed this in 4116914 Jan 19, 2015
timmywil pushed a commit that referenced this pull request Jan 19, 2015
@Krinkle Krinkle deleted the issue/1997 branch January 20, 2015 00:33
@markelog markelog mentioned this pull request Nov 16, 2015
@markelog markelog mentioned this pull request Dec 22, 2015
@lock lock bot locked as resolved and limited conversation to collaborators Jan 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

$.parseHTML( "" ) should not return null
4 participants