This is an archived snapshot of W3C's public bugzilla bug tracker, decommissioned in April 2019. Please see the home page for more details.
Presto/WebKit/Trident support this Trident invention. It covers a subset of compareDocumentPosition() with a simpler API.
+1 from me in favor of contains() being defined on Node and working on all Nodes. Some kind of before()/after() would also be cool, and a way to retrieve the furthest ancestor of a Node so you can check if two Nodes are in the same tree. compareDocumentPosition() is the kind of incredible evil you get when JS APIs are made up by C++ programmers, and needs to be burned with fire.
Based on the use cases listed, it sounds like an API like: interface Node { ... Node getCommonAncestor(in Node other); ... }; might be a good solution. It would let you check if two nodes are in the same subtree by doing: if (node1.getCommonAncestor(node2)) { // In same subtree } and check for contains() by doing: if (node1.getCommonAncestor(node2) == node1) { // node1 contains node2 } Of course, performance would be better with separate .contains() and .inSameSubtree() functions.
contains() already exists, so it seems better to pave the cowpaths. getCommonAncestor() as you define it isn't well-named, because the result might not be an ancestor at all: it might be equal to one or both of the nodes. The Range interface already has a member called commonAncestorContainer, in fact, and the DOM Range spec uses the term "ancestor container of" to mean "the same as or ancestor of". Also, I'm not sure how intuitive it is to ask authors to use getCommonAncestor(). It seems better to have a few simple orthogonal tools than to have one general tool that you can use for several purposes -- the latter is half the problem with compareDocumentPosition() to start with. Asking authors to do node.getCommonAncestor(node) to get the furthest ancestor of a node or node1.getCommonAncestor(node2) == node 1 check for containment is roundabout.
Moving contains to Node seems like a no brainer to me.
https://bitbucket.org/ms2ger/dom-core/changeset/9b02836d073d
For those interested, Mozilla bug: https://bugzilla.mozilla.org/show_bug.cgi?id=683852
The spec doesn't match Trident/WebKit/Presto for the case of |node.contains(node)|: per spec this returns false, but Trident/WebKit/Presto return true. Is this difference purposeful? The Gecko implementation seems to be following the spec at the moment. Really wish we wrote tests as we spec stuff. :(
https://bitbucket.org/ms2ger/dom-core/changeset/fa4879cb584d (Still need to write tests :-()