Changeset 234596 in webkit


Ignore:
Timestamp:
Aug 6, 2018 2:56:08 AM (6 years ago)
Author:
commit-queue@webkit.org
Message:

Make two-arguments versions of scrollBy/scrollTo depend on the one-argument versions
https://bugs.webkit.org/show_bug.cgi?id=188300

Patch by Frederic Wang <fwang@igalia.com> on 2018-08-06
Reviewed by Darin Adler.

This patch refactors a bit the scrollBy/scrollTo code, so that the two-arguments versions
share the same code path as the more generic one-argument versions. In particular, this
helps to implement the ScrollBehavior option (bug 188043) since the one-argument versions
will require to distinguish between smooth and instant scrolling. The logic to normalize
non finite left/right values or to use a fallback when they are absent is also factored out
into ScrollToOptions.

References:
https://drafts.csswg.org/cssom-view/#dom-element-scroll
https://drafts.csswg.org/cssom-view/#dom-element-scrollby
https://drafts.csswg.org/cssom-view/#dom-window-scroll
https://drafts.csswg.org/cssom-view/#dom-window-scrollby

No new tests, behavior is unchanged.

  • dom/Element.cpp:

(WebCore::Element::scrollBy): Make two-parameter version depends on one-parameter version
and rewrite the normalize / fallback logic.
(WebCore::Element::scrollTo): Rewrite the normalize / fallback logic.
(WebCore::normalizeNonFiniteValue): Deleted. The logic is moved to ScrollToOptions.

  • page/DOMWindow.cpp:

(WebCore::DOMWindow::scrollBy const): Make two-parameter version depends on one-parameter
version and rewrite the normalize / fallback logic.
(WebCore::DOMWindow::scrollTo const): Make two-parameter version depends on one-parameter
version and rewrite the normalize / fallback logic.

  • page/ScrollToOptions.h: Add <cmath> to use std::isfinite

(WebCore::ScrollToOptions::normalizeNonFiniteCoordinatesOrFallBackTo): New function to
normalize left/right values or fallback to the specified value if it is missing.

Location:
trunk/Source/WebCore
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r234595 r234596  
     12018-08-06  Frederic Wang  <fwang@igalia.com>
     2
     3        Make two-arguments versions of scrollBy/scrollTo depend on the one-argument versions
     4        https://bugs.webkit.org/show_bug.cgi?id=188300
     5
     6        Reviewed by Darin Adler.
     7
     8        This patch refactors a bit the scrollBy/scrollTo code, so that the two-arguments versions
     9        share the same code path as the more generic one-argument versions. In particular, this
     10        helps to implement the ScrollBehavior option (bug 188043) since the one-argument versions
     11        will require to distinguish between smooth and instant scrolling. The logic to normalize
     12        non finite left/right values or to use a fallback when they are absent is also factored out
     13        into ScrollToOptions.
     14
     15        References:
     16        https://drafts.csswg.org/cssom-view/#dom-element-scroll
     17        https://drafts.csswg.org/cssom-view/#dom-element-scrollby
     18        https://drafts.csswg.org/cssom-view/#dom-window-scroll
     19        https://drafts.csswg.org/cssom-view/#dom-window-scrollby
     20
     21        No new tests, behavior is unchanged.
     22
     23        * dom/Element.cpp:
     24        (WebCore::Element::scrollBy): Make two-parameter version depends on one-parameter version
     25        and rewrite the normalize / fallback logic.
     26        (WebCore::Element::scrollTo): Rewrite the normalize / fallback logic.
     27        (WebCore::normalizeNonFiniteValue): Deleted. The logic is moved to ScrollToOptions.
     28        * page/DOMWindow.cpp:
     29        (WebCore::DOMWindow::scrollBy const): Make two-parameter version depends on one-parameter
     30        version and rewrite the normalize / fallback logic.
     31        (WebCore::DOMWindow::scrollTo const): Make two-parameter version depends on one-parameter
     32        version and rewrite the normalize / fallback logic.
     33        * page/ScrollToOptions.h: Add <cmath> to use std::isfinite
     34        (WebCore::ScrollToOptions::normalizeNonFiniteCoordinatesOrFallBackTo): New function to
     35        normalize left/right values or fallback to the specified value if it is missing.
     36
    1372018-08-06  Zan Dobersek  <zdobersek@igalia.com>
    238
  • trunk/Source/WebCore/dom/Element.cpp

    r234577 r234596  
    693693void Element::scrollBy(const ScrollToOptions& options)
    694694{
    695     return scrollBy(options.left.value_or(0), options.top.value_or(0));
    696 }
    697 
    698 static inline double normalizeNonFiniteValue(double f)
    699 {
    700     return std::isfinite(f) ? f : 0;
     695    ScrollToOptions scrollToOptions = normalizeNonFiniteCoordinatesOrFallBackTo(options, 0, 0);
     696    scrollToOptions.left.value() += scrollLeft();
     697    scrollToOptions.top.value() += scrollTop();
     698    scrollTo(scrollToOptions);
    701699}
    702700
    703701void Element::scrollBy(double x, double y)
    704702{
    705     scrollTo(scrollLeft() + normalizeNonFiniteValue(x), scrollTop() + normalizeNonFiniteValue(y));
     703    scrollBy({ x, y });
    706704}
    707705
     
    721719        return;
    722720
    723     // Normalize non-finite values for left and top dictionary members of options, if present.
    724     double x = options.left ? normalizeNonFiniteValue(options.left.value()) : adjustForAbsoluteZoom(renderer->scrollLeft(), *renderer);
    725     double y = options.top ? normalizeNonFiniteValue(options.top.value()) : adjustForAbsoluteZoom(renderer->scrollTop(), *renderer);
    726 
    727     renderer->setScrollLeft(clampToInteger(x * renderer->style().effectiveZoom()), clamping);
    728     renderer->setScrollTop(clampToInteger(y * renderer->style().effectiveZoom()), clamping);
     721    ScrollToOptions scrollToOptions = normalizeNonFiniteCoordinatesOrFallBackTo(options,
     722        adjustForAbsoluteZoom(renderer->scrollLeft(), *renderer),
     723        adjustForAbsoluteZoom(renderer->scrollTop(), *renderer)
     724    );
     725    renderer->setScrollLeft(clampToInteger(scrollToOptions.left.value() * renderer->style().effectiveZoom()), clamping);
     726    renderer->setScrollTop(clampToInteger(scrollToOptions.top.value() * renderer->style().effectiveZoom()), clamping);
    729727}
    730728
  • trunk/Source/WebCore/page/DOMWindow.cpp

    r234586 r234596  
    15751575}
    15761576
     1577void DOMWindow::scrollBy(double x, double y) const
     1578{
     1579    scrollBy({ x, y });
     1580}
     1581
    15771582void DOMWindow::scrollBy(const ScrollToOptions& options) const
    1578 {
    1579     return scrollBy(options.left.value_or(0), options.top.value_or(0));
    1580 }
    1581 
    1582 void DOMWindow::scrollBy(double x, double y) const
    15831583{
    15841584    if (!isCurrentlyDisplayedInFrame())
     
    15911591        return;
    15921592
    1593     // Normalize non-finite values (https://drafts.csswg.org/cssom-view/#normalize-non-finite-values).
    1594     x = std::isfinite(x) ? x : 0;
    1595     y = std::isfinite(y) ? y : 0;
    1596 
    1597     IntSize scaledOffset(view->mapFromCSSToLayoutUnits(x), view->mapFromCSSToLayoutUnits(y));
     1593    ScrollToOptions scrollToOptions = normalizeNonFiniteCoordinatesOrFallBackTo(options, 0, 0);
     1594    IntSize scaledOffset(view->mapFromCSSToLayoutUnits(scrollToOptions.left.value()), view->mapFromCSSToLayoutUnits(scrollToOptions.top.value()));
    15981595    view->setContentsScrollPosition(view->contentsScrollPosition() + scaledOffset);
    15991596}
    16001597
    1601 void DOMWindow::scrollTo(const ScrollToOptions& options, ScrollClamping clamping) const
     1598void DOMWindow::scrollTo(double x, double y, ScrollClamping clamping) const
     1599{
     1600    scrollTo({ x, y }, clamping);
     1601}
     1602
     1603void DOMWindow::scrollTo(const ScrollToOptions& options, ScrollClamping) const
    16021604{
    16031605    if (!isCurrentlyDisplayedInFrame())
     
    16081610        return;
    16091611
    1610     double x = options.left ? options.left.value() : view->contentsScrollPosition().x();
    1611     double y = options.top ? options.top.value() : view->contentsScrollPosition().y();
    1612     return scrollTo(x, y, clamping);
    1613 }
    1614 
    1615 void DOMWindow::scrollTo(double x, double y, ScrollClamping) const
    1616 {
    1617     if (!isCurrentlyDisplayedInFrame())
    1618         return;
    1619 
    1620     RefPtr<FrameView> view = m_frame->view();
    1621     if (!view)
    1622         return;
    1623 
    1624     // Normalize non-finite values (https://drafts.csswg.org/cssom-view/#normalize-non-finite-values).
    1625     x = std::isfinite(x) ? x : 0;
    1626     y = std::isfinite(y) ? y : 0;
    1627 
    1628     if (!x && !y && view->contentsScrollPosition() == IntPoint(0, 0))
     1612    ScrollToOptions scrollToOptions = normalizeNonFiniteCoordinatesOrFallBackTo(options,
     1613        view->contentsScrollPosition().x(), view->contentsScrollPosition().y()
     1614    );
     1615
     1616    if (!scrollToOptions.left.value() && !scrollToOptions.top.value() && view->contentsScrollPosition() == IntPoint(0, 0))
    16291617        return;
    16301618
    16311619    document()->updateLayoutIgnorePendingStylesheets();
    16321620
    1633     IntPoint layoutPos(view->mapFromCSSToLayoutUnits(x), view->mapFromCSSToLayoutUnits(y));
     1621    IntPoint layoutPos(view->mapFromCSSToLayoutUnits(scrollToOptions.left.value()), view->mapFromCSSToLayoutUnits(scrollToOptions.top.value()));
    16341622    view->setContentsScrollPosition(layoutPos);
    16351623}
  • trunk/Source/WebCore/page/ScrollToOptions.h

    r208985 r234596  
    2929#pragma once
    3030
     31#include <cmath>
    3132#include <wtf/Optional.h>
    3233
     
    3839};
    3940
     41inline double normalizeNonFiniteValueOrFallBackTo(std::optional<double> value, double fallbackValue)
     42{
     43    // Normalize non-finite values (https://drafts.csswg.org/cssom-view/#normalize-non-finite-values).
     44    return value ? (std::isfinite(*value) ? *value : 0) : fallbackValue;
    4045}
     46
     47// FIXME(https://webkit.org/b/88339): Consider using FloatPoint or DoublePoint for fallback and return values.
     48inline ScrollToOptions normalizeNonFiniteCoordinatesOrFallBackTo(const ScrollToOptions& value, double x, double y)
     49{
     50    ScrollToOptions options;
     51    options.left = normalizeNonFiniteValueOrFallBackTo(value.left, x);
     52    options.top = normalizeNonFiniteValueOrFallBackTo(value.top, y);
     53    return options;
     54}
     55
     56}
Note: See TracChangeset for help on using the changeset viewer.