Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-23-2003, 04:29 PM   PM User | #1
brothercake
Senior Coder


 
Join Date: Jun 2002
Location: near Oswestry
Posts: 4,508
Thanks: 0
Thanked 0 Times in 0 Posts
brothercake is an unknown quantity at this point
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
brothercake is offline   Reply With Quote
Old 11-23-2003, 06:58 PM   PM User | #2
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enoughliorean will become famous soon enough
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
__________________
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

Last edited by liorean; 11-25-2003 at 10:32 PM..
liorean is offline   Reply With Quote
Old 11-24-2003, 09:05 AM   PM User | #3
brothercake
Senior Coder


 
Join Date: Jun 2002
Location: near Oswestry
Posts: 4,508
Thanks: 0
Thanked 0 Times in 0 Posts
brothercake is an unknown quantity at this point
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
brothercake is offline   Reply With Quote
Old 11-24-2003, 11:34 AM   PM User | #4
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enoughliorean will become famous soon enough
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...
__________________
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

Last edited by liorean; 11-24-2003 at 11:38 AM..
liorean is offline   Reply With Quote
Old 11-24-2003, 12:22 PM   PM User | #5
brothercake
Senior Coder


 
Join Date: Jun 2002
Location: near Oswestry
Posts: 4,508
Thanks: 0
Thanked 0 Times in 0 Posts
brothercake is an unknown quantity at this point
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
brothercake is offline   Reply With Quote
Old 11-24-2003, 12:29 PM   PM User | #6
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enoughliorean will become famous soon enough
Yah, but remember that Opera doesn't support the script element in XHTML mode yet, either...

(I wonder if they aren't supporting javascript: 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
liorean is offline   Reply With Quote
Old 11-25-2003, 10:17 PM   PM User | #7
COBOLdinosaur
Regular Coder

 
COBOLdinosaur's Avatar
 
Join Date: Jul 2002
Location: Canada
Posts: 293
Thanks: 0
Thanked 18 Times in 18 Posts
COBOLdinosaur is an unknown quantity at this point
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&
COBOLdinosaur is offline   Reply With Quote
Old 11-25-2003, 10:30 PM   PM User | #8
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enoughliorean will become famous soon enough
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
liorean is offline   Reply With Quote
Old 11-25-2003, 11:44 PM   PM User | #9
brothercake
Senior Coder


 
Join Date: Jun 2002
Location: near Oswestry
Posts: 4,508
Thanks: 0
Thanked 0 Times in 0 Posts
brothercake is an unknown quantity at this point
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.
__________________
"Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

Last edited by brothercake; 11-25-2003 at 11:49 PM..
brothercake is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:38 PM.


Advertisement
Log in to turn off these ads.