[webkit-dev] appendChild does not work for elements produced by DOMParser
avshabanov at gmail.com
Mon Nov 30 07:20:27 PST 2009
2009/11/30 Maciej Stachowiak <mjs at apple.com>:
> Probably because parsing with DOMParser parses as XML, thus you get a <p>
> element that's not an HTML element (it is in the null namespace instead of
> the HTML namespace). To get an HTML p element you need to either do this:
> var str = "<p xmlns='http://www.w3.org/1999/xhtml/' style='color:
> red;'>This is a test</p>";
> or just use innerHTML or createContextualFragment to directly insert HTML
> text into a document.
Thank you, importNode works fine for my snippet given above.
But style information is omitted no matter whether xmlns attribute
specified or not.
This is because I was wrong about using
doc.documentElement.firstChild, I suppose doc.documentElement shall be
var str = "<p xmlns='http://www.w3.org/1999/xhtml/' style='color:
red;'>This is a test</p>";
var parser = new DOMParser();
var doc = parser.parseFromString(str, "text/xml");
And the fun thing is how different Gecko and WebKit(with the only
difference in added call to document.importNode) handle the code given
Node inserted, text is visible but in black, e.g. style is still
omitted and Firebug indicates, that inserted element has no style
information, at the same time it indicates that style attribute exists
in the DOM tree.
Node inserted but in the strange way. Text content is not visible on
the screen. WebInspector indicates that new <p> node exists in the DOM
tree but without it's content and closing tag and with xmlns and style
I believe that this is because I'm playing with DOMParser in the wrong way.
Could you please clarify what is the error here?
More information about the webkit-dev