Skip to content

Commit 605f5eb

Browse files
committed
[selectors] Rejigger the absolutizing algorithm to be more correct.
--HG-- extra : rebase_source : 20ac2350bed9ed3383c41d0cc0d59a4753d15024
1 parent 37ea780 commit 605f5eb

2 files changed

Lines changed: 39 additions & 19 deletions

File tree

selectors/Overview.html

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ <h2 class="no-num no-toc" id=contents>Table of Contents</h2>
209209
Selectors</a>
210210
<ul class=toc>
211211
<li><a href="#absolutizing"><span class=secno>3.4.1. </span>
212-
Absolutizing a Scope-relative Selector</a>
212+
Absolutizing a Relative Selector</a>
213213
</ul>
214214

215215
<li><a href="#pseudo-classes"><span class=secno>3.5. </span>
@@ -1407,12 +1407,12 @@ <h3 id=relative><span class=secno>3.4. </span> Relative Selectors</h3>
14071407
href="#scope-filtered"><i>scope-filtered</i></a>.
14081408

14091409
<h4 id=absolutizing><span class=secno>3.4.1. </span> Absolutizing a
1410-
Scope-relative Selector</h4>
1410+
Relative Selector</h4>
14111411

14121412
<p> To <dfn id=absolutize>absolutize a relative selector</dfn>:
14131413

14141414
<p> If the <a href="#reference-element-set"><i>reference element
1415-
set</i></a> is non-empty,
1415+
set</i></a> is empty or consists of one or more elements:
14161416

14171417
<ol>
14181418
<li> If the selector starts with a <a
@@ -1421,9 +1421,6 @@ <h4 id=absolutizing><span class=secno>3.4.1. </span> Absolutizing a
14211421
<a href="#scope-pseudo"><code>:scope</code></a> as the initial <a
14221422
href="#compound"><i>compound selector</i></a>.
14231423

1424-
<li> Otherwise, if the <a href="#reference-element-set"><i>reference
1425-
element set</i></a> is empty, the selector is already absolute.
1426-
14271424
<li> Otherwise, if the selector does not contain any instance of the <a
14281425
href="#scope-pseudo"><code>:scope</code></a> pseudo-class (either at the
14291426
top-level or as an argument to a functional pseudo-class), prepend <a
@@ -1433,22 +1430,37 @@ <h4 id=absolutizing><span class=secno>3.4.1. </span> Absolutizing a
14331430
<li> Otherwise, the selector is already absolute.
14341431
</ol>
14351432

1433+
<p class=issue> The above algorithm is different from Selectors API: its
1434+
step 2, which bailed if the reference element set was empty, has been
1435+
removed. This is because handling an explicitly empty list should return
1436+
nothing; if a reference element set was not given at all, Selectors API 2
1437+
should be amended to say that the selector must be absolute already.
1438+
(Broken example: doing <code>document.find('img', [list-of-links]))</code>
1439+
finds images that are descendants of links normally, but instead finds all
1440+
images in the document if the list-of-links is empty.)
1441+
14361442
<p> Otherwise, if the <a href="#reference-element-set"><i>reference element
1437-
set</i></a> is empty,
1443+
set</i></a> consists of only a <a href="#virtual"><i>virtual scoping
1444+
root</i></a>:
14381445

14391446
<ol>
14401447
<li> If the selector starts with a <a href="#child-combinator"><i>child
14411448
combinator</i></a>, remove the child combinator. The selector is now
14421449
absolute, with the additional constraint that the first compound selector
14431450
in the selector only matches elements without a parent.
14441451

1445-
<li> If the selectors starts with any other combinator other than the <a
1446-
href="#descendant-combinator"><i>descendant combinator</i></a>, the
1447-
selector represents nothing.
1452+
<li> Otherwise, if the selector starts with any combinator other than the
1453+
<a href="#descendant-combinator"><i>descendant combinator</i></a>, change
1454+
the selector to ‘<a href="#negation-pseudo"><code
1455+
class=css>:not(*)</code></a>’. <span class=note>This is the shortest
1456+
selector that is valid, but guaranteed to match nothing.</span>
14481457

14491458
<li> Otherwise, the selector is already absolute.
14501459
</ol>
14511460

1461+
<p> Otherwise, this is a spec error. Please report it to the relevant
1462+
standards body.
1463+
14521464
<p> To <dfn id=absolutize-list>absolutize a relative selector list</dfn>,
14531465
absolutize each relative selector in the list.
14541466

selectors/Overview.src.html

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -700,23 +700,19 @@ <h3 id="relative">
700700
or <i>scope-filtered</i>.
701701

702702
<h4 id='absolutizing'>
703-
Absolutizing a Scope-relative Selector</h4>
703+
Absolutizing a Relative Selector</h4>
704704

705705
<p>
706706
To <dfn id='absolutize'>absolutize a relative selector</dfn>:
707707

708708
<p>
709-
If the <i>reference element set</i> is non-empty,
709+
If the <i>reference element set</i> is empty or consists of one or more elements:
710710

711711
<ol>
712712
<li>
713713
If the selector starts with a <i>combinator</i> other than the <i>descendant combinator</i>,
714714
prepend <code>:scope</code> as the initial <i>compound selector</i>.
715715

716-
<li>
717-
Otherwise, if the <i>reference element set</i> is empty,
718-
the selector is already absolute.
719-
720716
<li>
721717
Otherwise, if the selector does not contain any instance of the <code>:scope</code> pseudo-class
722718
(either at the top-level or as an argument to a functional pseudo-class),
@@ -726,8 +722,16 @@ <h4 id='absolutizing'>
726722
Otherwise, the selector is already absolute.
727723
</ol>
728724

725+
<p class='issue'>
726+
The above algorithm is different from Selectors API: its step 2, which bailed if the reference element set was empty, has been removed.
727+
This is because handling an explicitly empty list should return nothing;
728+
if a reference element set was not given at all,
729+
Selectors API 2 should be amended to say that the selector must be absolute already.
730+
(Broken example: doing <code>document.find('img', [list-of-links]))</code> finds images that are descendants of links normally,
731+
but instead finds all images in the document if the list-of-links is empty.)
732+
729733
<p>
730-
Otherwise, if the <i>reference element set</i> is empty,
734+
Otherwise, if the <i>reference element set</i> consists of only a <i>virtual scoping root</i>:
731735

732736
<ol>
733737
<li>
@@ -738,13 +742,17 @@ <h4 id='absolutizing'>
738742
only matches elements without a parent.
739743

740744
<li>
741-
If the selectors starts with any other combinator other than the <i>descendant combinator</i>,
742-
the selector represents nothing.
745+
Otherwise, if the selector starts with any combinator other than the <i>descendant combinator</i>,
746+
change the selector to '':not(*)''.
747+
<span class='note'>This is the shortest selector that is valid, but guaranteed to match nothing.</span>
743748

744749
<li>
745750
Otherwise, the selector is already absolute.
746751
</ol>
747752

753+
<p>
754+
Otherwise, this is a spec error. Please report it to the relevant standards body.
755+
748756
<p>
749757
To <dfn id='absolutize-list'>absolutize a relative selector list</dfn>,
750758
absolutize each relative selector in the list.

0 commit comments

Comments
 (0)