html() stripping <style> in IE sometimes

html() stripping <style> in IE sometimes

In the following test case, a textarea contains an html snippet/template that we'll try to insert into a div directly above the textarea.


  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.1.js" ></script>
    <script type="text/javascript">
    $(document).ready(function(){
        alert($("#template").val());
        $("#test").html($("#template").val());
        alert($("#test").html())
    });
    </script>

    <div id="test"></div>
    <fieldset>
    <textarea id="template">
    <style type="text/css">
        .test { color: red; }
    </style>
    <div id="nav_controls" class="navControls">
        <div id="test_div" class="test">A test</div>
    </div>

    </textarea>
    </fieldset>
    </body>
    </html>






























In IE 8 (haven't tested other version of IE - works fine in FF3.6), the style will be stripped in line #4058 of 1.4.1, which is where it passes the snippet into innerHTML.

IF you put a single linebreak into the textarea before the open style tag, it works without a problem.  So you can see it's a bit delicate to reproduce, but we're definitely seeing it in the alpha release code of our jquery app which I'm trying to upgrade to 1.4.1.  Bug does not appear in 1.3.x.  I suggest strengthening the if statement on line 4047 to 4049 to try to trap this condition, but I don't have any suggestions as to how to do so.  Here's the if statement in question:

  1.         } else if ( typeof value === "string" && !/<script/i.test( value ) &&
                (jQuery.support.leadingWhitespace || !rleadingWhitespace.test( value )) &&
                !wrapMap[ (rtagName.exec( value ) || ["", ""])[1].toLowerCase() ] ) {


trac issue: http://dev.jquery.com/ticket/5977


Cheers,

Charles