W3C DOM tests - text nodes and data

The div below is element x. The text node in the div is element y.

The quick brown fox jumped over the lazy dog.

These tests are about text nodes, which are also called data.

Test scripts

appendData()

y.appendData(' An extra text.');
alert(y.data);

data

The first test should return undefined, since x is not a text node.

alert(x.data)
alert(y.data)
y.data = 'A new sentence takes the place of the old.'

deleteData()

y.deleteData(4,3);
alert(y.data);

insertData()

y.insertData(4,' inserted data ');
alert(y.data);

replaceData()

y.replaceData(4,12,' replaced data ');
alert(y.data);

substringData()

alert(y.substringData(4,12));

splitText() and normalize()

First split the text, then normalize it.

alert('Before: ' + x.firstChild.nodeValue);
y.splitText(10);
alert('After: ' + x.firstChild.nodeValue);
alert('Before: ' + x.firstChild.nodeValue);
x.normalize();
alert('After: ' + x.firstChild.nodeValue);

wholeText

Split the text node first; wholeText should still give you the entire text.

alert(y.wholeText)
y.wholeText = "Let's change it!"
y.wholeText = "Let's <u>change</u> it!"