Changeset 234761 in webkit


Ignore:
Timestamp:
Aug 10, 2018 10:08:44 AM (6 years ago)
Author:
ajuma@chromium.org
Message:

[IntersectionObserver] Implement rootMargin parsing
https://bugs.webkit.org/show_bug.cgi?id=188469

Reviewed by Darin Adler.

LayoutTests/imported/w3c:

Update expectations for newly passing test cases.

  • web-platform-tests/intersection-observer/observer-attributes-expected.txt:
  • web-platform-tests/intersection-observer/observer-exceptions-expected.txt:

Source/WebCore:

Parse IntersectionObserver's rootMargin argument and throw an exception if it's invalid.
Change the stored rootMargin from a String to a LengthBox so that future patches can
use this value in intersection logic.

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

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

  • page/IntersectionObserver.cpp:

(WebCore::parseRootMargin):
(WebCore::IntersectionObserver::create):
(WebCore::IntersectionObserver::IntersectionObserver):
(WebCore::IntersectionObserver::rootMargin const):

  • page/IntersectionObserver.h:

(WebCore::IntersectionObserver::create): Deleted.
(WebCore::IntersectionObserver::rootMargin const): Deleted.

  • page/IntersectionObserver.idl:

LayoutTests:

  • intersection-observer/intersection-observer-interface-expected.txt:
  • intersection-observer/intersection-observer-interface.html:
Location:
trunk
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r234756 r234761  
     12018-08-10  Ali Juma  <ajuma@chromium.org>
     2
     3        [IntersectionObserver] Implement rootMargin parsing
     4        https://bugs.webkit.org/show_bug.cgi?id=188469
     5
     6        Reviewed by Darin Adler.
     7
     8        * intersection-observer/intersection-observer-interface-expected.txt:
     9        * intersection-observer/intersection-observer-interface.html:
     10
    1112018-08-10  Joseph Pecoraro  <pecoraro@apple.com>
    212
  • trunk/LayoutTests/imported/w3c/ChangeLog

    r234723 r234761  
     12018-08-10  Ali Juma  <ajuma@chromium.org>
     2
     3        [IntersectionObserver] Implement rootMargin parsing
     4        https://bugs.webkit.org/show_bug.cgi?id=188469
     5
     6        Reviewed by Darin Adler.
     7
     8        Update expectations for newly passing test cases.
     9
     10        * web-platform-tests/intersection-observer/observer-attributes-expected.txt:
     11        * web-platform-tests/intersection-observer/observer-exceptions-expected.txt:
     12
    1132018-08-09  Ali Juma  <ajuma@chromium.org>
    214
  • trunk/LayoutTests/imported/w3c/web-platform-tests/intersection-observer/observer-attributes-expected.txt

    r234723 r234761  
    33PASS observer.root
    44PASS observer.thresholds
    5 FAIL observer.rootMargin assert_equals: expected "0px 0px 0px 0px" but got "0px"
     5PASS observer.rootMargin
    66PASS set observer.root
    77PASS set observer.thresholds
    8 FAIL set observer.rootMargin assert_equals: expected "10% 20px 10% 20px" but got "10% 20px"
     8PASS set observer.rootMargin
    99
  • trunk/LayoutTests/imported/w3c/web-platform-tests/intersection-observer/observer-exceptions-expected.txt

    r234723 r234761  
    44  }" did not throw
    55PASS IntersectionObserver constructor with { threshold: ["foo"] }
    6 FAIL IntersectionObserver constructor witth { rootMargin: "1" } assert_throws: function "function () {
    7     new IntersectionObserver(e => {}, {rootMargin: "1"})
    8   }" did not throw
    9 FAIL IntersectionObserver constructor with { rootMargin: "2em" } assert_throws: function "function () {
    10     new IntersectionObserver(e => {}, {rootMargin: "2em"})
    11   }" did not throw
    12 FAIL IntersectionObserver constructor with { rootMargin: "auto" } assert_throws: function "function () {
    13     new IntersectionObserver(e => {}, {rootMargin: "auto"})
    14   }" did not throw
    15 FAIL IntersectionObserver constructor with { rootMargin: "calc(1px + 2px)" } assert_throws: function "function () {
    16     new IntersectionObserver(e => {}, {rootMargin: "calc(1px + 2px)"})
    17   }" did not throw
    18 FAIL IntersectionObserver constructor with { rootMargin: "1px !important" } assert_throws: function "function () {
    19     new IntersectionObserver(e => {}, {rootMargin: "1px !important"})
    20   }" did not throw
    21 FAIL IntersectionObserver constructor with { rootMargin: "1px 1px 1px 1px 1px" } assert_throws: function "function () {
    22     new IntersectionObserver(e => {}, {rootMargin: "1px 1px 1px 1px 1px"})
    23   }" did not throw
     6PASS IntersectionObserver constructor witth { rootMargin: "1" }
     7PASS IntersectionObserver constructor with { rootMargin: "2em" }
     8PASS IntersectionObserver constructor with { rootMargin: "auto" }
     9PASS IntersectionObserver constructor with { rootMargin: "calc(1px + 2px)" }
     10PASS IntersectionObserver constructor with { rootMargin: "1px !important" }
     11PASS IntersectionObserver constructor with { rootMargin: "1px 1px 1px 1px 1px" }
    2412PASS IntersectionObserver.observe("foo")
    2513
  • trunk/LayoutTests/intersection-observer/intersection-observer-interface-expected.txt

    r208181 r234761  
    44PASS DefaultRoot
    55PASS DefaultThresholds
    6 PASS ExplicitRootMargin
     6PASS ExplicitOneArgumentRootMargin
     7PASS ExplicitTwoArgumentRootMargin
     8PASS ExplicitThreeArgumentRootMargin
     9PASS ExplicitFourArgumentRootMargin
    710PASS ExplicitRoot
    811PASS ExplicitThreshold
  • trunk/LayoutTests/intersection-observer/intersection-observer-interface.html

    r208983 r234761  
    1616    test(function() {
    1717        var observer = new IntersectionObserver(function() {});
    18         assert_equals(observer.rootMargin, '0px');
     18        assert_equals(observer.rootMargin, '0px 0px 0px 0px');
    1919    },'DefaultRootMargin');
    2020    test(function() {
     
    2727    },'DefaultThresholds');
    2828    test(function() {
    29         var observer = new IntersectionObserver(function() {}, { rootMargin: '33em 10px -120px 3pt' });
    30         assert_equals(observer.rootMargin, '33em 10px -120px 3pt');
    31     },'ExplicitRootMargin');
     29        var observer = new IntersectionObserver(function() {}, { rootMargin: '33%' });
     30        assert_equals(observer.rootMargin, '33% 33% 33% 33%');
     31    },'ExplicitOneArgumentRootMargin');
     32    test(function() {
     33        var observer = new IntersectionObserver(function() {}, { rootMargin: '33% 10px' });
     34        assert_equals(observer.rootMargin, '33% 10px 33% 10px');
     35    },'ExplicitTwoArgumentRootMargin');
     36    test(function() {
     37        var observer = new IntersectionObserver(function() {}, { rootMargin: '33% 10px -120px' });
     38        assert_equals(observer.rootMargin, '33% 10px -120px 10px');
     39    },'ExplicitThreeArgumentRootMargin');
     40    test(function() {
     41        var observer = new IntersectionObserver(function() {}, { rootMargin: '33% 10px -120px 3%' });
     42        assert_equals(observer.rootMargin, '33% 10px -120px 3%');
     43    },'ExplicitFourArgumentRootMargin');
    3244    test(function() {
    3345        var observer = new IntersectionObserver(function() {}, { root: document.body });
  • trunk/Source/WebCore/ChangeLog

    r234747 r234761  
     12018-08-10  Ali Juma  <ajuma@chromium.org>
     2
     3        [IntersectionObserver] Implement rootMargin parsing
     4        https://bugs.webkit.org/show_bug.cgi?id=188469
     5
     6        Reviewed by Darin Adler.
     7
     8        Parse IntersectionObserver's rootMargin argument and throw an exception if it's invalid.
     9        Change the stored rootMargin from a String to a LengthBox so that future patches can
     10        use this value in intersection logic.
     11
     12        Tested by: imported/w3c/web-platform-tests/intersection-observer/observer-attributes.html
     13                   imported/w3c/web-platform-tests/intersection-observer/observer-exceptions.html
     14                   intersection-observer/intersection-observer-interface.html
     15
     16        * page/IntersectionObserver.cpp:
     17        (WebCore::parseRootMargin):
     18        (WebCore::IntersectionObserver::create):
     19        (WebCore::IntersectionObserver::IntersectionObserver):
     20        (WebCore::IntersectionObserver::rootMargin const):
     21        * page/IntersectionObserver.h:
     22        (WebCore::IntersectionObserver::create): Deleted.
     23        (WebCore::IntersectionObserver::rootMargin const): Deleted.
     24        * page/IntersectionObserver.idl:
     25
    1262018-08-09  Ben Richards  <benton_richards@apple.com>
    227
  • trunk/Source/WebCore/page/IntersectionObserver.cpp

    r208181 r234761  
    2929#include "IntersectionObserver.h"
    3030
     31#include "CSSParserTokenRange.h"
     32#include "CSSPropertyParserHelpers.h"
     33#include "CSSTokenizer.h"
    3134#include "Element.h"
    3235#include "IntersectionObserverCallback.h"
     
    3639namespace WebCore {
    3740
    38 IntersectionObserver::IntersectionObserver(Ref<IntersectionObserverCallback>&& callback, Init&& init)
     41static ExceptionOr<LengthBox> parseRootMargin(String& rootMargin)
     42{
     43    CSSTokenizer tokenizer(rootMargin);
     44    auto tokenRange = tokenizer.tokenRange();
     45    Vector<Length, 4> margins;
     46    while (!tokenRange.atEnd()) {
     47        if (margins.size() == 4)
     48            return Exception { SyntaxError, "Failed to construct 'IntersectionObserver': Extra text found at the end of rootMargin." };
     49        RefPtr<CSSPrimitiveValue> parsedValue = CSSPropertyParserHelpers::consumeLengthOrPercent(tokenRange, HTMLStandardMode, ValueRangeAll);
     50        if (!parsedValue || parsedValue->isCalculated())
     51            return Exception { SyntaxError, "Failed to construct 'IntersectionObserver': rootMargin must be specified in pixels or percent." };
     52        if (parsedValue->isPercentage())
     53            margins.append(Length(parsedValue->doubleValue(), Percent));
     54        else if (parsedValue->isPx())
     55            margins.append(Length(parsedValue->intValue(), Fixed));
     56        else
     57            return Exception { SyntaxError, "Failed to construct 'IntersectionObserver': rootMargin must be specified in pixels or percent." };
     58    }
     59    switch (margins.size()) {
     60    case 0:
     61        for (unsigned i = 0; i < 4; ++i)
     62            margins.append(Length());
     63        break;
     64    case 1:
     65        for (unsigned i = 0; i < 3; ++i)
     66            margins.append(margins[0]);
     67        break;
     68    case 2:
     69        margins.append(margins[0]);
     70        margins.append(margins[1]);
     71        break;
     72    case 3:
     73        margins.append(margins[1]);
     74        break;
     75    case 4:
     76        break;
     77    default:
     78        ASSERT_NOT_REACHED();
     79    }
     80
     81    return LengthBox(WTFMove(margins[0]), WTFMove(margins[1]), WTFMove(margins[2]), WTFMove(margins[3]));
     82}
     83
     84ExceptionOr<Ref<IntersectionObserver>> IntersectionObserver::create(Ref<IntersectionObserverCallback>&& callback, IntersectionObserver::Init&& init)
     85{
     86    auto rootMarginOrException = parseRootMargin(init.rootMargin);
     87    if (rootMarginOrException.hasException())
     88        return rootMarginOrException.releaseException();
     89
     90    return adoptRef(*new IntersectionObserver(WTFMove(callback), WTFMove(init), rootMarginOrException.releaseReturnValue()));
     91}
     92
     93IntersectionObserver::IntersectionObserver(Ref<IntersectionObserverCallback>&& callback, Init&& init, LengthBox&& parsedRootMargin)
    3994    : m_root(init.root)
    40     , m_rootMargin(WTFMove(init.rootMargin))
     95    , m_rootMargin(WTFMove(parsedRootMargin))
    4196    , m_callback(WTFMove(callback))
    4297{
     
    45100    else
    46101        m_thresholds = WTF::get<Vector<double>>(WTFMove(init.threshold));
     102}
     103
     104String IntersectionObserver::rootMargin() const
     105{
     106    StringBuilder stringBuilder;
     107    PhysicalBoxSide sides[4] = { PhysicalBoxSide::Top, PhysicalBoxSide::Right, PhysicalBoxSide::Bottom, PhysicalBoxSide::Left };
     108    for (auto side : sides) {
     109        auto& length = m_rootMargin.at(side);
     110        stringBuilder.appendNumber(length.intValue());
     111        if (length.type() == Percent)
     112            stringBuilder.append('%');
     113        else
     114            stringBuilder.append("px", 2);
     115        if (side != PhysicalBoxSide::Left)
     116            stringBuilder.append(' ');
     117    }
     118    return stringBuilder.toString();
    47119}
    48120
  • trunk/Source/WebCore/page/IntersectionObserver.h

    r211406 r234761  
    3030#include "IntersectionObserverCallback.h"
    3131#include "IntersectionObserverEntry.h"
     32#include "LengthBox.h"
    3233#include <wtf/RefCounted.h>
    3334#include <wtf/Variant.h>
     
    4647    };
    4748
    48     static Ref<IntersectionObserver> create(Ref<IntersectionObserverCallback>&& callback, Init&& init)
    49     {
    50         return adoptRef(*new IntersectionObserver(WTFMove(callback), WTFMove(init)));
    51     }
     49    static ExceptionOr<Ref<IntersectionObserver>> create(Ref<IntersectionObserverCallback>&&, Init&&);
    5250   
    5351    Element* root() const { return m_root.get(); }
    54     String rootMargin() const { return m_rootMargin; }
     52    String rootMargin() const;
    5553    const Vector<double>& thresholds() const { return m_thresholds; }
    5654
     
    6260
    6361private:
    64     IntersectionObserver(Ref<IntersectionObserverCallback>&&, Init&&);
     62    IntersectionObserver(Ref<IntersectionObserverCallback>&&, Init&&, LengthBox&& parsedRootMargin);
    6563   
    6664    RefPtr<Element> m_root;
    67     String m_rootMargin;
     65    LengthBox m_rootMargin;
    6866    Vector<double> m_thresholds;
    6967    Ref<IntersectionObserverCallback> m_callback;
  • trunk/Source/WebCore/page/IntersectionObserver.idl

    r208983 r234761  
    2828[
    2929    Conditional=INTERSECTION_OBSERVER,
     30    ConstructorMayThrowException,
    3031    Constructor(IntersectionObserverCallback callback, optional IntersectionObserverInit options),
    3132    ImplementationLacksVTable,
Note: See TracChangeset for help on using the changeset viewer.