Rick Waldron
unread,Feb 18, 2013, 10:59:39 AM2/18/13You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to dev-gaia
TL;DR: Use *node.children*, do not use node.childNodes.
After speaking with Julien on IRC this morning, regarding the use of
node.childNodes[ explicit index ], he suggested that I send an email to
list explaining the danger in using this pattern to identify DOM nodes.
There are currently 23 uses of node.childNodes[ explicit index ] in the
Gaia codebase (master), all of which are refactoring hazards/bugs. The
node.childNodes property is a NodeList that contains all child nodes,
regardless of type.
For example, given the following HTML:
<div id="container">
<!-- Put something here -->
<p>lorem ipsum</p>
</div>
The childNodes NodeList of "container" would look like:
[
#text,
<!-- Put something here -->,
#text,
<!-- Updated the UI to use .paragraphs; UI Bug #999 -->,
#text,
<p class="paragraphs">lorem ipsum</p>
]
That's:
0. the space before the comment
1. the comment
2. the space before the <p>
3. the <p>
Given the imaginary app JavaScript:
var container = document.getElementById("container");
container.childNodes[3].innerHTML = "Real text";
If a UI designer were to make changes to the layout in the given HTML,
without altering any of the app JavaScript, to:
<div id="container">
<!-- Put something here -->
<!-- Updated the UI to use .paragraphs; UI Bug #999 -->
<p class="paragraphs">lorem ipsum</p>
</div>
The childNodes NodeList would now look like this:
[
#text,
<!-- Put something here -->,
#text,
<!-- Updated the UI to use .paragraphs; UI Bug #999 -->,
#text,
<p class="paragraphs">lorem ipsum</p>
]
Which means that the app JavaScript:
container.childNodes[3].innerHTML
Is now going to be the newly added comment line.
This can be completely avoided by only using node.children, which is
limited to node of nodeType = 1.
Rick