This is an archived snapshot of W3C's public bugzilla bug tracker, decommissioned in April 2019. Please see the home page for more details.

Bug 13464 - Add Element.contains()?
Summary: Add Element.contains()?
Status: RESOLVED FIXED
Alias: None
Product: WebAppsWG
Classification: Unclassified
Component: DOM (show other bugs)
Version: unspecified
Hardware: PC All
: P2 normal
Target Milestone: ---
Assignee: Anne
QA Contact: public-webapps-bugzilla
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-07-30 14:16 UTC by Anne
Modified: 2011-09-07 14:28 UTC (History)
9 users (show)

See Also:


Attachments

Description Anne 2011-07-30 14:16:35 UTC
Presto/WebKit/Trident support this Trident invention. It covers a subset of compareDocumentPosition() with a simpler API.
Comment 1 Aryeh Gregor 2011-07-31 14:18:39 UTC
+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.
Comment 2 Jonas Sicking (Not reading bugmail) 2011-08-01 17:11:37 UTC
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.
Comment 3 Aryeh Gregor 2011-08-01 18:31:08 UTC
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.
Comment 4 Erik Arvidsson 2011-08-30 16:30:28 UTC
Moving contains to Node seems like a no brainer to me.
Comment 6 Aryeh Gregor 2011-09-01 22:39:36 UTC
For those interested, Mozilla bug: https://bugzilla.mozilla.org/show_bug.cgi?id=683852
Comment 7 Boris Zbarsky 2011-09-07 13:32:13 UTC
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.  :(
Comment 8 Anne 2011-09-07 14:28:52 UTC
https://bitbucket.org/ms2ger/dom-core/changeset/fa4879cb584d

(Still need to write tests :-()