Bug Tracker

Opened 9 years ago

Closed 9 years ago

#7133 closed bug (invalid)

Adding an array to html() causes an error

Reported by: Motty Owned by:
Priority: low Milestone: 1.4.3
Component: manipulation Version: 1.4.2
Keywords: .html() array Cc:
Blocked by: Blocking:

Description

I know this is not the intended purpose of .html(), but I think it needs to at least not crash when the function is presented with an array. Using .text() will fail gracefully, but not .html().

I posted a demo of it here (http://jsfiddle.net/fqsSa/)

 var testing = ['hello','world'];
 $('#test').html(testing);

Change History (2)

comment:1 Changed 9 years ago by addyosmani

Component: coremanipulation
Keywords: .html() array added
need: ReviewPatch
Priority: undecidedlow

Although I haven't had any browsers crash in testing the submitted bug , I did get the following FireBug console output when running the test case in FF:

uncaught exception: [Exception... "Could not convert JavaScript argument arg 0 [nsIDOMDocumentFragment.appendChild]" nsresult: "0x80570009 (NS_ERROR_XPC_BAD_CONVERT_JS)" location: "JS frame :: http://code.jquery.com/jquery-1.4.2.js :: anonymous :: line 4500" data: no]

Using .html(testing.join(' ')) is a valid workaround (as you correctly mentioned) but we should probably be addressing this in the core (at least ensuring it fails gracefully in the same way that .text() does).

Looking between lines 4219 and 4225:

in this case we're falling back on this.empty().append( value );. Were we to consider adding an additional case similar to .text() where we fall back on

this.empty().append( (this[0] && this[0].ownerDocument
document).createTextNode( value ) );

or some variant, this may be preferable compared to an error output.

We probably also need to address what is the expected behavior when appending an array through .html() in this manner. I'm not a fan of .createTextNode() as the fallback,but it would be interesting to hear that others might think would be a more appropriate solution.

Flagging for further review and patch.

comment:2 Changed 9 years ago by snover

Resolution: invalid
Status: newclosed

Nowhere is it stated that passing an array to .html and .text is valid. The only reason it works in .text is because it checks to see if the passed content is not an object (typeof Array is object). So all you are really doing when you call .text and pass an array is calling the getter method—it doesn’t actually work.

Note: See TracTickets for help on using tickets.