some tests:
I added a few tests to manipulation.js in the test suite:
- jQuery("#main").html(valueObj("<embed src='data/cow.jpg'></embed>"));
- ok((jQuery("#main").children().length > 0), "Make sure there is a child EMBED element." );
- equals( jQuery("#main").children()[0].nodeName.toUpperCase(), "EMBED", "And that an embed element was inserted." );
- reset();
- jQuery("#main").html(valueObj("<object data='data/cow.jpg'></object>"));
- equals( jQuery("#main").children().length, 1, "Make sure there is a child OBJECT element." );
- equals( jQuery("#main").children()[0].nodeName.toUpperCase(), "OBJECT", "And that an object element was inserted." );
- reset();
And then I added the following regex to jquery.js:
- rnoInnerhtml = /<script|<style/i,
and changed the .html() test so that it used rnoInnerhtml instead of rnocache:
- // See if we can take a shortcut and just use innerHTML
- } else if ( typeof value === "string" && !rnoInnerhtml.test( value ) &&
- (jQuery.support.leadingWhitespace || !rleadingWhitespace.test( value )) &&
- !wrapMap[ (rtagName.exec( value ) || ["", ""])[1].toLowerCase() ] ) {
I confirmed that all of the tests pass IE8. FF 3.6 and Chrome 4, and that values with <object and <embed do in fact use the innerHTML shortcut. Oddly when inserting an <embed into IE it returns two children instead of one. Not sure what's going on there.
Regarding <option, It's caught by the wrapMap in any case, because you're forbidden from using innerHTML to insert tags that require wrapping tags . However, given that <option has it's own issues with IE, it might make sense to also include that in the noInnerhtml for added transparency, and also to quit the if statement sooner:
- rnoInnerhtml = /<script|<style|<option/i,
What is the net effect of these changes? Html strings with <object and <embed (but with no options, scripts or styles) will now use innerHTML rather than .append. I hardly think this will result in any real-world performance improvements. It's possible that simplifying the regex a tiny bit would result in an infinitesimally small performance increase, but I wouldn't really count on that either.
However, what I like about it is that the code is a bit more transparent and logical. It uses a noInnerhtml test to determine whether to use innerHTML and a nocache test to determine whether to cache, and the contents of each regex are a bit more easily explained and documented to particular browser issues.