Ticket #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: | |
| Blocking: | Blocked by: |
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
comment:1 Changed 3 years ago by addyosmani
- Keywords .html() array added
- need changed from Review to Patch
- Component changed from core to manipulation
- Priority changed from undecided to low
comment:2 Changed 3 years ago by snover
- Status changed from new to closed
- Resolution set to invalid
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.
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

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
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.