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

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* obj,
59 const PaintLayer* ancestorOverflowLayer) {
60 if (!obj)
61 return nullptr;
62
63 PaintLayerScrollableArea* scrollableArea =
64 ancestorOverflowLayer->getScrollableArea();
65 auto it = scrollableArea->stickyConstraintsMap().find(obj->layer());
66 if (it == scrollableArea->stickyConstraintsMap().end())
67 return nullptr;
68
69 return &it->value;
70 }
71
72 // Inclusive of |from|, exclusive of |to|.
73 LayoutBoxModelObject* findFirstStickyBetween(LayoutObject* from,
74 LayoutObject* to) {
75 LayoutObject* maybeStickyAncestor = from;
76 while (maybeStickyAncestor && maybeStickyAncestor != to) {
77 if (maybeStickyAncestor->isStickyPositioned()) {
78 return toLayoutBoxModelObject(maybeStickyAncestor);
79 }
80
81 maybeStickyAncestor =
82 maybeStickyAncestor->isLayoutInline()
83 ? maybeStickyAncestor->containingBlock()
84 : toLayoutBox(maybeStickyAncestor)->locationContainer();
85 }
86 return nullptr;
87 }
88 } // namespace
89
49 class FloatStateForStyleChange { 90 class FloatStateForStyleChange {
50 public: 91 public:
51 static void setWasFloating(LayoutBoxModelObject* boxModelObject, 92 static void setWasFloating(LayoutBoxModelObject* boxModelObject,
52 bool wasFloating) { 93 bool wasFloating) {
53 s_wasFloating = wasFloating; 94 s_wasFloating = wasFloating;
54 s_boxModelObject = boxModelObject; 95 s_boxModelObject = boxModelObject;
55 } 96 }
56 97
57 static bool wasFloating(LayoutBoxModelObject* boxModelObject) { 98 static bool wasFloating(LayoutBoxModelObject* boxModelObject) {
58 ASSERT(boxModelObject == s_boxModelObject); 99 ASSERT(boxModelObject == s_boxModelObject);
(...skipping 596 matching lines...) Expand 10 before | Expand all | Expand 10 after
655 } 696 }
656 697
657 void LayoutBoxModelObject::updateFromStyle() { 698 void LayoutBoxModelObject::updateFromStyle() {
658 const ComputedStyle& styleToUse = styleRef(); 699 const ComputedStyle& styleToUse = styleRef();
659 setHasBoxDecorationBackground(styleToUse.hasBoxDecorationBackground()); 700 setHasBoxDecorationBackground(styleToUse.hasBoxDecorationBackground());
660 setInline(styleToUse.isDisplayInlineType()); 701 setInline(styleToUse.isDisplayInlineType());
661 setPositionState(styleToUse.position()); 702 setPositionState(styleToUse.position());
662 setHorizontalWritingMode(styleToUse.isHorizontalWritingMode()); 703 setHorizontalWritingMode(styleToUse.isHorizontalWritingMode());
663 } 704 }
664 705
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( 706 LayoutBlock* LayoutBoxModelObject::containingBlockForAutoHeightDetection(
673 Length logicalHeight) const { 707 Length logicalHeight) const {
674 // For percentage heights: The percentage is calculated with respect to the 708 // 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 709 // 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 710 // 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 711 // height), and this element is not absolutely positioned, the used height is
678 // calculated as if 'auto' was specified. 712 // calculated as if 'auto' was specified.
679 if (!logicalHeight.isPercentOrCalc() || isOutOfFlowPositioned()) 713 if (!logicalHeight.isPercentOrCalc() || isOutOfFlowPositioned())
680 return nullptr; 714 return nullptr;
681 715
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
787 821
788 void LayoutBoxModelObject::updateStickyPositionConstraints() const { 822 void LayoutBoxModelObject::updateStickyPositionConstraints() const {
789 const FloatSize constrainingSize = computeStickyConstrainingRect().size(); 823 const FloatSize constrainingSize = computeStickyConstrainingRect().size();
790 824
791 PaintLayerScrollableArea* scrollableArea = 825 PaintLayerScrollableArea* scrollableArea =
792 layer()->ancestorOverflowLayer()->getScrollableArea(); 826 layer()->ancestorOverflowLayer()->getScrollableArea();
793 StickyPositionScrollingConstraints constraints; 827 StickyPositionScrollingConstraints constraints;
794 FloatSize skippedContainersOffset; 828 FloatSize skippedContainersOffset;
795 LayoutBlock* containingBlock = this->containingBlock(); 829 LayoutBlock* containingBlock = this->containingBlock();
796 // The location container for boxes is not always the containing block. 830 // The location container for boxes is not always the containing block.
797 LayoutBox* locationContainer = isLayoutInline() 831 LayoutObject* locationContainer =
798 ? containingBlock 832 isLayoutInline() ? container() : toLayoutBox(this)->locationContainer();
799 : toLayoutBox(this)->locationContainer();
800 // Skip anonymous containing blocks. 833 // Skip anonymous containing blocks.
801 while (containingBlock->isAnonymous()) { 834 while (containingBlock->isAnonymous()) {
802 containingBlock = containingBlock->containingBlock(); 835 containingBlock = containingBlock->containingBlock();
803 } 836 }
804 MapCoordinatesFlags flags = 0; 837 MapCoordinatesFlags flags = IgnoreStickyOffset;
805 skippedContainersOffset = 838 skippedContainersOffset =
806 toFloatSize(locationContainer 839 toFloatSize(locationContainer
807 ->localToAncestorQuadWithoutTransforms( 840 ->localToAncestorQuadWithoutTransforms(
808 FloatQuad(), containingBlock, flags) 841 FloatQuad(), containingBlock, flags)
809 .boundingBox() 842 .boundingBox()
810 .location()); 843 .location());
811 LayoutBox* scrollAncestor = 844 LayoutBox* scrollAncestor =
812 layer()->ancestorOverflowLayer()->isRootLayer() 845 layer()->ancestorOverflowLayer()->isRootLayer()
813 ? nullptr 846 ? nullptr
814 : &toLayoutBox(layer()->ancestorOverflowLayer()->layoutObject()); 847 : &toLayoutBox(layer()->ancestorOverflowLayer()->layoutObject());
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
874 minimumValueForLength(containingBlock->style()->paddingLeft(), 907 minimumValueForLength(containingBlock->style()->paddingLeft(),
875 maxContainerWidth) + 908 maxContainerWidth) +
876 minimumValueForLength(style()->marginLeft(), maxWidth)); 909 minimumValueForLength(style()->marginLeft(), maxWidth));
877 910
878 constraints.setScrollContainerRelativeContainingBlockRect( 911 constraints.setScrollContainerRelativeContainingBlockRect(
879 FloatRect(scrollContainerRelativeContainingBlockRect)); 912 FloatRect(scrollContainerRelativeContainingBlockRect));
880 913
881 FloatRect stickyBoxRect = 914 FloatRect stickyBoxRect =
882 isLayoutInline() ? FloatRect(toLayoutInline(this)->linesBoundingBox()) 915 isLayoutInline() ? FloatRect(toLayoutInline(this)->linesBoundingBox())
883 : FloatRect(toLayoutBox(this)->frameRect()); 916 : FloatRect(toLayoutBox(this)->frameRect());
917
884 FloatRect flippedStickyBoxRect = stickyBoxRect; 918 FloatRect flippedStickyBoxRect = stickyBoxRect;
885 containingBlock->flipForWritingMode(flippedStickyBoxRect); 919 containingBlock->flipForWritingMode(flippedStickyBoxRect);
886 FloatPoint stickyLocation = 920 FloatPoint stickyLocation =
887 flippedStickyBoxRect.location() + skippedContainersOffset; 921 flippedStickyBoxRect.location() + skippedContainersOffset;
888 922
889 // The scrollContainerRelativePaddingBoxRect's position is the padding box so 923 // The scrollContainerRelativePaddingBoxRect's position is the padding box so
890 // we need to remove the border when finding the position of the sticky box 924 // 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 925 // 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 926 // 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. 927 // box because we want the position from within the scroller border.
894 FloatSize containerBorderOffset(containingBlock->borderLeft(), 928 FloatSize containerBorderOffset(containingBlock->borderLeft(),
895 containingBlock->borderTop()); 929 containingBlock->borderTop());
896 stickyLocation -= containerBorderOffset; 930 stickyLocation -= containerBorderOffset;
897 constraints.setScrollContainerRelativeStickyBoxRect( 931 constraints.setScrollContainerRelativeStickyBoxRect(
898 FloatRect(scrollContainerRelativePaddingBoxRect.location() + 932 FloatRect(scrollContainerRelativePaddingBoxRect.location() +
899 toFloatSize(stickyLocation), 933 toFloatSize(stickyLocation),
900 flippedStickyBoxRect.size())); 934 flippedStickyBoxRect.size()));
901 935
936 // To correctly compute the offsets, the constraints need to know about any
937 // nested position:sticky elements between themselves and their
938 // containingBlock, and between the containingBlock and their scrollAncestor.
939 //
940 // The respective search ranges are [container, containingBlock) and
941 // [containingBlock, scrollAncestor).
942 constraints.setNearestStickyBoxShiftingStickyBox(
943 findFirstStickyBetween(locationContainer, containingBlock));
944 // We cannot use |scrollAncestor| here as it disregards the root
945 // ancestorOverflowLayer(), which we should include.
946 constraints.setNearestStickyBoxShiftingContainingBlock(findFirstStickyBetween(
947 containingBlock, &layer()->ancestorOverflowLayer()->layoutObject()));
948
902 // We skip the right or top sticky offset if there is not enough space to 949 // 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. 950 // honor both the left/right or top/bottom offsets.
904 LayoutUnit horizontalOffsets = 951 LayoutUnit horizontalOffsets =
905 minimumValueForLength(style()->right(), 952 minimumValueForLength(style()->right(),
906 LayoutUnit(constrainingSize.width())) + 953 LayoutUnit(constrainingSize.width())) +
907 minimumValueForLength(style()->left(), 954 minimumValueForLength(style()->left(),
908 LayoutUnit(constrainingSize.width())); 955 LayoutUnit(constrainingSize.width()));
909 bool skipRight = false; 956 bool skipRight = false;
910 bool skipLeft = false; 957 bool skipLeft = false;
911 if (!style()->left().isAuto() && !style()->right().isAuto()) { 958 if (!style()->left().isAuto() && !style()->right().isAuto()) {
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
983 FloatSize(enclosingClippingBox->paddingLeft() + 1030 FloatSize(enclosingClippingBox->paddingLeft() +
984 enclosingClippingBox->paddingRight(), 1031 enclosingClippingBox->paddingRight(),
985 enclosingClippingBox->paddingTop() + 1032 enclosingClippingBox->paddingTop() +
986 enclosingClippingBox->paddingBottom())); 1033 enclosingClippingBox->paddingBottom()));
987 return constrainingRect; 1034 return constrainingRect;
988 } 1035 }
989 1036
990 LayoutSize LayoutBoxModelObject::stickyPositionOffset() const { 1037 LayoutSize LayoutBoxModelObject::stickyPositionOffset() const {
991 const PaintLayer* ancestorOverflowLayer = layer()->ancestorOverflowLayer(); 1038 const PaintLayer* ancestorOverflowLayer = layer()->ancestorOverflowLayer();
992 // TODO: Force compositing input update if we ask for offset before 1039 // TODO: Force compositing input update if we ask for offset before
993 // compositing inputs have been computed? 1040 // compositing inputs have been computed?
flackr 2017/02/24 15:07:37 I don't think we'd want to do this here, as I thin
smcgruer 2017/02/24 15:27:56 Would you like me to remove this comment then? It
994 if (!ancestorOverflowLayer) 1041 if (!ancestorOverflowLayer)
995 return LayoutSize(); 1042 return LayoutSize();
996 FloatRect constrainingRect = computeStickyConstrainingRect(); 1043
997 PaintLayerScrollableArea* scrollableArea = 1044 StickyPositionScrollingConstraints* constraints =
998 ancestorOverflowLayer->getScrollableArea(); 1045 stickyConstraintsForLayoutObject(this, ancestorOverflowLayer);
1046 if (!constraints)
1047 return LayoutSize();
1048
1049 StickyPositionScrollingConstraints* shiftingStickyBoxConstraints =
1050 stickyConstraintsForLayoutObject(
1051 constraints->nearestStickyBoxShiftingStickyBox(),
1052 ancestorOverflowLayer);
1053
1054 StickyPositionScrollingConstraints* shiftingContainingBlockConstraints =
1055 stickyConstraintsForLayoutObject(
1056 constraints->nearestStickyBoxShiftingContainingBlock(),
1057 ancestorOverflowLayer);
999 1058
1000 // The sticky offset is physical, so we can just return the delta computed in 1059 // The sticky offset is physical, so we can just return the delta computed in
1001 // absolute coords (though it may be wrong with transforms). 1060 // absolute coords (though it may be wrong with transforms).
1002 // TODO: Force compositing input update if we ask for offset with stale 1061 FloatRect constrainingRect = computeStickyConstrainingRect();
1003 // compositing inputs. 1062 return LayoutSize(constraints->computeStickyOffset(
1004 if (!scrollableArea->stickyConstraintsMap().contains(layer())) 1063 constrainingRect, shiftingStickyBoxConstraints,
1005 return LayoutSize(); 1064 shiftingContainingBlockConstraints));
1006 return LayoutSize(
1007 scrollableArea->stickyConstraintsMap().get(layer()).computeStickyOffset(
1008 constrainingRect));
1009 } 1065 }
1010 1066
1011 LayoutPoint LayoutBoxModelObject::adjustedPositionRelativeTo( 1067 LayoutPoint LayoutBoxModelObject::adjustedPositionRelativeTo(
1012 const LayoutPoint& startPoint, 1068 const LayoutPoint& startPoint,
1013 const Element* offsetParent) const { 1069 const Element* offsetParent) const {
1014 // If the element is the HTML body element or doesn't have a parent 1070 // If the element is the HTML body element or doesn't have a parent
1015 // return 0 and stop this algorithm. 1071 // return 0 and stop this algorithm.
1016 if (isBody() || !parent()) 1072 if (isBody() || !parent())
1017 return LayoutPoint(); 1073 return LayoutPoint();
1018 1074
(...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after
1368 if (rootElementStyle->hasBackground()) 1424 if (rootElementStyle->hasBackground())
1369 return false; 1425 return false;
1370 1426
1371 if (node() != document().firstBodyElement()) 1427 if (node() != document().firstBodyElement())
1372 return false; 1428 return false;
1373 1429
1374 return true; 1430 return true;
1375 } 1431 }
1376 1432
1377 } // namespace blink 1433 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698