Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

jQuery $.parseHTML() does not seem to be identifying the style attribute when the attribute's value represents invalid CSS.

Here is sample code:

jQuery(document).ready(function()
{
var TestStringCSSValid = '<div style="width: 80px;"><p>hello world</p></div>';
var TestStringCSSInvalid = '<div style="invalidcss"><p>hello world</p></div>';

/*var ElementNodeList = $.parseHTML(TestStringCSSValid);*/
var ElementNodeList = $.parseHTML(TestStringCSSInvalid);

$.each(ElementNodeList, function(i, element)
{
    ItemizeElements(element);
});

function ItemizeElements(element)
{
    alert(element.nodeName.toLowerCase());

    if (element.attributes != null)
    {
        $.each(element.attributes, function(i, attribute)
        {
            alert(element.nodeName.toLowerCase() + "  " + attribute.name.toLowerCase());
        });
    }
    var ChildNodeList = element.childNodes;

    $.each(ChildNodeList, function(i, child)
    {
        ItemizeElements(child);
    });
}
});

When the first test string is parsed (commented out code) all five major browsers (IE9 and most recent versions of FF, Chrome, Opera, and Safari for PC) produce the expected series of alerts:

-div
-div style
-p
-#text

When the second string is parsed (code as shown) all but IE continue to show the same series. IE9 shows only the div, p, and #text. It is not detecting that the div has the style attribute. The code shown here is a simplification of more extensive code with deeper hierarchies, where the problem also appears.

Is this a known bug in IE, or in jQuery? What can be done about it?

Thanks for your help.

share|improve this question
parseHTML has caused me all sorts of nightmares... – Dhaivat Pandya Feb 14 at 1:00
I would suspect that this isn't a bug but rather that there is no spec that specifies this behavior. What is your use case? Why do you want an invalid style attribute? – James Montagne Feb 14 at 1:04
It is that I want to make sure there is not an invalid style attribute before loading into the dom. – Jack Herr Feb 14 at 2:13
Unless there is an elegant way around this problem, I am going to live with the parses that are produced, knowing that loading them into the dom in that browser should not cause that session any problems. – Jack Herr Feb 14 at 21:31

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.