W3C home > Mailing lists > Public > public-webapps@w3.org > October to December 2008

[selectors-api] Liveness versus NodeList

From: Gavin Kistner <gavin@phrogz.net>
Date: Tue, 18 Nov 2008 21:04:31 -0700
Message-Id: <CB4BD0CB-80CC-4603-8C24-1023C27B501D@phrogz.net>
To: public-webapps@w3.org

DOM Level 2 Core and DOM Level 3 Core both say:
"The NodeList interface provides the abstraction of an ordered  
collection of nodes, without defining or constraining how this  
collection is implemented. NodeList objects in the DOM are live." [1]  

" NodeList and NamedNodeMap objects in the DOM are live; that is,  
changes to the underlying document structure are reflected in all  
relevant NodeList and NamedNodeMap objects. For example, if a DOM user  
gets a NodeList object containing the children of an Element, then  
subsequently adds more children to that element (or removes children,  
or modifies them), those changes are automatically reflected in the  
NodeList, without further action on the user's part. Likewise, changes  
to a Node in the tree are reflected in all references to that Node in  
NodeListand NamedNodeMap objects." [3] [4]

The end of section 6 in the Selectors API explicitly contradicts or  
attempts to override this (citing the latter section in DOM 3):

"The NodeList object returned by the querySelectorAll() method must be  
static, not live. ([DOM-LEVEL-3-CORE], section 1.1.1) Subsequent  
changes to the structure of the underlying document must not be  
reflected in the NodeList object. This means that the object will  
instead contain a list of matching Element nodes that were in the  
document at the time the list was created." [5]

If DOM 3 said something like "NodeList objects may be implemented as  
either static or live, and in general default to being live unless  
specified otherwise", then the specification might seem reasonable. As  
it stands, however, I feel that it is incorrect to call the return  
value from querySelectorAll() to be a NodeList. It seems akin to  
saying "The return value is a cow, except that it's a cow with a beak  
and feathers and only 2 legs."

I recommend specifying a new derivative StaticNodeList type as the  
return value. I do not recommend using an actual live NodeList, as I  
agree with both the end-user usefulness and performance benefits of  
not having a live NodeList.

To be clear, when describing the StaticNodeList type, I recommend  
clarifying that while the list itself is static, the elements within  
the list are still live (as I presume they must be). Modifications to  
elements in the DOM after capturing them in a StaticNodeList are still  
reflected when inspecting the elements themselves.

(-, /\ \/ / /\/

[1] http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-536297177
[2] http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#ID-536297177
[3] http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#td-live
[4] http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#td-live
[5] http://www.w3.org/TR/2008/WD-selectors-api-20081114/#nodeselector
Received on Wednesday, 19 November 2008 04:05:07 UTC

This archive was generated by hypermail 2.3.1 : Friday, 27 October 2017 07:26:12 UTC