[dom] Consider new method: getNodesByType

prettydiff has just created a new issue for 
https://github.com/whatwg/dom:

== Consider new method: getNodesByType ==
Currently it is easy to get element node types from any given element 
in a document by `getElementsByTagName`, but other node types are less
 straight forward to access. This has many design implications.

Example 1 - comments
---

Comments exist so that code authors can provide commentary to the code
 in a way that is immediately available in the source code, but not 
parsed or processed for user consumption. Although comments are easy 
for humans to read they are hard to access dynamically, and so are 
largely worthless for storing any sort of data.

Example 2 - attributes
---

Attributes are easy to access from any given node, but are challenging
 to access without touch their respective node directly. This is not 
particularly helpful. It may be more important to know about the 
status of one or more attributes by name or their values without care 
for the element on which they reside. For instance if I wanted to find
 a data attribute dynamically from a large section of a document there
 would be no simple means to accomplish this.

Example 3 - text nodes
---

The most convenient way to access to text content is to ignore the DOM
 and instead use the less safe innerHTML property. Even still the 
innerHTML property does not return merely the text node value, but 
absolutely everything contained by the element.

All of these issues can be solved by a getNodesByType method.  Such a 
method should provide the same level of availability as the 
getElementsByTagName method in that:

* The method is available on all element nodes at node creation
* The method searches all descendants and not just immediate child 
nodes
* The method returns a node list similar to the limitations expressed 
by the return value of getElementsByTagName

A functional example written in JavaScript can be found at 
https://github.com/prettydiff/getNodesByType/blob/master/getNodesByType.js.
 *The code example automatically applies the proposed method to all 
element nodes present in the document, but not to element nodes 
created with the createElement method.*

In order to understand the value of this recommendation I strongly 
suggest playing with the linked code example on any given large markup
 document. It is liberating to have this level of immediate access 
without so much need for walking the DOM tree and without reliance 
upon a framework.

See https://github.com/whatwg/dom/issues/37

Received on Tuesday, 26 May 2015 21:29:53 UTC