From: kelvSYC on
I'm having a tough JavaScript problem. I'm fairly new to JavaScript,
and what I'm trying to do is to replace instances of a particular
string inside a particular element with another string. Here's what I
have so far:

var result = document.evaluate('//div[@class="code"]/code', document,
null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);

if (result) {
for (var i = 0, len = result.snapshotLength; i < len; i++) {
// use snapshotItem(i) to get nodes
var val = result.snapshotItem(i).nodeValue;
val = val.replace(/<-/gi, "←");
result.snapshotItem(i).nodeValue = val;
}
}

The problem is that, even though I checked that my xpath works, the
result list is empty for some reason even though I know that it should
not be empty. Because of this, I haven't even checked to see that the
inside code works. Why is that?
From: Thomas 'PointedEars' Lahn on
kelvSYC wrote:

> var result = document.evaluate('//div[@class="code"]/code', document,
> null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
>
> if (result) {
> for (var i = 0, len = result.snapshotLength; i < len; i++) {
> // use snapshotItem(i) to get nodes
> var val = result.snapshotItem(i).nodeValue;
> val = val.replace(/<-/gi, "←");
> result.snapshotItem(i).nodeValue = val;
> }
> }
>
> The problem is that, even though I checked that my xpath works, the
> result list is empty for some reason even though I know that it should
> not be empty. Because of this, I haven't even checked to see that the
> inside code works. Why is that?

The `nodeValue' of an element node is not the content of the element; it is
`null' by definition, and assigning to that property has no effect then.
The element node has a text node as child node, which `nodeValue' you need
to change. As an alternative, and to deal summarily with further child
nodes, you can change the `textContent' of the element node (DOM Level 3
Core):

<http://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-745549614>
<http://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-1950641247>

It is however possible that you only want to change the content of a
specific text node that is the child node of a specific child element of the
`code' element. In that case you can either modify exactly that text node,
which can be tricky if inline elements are nested, or you can use the
proprietary `innerHTML' property. The synhl() function used in
<http://PointedEars.de/es-matrix> does the latter, although it does not use
XPath (yet).


PointedEars
--
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
-- Richard Cornford, cljs, <cife6q$253$1$8300dec7(a)news.demon.co.uk> (2004)
From: VK on
On Jun 5, 10:34 pm, kelvSYC <kelv...(a)gmail.com> wrote:
> var result = document.evaluate('//div[@class="code"]/code', document,
> null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
>
> if (result) {
>         for (var i = 0, len = result.snapshotLength; i < len; i++) {
>                 // use snapshotItem(i) to get nodes
>                 var val = result.snapshotItem(i).nodeValue;
>                 val = val.replace(/<-/gi, "←");
>                 result.snapshotItem(i).nodeValue = val;
>         }
>
> }
>
> The problem is that, even though I checked that my xpath works, the
> result list is empty for some reason even though I know that it should
> not be empty.  Because of this, I haven't even checked to see that the
> inside code works.  Why is that?

nodeValue property has a completely idiotic standard mechanics and
very seldom does what its name seemingly suggests:
https://developer.mozilla.org/En/NodeValue
The purpose of such standardization and naming is obscure, possibly it
is of a too strong grass they smoke in W3C :-) :-|

To overcome the idioticy, browser producers introduced additional
textContent property that does what you need and what nodeValue should
be doing if W3C members would be using right part of the body for
thinking.
https://developer.mozilla.org/En/DOM/Node.textContent

IE doesn't have this property because it has for ages its own
innerText property that does the same.
http://msdn.microsoft.com/en-us/library/ms533899%28VS.85%29.aspx

To not be bothered with the environment check every single time, you
may define once a string variable with the right property name:

var text = (/*@cc_on true || @*/ false) ? 'innerText' : 'textContent';

// ...

var val = result.snapshotItem(i)[text];


From: VK on
On Jun 6, 10:38 am, VK <schools_r...(a)yahoo.com> wrote:
> To not be bothered with the environment check every single time, you
> may define once a string variable with the right property name:
>
> var text = (/*@cc_on true || @*/ false) ? 'innerText' : 'textContent';
>
> // ...
>
> var val = result.snapshotItem(i)[text];

To be complete, DOM1 defines for text nodes read/write "data"
property, so theoretically you may use it instead:
var val = result.snapshotItem(i).data;

I never used it so I cannot comment on possible advantages/
disadvantages.



From: Thomas 'PointedEars' Lahn on
VK wrote:

> nodeValue property has a completely idiotic standard mechanics and
> very seldom does what its name seemingly suggests:
> https://developer.mozilla.org/En/NodeValue
> The purpose of such standardization and naming is obscure, possibly it
> is of a too strong grass they smoke in W3C :-) :-|

Will you *please* stop perpetuating your ongoing delusions? The value of an
element node is not supposed to be the element's text content, for an
element can have non-text content.

> To overcome the idioticy, browser producers introduced additional
> textContent property that does what you need and what nodeValue should
> be doing if W3C members would be using right part of the body for
> thinking.

Utter nonsense. For one, Microsoft is a W3C member.

> https://developer.mozilla.org/En/DOM/Node.textContent
>
> IE doesn't have this property

You only know IE and Mozilla, don't you? The majority of DOM
implementations supports the `textContent' property. That is
why it became and stays a Web standard, not vice-versa.

> because it has for ages

"For ages" -- 13 years?

> its own innerText property that does the same.
>
> http://msdn.microsoft.com/en-us/library/ms533899%28VS.85%29.aspx

Those properties are _not_ equivalent. For one, `innerText' does not
include leading white-space text nodes, while `textContent' does.

> To not be bothered with the environment check every single time, you
> may define once a string variable with the right property name:
>
> var text = (/*@cc_on true || @*/ false) ? 'innerText' : 'textContent';

This is insane. The scripting language has nothing to do with whether
either of those DOM properties are supported.

OP: Ignore VK, they do not know what they are talking about.


PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann