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

Side by Side Diff: third_party/WebKit/Source/core/layout/LayoutBoxModelObject.cpp

Issue 2708883005: Handle nested position:sticky elements correctly (main thread) (Closed)
Patch Set: Address reviewer comments Created 3 years, 9 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 unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org) 3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2005 Allan Sandfeld Jensen (kde@carewolf.com) 4 * (C) 2005 Allan Sandfeld Jensen (kde@carewolf.com)
5 * (C) 2005, 2006 Samuel Weinig (sam.weinig@gmail.com) 5 * (C) 2005, 2006 Samuel Weinig (sam.weinig@gmail.com)
6 * Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved. 6 * Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
7 * Copyright (C) 2010 Google Inc. All rights reserved. 7 * Copyright (C) 2010 Google Inc. All rights reserved.
8 * 8 *
9 * This library is free software; you can redistribute it and/or 9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public 10 * modify it under the terms of the GNU Library General Public
(...skipping 28 matching lines...) Expand all
39 #include "core/paint/ObjectPaintInvalidator.h" 39 #include "core/paint/ObjectPaintInvalidator.h"
40 #include "core/paint/PaintLayer.h" 40 #include "core/paint/PaintLayer.h"
41 #include "core/style/ShadowList.h" 41 #include "core/style/ShadowList.h"
42 #include "platform/LengthFunctions.h" 42 #include "platform/LengthFunctions.h"
43 #include "platform/geometry/TransformState.h" 43 #include "platform/geometry/TransformState.h"
44 #include "platform/scroll/MainThreadScrollingReason.h" 44 #include "platform/scroll/MainThreadScrollingReason.h"
45 #include "wtf/PtrUtil.h" 45 #include "wtf/PtrUtil.h"
46 46
47 namespace blink { 47 namespace blink {
48 48
49 namespace {
50 inline bool isOutOfFlowPositionedWithImplicitHeight(
51 const LayoutBoxModelObject* child) {
52 return child->isOutOfFlowPositioned() &&
53 !child->style()->logicalTop().isAuto() &&
54 !child->style()->logicalBottom().isAuto();
55 }
56
57 StickyPositionScrollingConstraints* stickyConstraintsForLayoutObject(
58 const LayoutBoxModelObject* o) {
59 if (!o || !o->layer()->ancestorOverflowLayer())
flackr 2017/02/24 01:33:33 Maybe we should pass ancestorOverflowLayer in sinc
smcgruer 2017/02/24 14:17:43 Done, although we still need to null-check |o| as
60 return nullptr;
61
62 PaintLayerScrollableArea* scrollableArea =
63 o->layer()->ancestorOverflowLayer()->getScrollableArea();
64 auto it = scrollableArea->stickyConstraintsMap().find(o->layer());
65 if (it == scrollableArea->stickyConstraintsMap().end())
66 return nullptr;
67
68 return &it->value;
69 }
70
71 // Inclusive of |from|, exclusive of |to|.
72 LayoutBoxModelObject* findFirstStickyBetween(LayoutObject* from,
73 LayoutObject* to) {
74 LayoutObject* maybeStickyAncestor = from;
75 while (maybeStickyAncestor && maybeStickyAncestor != to) {
76 if (maybeStickyAncestor->isStickyPositioned()) {
77 return toLayoutBoxModelObject(maybeStickyAncestor);
78 }
79
80 maybeStickyAncestor =
81 maybeStickyAncestor->isLayoutInline()
82 ? maybeStickyAncestor->containingBlock()
83 : toLayoutBox(maybeStickyAncestor)->locationContainer();
84 }
85 return nullptr;
86 }
87 } // namespace
88
49 class FloatStateForStyleChange { 89 class FloatStateForStyleChange {
50 public: 90 public:
51 static void setWasFloating(LayoutBoxModelObject* boxModelObject, 91 static void setWasFloating(LayoutBoxModelObject* boxModelObject,
52 bool wasFloating) { 92 bool wasFloating) {
53 s_wasFloating = wasFloating; 93 s_wasFloating = wasFloating;
54 s_boxModelObject = boxModelObject; 94 s_boxModelObject = boxModelObject;
55 } 95 }
56 96
57 static bool wasFloating(LayoutBoxModelObject* boxModelObject) { 97 static bool wasFloating(LayoutBoxModelObject* boxModelObject) {
58 ASSERT(boxModelObject == s_boxModelObject); 98 ASSERT(boxModelObject == s_boxModelObject);
(...skipping 596 matching lines...) Expand 10 before | Expand all | Expand 10 after
655 } 695 }
656 696
657 void LayoutBoxModelObject::updateFromStyle() { 697 void LayoutBoxModelObject::updateFromStyle() {
658 const ComputedStyle& styleToUse = styleRef(); 698 const ComputedStyle& styleToUse = styleRef();
659 setHasBoxDecorationBackground(styleToUse.hasBoxDecorationBackground()); 699 setHasBoxDecorationBackground(styleToUse.hasBoxDecorationBackground());
660 setInline(styleToUse.isDisplayInlineType()); 700 setInline(styleToUse.isDisplayInlineType());
661 setPositionState(styleToUse.position()); 701 setPositionState(styleToUse.position());
662 setHorizontalWritingMode(styleToUse.isHorizontalWritingMode()); 702 setHorizontalWritingMode(styleToUse.isHorizontalWritingMode());
663 } 703 }
664 704
665 static inline bool isOutOfFlowPositionedWithImplicitHeight(
666 const LayoutBoxModelObject* child) {
667 return child->isOutOfFlowPositioned() &&
668 !child->style()->logicalTop().isAuto() &&
669 !child->style()->logicalBottom().isAuto();
670 }
671
672 LayoutBlock* LayoutBoxModelObject::containingBlockForAutoHeightDetection( 705 LayoutBlock* LayoutBoxModelObject::containingBlockForAutoHeightDetection(
673 Length logicalHeight) const { 706 Length logicalHeight) const {
674 // For percentage heights: The percentage is calculated with respect to the 707 // For percentage heights: The percentage is calculated with respect to the
675 // height of the generated box's containing block. If the height of the 708 // height of the generated box's containing block. If the height of the
676 // containing block is not specified explicitly (i.e., it depends on content 709 // containing block is not specified explicitly (i.e., it depends on content
677 // height), and this element is not absolutely positioned, the used height is 710 // height), and this element is not absolutely positioned, the used height is
678 // calculated as if 'auto' was specified. 711 // calculated as if 'auto' was specified.
679 if (!logicalHeight.isPercentOrCalc() || isOutOfFlowPositioned()) 712 if (!logicalHeight.isPercentOrCalc() || isOutOfFlowPositioned())
680 return nullptr; 713 return nullptr;
681 714
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
787 820
788 void LayoutBoxModelObject::updateStickyPositionConstraints() const { 821 void LayoutBoxModelObject::updateStickyPositionConstraints() const {
789 const FloatSize constrainingSize = computeStickyConstrainingRect().size(); 822 const FloatSize constrainingSize = computeStickyConstrainingRect().size();
790 823
791 PaintLayerScrollableArea* scrollableArea = 824 PaintLayerScrollableArea* scrollableArea =
792 layer()->ancestorOverflowLayer()->getScrollableArea(); 825 layer()->ancestorOverflowLayer()->getScrollableArea();
793 StickyPositionScrollingConstraints constraints; 826 StickyPositionScrollingConstraints constraints;
794 FloatSize skippedContainersOffset; 827 FloatSize skippedContainersOffset;
795 LayoutBlock* containingBlock = this->containingBlock(); 828 LayoutBlock* containingBlock = this->containingBlock();
796 // The location container for boxes is not always the containing block. 829 // The location container for boxes is not always the containing block.
797 LayoutBox* locationContainer = isLayoutInline() 830 LayoutObject* locationContainer =
798 ? containingBlock 831 isLayoutInline() ? container() : toLayoutBox(this)->locationContainer();
799 : toLayoutBox(this)->locationContainer();
800 // Skip anonymous containing blocks. 832 // Skip anonymous containing blocks.
801 while (containingBlock->isAnonymous()) { 833 while (containingBlock->isAnonymous()) {
802 containingBlock = containingBlock->containingBlock(); 834 containingBlock = containingBlock->containingBlock();
803 } 835 }
804 MapCoordinatesFlags flags = 0; 836 MapCoordinatesFlags flags = IgnoreStickyOffset;
805 skippedContainersOffset = 837 skippedContainersOffset =
806 toFloatSize(locationContainer 838 toFloatSize(locationContainer
807 ->localToAncestorQuadWithoutTransforms( 839 ->localToAncestorQuadWithoutTransforms(
808 FloatQuad(), containingBlock, flags) 840 FloatQuad(), containingBlock, flags)
809 .boundingBox() 841 .boundingBox()
810 .location()); 842 .location());
811 LayoutBox* scrollAncestor = 843 LayoutBox* scrollAncestor =
812 layer()->ancestorOverflowLayer()->isRootLayer() 844 layer()->ancestorOverflowLayer()->isRootLayer()
813 ? nullptr 845 ? nullptr
814 : &toLayoutBox(layer()->ancestorOverflowLayer()->layoutObject()); 846 : &toLayoutBox(layer()->ancestorOverflowLayer()->layoutObject());
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
874 minimumValueForLength(containingBlock->style()->paddingLeft(), 906 minimumValueForLength(containingBlock->style()->paddingLeft(),
875 maxContainerWidth) + 907 maxContainerWidth) +
876 minimumValueForLength(style()->marginLeft(), maxWidth)); 908 minimumValueForLength(style()->marginLeft(), maxWidth));
877 909
878 constraints.setScrollContainerRelativeContainingBlockRect( 910 constraints.setScrollContainerRelativeContainingBlockRect(
879 FloatRect(scrollContainerRelativeContainingBlockRect)); 911 FloatRect(scrollContainerRelativeContainingBlockRect));
880 912
881 FloatRect stickyBoxRect = 913 FloatRect stickyBoxRect =
882 isLayoutInline() ? FloatRect(toLayoutInline(this)->linesBoundingBox()) 914 isLayoutInline() ? FloatRect(toLayoutInline(this)->linesBoundingBox())
883 : FloatRect(toLayoutBox(this)->frameRect()); 915 : FloatRect(toLayoutBox(this)->frameRect());
916
884 FloatRect flippedStickyBoxRect = stickyBoxRect; 917 FloatRect flippedStickyBoxRect = stickyBoxRect;
885 containingBlock->flipForWritingMode(flippedStickyBoxRect); 918 containingBlock->flipForWritingMode(flippedStickyBoxRect);
886 FloatPoint stickyLocation = 919 FloatPoint stickyLocation =
887 flippedStickyBoxRect.location() + skippedContainersOffset; 920 flippedStickyBoxRect.location() + skippedContainersOffset;
888 921
889 // The scrollContainerRelativePaddingBoxRect's position is the padding box so 922 // The scrollContainerRelativePaddingBoxRect's position is the padding box so
890 // we need to remove the border when finding the position of the sticky box 923 // we need to remove the border when finding the position of the sticky box
891 // within the scroll ancestor if the container is not our scroll ancestor. If 924 // within the scroll ancestor if the container is not our scroll ancestor. If
892 // the container is our scroll ancestor, we also need to remove the border 925 // the container is our scroll ancestor, we also need to remove the border
893 // box because we want the position from within the scroller border. 926 // box because we want the position from within the scroller border.
894 FloatSize containerBorderOffset(containingBlock->borderLeft(), 927 FloatSize containerBorderOffset(containingBlock->borderLeft(),
895 containingBlock->borderTop()); 928 containingBlock->borderTop());
896 stickyLocation -= containerBorderOffset; 929 stickyLocation -= containerBorderOffset;
897 constraints.setScrollContainerRelativeStickyBoxRect( 930 constraints.setScrollContainerRelativeStickyBoxRect(
898 FloatRect(scrollContainerRelativePaddingBoxRect.location() + 931 FloatRect(scrollContainerRelativePaddingBoxRect.location() +
899 toFloatSize(stickyLocation), 932 toFloatSize(stickyLocation),
900 flippedStickyBoxRect.size())); 933 flippedStickyBoxRect.size()));
901 934
935 // To correctly compute the offsets, the constraints need to know about any
936 // nested position:sticky elements between themselves and their
937 // containingBlock, and between the containingBlock and their scrollAncestor.
938 //
939 // The respective search ranges are [container, containingBlock) and
940 // [containingBlock, scrollAncestor).
941 constraints.setNearestStickyBoxShiftingStickyBox(
942 findFirstStickyBetween(locationContainer, containingBlock));
943 // We cannot use |scrollAncestor| here as it disregards the root
944 // ancestorOverflowLayer(), which we should include.
945 constraints.setNearestStickyBoxShiftingContainingBlock(findFirstStickyBetween(
946 containingBlock, &layer()->ancestorOverflowLayer()->layoutObject()));
947
902 // We skip the right or top sticky offset if there is not enough space to 948 // We skip the right or top sticky offset if there is not enough space to
903 // honor both the left/right or top/bottom offsets. 949 // honor both the left/right or top/bottom offsets.
904 LayoutUnit horizontalOffsets = 950 LayoutUnit horizontalOffsets =
905 minimumValueForLength(style()->right(), 951 minimumValueForLength(style()->right(),
906 LayoutUnit(constrainingSize.width())) + 952 LayoutUnit(constrainingSize.width())) +
907 minimumValueForLength(style()->left(), 953 minimumValueForLength(style()->left(),
908 LayoutUnit(constrainingSize.width())); 954 LayoutUnit(constrainingSize.width()));
909 bool skipRight = false; 955 bool skipRight = false;
910 bool skipLeft = false; 956 bool skipLeft = false;
911 if (!style()->left().isAuto() && !style()->right().isAuto()) { 957 if (!style()->left().isAuto() && !style()->right().isAuto()) {
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
981 -enclosingClippingBox->borderTop() + enclosingClippingBox->paddingTop()); 1027 -enclosingClippingBox->borderTop() + enclosingClippingBox->paddingTop());
982 constrainingRect.contract( 1028 constrainingRect.contract(
983 FloatSize(enclosingClippingBox->paddingLeft() + 1029 FloatSize(enclosingClippingBox->paddingLeft() +
984 enclosingClippingBox->paddingRight(), 1030 enclosingClippingBox->paddingRight(),
985 enclosingClippingBox->paddingTop() + 1031 enclosingClippingBox->paddingTop() +
986 enclosingClippingBox->paddingBottom())); 1032 enclosingClippingBox->paddingBottom()));
987 return constrainingRect; 1033 return constrainingRect;
988 } 1034 }
989 1035
990 LayoutSize LayoutBoxModelObject::stickyPositionOffset() const { 1036 LayoutSize LayoutBoxModelObject::stickyPositionOffset() const {
991 const PaintLayer* ancestorOverflowLayer = layer()->ancestorOverflowLayer(); 1037 StickyPositionScrollingConstraints* constraints =
992 // TODO: Force compositing input update if we ask for offset before 1038 stickyConstraintsForLayoutObject(this);
993 // compositing inputs have been computed? 1039 if (!constraints)
994 if (!ancestorOverflowLayer)
995 return LayoutSize(); 1040 return LayoutSize();
996 FloatRect constrainingRect = computeStickyConstrainingRect(); 1041
997 PaintLayerScrollableArea* scrollableArea = 1042 StickyPositionScrollingConstraints* shiftingStickyBoxConstraints =
998 ancestorOverflowLayer->getScrollableArea(); 1043 stickyConstraintsForLayoutObject(
1044 constraints->nearestStickyBoxShiftingStickyBox());
1045
1046 StickyPositionScrollingConstraints* shiftingContainingBlockConstraints =
1047 stickyConstraintsForLayoutObject(
1048 constraints->nearestStickyBoxShiftingContainingBlock());
999 1049
1000 // The sticky offset is physical, so we can just return the delta computed in 1050 // The sticky offset is physical, so we can just return the delta computed in
1001 // absolute coords (though it may be wrong with transforms). 1051 // absolute coords (though it may be wrong with transforms).
1002 // TODO: Force compositing input update if we ask for offset with stale 1052 FloatRect constrainingRect = computeStickyConstrainingRect();
1003 // compositing inputs. 1053 return LayoutSize(constraints->computeStickyOffset(
1004 if (!scrollableArea->stickyConstraintsMap().contains(layer())) 1054 constrainingRect, shiftingStickyBoxConstraints,
1005 return LayoutSize(); 1055 shiftingContainingBlockConstraints));
1006 return LayoutSize(
1007 scrollableArea->stickyConstraintsMap().get(layer()).computeStickyOffset(
1008 constrainingRect));
1009 } 1056 }
1010 1057
1011 LayoutPoint LayoutBoxModelObject::adjustedPositionRelativeTo( 1058 LayoutPoint LayoutBoxModelObject::adjustedPositionRelativeTo(
1012 const LayoutPoint& startPoint, 1059 const LayoutPoint& startPoint,
1013 const Element* offsetParent) const { 1060 const Element* offsetParent) const {
1014 // If the element is the HTML body element or doesn't have a parent 1061 // If the element is the HTML body element or doesn't have a parent
1015 // return 0 and stop this algorithm. 1062 // return 0 and stop this algorithm.
1016 if (isBody() || !parent()) 1063 if (isBody() || !parent())
1017 return LayoutPoint(); 1064 return LayoutPoint();
1018 1065
(...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after
1368 if (rootElementStyle->hasBackground()) 1415 if (rootElementStyle->hasBackground())
1369 return false; 1416 return false;
1370 1417
1371 if (node() != document().firstBodyElement()) 1418 if (node() != document().firstBodyElement())
1372 return false; 1419 return false;
1373 1420
1374 return true; 1421 return true;
1375 } 1422 }
1376 1423
1377 } // namespace blink 1424 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698