Skip to content

Commit 24fe6a1

Browse files
committed
Allow setting rootMargin and thresholds
Fixes w3c#428.
1 parent cac7ad2 commit 24fe6a1

File tree

1 file changed

+40
-16
lines changed

1 file changed

+40
-16
lines changed

index.bs

+40-16
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,8 @@ use-cases. Per-{{observe()}} options could be provided in the future if the need
218218
interface IntersectionObserver {
219219
constructor(IntersectionObserverCallback callback, optional IntersectionObserverInit options = {});
220220
readonly attribute (Element or Document)? root;
221-
readonly attribute DOMString rootMargin;
222-
readonly attribute FrozenArray<double> thresholds;
221+
attribute DOMString rootMargin;
222+
attribute FrozenArray<double> thresholds;
223223
undefined observe(Element target);
224224
undefined unobserve(Element target);
225225
undefined disconnect();
@@ -279,15 +279,31 @@ interface IntersectionObserver {
279279
this is not guaranteed to be identical to the |options|.{{IntersectionObserverInit/rootMargin}}
280280
passed to the {{IntersectionObserver}} constructor. If no
281281
{{IntersectionObserverInit/rootMargin}} was passed to the {{IntersectionObserver}}
282-
constructor, the value of this attribute is "0px 0px 0px 0px".
282+
constructor, and the {{IntersectionObserver/rootMargin}} setter has not been invoked,
283+
the value of this attribute is "0px 0px 0px 0px".
284+
285+
On setting, attempt to <a>parse a root margin</a>
286+
from the given value.
287+
If a list is returned,
288+
set |this|'s internal {{[[rootMargin]]}} slot to that.
289+
Otherwise, <a>throw</a> a {{SyntaxError}} exception.
283290
: <dfn>thresholds</dfn>
284291
::
285-
A list of thresholds, sorted in increasing numeric order,
292+
On getting, returns {{[[thresholds]]}}, which is a list of thresholds,
293+
sorted in increasing numeric order,
286294
where each threshold is a ratio of intersection area to bounding box area
287295
of an observed target. Notifications for a target are generated when any
288296
of the thresholds are crossed for that target.
289297
If no |options|.{{IntersectionObserverInit/threshold}} was provided to the
290-
{{IntersectionObserver}} constructor, the value of this attribute will be [0].
298+
{{IntersectionObserver}} constructor,
299+
and the {{IntersectionObserver/thresholds}} setter has not been invoked,
300+
the value of this attribute will be [0].
301+
302+
On setting, let |sortedThresholds| be the result of
303+
<a>validate and sort a thresholds list</a> for
304+
|options|.{{IntersectionObserverInit/threshold}}.
305+
If |sortedThresholds| is failure, then <a>throw</a> a {{RangeError}} exception.
306+
Otherwise, set |this|'s internal {{[[thresholds]]}} slot to |sortedThresholds|.
291307
</div>
292308

293309
The <dfn for=IntersectionObserver>root intersection rectangle</dfn>
@@ -351,6 +367,15 @@ or failure:
351367
append a duplicate of the second element to |tokens|.
352368
7. Return |tokens|.
353369

370+
To <dfn>validate and sort a thresholds list</dfn> from a list |thresholds|,
371+
returning either a list or failure:
372+
373+
1. If any value in |thresholds| is less than 0.0 or greater than
374+
1.0, then return failure.
375+
2. Let |sortedThresholds| be the result of <a for="list" lt="sort">sorting |thresholds| in ascending order</a>.
376+
3. If |sortedThresholds| is empty, then append <code>0</code> to |sortedThresholds|.
377+
4. Return |sortedThresholds|.
378+
354379
<h3 id="intersection-observer-entry">
355380
The IntersectionObserverEntry interface</h3>
356381

@@ -495,7 +520,7 @@ which is initialized to an empty list.
495520
This list holds <dfn interface>IntersectionObserverRegistration</dfn> records,
496521
which have an <dfn attribute for=IntersectionObserverRegistration>observer</dfn> property
497522
holding an {{IntersectionObserver}}, a <dfn attribute for=IntersectionObserverRegistration>previousThresholdIndex</dfn> property
498-
holding a number between -1 and the length of the observer's {{IntersectionObserver/thresholds}} property (inclusive), and
523+
holding a number between -1 and the length of the observer's internal {{[[thresholds]]}} slot (inclusive), and
499524
a <dfn attribute for=IntersectionObserverRegistration>previousIsIntersecting</dfn> property holding a boolean.
500525

501526
<h4 id='intersection-observer-private-slots'>
@@ -508,7 +533,9 @@ which are initialized to empty lists and an internal
508533
<dfn attribute for=IntersectionObserver>\[[callback]]</dfn> slot
509534
which is initialized by {{IntersectionObserver(callback, options)}}</a>.
510535
They also have an internal <dfn attribute for=IntersectionObserver>\[[rootMargin]]</dfn> slot
511-
which is a list of four pixel lengths or percentages.
536+
which is a list of four pixel lengths or percentages, and
537+
an internal <dfn attribute for=IntersectionObserver>\[[thresholds]]</dfn> slot
538+
which is a sorted list of thresholds.
512539

513540
<h3 id='algorithms'>
514541
Algorithms</h2>
@@ -525,15 +552,12 @@ and an {{IntersectionObserverInit}} dictionary |options|, run these steps:
525552
If a list is returned,
526553
set |this|'s internal {{[[rootMargin]]}} slot to that.
527554
Otherwise, <a>throw</a> a {{SyntaxError}} exception.
528-
4. Let |thresholds| be a list equal to
555+
4. Let |sortedThresholds| be the result of
556+
<a>validate and sort a thresholds list</a> for
529557
|options|.{{IntersectionObserverInit/threshold}}.
530-
5. If any value in |thresholds| is less than 0.0 or greater than
531-
1.0, <a>throw</a> a {{RangeError}} exception.
532-
6. Sort |thresholds| in ascending order.
533-
7. If |thresholds| is empty, append <code>0</code> to |thresholds|.
534-
8. The {{IntersectionObserver/thresholds}} attribute getter will return
535-
this sorted |thresholds| list.
536-
9. Return |this|.
558+
5. If |sortedThresholds| is failure, then <a>throw</a> a {{RangeError}} exception.
559+
6. Set |this|'s internal {{[[thresholds]]}} slot to |sortedThresholds|.
560+
7. Return |this|.
537561

538562
<h4 id='observe-target-element'>Observe a target Element</h4>
539563

@@ -672,7 +696,7 @@ To <dfn>run the update intersection observations steps</dfn> for a
672696
zero area).
673697
9. If |targetArea| is non-zero, let |intersectionRatio| be |intersectionArea| divided by |targetArea|.<br>
674698
Otherwise, let |intersectionRatio| be <code>1</code> if |isIntersecting| is true, or <code>0</code> if |isIntersecting| is false.
675-
10. Set |thresholdIndex| to the index of the first entry in |observer|.{{thresholds}} whose value is greater than |intersectionRatio|, or the length of |observer|.{{thresholds}} if |intersectionRatio| is greater than or equal to the last entry in |observer|.{{thresholds}}.
699+
10. Set |thresholdIndex| to the index of the first entry in |observer|'s internal {{[[thresholds]]}} slot whose value is greater than |intersectionRatio|, or the length of |observer|'s internal {{[[thresholds]]}} slot if |intersectionRatio| is greater than or equal to the last entry in |observer|'s internal {{[[thresholds]]}} slot.
676700
11. Let |intersectionObserverRegistration| be the {{IntersectionObserverRegistration}} record
677701
in |target|'s internal {{[[RegisteredIntersectionObservers]]}} slot
678702
whose {{IntersectionObserverRegistration/observer}} property is equal to |observer|.

0 commit comments

Comments
 (0)