W3C DOM tests - Walking the node tree

This is the first paragraph. I deliberately left out all empty text nodes.

This is the second paragraph that starts with an underline. This paragraph is element x and contains a strong tag that is element y which is directly followed by an em tag

This is the third paragraph. It's preceded by a comment.

This page tests walking the DOM tree through firstChild and friends.

Test scripts

firstChild

Test firstChild. The correct answers are <U> and #text, respectively.

alert(x.firstChild.nodeName)
alert(y.firstChild.nodeName)

lastChild

Test lastChild. The correct answers are <EM>and <CODE>, respectively.

alert(x.lastChild.nodeName)
alert(y.lastChild.nodeName)

nextSibling

Test nextSibling. The correct answers are #comment and <EM>, respectively.

alert(x.nextSibling.nodeName)
alert(y.nextSibling.nodeName)

parentNode

Test parentNode. The correct answers are <DIV>and <P>, respectively.

alert(x.parentNode.nodeName)
alert(y.parentNode.nodeName)

previousSibling

Test previousSibling. The correct answers are <P>and #text, respectively.

alert(x.previousSibling.nodeName)
alert(y.previousSibling.nodeName)

sourceIndex

Correct: 51 and <p> twice

var test = x.sourceIndex;
alert(test);
alert(x.nodeName);
alert(document.getElementsByTagName('*')[test].nodeName);

Correct: 54 and <strong> twice

var test = y.sourceIndex;
alert(test);
alert(y.nodeName);
alert(document.getElementsByTagName('*')[test].nodeName);