View Full Version : Is a CSS rule a text node?

11-23-2003, 05:29 PM
For example, this works in mozilla, but should it?

var styleNode = document.createElementNS("http://www.w3.org/1999/xhtml", "html:style");
styleNode.appendChild(document.createTextNode("body { background-color:pink; }"));

I thought CSS rules were not text nodes in a similar way that comments and entities aren't text nodes. At any rate, this doesn't work, as I didn't think it would.

styleNode.innerText = "body { background-color:pink; }";

So what's the difference?

11-23-2003, 07:58 PM
The contents of the script and style elements should be represented as text nodes in the DOM, because that is exactly what they are in the XML document. That's not to say a style rule should be represented as a text node, however, because a style rule is not an XML construct, but a CSS construct.

Moz doesn't support innerText at all, and I don't think iew allows that easy access to the contents of the style element. (I know I had problems with it when I wrote my ViewStyles bookmarklet, which only displays external styles because of these problems I were having.)

Also, I think only the first child text node gets parsed as JavaScript/CSS in Moz, but I'm not sure as of that.
[:edit:]Seems I recalled wrong, at least when it comes to stylesheets

11-24-2003, 10:05 AM
I tried it with a whole bunch of rules as well, in a for loop adding each rule as a new text node, and it all works fine.

I'm asking because it doesn't work in O7, and I'm not sure how to report it as a bug.

11-24-2003, 12:34 PM
According to the W3C, there should technically be no difference in the handling of XML or HTML/SGML between the different ways to input it. Thus, anything that you can specify in the DOM (some things are read only, though, and can't be specified by the DOM) should be handled exactly the same as if it was part of the original document source sent from the server. However, there is nothing that indicates to the parser that a style or script tag should be parsed through a parser for their respective content languages, and thus some implementations may behave differently when you use the DOM and when you include it in the original document source (they use native host parsing for the latter, but only DOM/XML/HTML for the former) - iew's not sending some form fields that are created by the DOM is a good example, though that is a bug, not a difference in features as for example the fact that op7 can't load external scripts dynamically is (both Opera and Apple tell us that they are working on this, however. They just don't have dynamical reparsing for these types objects yet).

Hmm, about that bug filing, I can do it if you wish. I've just created a testcase: <http://liorean.web-graphics.com/bugreports/opera/parsedynamicallycreatedobjects.html>

Iew has the interessting notion that the DOM should have no property access to the other language parsed nodes...

11-24-2003, 01:22 PM
Thanks, but I'll do it - it's CSS that's specifically of interest, because if Opera supports XHTML, but not document.write or document.styleSheets, this is going to be the only way of getting script-generated CSS onto the page.

11-24-2003, 01:29 PM
Yah, but remember that Opera doesn't support the script element in XHTML mode yet, either...

(I wonder if they aren't supporting java&#0115;cript: urls though. I think I've tested those and they worked, but again I'm not sure of this.)

11-25-2003, 11:17 PM
If you are trying to get generated CSS into the page can you not use the methods of the stylesheet object:

For IE:
For Moz:
document.stylesheet[0].insertRule("h1 {color:red}", 0);

Or if you want to change the rule dynamically you can use:


Which is standard, but not as reliable (at least in IE) as:

document.styleSheets[i].rules[j].style.color = '12pt';

Oh.... rules is IE only for Moz you have to use cssRules instead

11-25-2003, 11:30 PM
Come on, this is brothercake and me you're talking to. We know that already. The question was more related to whether moz's handling is correct, because in that case op7 must be wrong, and since op7 doesn't support document.styleSheets at all, nor document.write in XML mode, this would be the only way to through scripting define style rules.

11-26-2003, 12:44 AM
Thanks for your suggestions though :thumbsup:

As a point of interest, I won't use document.styleSheets in IE because IE has perverted the syntax. I just use document.write, which is always going to be fine, because IE doesn't support XHTML in the first place.

As far as this goes - the question is essentially resolved - mozilla supports document.styleSheets, and the original example now works in Opera. So overall, there's a reliable solution for all.