Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Is a CSS rule a text node?

    For example, this works in mozilla, but should it?
    Code:
    var styleNode = document.createElementNS("http://www.w3.org/1999/xhtml", "html:style");
    document.getElementsByTagName("head").item(0).appendChild(styleNode);
    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.
    Code:
    styleNode.innerText = "body { background-color:pink; }";
    So what's the difference?
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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
    Last edited by liorean; 11-25-2003 at 10:32 PM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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/bugr...edobjects.html>

    Iew has the interessting notion that the DOM should have no property access to the other language parsed nodes...
    Last edited by liorean; 11-24-2003 at 11:38 AM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #6
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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.)
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #7
    Regular Coder COBOLdinosaur's Avatar
    Join Date
    Jul 2002
    Location
    Canada
    Posts
    396
    Thanks
    2
    Thanked 30 Times in 30 Posts
    If you are trying to get generated CSS into the page can you not use the methods of the stylesheet object:

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

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

    document.styleSheets[i].rules[j].style.setProperty('color','red',null);

    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
    100% standards compliant code is 100% correct 100% of the time.
    one of my toys from my repository and perhaps some help getting help

    Cd&

  • #8
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #9
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for your suggestions though

    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.
    Last edited by brothercake; 11-25-2003 at 11:49 PM.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •