Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(136)

Unified Diff: third_party/WebKit/Source/core/layout/LayoutBoxModelObject.cpp

Issue 2708883005: Handle nested position:sticky elements correctly (main thread) (Closed)
Patch Set: Rebase Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/core/layout/LayoutBoxModelObject.cpp
diff --git a/third_party/WebKit/Source/core/layout/LayoutBoxModelObject.cpp b/third_party/WebKit/Source/core/layout/LayoutBoxModelObject.cpp
index 144633cb36b4defe52d6961fdc5144f94d550701..079ec194bb7a420b9bde555002f14e631cffab49 100644
--- a/third_party/WebKit/Source/core/layout/LayoutBoxModelObject.cpp
+++ b/third_party/WebKit/Source/core/layout/LayoutBoxModelObject.cpp
@@ -46,6 +46,46 @@
namespace blink {
+namespace {
+inline bool isOutOfFlowPositionedWithImplicitHeight(
+ const LayoutBoxModelObject* child) {
+ return child->isOutOfFlowPositioned() &&
+ !child->style()->logicalTop().isAuto() &&
+ !child->style()->logicalBottom().isAuto();
+}
+
+StickyPositionScrollingConstraints* stickyConstraintsForLayoutObject(
+ const LayoutBoxModelObject* o) {
+ if (!o || !o->layer()->ancestorOverflowLayer())
+ return nullptr;
+
+ PaintLayerScrollableArea* scrollableArea =
+ o->layer()->ancestorOverflowLayer()->getScrollableArea();
+ auto it = scrollableArea->stickyConstraintsMap().find(o->layer());
+ if (it == scrollableArea->stickyConstraintsMap().end())
+ return nullptr;
+
+ return &it->value;
+}
+
+// Inclusive of |from|, exclusive of |to|.
+LayoutBoxModelObject* findFirstStickyBetween(LayoutObject* from,
+ LayoutObject* to) {
+ LayoutObject* maybeStickyAncestor = from;
+ while (maybeStickyAncestor && maybeStickyAncestor != to) {
+ if (maybeStickyAncestor->isStickyPositioned()) {
+ return toLayoutBoxModelObject(maybeStickyAncestor);
+ }
+
+ maybeStickyAncestor =
+ maybeStickyAncestor->isLayoutInline()
+ ? maybeStickyAncestor->containingBlock()
+ : toLayoutBox(maybeStickyAncestor)->locationContainer();
+ }
+ return nullptr;
+}
+} // namespace
+
class FloatStateForStyleChange {
public:
static void setWasFloating(LayoutBoxModelObject* boxModelObject,
@@ -662,13 +702,6 @@ void LayoutBoxModelObject::updateFromStyle() {
setHorizontalWritingMode(styleToUse.isHorizontalWritingMode());
}
-static inline bool isOutOfFlowPositionedWithImplicitHeight(
- const LayoutBoxModelObject* child) {
- return child->isOutOfFlowPositioned() &&
- !child->style()->logicalTop().isAuto() &&
- !child->style()->logicalBottom().isAuto();
-}
-
LayoutBlock* LayoutBoxModelObject::containingBlockForAutoHeightDetection(
Length logicalHeight) const {
// For percentage heights: The percentage is calculated with respect to the
@@ -794,14 +827,13 @@ void LayoutBoxModelObject::updateStickyPositionConstraints() const {
FloatSize skippedContainersOffset;
LayoutBlock* containingBlock = this->containingBlock();
// The location container for boxes is not always the containing block.
- LayoutBox* locationContainer = isLayoutInline()
- ? containingBlock
- : toLayoutBox(this)->locationContainer();
+ LayoutObject* locationContainer =
+ isLayoutInline() ? container() : toLayoutBox(this)->locationContainer();
// Skip anonymous containing blocks.
while (containingBlock->isAnonymous()) {
containingBlock = containingBlock->containingBlock();
}
- MapCoordinatesFlags flags = 0;
+ MapCoordinatesFlags flags = IgnoreStickyOffset;
skippedContainersOffset =
toFloatSize(locationContainer
->localToAncestorQuadWithoutTransforms(
@@ -881,6 +913,21 @@ void LayoutBoxModelObject::updateStickyPositionConstraints() const {
FloatRect stickyBoxRect =
isLayoutInline() ? FloatRect(toLayoutInline(this)->linesBoundingBox())
: FloatRect(toLayoutBox(this)->frameRect());
+
+ // If we have an anonymous container, then |skippedContainersOffset| above can
+ // incorrectly exclude the scrollOffset if our non-anonymous containing block
+ // is the direct child of the scrollAncestor. Add it back here.
+ // TODO(smcgruer): I think this is not quite the right conditional check.
+ // TODO(smcgruer): Refactor into the code above.
+ if (locationContainer != scrollAncestor &&
+ containingBlock == scrollAncestor) {
+ ScrollOffset scrollOffset(
+ scrollAncestor
+ ? toFloatSize(scrollAncestor->getScrollableArea()->scrollPosition())
+ : FloatSize());
+ stickyBoxRect.move(scrollOffset);
flackr 2017/02/22 22:40:14 This seems like a separate bug. Can we land this i
smcgruer 2017/02/23 20:14:17 Certainly, removed this and updated StickyPosition
+ }
+
FloatRect flippedStickyBoxRect = stickyBoxRect;
containingBlock->flipForWritingMode(flippedStickyBoxRect);
FloatPoint stickyLocation =
@@ -899,6 +946,20 @@ void LayoutBoxModelObject::updateStickyPositionConstraints() const {
toFloatSize(stickyLocation),
flippedStickyBoxRect.size()));
+ // To correctly compute the offsets, the constraints need to know about any
+ // nested position:sticky elements between themselves and their
+ // containingBlock, and between the containingBlock and their scrollAncestor.
+ //
+ // The respective search ranges are [container, containingBlock) and
+ // [containingBlock, scrollAncestor).
+ constraints.setNearestStickyElementShiftingStickyBox(
+ findFirstStickyBetween(locationContainer, containingBlock));
+ // We cannot use |scrollAncestor| here as it disregards the root
+ // ancestorOverflowLayer(), which we should include.
+ constraints.setNearestStickyElementShiftingContainingBlock(
+ findFirstStickyBetween(
+ containingBlock, &layer()->ancestorOverflowLayer()->layoutObject()));
+
// We skip the right or top sticky offset if there is not enough space to
// honor both the left/right or top/bottom offsets.
LayoutUnit horizontalOffsets =
@@ -988,24 +1049,25 @@ FloatRect LayoutBoxModelObject::computeStickyConstrainingRect() const {
}
LayoutSize LayoutBoxModelObject::stickyPositionOffset() const {
- const PaintLayer* ancestorOverflowLayer = layer()->ancestorOverflowLayer();
- // TODO: Force compositing input update if we ask for offset before
- // compositing inputs have been computed?
- if (!ancestorOverflowLayer)
+ StickyPositionScrollingConstraints* constraints =
+ stickyConstraintsForLayoutObject(this);
+ if (!constraints)
return LayoutSize();
- FloatRect constrainingRect = computeStickyConstrainingRect();
- PaintLayerScrollableArea* scrollableArea =
- ancestorOverflowLayer->getScrollableArea();
+
+ StickyPositionScrollingConstraints* shiftingStickyBoxConstraints =
+ stickyConstraintsForLayoutObject(
+ constraints->nearestStickyElementShiftingStickyBox());
+
+ StickyPositionScrollingConstraints* shiftingContainingBlockConstraints =
+ stickyConstraintsForLayoutObject(
+ constraints->nearestStickyElementShiftingContainingBlock());
// The sticky offset is physical, so we can just return the delta computed in
// absolute coords (though it may be wrong with transforms).
- // TODO: Force compositing input update if we ask for offset with stale
- // compositing inputs.
- if (!scrollableArea->stickyConstraintsMap().contains(layer()))
- return LayoutSize();
- return LayoutSize(
- scrollableArea->stickyConstraintsMap().get(layer()).computeStickyOffset(
- constrainingRect));
+ FloatRect constrainingRect = computeStickyConstrainingRect();
+ return LayoutSize(constraints->computeStickyOffset(
+ constrainingRect, shiftingStickyBoxConstraints,
+ shiftingContainingBlockConstraints));
}
LayoutPoint LayoutBoxModelObject::adjustedPositionRelativeTo(

Powered by Google App Engine
This is Rietveld 408576698