Skip to content

Commit

Permalink
Add Element.prototype.closest(selectors)
Browse files Browse the repository at this point in the history
  • Loading branch information
annevk committed May 26, 2014
1 parent 39c2927 commit fd69cc7
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 14 deletions.
35 changes: 29 additions & 6 deletions Overview.src.html
Original file line number Diff line number Diff line change
Expand Up @@ -5232,7 +5232,8 @@ <h3>Interface <code>Element</code></h3>
<span>Attr</span> <span title=dom-Element-setAttributeNode>setAttributeNode</span>(Attr newAttr);
<span>Attr</span> <span title=dom-Element-removeAttributeNode>removeAttributeNode</span>(Attr oldAttr);
-->
boolean <span title=dom-Element-matches>matches</span>(DOMString <var title>selectors</var>);
<span>Element</span>? <span title=dom-Element-closest>closest</span>(DOMString <var>selectors</var>);
boolean <span title=dom-Element-matches>matches</span>(DOMString <var>selectors</var>);

<span>HTMLCollection</span> <span title=dom-Element-getElementsByTagName>getElementsByTagName</span>(DOMString <var title>localName</var>);
<span>HTMLCollection</span> <span title=dom-Element-getElementsByTagNameNS>getElementsByTagNameNS</span>(DOMString? <var title>namespace</var>, DOMString <var title>localName</var>);
Expand Down Expand Up @@ -5774,18 +5775,23 @@ <h3>Interface <code>Element</code></h3>
<hr>

<dl class=domintro>
<dt><code><var title>element</var> . <span title=dom-Element-matches>matches</span>(<var title>selectors</var>)</code>
<dd><p>Returns true if matching <var title>selectors</var> against
<dt><code><var title>element</var> . <span title=dom-Element-closest>closest</span>(<var>selectors</var>)</code>
<dd><p>Returns the first (starting at <var title>element</var>)
<span title=concept-tree-inclusive-ancestor>inclusive ancestor</span> that matches
<var title>selectors</var>, and null otherwise.

<dt><code><var title>element</var> . <span title=dom-Element-matches>matches</span>(<var>selectors</var>)</code>
<dd><p>Returns true if matching <var>selectors</var> against
<var title>element</var>'s <span title=concept-tree-root>root</span> yields
<var title>element</var>, and false otherwise.
</dl>

<p>To <dfn>match a selectors string</dfn> <var title>selectors</var> against a
<p>To <dfn>match a selectors string</dfn> <var>selectors</var> against a
<var title>set</var>, run these steps:

<ol>
<li><p>Let <var title>s</var> be the result of
<span data-anolis-spec=selectors>parse a selector</span> from <var title>selectors</var>.
<span data-anolis-spec=selectors>parse a selector</span> from <var>selectors</var>.
<span data-anolis-ref>SELECTORS</span>

<li><p>If <var title>s</var> is failure, <span title=concept-throw>throw</span> a
Expand All @@ -5797,9 +5803,26 @@ <h3>Interface <code>Element</code></h3>
<var title>set</var>. <span data-anolis-ref>SELECTORS</span>
</ol>

<p>The <dfn title=dom-Element-closest><code>closest(<var>selectors</var>)</code></dfn>
method must run these steps:

<ol>
<li><p>Let <var>elements</var> be <span>context object</span>'s
<span title=concept-tree-inclusive-ancestor>inclusive ancestor</span> that are
<span title=concept-element>elements</span>, in reverse
<span title=concept-tree-order>tree order</span>.

<li><p>For each <var title>element</var> in <var>elements</var>, return
<var title>element</var> if <var title>element</var> is in the result of running
<span>match a selectors string</span> <var>selectors</var> against a set consisting
of <var title>element</var>.

<li><p>Return null.
</ol>

<p>The <dfn title=dom-Element-matches><code>matches(<var>selectors</var>)</code></dfn>
method must return true if the <span>context object</span> is in the result of running
<span>match a selectors string</span> <var title>selectors</var> against a set consisting
<span>match a selectors string</span> <var>selectors</var> against a set consisting
of <span>context object</span>, and false otherwise.

<hr>
Expand Down
39 changes: 31 additions & 8 deletions dom-core.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<hgroup>
<h1 class="allcaps">DOM</h1>
<h2>Living Standard — Last Updated 22 May 2014</h2>
<h2>Living Standard — Last Updated 26 May 2014</h2>
</hgroup>

<dl>
Expand Down Expand Up @@ -44,7 +44,7 @@ <h2>Living Standard — Last Updated 22 May 2014</h2>
<p class="copyright"><a href="http://creativecommons.org/publicdomain/zero/1.0/" rel="license"><img alt="CC0" src="http://i.creativecommons.org/p/zero/1.0/80x15.png"></a>
To the extent possible under law, the editors have waived all copyright and
related or neighboring rights to this work. In addition, as of
22 May 2014, the editors have made this specification available
26 May 2014, the editors have made this specification available
under the
<a href="http://www.openwebfoundation.org/legal/the-owf-1-0-agreements/owfa-1-0" rel="license">Open Web Foundation Agreement Version 1.0</a>,
which is available at
Expand Down Expand Up @@ -5306,7 +5306,8 @@ <h3 id="interface-element"><span class="secno">5.8 </span>Interface <code><a hre
<span>Attr</span> <span title=dom-Element-setAttributeNode>setAttributeNode</span>(Attr newAttr);
<span>Attr</span> <span title=dom-Element-removeAttributeNode>removeAttributeNode</span>(Attr oldAttr);
-->
boolean <a href="#dom-element-matches" title="dom-Element-matches">matches</a>(DOMString <var title="">selectors</var>);
<a href="#element">Element</a>? <a href="#dom-element-closest" title="dom-Element-closest">closest</a>(DOMString <var>selectors</var>);
boolean <a href="#dom-element-matches" title="dom-Element-matches">matches</a>(DOMString <var>selectors</var>);

<a href="#htmlcollection">HTMLCollection</a> <a href="#dom-element-getelementsbytagname" title="dom-Element-getElementsByTagName">getElementsByTagName</a>(DOMString <var title="">localName</var>);
<a href="#htmlcollection">HTMLCollection</a> <a href="#dom-element-getelementsbytagnamens" title="dom-Element-getElementsByTagNameNS">getElementsByTagNameNS</a>(DOMString? <var title="">namespace</var>, DOMString <var title="">localName</var>);
Expand Down Expand Up @@ -5848,18 +5849,23 @@ <h3 id="interface-element"><span class="secno">5.8 </span>Interface <code><a hre
<hr>

<dl class="domintro">
<dt><code><var title="">element</var> . <a href="#dom-element-matches" title="dom-Element-matches">matches</a>(<var title="">selectors</var>)</code>
<dd><p>Returns true if matching <var title="">selectors</var> against
<dt><code><var title="">element</var> . <a href="#dom-element-closest" title="dom-Element-closest">closest</a>(<var>selectors</var>)</code>
<dd><p>Returns the first (starting at <var title="">element</var>)
<a href="#concept-tree-inclusive-ancestor" title="concept-tree-inclusive-ancestor">inclusive ancestor</a> that matches
<var title="">selectors</var>, and null otherwise.

<dt><code><var title="">element</var> . <a href="#dom-element-matches" title="dom-Element-matches">matches</a>(<var>selectors</var>)</code>
<dd><p>Returns true if matching <var>selectors</var> against
<var title="">element</var>'s <a href="#concept-tree-root" title="concept-tree-root">root</a> yields
<var title="">element</var>, and false otherwise.
</dl>

<p>To <dfn id="match-a-selectors-string">match a selectors string</dfn> <var title="">selectors</var> against a
<p>To <dfn id="match-a-selectors-string">match a selectors string</dfn> <var>selectors</var> against a
<var title="">set</var>, run these steps:

<ol>
<li><p>Let <var title="">s</var> be the result of
<a class="external" data-anolis-spec="selectors" href="http://dev.w3.org/csswg/selectors/#parse-a-selector">parse a selector</a> from <var title="">selectors</var>.
<a class="external" data-anolis-spec="selectors" href="http://dev.w3.org/csswg/selectors/#parse-a-selector">parse a selector</a> from <var>selectors</var>.
<a href="#refsSELECTORS">[SELECTORS]</a>

<li><p>If <var title="">s</var> is failure, <a href="#concept-throw" title="concept-throw">throw</a> a
Expand All @@ -5871,9 +5877,26 @@ <h3 id="interface-element"><span class="secno">5.8 </span>Interface <code><a hre
<var title="">set</var>. <a href="#refsSELECTORS">[SELECTORS]</a>
</ol>

<p>The <dfn id="dom-element-closest" title="dom-Element-closest"><code>closest(<var>selectors</var>)</code></dfn>
method must run these steps:

<ol>
<li><p>Let <var><a href="#elements">elements</a></var> be <a href="#context-object">context object</a>'s
<a href="#concept-tree-inclusive-ancestor" title="concept-tree-inclusive-ancestor">inclusive ancestor</a> that are
<a href="#concept-element" title="concept-element">elements</a>, in reverse
<a href="#concept-tree-order" title="concept-tree-order">tree order</a>.

<li><p>For each <var title="">element</var> in <var><a href="#elements">elements</a></var>, return
<var title="">element</var> if <var title="">element</var> is in the result of running
<a href="#match-a-selectors-string">match a selectors string</a> <var>selectors</var> against a set consisting
of <var title="">element</var>.

<li><p>Return null.
</ol>

<p>The <dfn id="dom-element-matches" title="dom-Element-matches"><code>matches(<var>selectors</var>)</code></dfn>
method must return true if the <a href="#context-object">context object</a> is in the result of running
<a href="#match-a-selectors-string">match a selectors string</a> <var title="">selectors</var> against a set consisting
<a href="#match-a-selectors-string">match a selectors string</a> <var>selectors</var> against a set consisting
of <a href="#context-object">context object</a>, and false otherwise.

<hr>
Expand Down

5 comments on commit fd69cc7

@annevk
Copy link
Member Author

@annevk annevk commented on fd69cc7 May 26, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ArkadiuszMichalski
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@annevk Would not be more useful have this method in ChildNode or maybe better in NonDocumentTypeChildNode because from DocumentType we never get any ancestor element?

@annevk
Copy link
Member Author

@annevk annevk commented on fd69cc7 Apr 5, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, new issue and copy @hzr?

@hzr
Copy link
Contributor

@hzr hzr commented on fd69cc7 Apr 5, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extending it to more interfaces was recently discussed in #161

@ArkadiuszMichalski
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I didn't notice this bug, I'll watch next steps, thx for infos.

Please sign in to comment.