23.04.2013 Views

javascript

javascript

javascript

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 7 ■ TRAVERSING AND MODIFYING THE DOM TREE<br />

302<br />

Where Did the Formatting Text Nodes Go?<br />

Now let’s think this through. orderUL() plucked elements from the DOM tree and reinserted them<br />

at the very end of the NodeList in the childNodes member of their parent element. Say that three<br />

times fast. Anyway, the formatting Text nodes were left alone. Therefore, after plucking an from the<br />

tree, the formatting Text nodes that were on either side of the wind up next to each other. By the<br />

time orderUL() finishes its work, the formatting Text nodes are bunched up at the beginning of the<br />

NodeList in the childNodes members of the elements.<br />

Rather than take my word for it, refresh Firefox, and run the following amended sample:<br />

var myElements = document.getElementsByTagName("ul");<br />

function orderUL(root) {<br />

var nodeList = root.getElementsByTagName("li"), helperArray = [], i;<br />

for (i = 0; i < nodeList.length; i ++) {<br />

helperArray.push(nodeList[i]);<br />

}<br />

helperArray.sort(function(li1, li2) {<br />

var txt1 = li1.getElementsByTagName("a")[0].firstChild.nodeValue.toLowerCase();<br />

var txt2 = li2.getElementsByTagName("a")[0].firstChild.nodeValue.toLowerCase();<br />

if (txt1 < txt2) {<br />

return -1;<br />

} else if (txt1 > txt2) {<br />

return 1;<br />

} else {<br />

return 0;<br />

}<br />

});<br />

for (i = 0; i < helperArray.length; i ++) {<br />

helperArray[i].parentNode.appendChild(helperArray[i]);<br />

}<br />

}<br />

orderUL(myElements[0]);<br />

myElements[1].childNodes;<br />

JavaScript then prints the following in Firebug:<br />

[, , ,<br />

, , ,<br />

, , ,<br />

, , ,<br />

, , ,<br />

, li, li, li, li, li, li, li, li, li, li, li, li, li, li, li]<br />

So, although you initially had 17 formatting Text nodes interspersing 15 Element nodes, now<br />

you have 17 formatting Text nodes followed by 15 Element nodes. Having those formatting Text<br />

nodes bunched together does no harm. But if you want JavaScript to fold those 17 formatting Text nodes<br />

into one, call Node.normalize() on their parent or any other ancestor. JavaScript will then merge any<br />

adjacent Text nodes and delete any empty ones. Note that, by empty Text nodes, I mean those that do<br />

not even represent whitespace. As you might imagine, empty Text nodes are as rare as formatting Text<br />

nodes are prevalent.<br />

Refresh Firefox; then run the following amended sample:<br />

var myElements = document.getElementsByTagName("ul");<br />

function orderUL(root) {<br />

var nodeList = root.getElementsByTagName("li"), helperArray = [], i;

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!