1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
|
/*
* Copyright (C) 2022 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "ScrollAnchoringController.h"
#include "ElementChildIteratorInlines.h"
#include "ElementIterator.h"
#include "HTMLHtmlElement.h"
#include "LocalFrameView.h"
#include "Logging.h"
#include "RenderBox.h"
#include "RenderLayerScrollableArea.h"
#include "RenderObjectInlines.h"
#include "RenderView.h"
#include "TypedElementDescendantIteratorInlines.h"
#include <wtf/TZoneMallocInlines.h>
#include <wtf/text/TextStream.h>
namespace WebCore {
WTF_MAKE_TZONE_ALLOCATED_IMPL(ScrollAnchoringController);
ScrollAnchoringController::ScrollAnchoringController(ScrollableArea& owningScroller)
: m_owningScrollableArea(owningScroller)
{ }
ScrollAnchoringController::~ScrollAnchoringController()
{
invalidateAnchorElement();
}
LocalFrameView& ScrollAnchoringController::frameView()
{
if (auto* renderLayerScrollableArea = dynamicDowncast<RenderLayerScrollableArea>(m_owningScrollableArea))
return renderLayerScrollableArea->layer().renderer().view().frameView();
return downcast<LocalFrameView>(downcast<ScrollView>(m_owningScrollableArea));
}
static bool elementIsScrollableArea(const Element& element, const ScrollableArea& scrollableArea)
{
return element.renderBox() && element.renderBox()->layer() && element.renderBox()->layer()->scrollableArea() == &scrollableArea;
}
static Element* elementForScrollableArea(ScrollableArea& scrollableArea)
{
if (auto* renderLayerScrollableArea = dynamicDowncast<RenderLayerScrollableArea>(scrollableArea))
return renderLayerScrollableArea->layer().renderer().element();
if (auto* document = downcast<LocalFrameView>(downcast<ScrollView>(scrollableArea)).frame().document())
return document->documentElement();
return nullptr;
}
void ScrollAnchoringController::invalidateAnchorElement()
{
if (m_midUpdatingScrollPositionForAnchorElement)
return;
LOG_WITH_STREAM(ScrollAnchoring, stream << "ScrollAnchoringController::invalidateAnchorElement() invalidating anchor for frame: " << frameView() << " for scroller: " << m_owningScrollableArea);
if (!m_anchorElement) {
if (auto* element = elementForScrollableArea(m_owningScrollableArea)) {
if (auto* renderer = element->renderer()) {
auto* scrollAnchoringControllerForScrollableArea = RenderObject::searchParentChainForScrollAnchoringController(*renderer);
if (scrollAnchoringControllerForScrollableArea && scrollAnchoringControllerForScrollableArea->isInScrollAnchoringAncestorChain(*renderer))
scrollAnchoringControllerForScrollableArea->invalidateAnchorElement();
}
}
}
m_anchorElement = nullptr;
m_lastOffsetForAnchorElement = { };
m_isQueuedForScrollPositionUpdate = false;
frameView().dequeueScrollableAreaForScrollAnchoringUpdate(m_owningScrollableArea);
}
static IntRect boundingRectForScrollableArea(ScrollableArea& scrollableArea)
{
if (auto* renderLayerScrollableArea = dynamicDowncast<RenderLayerScrollableArea>(scrollableArea))
return renderLayerScrollableArea->layer().renderer().absoluteBoundingBoxRect();
return IntRect(downcast<LocalFrameView>(downcast<ScrollView>(scrollableArea)).layoutViewportRect());
}
FloatPoint ScrollAnchoringController::computeOffsetFromOwningScroller(RenderObject& candidate)
{
// TODO: investigate this for zoom/rtl
return FloatPoint(candidate.absoluteBoundingBoxRect().location() - boundingRectForScrollableArea(m_owningScrollableArea).location());
}
void ScrollAnchoringController::notifyChildHadSuppressingStyleChange()
{
LOG_WITH_STREAM(ScrollAnchoring, stream << "ScrollAnchoringController::notifyChildHadSuppressingStyleChange() for scroller: " << m_owningScrollableArea);
m_shouldSuppressScrollPositionUpdate = true;
}
bool ScrollAnchoringController::isInScrollAnchoringAncestorChain(const RenderObject& object)
{
RefPtr iterElement = m_anchorElement.get();
while (iterElement) {
if (auto* renderer = iterElement->renderer()) {
LOG_WITH_STREAM(ScrollAnchoring, stream << "ScrollAnchoringController::isInScrollAnchoringAncestorChain() checking for : " <<object << " current Element: " << *iterElement);
if (&object == renderer)
return true;
}
if (iterElement && elementIsScrollableArea(*iterElement, m_owningScrollableArea))
break;
iterElement = iterElement->parentElementInComposedTree();
}
return false;
}
static RefPtr<Element> anchorElementForPriorityCandidate(Element* element)
{
while (element) {
if (auto renderer = element->renderer()) {
if (!renderer->isAnonymousBlock() && (!renderer->isInline() || renderer->isAtomicInlineLevelBox()))
return element;
}
element = element->parentElement();
}
return nullptr;
}
static ScrollAnchoringController* scrollAnchoringControllerForElement(Element& element)
{
if (auto renderer = element.renderer()) {
if (renderer->hasLayer()) {
if (auto layer = downcast<RenderLayerModelObject>(*renderer).layer()) {
if (auto scrollableArea = layer->scrollableArea())
return scrollableArea->scrollAnchoringController();
}
}
}
return nullptr;
}
// This function ensures that each element in the chain from the priorityCandidateElement to the parentController are viable according to
// ScrollAnchoringController::examineAnchorCandidate and that none of them are maintaining an anchor element.
static bool canIncludeElementInPriorityCandidateChain(Element& element, Element& priorityCandidateElement, ScrollAnchoringController& parentController)
{
auto candidateResult = parentController.examineAnchorCandidate(element);
auto elementsController = scrollAnchoringControllerForElement(element);
return !(candidateResult == CandidateExaminationResult::Exclude || (&element == &priorityCandidateElement && candidateResult == CandidateExaminationResult::Skip) || (elementsController && elementsController->anchorElement()));
}
bool ScrollAnchoringController::didFindPriorityCandidate(Document& document)
{
auto viablePriorityCandidateForElement = [this](Element* element) -> RefPtr<Element> {
RefPtr candidateElement = anchorElementForPriorityCandidate(element);
if (!candidateElement || candidateElement == elementForScrollableArea(m_owningScrollableArea))
return nullptr;
RefPtr iterElement = candidateElement;
while (iterElement && iterElement.get() != elementForScrollableArea(m_owningScrollableArea)) {
if (!canIncludeElementInPriorityCandidateChain(*iterElement, *candidateElement, *this))
return nullptr;
iterElement = iterElement->parentElement();
}
// Ensure that candidateElement is a child of m_owningScrollableArea
if (iterElement.get() != elementForScrollableArea(m_owningScrollableArea))
return nullptr;
return candidateElement;
};
// TODO: need to check if focused element is text editable
// TODO: need to figure out how to get element that is the current find-in-page element (look into FindController)
if (RefPtr priorityCandidate = viablePriorityCandidateForElement(document.focusedElement())) {
m_anchorElement = priorityCandidate;
m_lastOffsetForAnchorElement = computeOffsetFromOwningScroller(*m_anchorElement->renderer());
LOG_WITH_STREAM(ScrollAnchoring, stream << "ScrollAnchoringController::viablePriorityCandidateForElement() found priority candidate: " << *priorityCandidate << " for element: " << ValueOrNull(elementForScrollableArea(m_owningScrollableArea)));
return true;
}
return false;
}
static bool absolutePositionedElementOutsideScroller(RenderElement& renderer, ScrollableArea& scroller)
{
if (auto* renderLayerScrollableArea = dynamicDowncast<RenderLayerScrollableArea>(scroller); renderLayerScrollableArea && renderer.hasLayer()) {
if (auto* layerForRenderer = downcast<RenderLayerModelObject>(renderer).layer())
return !layerForRenderer->ancestorLayerIsInContainingBlockChain(renderLayerScrollableArea->layer());
}
return false;
}
static bool canDescendIntoElement(Element& element)
{
if (auto* scrollAnchoringController = scrollAnchoringControllerForElement(element))
return !scrollAnchoringController->anchorElement();
return false;
}
CandidateExaminationResult ScrollAnchoringController::examineAnchorCandidate(Element& element)
{
if (elementForScrollableArea(m_owningScrollableArea) && elementForScrollableArea(m_owningScrollableArea)->identifier() == element.identifier())
return CandidateExaminationResult::Skip;
auto containingRect = boundingRectForScrollableArea(m_owningScrollableArea);
auto* document = frameView().frame().document();
if (auto* element = elementForScrollableArea(m_owningScrollableArea)) {
if (auto* box = element->renderBox()) {
LayoutRect paddedLayerBounds(containingRect);
paddedLayerBounds.contract(box->scrollPaddingForViewportRect(paddedLayerBounds));
LOG_WITH_STREAM(ScrollAnchoring, stream << "ScrollAnchoringController::examineAnchorCandidate() contracted rect: "<< IntRect(paddedLayerBounds));
containingRect = IntRect(paddedLayerBounds);
}
}
auto isExcludedSubtree = [this](RenderElement* renderer, bool intersects) {
return renderer->style().overflowAnchor() == OverflowAnchor::None || renderer->isStickilyPositioned() || renderer->isFixedPositioned() || renderer->isPseudoElement() || renderer->isAnonymousBlock() || (renderer->isAbsolutelyPositioned() && absolutePositionedElementOutsideScroller(*renderer, m_owningScrollableArea)) || (!intersects && renderer->style().containsPaint());
};
if (auto renderer = element.renderer()) {
// TODO: figure out how to get scrollable area for renderer to check if it is maintaining scroll anchor
auto boxRect = renderer->absoluteBoundingBoxRect();
bool intersects = containingRect.intersects(boxRect);
if (isExcludedSubtree(renderer, intersects))
return CandidateExaminationResult::Exclude;
if (&element == document->bodyOrFrameset() || is<HTMLHtmlElement>(&element) || (renderer->isInline() && !renderer->isAtomicInlineLevelBox()))
return CandidateExaminationResult::Skip;
if (!boxRect.width() || !boxRect.height())
return CandidateExaminationResult::Skip;
if (containingRect.contains(boxRect))
return CandidateExaminationResult::Select;
auto isScrollingNode = false;
if (auto* renderBox = dynamicDowncast<RenderBox>(renderer))
isScrollingNode = renderBox->hasPotentiallyScrollableOverflow();
if (intersects)
return !isScrollingNode || canDescendIntoElement(element) ? CandidateExaminationResult::Descend : CandidateExaminationResult::Select;
if (isScrollingNode)
return CandidateExaminationResult::Exclude;
}
return CandidateExaminationResult::Skip;
}
#if !LOG_DISABLED
static TextStream& operator<<(TextStream& ts, CandidateExaminationResult result)
{
switch (result) {
case CandidateExaminationResult::Exclude:
ts << "Exclude";
break;
case CandidateExaminationResult::Select:
ts << "Select";
break;
case CandidateExaminationResult::Descend:
ts << "Descend";
break;
case CandidateExaminationResult::Skip:
ts << "Skip";
break;
}
return ts;
}
#endif
Element* ScrollAnchoringController::findAnchorElementRecursive(Element* element)
{
if (!element)
return nullptr;
auto result = examineAnchorCandidate(*element);
LOG_WITH_STREAM(ScrollAnchoring, stream << "ScrollAnchoringController::findAnchorElementRecursive() element: "<< *element<<" examination result: " << result);
switch (result) {
case CandidateExaminationResult::Select:
return element;
case CandidateExaminationResult::Exclude:
return nullptr;
case CandidateExaminationResult::Skip:
case CandidateExaminationResult::Descend: {
for (auto& child : childrenOfType<Element>(*element)) {
if (auto* anchorElement = findAnchorElementRecursive(&child))
return anchorElement;
}
break;
}
}
if (result == CandidateExaminationResult::Skip)
return nullptr;
return element;
}
void ScrollAnchoringController::chooseAnchorElement(Document& document)
{
LOG_WITH_STREAM(ScrollAnchoring, stream << "ScrollAnchoringController::chooseAnchorElement() starting findAnchorElementRecursive: for element: " << ValueOrNull(elementForScrollableArea(m_owningScrollableArea)));
if (didFindPriorityCandidate(document))
return;
RefPtr<Element> anchorElement;
if (!m_anchorElement) {
anchorElement = findAnchorElementRecursive(elementForScrollableArea(m_owningScrollableArea));
if (!anchorElement)
return;
}
m_anchorElement = anchorElement;
m_lastOffsetForAnchorElement = computeOffsetFromOwningScroller(*m_anchorElement->renderer());
LOG_WITH_STREAM(ScrollAnchoring, stream << "ScrollAnchoringController::chooseAnchorElement() found anchor node: " << *anchorElement << " offset: " << computeOffsetFromOwningScroller(*m_anchorElement->renderer()));
}
void ScrollAnchoringController::updateAnchorElement()
{
if (m_owningScrollableArea.scrollPosition().isZero() || m_isQueuedForScrollPositionUpdate || frameView().layoutContext().layoutPhase() != LocalFrameViewLayoutContext::LayoutPhase::OutsideLayout)
return;
RefPtr document = frameView().frame().document();
if (!document)
return;
if (m_anchorElement && !m_anchorElement->renderer())
invalidateAnchorElement();
if (!m_anchorElement) {
chooseAnchorElement(*document);
if (!m_anchorElement)
return;
}
m_isQueuedForScrollPositionUpdate = true;
frameView().queueScrollableAreaForScrollAnchoringUpdate(m_owningScrollableArea);
}
void ScrollAnchoringController::adjustScrollPositionForAnchoring()
{
auto queued = std::exchange(m_isQueuedForScrollPositionUpdate, false);
auto suppressed = std::exchange(m_shouldSuppressScrollPositionUpdate, false);
if (!m_anchorElement || !queued)
return;
auto* renderer = m_anchorElement->renderer();
if (!renderer || suppressed) {
invalidateAnchorElement();
updateAnchorElement();
if (suppressed)
LOG_WITH_STREAM(ScrollAnchoring, stream << "ScrollAnchoringController::updateScrollPosition() suppressing scroll adjustment for frame: " << frameView() << " for scroller: " << m_owningScrollableArea);
return;
}
SetForScope midUpdatingScrollPositionForAnchorElement(m_midUpdatingScrollPositionForAnchorElement, true);
FloatSize adjustment = computeOffsetFromOwningScroller(*renderer) - m_lastOffsetForAnchorElement;
if (!adjustment.isZero()) {
if (m_owningScrollableArea.isUserScrollInProgress()) {
invalidateAnchorElement();
updateAnchorElement();
return;
}
auto newScrollPosition = m_owningScrollableArea.scrollPosition() + IntPoint(adjustment.width(), adjustment.height());
RELEASE_LOG(ScrollAnchoring, "ScrollAnchoringController::updateScrollPosition() is main frame: %d, is main scroller: %d, adjusting from: (%d, %d) to: (%d, %d)", frameView().frame().isMainFrame(), !m_owningScrollableArea.isRenderLayer(), m_owningScrollableArea.scrollPosition().x(), m_owningScrollableArea.scrollPosition().y(), newScrollPosition.x(), newScrollPosition.y());
LOG_WITH_STREAM(ScrollAnchoring, stream << "ScrollAnchoringController::updateScrollPosition() for scroller element: " << ValueOrNull(elementForScrollableArea(m_owningScrollableArea)) << " anchor node: " << *m_anchorElement << "adjusting from: " << m_owningScrollableArea.scrollPosition() << " to: " << newScrollPosition);
auto options = ScrollPositionChangeOptions::createProgrammatic();
options.originalScrollDelta = adjustment;
auto oldScrollType = m_owningScrollableArea.currentScrollType();
m_owningScrollableArea.setCurrentScrollType(ScrollType::Programmatic);
if (!m_owningScrollableArea.requestScrollToPosition(newScrollPosition, options))
m_owningScrollableArea.scrollToPositionWithoutAnimation(newScrollPosition);
m_owningScrollableArea.setCurrentScrollType(oldScrollType);
}
}
} // namespace WebCore
|