W3C home > Mailing lists > Public > www-style@w3.org > January 2014

Re: [selectors4] Upwards Ancestor Traversal API

From: Simon Sapin <simon.sapin@exyr.org>
Date: Mon, 06 Jan 2014 19:25:43 +0000
Message-ID: <52CB0337.6070603@exyr.org>
To: Garrett Smith <dhtmlkitchen@gmail.com>, www-style@w3.org
On 06/01/2014 18:13, Garrett Smith wrote:
> For event bubbling, it is very useful to traverse up the DOM tree to
> find an ancestor. For example:
>   findAncestorWithClass(el, className,  /*opt*/ container);
>   findAncestorWithAttribute(el, attName, /*opt*/ value);
>   jqueryObj.closest(selectorText, /*opt*/ context)
> It would be useful to have an API that traverses up the DOM tree and
> tests each Element against a selector and returns either an Element or
> null.
> Proposal:
>   querySelectorAncestor(startNode, selectorText[, optional Node container);
> If container is not specified, container is the root node.
> Let currentParent be startNode.parentNode. While currentParent is not
> container, if currentParent matches selectorText, it is returned.

I donít think we need new APIs for this. Using the subject indicator 
with the :scope pseudo-class should be enough:

   ancestor = scope_element.querySelector('!.some_class :scope');

(By the way, where is :scope being specified nowadays?)

Simon Sapin
Received on Monday, 6 January 2014 19:26:13 UTC

This archive was generated by hypermail 2.4.0 : Monday, 23 January 2023 02:14:36 UTC