[IntersectionObserver] Validate threshold values
authorajuma@chromium.org <ajuma@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 13 Aug 2018 22:02:45 +0000 (22:02 +0000)
committerajuma@chromium.org <ajuma@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 13 Aug 2018 22:02:45 +0000 (22:02 +0000)
https://bugs.webkit.org/show_bug.cgi?id=188475

Reviewed by Simon Fraser.

LayoutTests/imported/w3c:

Update expectation for newly passing test case.

* web-platform-tests/intersection-observer/observer-exceptions-expected.txt:
* web-platform-tests/intersection-observer/observer-exceptions.html: Fix typo already fixed upstream.

Source/WebCore:

Throw an exception if any of an IntersectionObserver's thresholds are outside
the range [0, 1].

Tested by: imported/w3c/web-platform-tests/intersection-observer/observer-exceptions.html
           intersection-observer/intersection-observer-interface.html

* page/IntersectionObserver.cpp:
(WebCore::IntersectionObserver::create):
(WebCore::IntersectionObserver::IntersectionObserver):
* page/IntersectionObserver.h:

LayoutTests:

Add test coverage for interesting floating point threshold values.

* intersection-observer/intersection-observer-interface-expected.txt:
* intersection-observer/intersection-observer-interface.html:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@234818 268f45cc-cd09-0410-ab3c-d52691b4dbfc

LayoutTests/ChangeLog
LayoutTests/imported/w3c/ChangeLog
LayoutTests/imported/w3c/web-platform-tests/intersection-observer/observer-exceptions-expected.txt
LayoutTests/imported/w3c/web-platform-tests/intersection-observer/observer-exceptions.html
LayoutTests/intersection-observer/intersection-observer-interface-expected.txt
LayoutTests/intersection-observer/intersection-observer-interface.html
Source/WebCore/ChangeLog
Source/WebCore/page/IntersectionObserver.cpp
Source/WebCore/page/IntersectionObserver.h

index 3265b09..f380387 100644 (file)
@@ -1,3 +1,15 @@
+2018-08-13  Ali Juma  <ajuma@chromium.org>
+
+        [IntersectionObserver] Validate threshold values
+        https://bugs.webkit.org/show_bug.cgi?id=188475
+
+        Reviewed by Simon Fraser.
+
+        Add test coverage for interesting floating point threshold values.
+
+        * intersection-observer/intersection-observer-interface-expected.txt:
+        * intersection-observer/intersection-observer-interface.html:
+
 2018-08-13  Ryan Haddad  <ryanhaddad@apple.com>
 
         [iOS] Rebaseline and re-enable svg tests
index d12365a..ad581c3 100644 (file)
@@ -1,3 +1,15 @@
+2018-08-13  Ali Juma  <ajuma@chromium.org>
+
+        [IntersectionObserver] Validate threshold values
+        https://bugs.webkit.org/show_bug.cgi?id=188475
+
+        Reviewed by Simon Fraser.
+
+        Update expectation for newly passing test case.
+
+        * web-platform-tests/intersection-observer/observer-exceptions-expected.txt:
+        * web-platform-tests/intersection-observer/observer-exceptions.html: Fix typo already fixed upstream.
+
 2018-08-13  Yusuke Suzuki  <yusukesuzuki@slowstart.org>
 
         Expose CloseEvent and CustomEvent to workers
index 69880cc..4a05818 100644 (file)
@@ -1,9 +1,7 @@
 
-FAIL IntersectionObserver constructor with { threshold: [1.1] } assert_throws: function "function () {
-    new IntersectionObserver(e => {}, {threshold: [1.1]})
-  }" did not throw
+PASS IntersectionObserver constructor with { threshold: [1.1] } 
 PASS IntersectionObserver constructor with { threshold: ["foo"] } 
-PASS IntersectionObserver constructor witth { rootMargin: "1" } 
+PASS IntersectionObserver constructor with { rootMargin: "1" } 
 PASS IntersectionObserver constructor with { rootMargin: "2em" } 
 PASS IntersectionObserver constructor with { rootMargin: "auto" } 
 PASS IntersectionObserver constructor with { rootMargin: "calc(1px + 2px)" } 
index d4f178b..5d29234 100644 (file)
@@ -19,7 +19,7 @@ test(function () {
   assert_throws("SYNTAX_ERR", function() {
     new IntersectionObserver(e => {}, {rootMargin: "1"})
   })
-}, 'IntersectionObserver constructor witth { rootMargin: "1" }');
+}, 'IntersectionObserver constructor with { rootMargin: "1" }');
 
 test(function () {
   assert_throws("SYNTAX_ERR", function() {
index 16b6150..60690dd 100644 (file)
@@ -10,4 +10,11 @@ PASS ExplicitFourArgumentRootMargin
 PASS ExplicitRoot 
 PASS ExplicitThreshold 
 PASS ExplicitThresholds 
+PASS SmallPositiveThreshold 
+PASS SmallNegativeThreshold 
+PASS LargePositiveThreshold 
+PASS LargeNegativeThreshold 
+PASS PositiveInfinityThreshold 
+PASS NegativeInfinityThreshold 
+PASS NaNThreshold 
 
index 39fa59e..90d24ff 100644 (file)
         var observer = new IntersectionObserver(function() {},  { threshold: [0, 0.33333678, 0.5, 0.76645] });
         assert_array_equals(observer.thresholds, [0, 0.33333678, 0.5, 0.76645]);
     },'ExplicitThresholds');
+    test(function() {
+        var observer = new IntersectionObserver(function() {},  { threshold: Number.MIN_VALUE });
+        assert_array_equals(observer.thresholds, [Number.MIN_VALUE]);
+    },'SmallPositiveThreshold');
+    test(function() {
+        assert_throws(RangeError(), function() {
+            new IntersectionObserver(function() {},  { threshold: -Number.MIN_VALUE });
+        })
+    },'SmallNegativeThreshold');
+    test(function() {
+        assert_throws(RangeError(), function() {
+            new IntersectionObserver(function() {},  { threshold: Number.MAX_VALUE });
+        })
+    },'LargePositiveThreshold');
+    test(function() {
+        assert_throws(RangeError(), function() {
+            new IntersectionObserver(function() {},  { threshold: -Number.MAX_VALUE });
+        })
+    },'LargeNegativeThreshold');
+    test(function() {
+        assert_throws(TypeError(), function() {
+            new IntersectionObserver(function() {},  { threshold: Number.POSITIVE_INFINITY });
+        })
+    },'PositiveInfinityThreshold');
+    test(function() {
+        assert_throws(TypeError(), function() {
+            new IntersectionObserver(function() {},  { threshold: Number.NEGATIVE_INFINITY });
+        })
+    },'NegativeInfinityThreshold');
+    test(function() {
+        assert_throws(TypeError(), function() {
+            new IntersectionObserver(function() {},  { threshold: Number.NaN });
+        })
+    },'NaNThreshold');
 </script>
 </body>
 </html>
index ca10e17..069efc5 100644 (file)
@@ -1,3 +1,21 @@
+2018-08-13  Ali Juma  <ajuma@chromium.org>
+
+        [IntersectionObserver] Validate threshold values
+        https://bugs.webkit.org/show_bug.cgi?id=188475
+
+        Reviewed by Simon Fraser.
+
+        Throw an exception if any of an IntersectionObserver's thresholds are outside
+        the range [0, 1].
+
+        Tested by: imported/w3c/web-platform-tests/intersection-observer/observer-exceptions.html
+                   intersection-observer/intersection-observer-interface.html
+
+        * page/IntersectionObserver.cpp:
+        (WebCore::IntersectionObserver::create):
+        (WebCore::IntersectionObserver::IntersectionObserver):
+        * page/IntersectionObserver.h:
+
 2018-08-13  Alex Christensen  <achristensen@webkit.org>
 
         Remove unused CSSSelector::parseNth
index fa7a2cb..604dbcc 100644 (file)
@@ -87,18 +87,28 @@ ExceptionOr<Ref<IntersectionObserver>> IntersectionObserver::create(Ref<Intersec
     if (rootMarginOrException.hasException())
         return rootMarginOrException.releaseException();
 
-    return adoptRef(*new IntersectionObserver(WTFMove(callback), WTFMove(init), rootMarginOrException.releaseReturnValue()));
+    Vector<double> thresholds;
+    WTF::switchOn(init.threshold, [&thresholds] (double initThreshold) {
+        thresholds.reserveInitialCapacity(1);
+        thresholds.uncheckedAppend(initThreshold);
+    }, [&thresholds] (Vector<double>& initThresholds) {
+        thresholds = WTFMove(initThresholds);
+    });
+
+    for (auto threshold : thresholds) {
+        if (!(threshold >= 0 && threshold <= 1))
+            return Exception { RangeError, "Failed to construct 'IntersectionObserver': all thresholds must lie in the range [0.0, 1.0]." };
+    }
+
+    return adoptRef(*new IntersectionObserver(WTFMove(callback), WTFMove(init.root), rootMarginOrException.releaseReturnValue(), WTFMove(thresholds)));
 }
 
-IntersectionObserver::IntersectionObserver(Ref<IntersectionObserverCallback>&& callback, Init&& init, LengthBox&& parsedRootMargin)
-    : m_root(init.root)
+IntersectionObserver::IntersectionObserver(Ref<IntersectionObserverCallback>&& callback, RefPtr<Element>&& root, LengthBox&& parsedRootMargin, Vector<double>&& thresholds)
+    : m_root(WTFMove(root))
     , m_rootMargin(WTFMove(parsedRootMargin))
+    , m_thresholds(WTFMove(thresholds))
     , m_callback(WTFMove(callback))
 {
-    if (WTF::holds_alternative<double>(init.threshold))
-        m_thresholds.append(WTF::get<double>(init.threshold));
-    else
-        m_thresholds = WTF::get<Vector<double>>(WTFMove(init.threshold));
 }
 
 String IntersectionObserver::rootMargin() const
index 1d3f012..b04cfb2 100644 (file)
@@ -59,7 +59,7 @@ public:
     Vector<RefPtr<IntersectionObserverEntry>> takeRecords();
 
 private:
-    IntersectionObserver(Ref<IntersectionObserverCallback>&&, Init&&, LengthBox&& parsedRootMargin);
+    IntersectionObserver(Ref<IntersectionObserverCallback>&&, RefPtr<Element>&& root, LengthBox&& parsedRootMargin, Vector<double>&& thresholds);
     
     RefPtr<Element> m_root;
     LengthBox m_rootMargin;