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 3cf9c29073131c37b32e3e797e6b6b4b05d36186..5e69739e77d0bdaa82f6835a629a74b580cda4f3 100644 |
--- a/third_party/WebKit/Source/core/layout/LayoutBoxModelObject.cpp |
+++ b/third_party/WebKit/Source/core/layout/LayoutBoxModelObject.cpp |
@@ -266,8 +266,29 @@ void LayoutBoxModelObject::styleDidChange(StyleDifference diff, const ComputedSt |
} |
if (FrameView *frameView = view()->frameView()) { |
- bool newStyleIsViewportConstained = style()->hasViewportConstrainedPosition(); |
- bool oldStyleIsViewportConstrained = oldStyle && oldStyle->hasViewportConstrainedPosition(); |
+ bool newStyleIsViewportConstained = style()->position() == FixedPosition; |
+ bool oldStyleIsViewportConstrained = oldStyle && oldStyle->position() == FixedPosition; |
+ bool newStyleIsSticky = style()->position() == StickyPosition; |
+ bool oldStyleIsSticky = oldStyle && oldStyle->position() == StickyPosition; |
+ |
+ if (newStyleIsSticky != oldStyleIsSticky) { |
+ if (newStyleIsSticky) { |
+ frameView->addStickyPositionObject(); |
+ // During compositing inputs update we'll have the scroll |
+ // ancestor without having to walk up the tree and can compute |
+ // the sticky position constraints then. |
+ if (layer()) |
+ layer()->setNeedsCompositingInputsUpdate(); |
+ } else { |
+ // TODO: This should only remove the constraint for the current object. |
+ invalidateScrollAncestorConstraints(); |
+ // This may get re-added to viewport constrained objects if the object went |
+ // from sticky to fixed. |
+ frameView->removeViewportConstrainedObject(this); |
+ frameView->removeStickyPositionObject(); |
+ } |
+ } |
+ |
if (newStyleIsViewportConstained != oldStyleIsViewportConstrained) { |
if (newStyleIsViewportConstained && layer()) |
frameView->addViewportConstrainedObject(this); |
@@ -340,6 +361,18 @@ static bool hasPercentageTransform(const ComputedStyle& style) |
|| (style.transformOriginY() != Length(50, Percent) && style.transformOriginY().hasPercent()); |
} |
+void LayoutBoxModelObject::invalidateScrollAncestorConstraints() |
+{ |
+ if (!layer()) |
+ return; |
+ // This intentionally uses the stale ancestor overflow layer compositing |
+ // input as if we have saved constraints for this layer they were saved |
+ // in the previous frame. |
+ DisableCompositingQueryAsserts disabler; |
+ if (const PaintLayer* ancestorScrollingLayer = enclosingLayer()->ancestorOverflowLayer()) |
+ ancestorScrollingLayer->scrollableArea()->invalidateStickyConstraints(); |
+} |
+ |
void LayoutBoxModelObject::invalidateTreeIfNeeded(PaintInvalidationState& paintInvalidationState) |
{ |
ASSERT(!needsLayout()); |
@@ -595,6 +628,139 @@ LayoutSize LayoutBoxModelObject::relativePositionOffset() const |
return offset; |
} |
+void LayoutBoxModelObject::updateStickyPositionConstraints(PaintLayer* ancestorOverflowLayer) const |
+{ |
+ const FloatSize constrainingSize = computeStickyConstrainingRect(ancestorOverflowLayer).size(); |
+ |
+ // Remove sticky constraints from old ancestor scroller if there was a change. |
+ if (const PaintLayer* oldAncestorOverflowLayer = enclosingLayer()->ancestorOverflowLayer()) { |
+ if (oldAncestorOverflowLayer != ancestorOverflowLayer) |
+ oldAncestorOverflowLayer->scrollableArea()->stickyConstraintsMap().remove(layer()); |
+ } |
+ |
+ PaintLayerScrollableArea* scrollableArea = ancestorOverflowLayer->scrollableArea(); |
+ StickyPositionScrollingConstraints constraints; |
+ FloatSize skippedContainersOffset; |
+ LayoutBlock* containingBlock = this->containingBlock(); |
+ // Skip anonymous containing blocks. |
+ while (containingBlock->isAnonymous()) { |
+ skippedContainersOffset += toFloatSize(FloatPoint(containingBlock->frameRect().location())); |
+ containingBlock = containingBlock->containingBlock(); |
+ } |
+ LayoutBox* scrollAncestor = ancestorOverflowLayer->isRootLayer() ? nullptr : toLayoutBox(ancestorOverflowLayer->layoutObject()); |
+ |
+ LayoutRect containerContentRect = containingBlock->contentBoxRect(); |
+ LayoutUnit maxWidth = containingBlock->availableLogicalWidth(); |
+ |
+ // Sticky positioned element ignore any override logical width on the containing block (as they don't call |
+ // containingBlockLogicalWidthForContent). It's unclear whether this is totally fine. |
+ // Compute the container-relative area within which the sticky element is allowed to move. |
+ containerContentRect.contractEdges( |
+ minimumValueForLength(style()->marginTop(), maxWidth), |
+ minimumValueForLength(style()->marginRight(), maxWidth), |
+ minimumValueForLength(style()->marginBottom(), maxWidth), |
+ minimumValueForLength(style()->marginLeft(), maxWidth)); |
+ |
+ // Map to the scroll ancestor. |
+ constraints.setScrollContainerRelativeContainingBlockRect(containingBlock->localToAncestorQuad(FloatRect(containerContentRect), scrollAncestor).boundingBox()); |
+ |
+ FloatRect stickyBoxRect = isLayoutInline() |
+ ? FloatRect(toLayoutInline(this)->linesBoundingBox()) |
+ : FloatRect(toLayoutBox(this)->frameRect()); |
+ FloatRect flippedStickyBoxRect = stickyBoxRect; |
+ containingBlock->flipForWritingMode(flippedStickyBoxRect); |
+ FloatPoint stickyLocation = flippedStickyBoxRect.location() + skippedContainersOffset; |
+ |
+ // TODO(flackr): Unfortunate to call localToAncestorQuad again, but we can't just offset from the previously computed rect if there are transforms. |
+ // Map to the scroll ancestor. |
+ FloatRect scrollContainerRelativeContainerFrame = containingBlock->localToAncestorQuad(FloatRect(FloatPoint(), FloatSize(containingBlock->size())), scrollAncestor).boundingBox(); |
+ |
+ // If the containing block is our scroll ancestor, its location will not include the scroll offset which we need to include as |
+ // part of the sticky box rect so we include it here. |
+ if (containingBlock->hasOverflowClip()) { |
+ FloatSize scrollOffset(toFloatSize(containingBlock->layer()->scrollableArea()->adjustedScrollOffset())); |
+ stickyLocation -= scrollOffset; |
+ } |
+ |
+ constraints.setScrollContainerRelativeStickyBoxRect(FloatRect(scrollContainerRelativeContainerFrame.location() + toFloatSize(stickyLocation), flippedStickyBoxRect.size())); |
+ |
+ // 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 = minimumValueForLength(style()->right(), constrainingSize.width()) + |
+ minimumValueForLength(style()->left(), constrainingSize.width()); |
+ bool skipRight = false; |
+ bool skipLeft = false; |
+ if (!style()->left().isAuto() && !style()->right().isAuto()) { |
+ if (horizontalOffsets > containerContentRect.width() |
+ || horizontalOffsets + containerContentRect.width() > constrainingSize.width()) { |
+ skipRight = style()->isLeftToRightDirection(); |
+ skipLeft = !skipRight; |
+ } |
+ } |
+ |
+ if (!style()->left().isAuto() && !skipLeft) { |
+ constraints.setLeftOffset(minimumValueForLength(style()->left(), constrainingSize.width())); |
+ constraints.addAnchorEdge(StickyPositionScrollingConstraints::AnchorEdgeLeft); |
+ } |
+ |
+ if (!style()->right().isAuto() && !skipRight) { |
+ constraints.setRightOffset(minimumValueForLength(style()->right(), constrainingSize.width())); |
+ constraints.addAnchorEdge(StickyPositionScrollingConstraints::AnchorEdgeRight); |
+ } |
+ |
+ bool skipBottom = false; |
+ // TODO(flackr): Exclude top or bottom edge offset depending on the writing mode when related |
+ // sections are fixed in spec: http://lists.w3.org/Archives/Public/www-style/2014May/0286.html |
+ LayoutUnit verticalOffsets = minimumValueForLength(style()->top(), constrainingSize.height()) + |
+ minimumValueForLength(style()->bottom(), constrainingSize.height()); |
+ if (!style()->top().isAuto() && !style()->bottom().isAuto()) { |
+ if (verticalOffsets > containerContentRect.height() |
+ || verticalOffsets + containerContentRect.height() > constrainingSize.height()) { |
+ skipBottom = true; |
+ } |
+ } |
+ |
+ if (!style()->top().isAuto()) { |
+ constraints.setTopOffset(minimumValueForLength(style()->top(), constrainingSize.height())); |
+ constraints.addAnchorEdge(StickyPositionScrollingConstraints::AnchorEdgeTop); |
+ } |
+ |
+ if (!style()->bottom().isAuto() && !skipBottom) { |
+ constraints.setBottomOffset(minimumValueForLength(style()->bottom(), constrainingSize.height())); |
+ constraints.addAnchorEdge(StickyPositionScrollingConstraints::AnchorEdgeBottom); |
+ } |
+ scrollableArea->stickyConstraintsMap().set(layer(), constraints); |
+} |
+ |
+FloatRect LayoutBoxModelObject::computeStickyConstrainingRect(const PaintLayer* ancestorOverflowLayer) const |
+{ |
+ if (ancestorOverflowLayer->isRootLayer()) |
+ return view()->frameView()->visibleContentRect(); |
+ |
+ LayoutBox* enclosingClippingBox = toLayoutBox(ancestorOverflowLayer->layoutObject()); |
+ FloatRect constrainingRect; |
+ constrainingRect = FloatRect(enclosingClippingBox->overflowClipRect(LayoutPoint())); |
+ constrainingRect.move(enclosingClippingBox->paddingLeft(), enclosingClippingBox->paddingTop()); |
+ constrainingRect.contract(FloatSize(enclosingClippingBox->paddingLeft() + enclosingClippingBox->paddingRight(), |
+ enclosingClippingBox->paddingTop() + enclosingClippingBox->paddingBottom())); |
+ return constrainingRect; |
+} |
+ |
+LayoutSize LayoutBoxModelObject::stickyPositionOffset() const |
+{ |
+ const PaintLayer* ancestorOverflowLayer = enclosingLayer()->ancestorOverflowLayer(); |
+ // TODO: Force compositing input update if we ask for offset before compositing inputs have been computed? |
+ if (!ancestorOverflowLayer) |
+ return LayoutSize(); |
+ FloatRect constrainingRect = computeStickyConstrainingRect(ancestorOverflowLayer); |
+ PaintLayerScrollableArea* scrollableArea = ancestorOverflowLayer->scrollableArea(); |
+ |
+ // 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)); |
+} |
+ |
LayoutPoint LayoutBoxModelObject::adjustedPositionRelativeToOffsetParent(const LayoutPoint& startPoint) const |
{ |
// If the element is the HTML body element or doesn't have a parent |
@@ -617,7 +783,7 @@ LayoutPoint LayoutBoxModelObject::adjustedPositionRelativeToOffsetParent(const L |
referencePoint.move(-toLayoutBox(offsetParent)->borderLeft(), -toLayoutBox(offsetParent)->borderTop()); |
if (!isOutOfFlowPositioned() || flowThreadContainingBlock()) { |
if (isInFlowPositioned()) |
- referencePoint.move(relativePositionOffset()); |
+ referencePoint.move(offsetForInFlowPosition()); |
LayoutObject* current; |
for (current = parent(); current != offsetParent && current->parent(); current = current->parent()) { |
@@ -639,7 +805,13 @@ LayoutPoint LayoutBoxModelObject::adjustedPositionRelativeToOffsetParent(const L |
LayoutSize LayoutBoxModelObject::offsetForInFlowPosition() const |
{ |
- return isRelPositioned() ? relativePositionOffset() : LayoutSize(); |
+ if (isRelPositioned()) |
+ return relativePositionOffset(); |
+ |
+ if (isStickyPositioned()) |
+ return stickyPositionOffset(); |
+ |
+ return LayoutSize(); |
} |
LayoutUnit LayoutBoxModelObject::offsetLeft() const |
@@ -962,7 +1134,7 @@ const LayoutObject* LayoutBoxModelObject::pushMappingToContainer(const LayoutBox |
return nullptr; |
bool isInline = isLayoutInline(); |
- bool isFixedPos = !isInline && style()->position() == FixedPosition; |
+ bool isFixedPosition = !isInline && style()->position() == FixedPosition; |
bool hasTransform = !isInline && hasLayer() && layer()->transform(); |
LayoutSize adjustmentForSkippedAncestor; |
@@ -974,16 +1146,19 @@ const LayoutObject* LayoutBoxModelObject::pushMappingToContainer(const LayoutBox |
bool offsetDependsOnPoint = false; |
LayoutSize containerOffset = offsetFromContainer(container, LayoutPoint(), &offsetDependsOnPoint); |
+ LayoutSize stickyOffset; |
+ if (style()->position() == StickyPosition) |
+ stickyOffset = offsetForInFlowPosition(); |
bool preserve3D = container->style()->preserves3D() || style()->preserves3D(); |
if (shouldUseTransformFromContainer(container)) { |
TransformationMatrix t; |
getTransformFromContainer(container, containerOffset, t); |
t.translateRight(adjustmentForSkippedAncestor.width().toFloat(), adjustmentForSkippedAncestor.height().toFloat()); |
- geometryMap.push(this, t, preserve3D, offsetDependsOnPoint, isFixedPos, hasTransform); |
+ geometryMap.push(this, t, preserve3D, offsetDependsOnPoint, isFixedPosition, hasTransform, LayoutSize(), stickyOffset); |
} else { |
containerOffset += adjustmentForSkippedAncestor; |
- geometryMap.push(this, containerOffset, preserve3D, offsetDependsOnPoint, isFixedPos, hasTransform); |
+ geometryMap.push(this, containerOffset, preserve3D, offsetDependsOnPoint, isFixedPosition, hasTransform, LayoutSize(), stickyOffset); |
} |
return ancestorSkipped ? ancestorToStopAt : container; |