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

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

Issue 2636253002: Handle nested position:sticky elements (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 2b869f79db28311e900a4ba41ad5071f9a8bebc6..4c41596ae6b355c43b0d301ace83870dbf510f6c 100644
--- a/third_party/WebKit/Source/core/layout/LayoutBoxModelObject.cpp
+++ b/third_party/WebKit/Source/core/layout/LayoutBoxModelObject.cpp
@@ -46,6 +46,29 @@
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;
+}
+} // namespace
+
class FloatStateForStyleChange {
public:
static void setWasFloating(LayoutBoxModelObject* boxModelObject,
@@ -662,13 +685,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
@@ -785,6 +801,23 @@ LayoutSize LayoutBoxModelObject::relativePositionOffset() const {
return offset;
}
+// Inclusive of |from|, exclusive of |to|.
+static 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;
+}
+
void LayoutBoxModelObject::updateStickyPositionConstraints() const {
const FloatSize constrainingSize = computeStickyConstrainingRect().size();
@@ -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);
+ }
+
FloatRect flippedStickyBoxRect = stickyBoxRect;
containingBlock->flipForWritingMode(flippedStickyBoxRect);
FloatPoint stickyLocation =
@@ -899,6 +946,21 @@ 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.
+ LayoutObject* ancestorOverflowLayer =
+ layer()->ancestorOverflowLayer()->layoutObject();
+ constraints.setNearestStickyElementShiftingContainingBlock(
+ findFirstStickyBetween(containingBlock, ancestorOverflowLayer));
+
// 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 =
@@ -987,24 +1049,24 @@ 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* toContainingBlockConstraints =
+ stickyConstraintsForLayoutObject(
+ constraints->nearestStickyElementShiftingStickyBox());
+
+ StickyPositionScrollingConstraints* toViewportConstraints =
+ 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, toContainingBlockConstraints, toViewportConstraints));
}
LayoutPoint LayoutBoxModelObject::adjustedPositionRelativeTo(
« no previous file with comments | « third_party/WebKit/Source/core/frame/FrameView.cpp ('k') | third_party/WebKit/Source/core/layout/LayoutBoxModelObjectTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698