change CSS class

A

Andrew Poulos

I have a CSS class selector in an external stylesheet that looks like this:

..question {
position: relative;
left: 40px;
width: 780px;
margin-top: 20px;
cursor: pointer;
}

how do I change the value of a specific property? Say, left to 60px?

I could add a new CSS rule but I'd rather exist the existing one.

Andrew Poulos
 
T

Thomas 'PointedEars' Lahn

Andrew said:
I have a CSS class selector in an external stylesheet that looks like this:

..question {
position: relative;
left: 40px;
width: 780px;
margin-top: 20px;
cursor: pointer;
}

how do I change the value of a specific property? Say, left to 60px?

RTFM, STFW: document.styleSheets

<http://jibbering.com/faq/#posting>


PointedEars
 
S

SAM

Le 4/24/09 7:18 AM, Andrew Poulos a écrit :
I have a CSS class selector in an external stylesheet that looks like this:

.question {
position: relative;
left: 40px;

with a position relative, left will do nothing ...

margin-left: 40px;
width: 780px;
margin-top: 20px;
cursor: pointer;
}

how do I change the value of a specific property? Say, left to 60px?

I could add a new CSS rule but I'd rather exist the existing one.

Andrew Poulos

JS :
====

function changeLeft(selector, value) {
var s = document.styleSheets[0].cssRules ||
document.styleSheets[0].rules;
for(var i=0, n=s.length; i<n; i++) {
if(s.selectorText == selector) s.style.marginLeft = value;
}
}

html :
======

<a class="question" href="javascript:changeLeft('.question','80px')">
move questions to 80px</a>
<p class="question" onclick="changeLeft('.question','0')">
move questions to 0px</p>

See :
<http://www.quirksmode.org/dom/tests/stylesheets.html>
 
G

Gregor Kofler

SAM meinte:
Le 4/24/09 7:18 AM, Andrew Poulos a écrit :

with a position relative, left will do nothing ...

With a position *static*, left would do nothing...

Gregor
 
S

SAM

Le 4/24/09 2:37 PM, Gregor Kofler a écrit :
SAM meinte:

With a position *static*, left would do nothing...

Ha! Tien? Oui! left is OK for relative positioning
 
G

Garrett Smith

SAM said:
Le 4/24/09 7:18 AM, Andrew Poulos a écrit :
[...]


function changeLeft(selector, value) {
var s = document.styleSheets[0].cssRules ||
document.styleSheets[0].rules;
for(var i=0, n=s.length; i<n; i++) {
if(s.selectorText == selector) s.style.marginLeft = value;
}
}


That won't work reliably. IE changes element selectors and pseuedoclass
selectors to upper case.

When IE sees this:-

body {
color: #0000FF;
}

- it changes the representation of the selectorText, as if it had been
written:-

BODY {
COLOR: #0000FF;
}

"body" == "BODY" results in false.

This means that == will not work for elemenet selectors in IE.

Garrett
 
S

SAM

Le 4/25/09 12:27 AM, Garrett Smith a écrit :
SAM said:
Le 4/24/09 7:18 AM, Andrew Poulos a écrit :
[...]


function changeLeft(selector, value) {
var s = document.styleSheets[0].cssRules ||
document.styleSheets[0].rules;
for(var i=0, n=s.length; i<n; i++) {
if(s.selectorText == selector) s.style.marginLeft = value;
}
}


That won't work reliably. IE changes element selectors and pseuedoclass
selectors to upper case.


No ? It's mad ! ? !

so ...

function changeLeft(selector, val) {
var s = document.styleSheets[0].cssRules ||
document.styleSheets[0].rules;
for(var i=0, n=s.length; i<n; i++) {
var z = s.selectorText;
if(z == selector || z == selector.toUpperCase()) {
s.style.marginLeft = val;
}
}
}
 
G

Garrett Smith

SAM said:
Le 4/25/09 12:27 AM, Garrett Smith a écrit :
SAM said:
Le 4/24/09 7:18 AM, Andrew Poulos a écrit :
[...]


function changeLeft(selector, value) {
var s = document.styleSheets[0].cssRules ||
document.styleSheets[0].rules;
for(var i=0, n=s.length; i<n; i++) {
if(s.selectorText == selector) s.style.marginLeft = value;
}
}


That won't work reliably. IE changes element selectors and
pseuedoclass selectors to upper case.


No ? It's mad ! ? !

so ...

function changeLeft(selector, val) {
var s = document.styleSheets[0].cssRules ||
document.styleSheets[0].rules;
for(var i=0, n=s.length; i<n; i++) {
var z = s.selectorText;
if(z == selector || z == selector.toUpperCase()) {
s.style.marginLeft = val;
}
}
}


That's a quick and dirty solution.

It would of course fail if searching for ".F" and the stylesheet had
".f" because the function would match that (".F" == ".f".toUpperCase()).

I can't think of a good reason for having class or id selector in the
stylesheet that is a case-transformed version another class or id (
normal stylesheet wouldn't have both ".f" and ".F").

Garrett
 
S

SAM

Le 4/25/09 6:34 AM, Garrett Smith a écrit :
SAM said:
Le 4/25/09 12:27 AM, Garrett Smith a écrit :
IE changes element selectors and
pseuedoclass selectors to upper case.

so ...

function changeLeft(selector, val) {
var s = document.styleSheets[0].cssRules ||
document.styleSheets[0].rules;
for(var i=0, n=s.length; i<n; i++) {
var z = s.selectorText;
if(z == selector || z == selector.toUpperCase()) {
s.style.marginLeft = val;
}
}
}


That's a quick and dirty solution.


That's I did test successfully by myself.
It would of course fail if searching for ".F" and the stylesheet had
".f" because the function would match that (".F" == ".f".toUpperCase()).

You're really looking for the little beast.

I though to introduce indexOf('.') indexOf('#') to exclude classes and
ids but as the dirty solution did the job ...

I tested few more and I see that if IE transforms the styles sheet in
uppercase it doesn't it for values ( #c63 ! -> #C63 blue ! -> BLUE)
contrary what you seemed to say
while, for instance, Firefox transforms the colors values in RGB.

This history of colors in navigators has always been a problem.
I can't think of a good reason for having class or id selector in the
stylesheet that is a case-transformed version another class or id (
normal stylesheet wouldn't have both ".f" and ".F").

Hope that IE is not contagious and that "webmasters" aren't so mad as him.
 
A

Andrew Poulos

Garrett said:
SAM said:
Le 4/25/09 12:27 AM, Garrett Smith a écrit :
SAM wrote:
Le 4/24/09 7:18 AM, Andrew Poulos a écrit :

[...]


function changeLeft(selector, value) {
var s = document.styleSheets[0].cssRules ||
document.styleSheets[0].rules;
for(var i=0, n=s.length; i<n; i++) {
if(s.selectorText == selector) s.style.marginLeft = value;
}
}

That won't work reliably. IE changes element selectors and
pseuedoclass selectors to upper case.


No ? It's mad ! ? !

so ...

function changeLeft(selector, val) {
var s = document.styleSheets[0].cssRules ||
document.styleSheets[0].rules;
for(var i=0, n=s.length; i<n; i++) {
var z = s.selectorText;
if(z == selector || z == selector.toUpperCase()) {
s.style.marginLeft = val;
}
}
}


Can you refer to a rule by name rather than index? For example,

function changeLeft(sel, val) {
var s = document.styleSheets[0].cssRules ||
document.styleSheets[0].rules;
var sr = sel.toUpperCase();

if (s[sr] &&
s[sr].selectorText &&
s[sr].selectorText.toUpperCase() == sr ) {
s[sr].style.marginLeft = val;
}
}

Andrew Poulos
 
T

Thomas 'PointedEars' Lahn

Andrew said:
Garrett said:
[...] IE changes element selectors and [pseudoclass] selectors to upper
case.
[...]
Can you refer to a rule by name rather than index? For example,

function changeLeft(sel, val) {
var s = document.styleSheets[0].cssRules ||I could add a new CSS rule but I'd rather exist the existing one.
document.styleSheets[0].rules;
var sr = sel.toUpperCase();

if (s[sr] &&
s[sr].selectorText &&
s[sr].selectorText.toUpperCase() == sr ) {

While "Pseudo-element and pseudo-class names are case-insensitive." (CSS
2.1I could add a new CSS rule but I'd rather exist the existing one. CR
[1]), "The case-sensitivity of document language element names in selectors
depends on the document language. For example, in HTML, element names are
case-insensitive, but in XML they are case-sensitive." (ibid.), and "The
case-sensitivity of attribute names and values in selectors depends on the
document language." (ibid.; therefore, MSHTML is correct to a certain extent
in transforming to uppercase in the instances mentioned by Garrett since it
does not support XHTML.)

s[sr].style.marginLeft = val;
}
}

Since the CSSRuleList interface does not inherit from a collection
interface, you could, but only after you've built a map yourself. In its
simplest form (cf. the recent thread about Hashtable and Map implementations
for caveats):

function getRulesMap(s)
{
function normalizeSelector(sel)
{
return sel.replace(
/([^#.])(\w+)/g,
function(m, p1, p2) {
return p1 + p2.toLowerCase();
});
}

var map = {};

for (var i = 0,
rs = s.cssRules || s.rules,
len = rs && rs.length;
i < len;
i++)
{
var r = rs;
map[normalizeSelector(r.selectorText)] = r;
}

return map;
}

var rulesMap = getRulesMap(document.styleSheets[0]);
rulesMap[".question"].style.left = "60px";

(Untested.)
I could add a new CSS rule but I'd rather [modify] the existing one.

I'm sure you can see now that modifying existing stylesheets is a lot more
complicated (and less compatible) than adding new ones that make use of CSS
specificity rules to achieve the goal.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Andrew said:
Garrett said:
[...] IE changes element selectors and [pseudoclass] selectors to
upper case.
[...]
Can you refer to a rule by name rather than index? For example,

function changeLeft(sel, val) {
var s = document.styleSheets[0].cssRules ||
document.styleSheets[0].rules;
var sr = sel.toUpperCase();

if (s[sr] &&
s[sr].selectorText &&
s[sr].selectorText.toUpperCase() == sr ) {

While "Pseudo-element and pseudo-class names are case-insensitive." (CSS
2.1 CR [1]), "The case-sensitivity of document language element names in
selectors depends on the document language. For example, in HTML, element
names are case-insensitive, but in XML they are case-sensitive." (ibid.),
and "The case-sensitivity of attribute names and values in selectors depends
on the document language." (ibid.; therefore, MSHTML is correct to a certain
extent in transforming to uppercase in the instances mentioned by Garrett
since it does not support XHTML.)

s[sr].style.marginLeft = val;
}
}

Since the CSSRuleList interface does not inherit from a collection
interface, you could, but only after you've built a map yourself. In its
simplest form (cf. the recent thread about Hashtable and Map implementations
for caveats):

function getRulesMap(s)
{
function normalizeSelector(sel)
{
return sel.replace(
/([^#.])(\w+)/g,
function(m, p1, p2) {
return p1 + p2.toLowerCase();
});
}

var map = {};

for (var i = 0,
rs = s.cssRules || s.rules,
len = rs && rs.length;
i < len;
i++)
{
var r = rs;
map[normalizeSelector(r.selectorText)] = r;
}

return map;
}

var rulesMap = getRulesMap(document.styleSheets[0]);
rulesMap[".question"].style.left = "60px";

(Untested.)
I could add a new CSS rule but I'd rather [modify] the existing one.

I'm sure you can see now that modifying existing stylesheets is a lot more
complicated (and less compatible) than adding new ones that make use of CSS
specificity rules to achieve the goal.


PointedEars
 
S

SAM

Le 4/25/09 3:42 PM, kangax a écrit :
Thomas 'PointedEars' Lahn wrote:

[...]
return sel.replace(
/([^#.])(\w+)/g,
function(m, p1, p2) {
return p1 + p2.toLowerCase();
});

This will turn "BODY" into "Body" and "#FOO" into "#Foo".

What do we need ?
And if you
think about attribute selectors - div[title="foo#bar"] - it becomes
clear that one would need a much more specialized regex for this
normalization.

Anyway my IEs write :
(IE 6) - UNKNOWN { FONT-WEIGHT: bold }
(IE 7) - DIV[title='foo#bar'] { FONT-WEIGHT: bold }
for :
div[title="foo#bar"] { font-weight: bold }

With the unknown selector that will no be easy to fix something, no ?
 
T

Thomas 'PointedEars' Lahn

kangax said:
Thomas said:
function getRulesMap(s)
{
function normalizeSelector(sel)
{
return sel.replace(
/([^#.])(\w+)/g,
function(m, p1, p2) {
return p1 + p2.toLowerCase();
});
}

This will turn "BODY" into "Body" and "#FOO" into "#Foo".

True, should be at least

function normalizeSelector(sel)
{
return sel.replace(
/[^#.]\S*/g,
function(m) {
return m.toLowerCase();
});
}
And if you think about attribute selectors - div[title="foo#bar"]
- it becomes clear that one would need a much more specialized
regex for this normalization.

Apparently you have overlooked this:
Since the CSSRuleList interface does not inherit from a collection
interface, you could, but only after you've built a map yourself.
In its simplest form [...]:
^^^^^^^^^^^^^^^^^^^^

PointedEars
 
G

Garrett Smith

kangax said:
Thomas 'PointedEars' Lahn wrote:

[...]
function getRulesMap(s)
{
function normalizeSelector(sel)
{
return sel.replace(
/([^#.])(\w+)/g,
function(m, p1, p2) {
return p1 + p2.toLowerCase();
});
}

This will turn "BODY" into "Body" and "#FOO" into "#Foo". And if you

Yeah, that wouldn't work as-is. Even with a small tweak to the RegExp to
add a '+' after the group, the approach would fail because you can't
modify selectorText in IE.

MSDN's four-star documentation[1] says that selectorText is read/write
but I just get the error "Not implemented." (example below).

Actually, MSDN says two things:-
| selectorText Property
|
| Retrieves a string that identifies which elements the corresponding
| style sheet rule applies to.
| Syntax
|
| [ sSelectorText = ] object.selectorText

Notice it does *not* read "Sets or retrieves[...]" but "retrieves[...]".

Immediately below that:-

| Possible Values
|
| sSelectorText String that specifies or receives the element.
|
| The property is read/write. The property has no default value.

I am lost as to how to interpret that first sentence. "the element"
could be interpreted as "selector-text to be applied to the styleSheet."

The second sentence clearly states: "The property is read/write".
Seems to be factually false, from IE5.5 to IE8. Perhaps the selectorText
needs massaging. Maybe whitespace? I don't know couldn't get it to work,
as the example below demonstrates.

Even if IE *did* allow modifying the selectorText, it might still
convert that rule.selectorText to upper case, either upon setting or
upon subsequent retrieval. Element selectors are case sensitive in CSS,
just IE changes them to upper case.

You might have noticed the one in APE:-
http://github.com/GarrettS/ape-java...5ce25746a2/src/dom/StyleSheetAdapter.js#LC105

APE.dom.StyleSheetAdapter._tagNamesToUpperCase = function(s) {
// Element Selector = Start of string or ws,
// followed by one or more letter chars.
var elementSelector = /(^|\s)([a-z]+)/;
var matches = s.match( elementSelector ), R = RegExp;
while( elementSelector.test(s) ) s = s.replace(elementSelector,
R.$1+R.$2.toUpperCase());
return s;
};

(also in your branch)

- does not account for pseudo class and does not account for any browser
changing case of attribute selectors. That could be added, but I didn't
really need it. I only used it for some testing files, not in
production. Putting that function in a closure would make a lot more
sense than keeping it where it is now.

Example
<html>

<head>

<title></title>
<style type="text/css">
body{ }
</style>
</head>

<body>
<script type="text/javascript">
var sheet = document.styleSheets[0],
rules = "cssRules" in sheet ? sheet.cssRules : sheet.rules;
if(typeof rules != "undefined") {
document.write("rules[0].selectorText: " + rules[0].selectorText);
s = "<br>assign rules[0].selectorText = 'body'";
try {
rules[0].selectorText = "body";
document.write("rules[0].selectorText: " + rules[0].selectorText);
} catch(ex) {
document.write("error: " + ex.message);
}
}
</script>
</body>
</html>

Firefox, Safari, Opera, Chrome:
rules[0].selectorText: body
assign rules[0].selectorText = 'body'...
rules[0].selectorText: body

IE5.5 - IE8:
rules[0].selectorText: BODY
assign rules[0].selectorText = 'body'...
error: Not implemented

Garrett
[1]http://msdn.microsoft.com/en-us/library/ms534626(VS.85).aspx
 
G

Garrett Smith

Garrett said:
kangax said:
Thomas 'PointedEars' Lahn wrote:
[...]

Yeah, that wouldn't work as-is. Even with a small tweak to the RegExp to
add a '+' after the group, the approach would fail because you can't
modify selectorText in IE.

Ah, but that approach was a custom map. I misread or misunderstood.
Modifying the selectorText would not work, but that is not what that
code did anyway. Accidental red herring.

Garrett
 
G

Garrett Smith

Garrett said:
kangax said:
Thomas 'PointedEars' Lahn wrote:
[...]

Example

That was the wrong example, try again:-
<!doctype html>
<html>

<head>

<title>assign to selectorText</title>
<style type="text/css">
body{ }
</style>
</head>

<body>
<script type="text/javascript">
var sheet = document.styleSheets[0],
rules = "cssRules" in sheet ? sheet.cssRules : sheet.rules;
if(typeof rules != "undefined") {
document.write("<pre>rules[0].selectorText: " + rules[0].selectorText
+ "<" + "/pre>");
document.write("<p>assign rules[0].selectorText = 'body'...</p>");
try {
rules[0].selectorText = "body";
document.write("<pre>rules[0].selectorText: " +
rules[0].selectorText+ "<" + "/pre>");
} catch(ex) {
document.write("<b>error: " + ex.message+ "<" + "/b>");
}
}
</script>
</body>
</html>
 
G

Garrett Smith

kangax said:
Garrett said:
kangax said:
Thomas 'PointedEars' Lahn wrote:

[...]

function getRulesMap(s)
{
function normalizeSelector(sel)
{
return sel.replace(
/([^#.])(\w+)/g,
function(m, p1, p2) {
return p1 + p2.toLowerCase();
});
}

This will turn "BODY" into "Body" and "#FOO" into "#Foo". And if you

Yeah, that wouldn't work as-is. Even with a small tweak to the RegExp
to add a '+' after the group, the approach would fail because you
can't modify selectorText in IE.

MSDN's four-star documentation[1] says that selectorText is read/write
but I just get the error "Not implemented." (example below).

Actually, MSDN says two things:-
| selectorText Property
|
| Retrieves a string that identifies which elements the corresponding
| style sheet rule applies to.
| Syntax
|
| [ sSelectorText = ] object.selectorText

Notice it does *not* read "Sets or retrieves[...]" but "retrieves[...]".

Immediately below that:-

| Possible Values
|
| sSelectorText String that specifies or receives the element.
|
| The property is read/write. The property has no default value.

I am lost as to how to interpret that first sentence. "the element"
could be interpreted as "selector-text to be applied to the styleSheet."

The second sentence clearly states: "The property is read/write".
Seems to be factually false, from IE5.5 to IE8. Perhaps the
selectorText needs massaging. Maybe whitespace? I don't know couldn't
get it to work, as the example below demonstrates.

Well, DOM Level 2 Style specs do not mention that `selectorText`
property of a `CSStyleRule` interface is readonly
<http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleRule>

They say that there might be two types of errors thrown, but an error
you mentioned doesn't look like one of them.

On the other hand, FF (3.0.9) doesn't seem to allow to modify
`selectorText` either. It doesn't throw, as IE does, but it doesn't
mutate it either.
Even if IE *did* allow modifying the selectorText, it might still
convert that rule.selectorText to upper case, either upon setting or
upon subsequent retrieval. Element selectors are case sensitive in
CSS, just IE changes them to upper case.

You might have noticed the one in APE:-
http://github.com/GarrettS/ape-java...5ce25746a2/src/dom/StyleSheetAdapter.js#LC105


APE.dom.StyleSheetAdapter._tagNamesToUpperCase = function(s) {
// Element Selector = Start of string or ws,
// followed by one or more letter chars.
var elementSelector = /(^|\s)([a-z]+)/;
var matches = s.match( elementSelector ), R = RegExp;
while( elementSelector.test(s) ) s = s.replace(elementSelector,
R.$1+R.$2.toUpperCase());
return s;
};

Interesting. I'm not familiar with DOM L2 Style module. Will take a look
at your `StyleSheetAdapter`.

[...]
Example
<html>

<head>

<title></title>
<style type="text/css">
body{ }
</style>
</head>

<body>
<script type="text/javascript">
var sheet = document.styleSheets[0],
rules = "cssRules" in sheet ? sheet.cssRules : sheet.rules;
if(typeof rules != "undefined") {
document.write("rules[0].selectorText: " + rules[0].selectorText);
s = "<br>assign rules[0].selectorText = 'body'";
try {
rules[0].selectorText = "body";
document.write("rules[0].selectorText: " + rules[0].selectorText);
} catch(ex) {
document.write("error: " + ex.message);
}
}
</script>
</body>
</html>

Firefox, Safari, Opera, Chrome:
rules[0].selectorText: body
assign rules[0].selectorText = 'body'...
rules[0].selectorText: body

Was this example aimed at testing whether setter throws or if setting
operation is successful? I assume it's the former one, since you're
assigning the same selector string, making it impossible to check if
modification took place.

Yeah, right.

The example should use new selectorText. This next test case affects a
visual update on the page that can be confirmed visually and
programmatically (by checking computed style or currentStyle). (Example
below).

First a look at DOM 2 Style spec:-
http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleRule

| selectorText of type DOMString
| The textual representation of the selector for the rule set. The
| implementation may have stripped out insignificant whitespace while
| parsing the selector.
| Exceptions on setting
|
| DOMException
|
|
| SYNTAX_ERR: Raised if the specified CSS string value has a syntax
| error and is unparsable.
|
| NO_MODIFICATION_ALLOWED_ERR: Raised if this rule is readonly.
| style of type CSSStyleDeclaration, readonly
| The declaration-block of this rule set.

A SYNTAX_ERR should be raise if the selectorText is set with a value
that is unparseable. That I understand.

A NO_MODIFICATION_ALLOWED_ERR if the rule is readonly. How can I know if
the rule should be readonly?

The following example sets selectorText of a cssRule. I create a rule
with selectorText "u" and try to modify that to "#pass". This affects
the div id="pass" (and class="pass"). Since id has higher specificity
than class, applying the style rules for ID should take precedence.
<!doctype html>
<html>

<head>

<title>assign to selectorText</title>
<style type="text/css">
u {
/* try to modify this rule */
background: #090;
display: block;
color: #cfc;
}

..pass {
display: none;
}
</style>
</head>

<body>
<pre id="pass" class="pass">pass</pre>
<script type="text/javascript">
var sheet = document.styleSheets[0],
rules = "cssRules" in sheet ? sheet.cssRules : sheet.rules,
newSelectorText = "#pass";
if(typeof rules != "undefined") {
document.write("<pre>rules[0].selectorText: " + rules[0].selectorText
+ "<" + "/pre>");
document.write("<p>assign rules[0].selectorText = " + newSelectorText
+ "...</p>");
try {
rules[0].selectorText = newSelectorText;
document.write("<pre>rules[0].selectorText: " +
rules[0].selectorText+ "<" + "/pre>");
} catch(ex) {
document.write("<b>error: " + ex.message+ "<" + "/b>");
}
var resultEl = document.getElementById("pass");
var dv = document.defaultView;
var display = dv && "getComputedStyle" in dv ?
dv.getComputedStyle(resultEl, "").display :
resultEl.currentStyle.display;
document.write("<br>", display == "block" ? "view updated" : "view
not updated");
}
</script>
</body>
</html>

Result:
Opera:
| pass
| rules[0].selectorText: u
|
| assign rules[0].selectorText = #pass...
| rules[0].selectorText: #pass
|
| view updated

Firefox, Safari, Chrome:-
| rules[0].selectorText: u
| assign rules[0].selectorText = #pass...
|
| rules[0].selectorText: u
|
| view not updated

IE7:
| rules[0].selectorText: BODY
| assign rules[0].selectorText = #pass...
|
| error: Not implemented
| view not updated

IE Usability Note: select-all and copy (ctrl+c) did not include newlines
and included the "pass" text that was not visually displayed.

Modifying the selectorText resulted in Error in IE, failed silently in
FF, Chrome, and Safari, and succeeded in Opera.

Garrett
 
T

Thomas 'PointedEars' Lahn

Garrett said:
Garrett said:
kangax said:
Thomas 'PointedEars' Lahn wrote:
[...]
Example

That was the wrong example, try again:-
<!doctype html>

Not Valid.
<html>

<head>

<title>assign to selectorText</title>
<style type="text/css">
body{ }
</style>
</head>

<body>
<script type="text/javascript">
var sheet = document.styleSheets[0],
rules = "cssRules" in sheet ? sheet.cssRules : sheet.rules;

How many times do you need to be told that `in' is inappropriate for host
objects?
if(typeof rules != "undefined") {

Given that we are talking host objects, `rules' may store any value at this
point. The test is insufficient.
document.write("<pre>rules[0].selectorText: " + rules[0].selectorText
+ "<" + "/pre>");

Smart people use "<\/" to hide ETAGO delimiters from SGML(-ish) parsers
since about the turn of the century.
document.write("<p>assign rules[0].selectorText = 'body'...</p>");
try {
rules[0].selectorText = "body";

Nobody tried (or would even attempt trying) to modify `selectorText' but
you. You were willing to concede that point in
yet one hour 22 minutes said:

PointedEars
 
G

Garrett Smith

Thomas said:
Garrett said:
Garrett said:
kangax wrote:
Thomas 'PointedEars' Lahn wrote: [...]
Example
That was the wrong example, try again:-
<!doctype html>

Not Valid.

True, but does trigger standards mode in the browsers tested (and that
has been discussed here).

How many times do you need to be told that `in' is inappropriate for host
objects?

Is that scorn or are you trying to discuss the use of |in| in host object?

Regarding the use of |in| with host object, I mentioned the "false
positive" edge case using "99999" in document.styleSheets in IE, nearly
1 month ago. I have been looking for more cases that fail. Such failure
cases would give more weight to the argument of |in| being inappropriate
for host objects.
if(typeof rules != "undefined") {

Given that we are talking host objects, `rules' may store any value at this
point. The test is insufficient.
document.write("<pre>rules[0].selectorText: " + rules[0].selectorText
+ "<" + "/pre>");

Smart people use "<\/" to hide ETAGO delimiters from SGML(-ish) parsers
since about the turn of the century.

It is a little easier to do it with a backslash. Certainly much easier
than arguing about it.

Either way would work, though.
document.write("<p>assign rules[0].selectorText = 'body'...</p>");
try {
rules[0].selectorText = "body";

Nobody tried (or would even attempt trying) to modify `selectorText' but
you. You were willing to concede that point in
<yet one hour 22 minutes
later you are starting this again. What the heck is wrong with you?

I have noticed you have a tendency to blame others for not understanding
what you do not understand.

In this case, you seem to not understand is why I am testing assigning
to selectorText. The reason I am doing that is because I am curious as
to how browsers would handle that.

The reason for posting a second test example was to correct some
problems with the first one I provided, as kangax pointed out.

Garrett
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,102
Messages
2,570,645
Members
47,245
Latest member
ShannonEat

Latest Threads

Top