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

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

Issue 1709963002: [css-align] New CSS Value 'normal' for Self Alignment (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Minor changes. Created 4 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) 2011 Google Inc. All rights reserved. 2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 24 matching lines...) Expand all
35 #include "core/layout/TextAutosizer.h" 35 #include "core/layout/TextAutosizer.h"
36 #include "core/paint/BlockPainter.h" 36 #include "core/paint/BlockPainter.h"
37 #include "core/paint/PaintLayer.h" 37 #include "core/paint/PaintLayer.h"
38 #include "core/style/ComputedStyle.h" 38 #include "core/style/ComputedStyle.h"
39 #include "platform/LengthFunctions.h" 39 #include "platform/LengthFunctions.h"
40 #include "wtf/MathExtras.h" 40 #include "wtf/MathExtras.h"
41 #include <limits> 41 #include <limits>
42 42
43 namespace blink { 43 namespace blink {
44 44
45 static const ItemPosition selfAlignmentNormalBehavior = ItemPositionStretch;
46
45 static bool hasAspectRatio(const LayoutBox& child) 47 static bool hasAspectRatio(const LayoutBox& child)
46 { 48 {
47 return child.isImage() || child.isCanvas() || child.isVideo(); 49 return child.isImage() || child.isCanvas() || child.isVideo();
48 } 50 }
49 51
50 struct LayoutFlexibleBox::LineContext { 52 struct LayoutFlexibleBox::LineContext {
51 LineContext(LayoutUnit crossAxisOffset, LayoutUnit crossAxisExtent, size_t n umberOfChildren, LayoutUnit maxAscent) 53 LineContext(LayoutUnit crossAxisOffset, LayoutUnit crossAxisExtent, size_t n umberOfChildren, LayoutUnit maxAscent)
52 : crossAxisOffset(crossAxisOffset) 54 : crossAxisOffset(crossAxisOffset)
53 , crossAxisExtent(crossAxisExtent) 55 , crossAxisExtent(crossAxisExtent)
54 , numberOfChildren(numberOfChildren) 56 , numberOfChildren(numberOfChildren)
(...skipping 13 matching lines...) Expand all
68 , childSize(childSize) 70 , childSize(childSize)
69 , childInnerFlexBaseSize(childInnerFlexBaseSize) 71 , childInnerFlexBaseSize(childInnerFlexBaseSize)
70 { 72 {
71 } 73 }
72 74
73 LayoutBox* child; 75 LayoutBox* child;
74 LayoutUnit childSize; 76 LayoutUnit childSize;
75 LayoutUnit childInnerFlexBaseSize; 77 LayoutUnit childInnerFlexBaseSize;
76 }; 78 };
77 79
78
79 LayoutFlexibleBox::LayoutFlexibleBox(Element* element) 80 LayoutFlexibleBox::LayoutFlexibleBox(Element* element)
80 : LayoutBlock(element) 81 : LayoutBlock(element)
81 , m_orderIterator(this) 82 , m_orderIterator(this)
82 , m_numberOfInFlowChildrenOnFirstLine(-1) 83 , m_numberOfInFlowChildrenOnFirstLine(-1)
83 { 84 {
84 ASSERT(!childrenInline()); 85 ASSERT(!childrenInline());
85 } 86 }
86 87
87 LayoutFlexibleBox::~LayoutFlexibleBox() 88 LayoutFlexibleBox::~LayoutFlexibleBox()
88 { 89 {
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 int LayoutFlexibleBox::baselinePosition(FontBaseline, bool, LineDirectionMode di rection, LinePositionMode mode) const 175 int LayoutFlexibleBox::baselinePosition(FontBaseline, bool, LineDirectionMode di rection, LinePositionMode mode) const
175 { 176 {
176 ASSERT(mode == PositionOnContainingLine); 177 ASSERT(mode == PositionOnContainingLine);
177 int baseline = firstLineBoxBaseline(); 178 int baseline = firstLineBoxBaseline();
178 if (baseline == -1) 179 if (baseline == -1)
179 baseline = synthesizedBaselineFromContentBox(*this, direction); 180 baseline = synthesizedBaselineFromContentBox(*this, direction);
180 181
181 return beforeMarginInLineDirection(direction) + baseline; 182 return beforeMarginInLineDirection(direction) + baseline;
182 } 183 }
183 184
184 static const StyleContentAlignmentData& normalValueBehavior() 185 static const StyleContentAlignmentData& contentAlignmentNormalBehavior()
185 { 186 {
186 // The justify-content property applies along the main axis, but since flexi ng 187 // The justify-content property applies along the main axis, but since flexi ng
187 // in the main axis is controlled by flex, stretch behaves as flex-start (ig noring 188 // in the main axis is controlled by flex, stretch behaves as flex-start (ig noring
188 // the specified fallback alignment, if any). 189 // the specified fallback alignment, if any).
189 // https://drafts.csswg.org/css-align/#distribution-flex 190 // https://drafts.csswg.org/css-align/#distribution-flex
190 static const StyleContentAlignmentData normalBehavior = {ContentPositionNorm al, ContentDistributionStretch}; 191 static const StyleContentAlignmentData normalBehavior = {ContentPositionNorm al, ContentDistributionStretch};
191 return normalBehavior; 192 return normalBehavior;
192 } 193 }
193 194
194 int LayoutFlexibleBox::firstLineBoxBaseline() const 195 int LayoutFlexibleBox::firstLineBoxBaseline() const
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
244 void LayoutFlexibleBox::removeChild(LayoutObject* child) 245 void LayoutFlexibleBox::removeChild(LayoutObject* child)
245 { 246 {
246 LayoutBlock::removeChild(child); 247 LayoutBlock::removeChild(child);
247 m_intrinsicSizeAlongMainAxis.remove(child); 248 m_intrinsicSizeAlongMainAxis.remove(child);
248 } 249 }
249 250
250 void LayoutFlexibleBox::styleDidChange(StyleDifference diff, const ComputedStyle * oldStyle) 251 void LayoutFlexibleBox::styleDidChange(StyleDifference diff, const ComputedStyle * oldStyle)
251 { 252 {
252 LayoutBlock::styleDidChange(diff, oldStyle); 253 LayoutBlock::styleDidChange(diff, oldStyle);
253 254
254 if (oldStyle && oldStyle->alignItemsPosition() == ItemPositionStretch && dif f.needsFullLayout()) { 255 if (oldStyle && oldStyle->resolvedAlignItems(selfAlignmentNormalBehavior).po sition() == ItemPositionStretch && diff.needsFullLayout()) {
255 // Flex items that were previously stretching need to be relayed out so we can compute new available cross axis space. 256 // Flex items that were previously stretching need to be relayed out so we can compute new available cross axis space.
256 // This is only necessary for stretching since other alignment values do n't change the size of the box. 257 // This is only necessary for stretching since other alignment values do n't change the size of the box.
258 const ComputedStyle& newStyle = styleRef();
257 for (LayoutBox* child = firstChildBox(); child; child = child->nextSibli ngBox()) { 259 for (LayoutBox* child = firstChildBox(); child; child = child->nextSibli ngBox()) {
258 ItemPosition previousAlignment = ComputedStyle::resolveAlignment(*ol dStyle, child->styleRef(), ItemPositionStretch); 260 const ComputedStyle& childStyle = child->styleRef();
259 if (previousAlignment == ItemPositionStretch && previousAlignment != ComputedStyle::resolveAlignment(styleRef(), child->styleRef(), ItemPositionStre tch)) 261 ItemPosition previousAlignment = childStyle.resolvedAlignSelf(*oldSt yle, selfAlignmentNormalBehavior).position();
262 if (previousAlignment == ItemPositionStretch && previousAlignment != childStyle.resolvedAlignSelf(newStyle, selfAlignmentNormalBehavior).position())
260 child->setChildNeedsLayout(MarkOnlyThis); 263 child->setChildNeedsLayout(MarkOnlyThis);
261 } 264 }
262 } 265 }
263 } 266 }
264 267
265 void LayoutFlexibleBox::layoutBlock(bool relayoutChildren) 268 void LayoutFlexibleBox::layoutBlock(bool relayoutChildren)
266 { 269 {
267 ASSERT(needsLayout()); 270 ASSERT(needsLayout());
268 271
269 if (!relayoutChildren && simplifiedLayout()) 272 if (!relayoutChildren && simplifiedLayout())
(...skipping 948 matching lines...) Expand 10 before | Expand all | Expand 10 after
1218 LayoutUnit staticBlockPosition = isColumnFlow() ? mainAxisOffset : crossAxis Offset; 1221 LayoutUnit staticBlockPosition = isColumnFlow() ? mainAxisOffset : crossAxis Offset;
1219 if (childLayer->staticBlockPosition() != staticBlockPosition) { 1222 if (childLayer->staticBlockPosition() != staticBlockPosition) {
1220 childLayer->setStaticBlockPosition(staticBlockPosition); 1223 childLayer->setStaticBlockPosition(staticBlockPosition);
1221 if (child.style()->hasStaticBlockPosition(style()->isHorizontalWritingMo de())) 1224 if (child.style()->hasStaticBlockPosition(style()->isHorizontalWritingMo de()))
1222 child.setChildNeedsLayout(MarkOnlyThis); 1225 child.setChildNeedsLayout(MarkOnlyThis);
1223 } 1226 }
1224 } 1227 }
1225 1228
1226 ItemPosition LayoutFlexibleBox::alignmentForChild(const LayoutBox& child) const 1229 ItemPosition LayoutFlexibleBox::alignmentForChild(const LayoutBox& child) const
1227 { 1230 {
1228 ItemPosition align = ComputedStyle::resolveAlignment(styleRef(), child.style Ref(), ItemPositionStretch); 1231 ItemPosition align = child.styleRef().resolvedAlignSelf(styleRef(), selfAlig nmentNormalBehavior).position();
1229 1232
1230 if (align == ItemPositionBaseline && hasOrthogonalFlow(child)) 1233 if (align == ItemPositionBaseline && hasOrthogonalFlow(child))
1231 align = ItemPositionFlexStart; 1234 align = ItemPositionFlexStart;
1232 1235
1233 if (style()->flexWrap() == FlexWrapReverse) { 1236 if (style()->flexWrap() == FlexWrapReverse) {
1234 if (align == ItemPositionFlexStart) 1237 if (align == ItemPositionFlexStart)
1235 align = ItemPositionFlexEnd; 1238 align = ItemPositionFlexEnd;
1236 else if (align == ItemPositionFlexEnd) 1239 else if (align == ItemPositionFlexEnd)
1237 align = ItemPositionFlexStart; 1240 align = ItemPositionFlexStart;
1238 } 1241 }
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
1311 { 1314 {
1312 if (isHorizontalFlow()) 1315 if (isHorizontalFlow())
1313 return child.styleRef().overflowY(); 1316 return child.styleRef().overflowY();
1314 return child.styleRef().overflowX(); 1317 return child.styleRef().overflowX();
1315 } 1318 }
1316 1319
1317 void LayoutFlexibleBox::layoutAndPlaceChildren(LayoutUnit& crossAxisOffset, cons t OrderedFlexItemList& children, const Vector<LayoutUnit, 16>& childSizes, Layou tUnit availableFreeSpace, bool relayoutChildren, SubtreeLayoutScope& layoutScope , Vector<LineContext>& lineContexts) 1320 void LayoutFlexibleBox::layoutAndPlaceChildren(LayoutUnit& crossAxisOffset, cons t OrderedFlexItemList& children, const Vector<LayoutUnit, 16>& childSizes, Layou tUnit availableFreeSpace, bool relayoutChildren, SubtreeLayoutScope& layoutScope , Vector<LineContext>& lineContexts)
1318 { 1321 {
1319 ASSERT(childSizes.size() == children.size()); 1322 ASSERT(childSizes.size() == children.size());
1320 1323
1321 ContentPosition position = styleRef().resolvedJustifyContentPosition(normalV alueBehavior()); 1324 ContentPosition position = styleRef().resolvedJustifyContentPosition(content AlignmentNormalBehavior());
1322 ContentDistributionType distribution = styleRef().resolvedJustifyContentDist ribution(normalValueBehavior()); 1325 ContentDistributionType distribution = styleRef().resolvedJustifyContentDist ribution(contentAlignmentNormalBehavior());
1323 1326
1324 size_t numberOfChildrenForJustifyContent = numberOfInFlowPositionedChildren( children); 1327 size_t numberOfChildrenForJustifyContent = numberOfInFlowPositionedChildren( children);
1325 LayoutUnit autoMarginOffset = autoMarginOffsetInMainAxis(children, available FreeSpace); 1328 LayoutUnit autoMarginOffset = autoMarginOffsetInMainAxis(children, available FreeSpace);
1326 LayoutUnit mainAxisOffset = flowAwareBorderStart() + flowAwarePaddingStart() ; 1329 LayoutUnit mainAxisOffset = flowAwareBorderStart() + flowAwarePaddingStart() ;
1327 mainAxisOffset += initialJustifyContentOffset(availableFreeSpace, position, distribution, numberOfChildrenForJustifyContent); 1330 mainAxisOffset += initialJustifyContentOffset(availableFreeSpace, position, distribution, numberOfChildrenForJustifyContent);
1328 if (style()->flexDirection() == FlowRowReverse) 1331 if (style()->flexDirection() == FlowRowReverse)
1329 mainAxisOffset += isHorizontalFlow() ? verticalScrollbarWidth() : horizo ntalScrollbarHeight(); 1332 mainAxisOffset += isHorizontalFlow() ? verticalScrollbarWidth() : horizo ntalScrollbarHeight();
1330 1333
1331 LayoutUnit totalMainExtent = mainAxisExtent(); 1334 LayoutUnit totalMainExtent = mainAxisExtent();
1332 LayoutUnit maxAscent, maxDescent; // Used when align-items: baseline. 1335 LayoutUnit maxAscent, maxDescent; // Used when align-items: baseline.
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
1405 } 1408 }
1406 1409
1407 if (m_numberOfInFlowChildrenOnFirstLine == -1) 1410 if (m_numberOfInFlowChildrenOnFirstLine == -1)
1408 m_numberOfInFlowChildrenOnFirstLine = seenInFlowPositionedChildren; 1411 m_numberOfInFlowChildrenOnFirstLine = seenInFlowPositionedChildren;
1409 lineContexts.append(LineContext(crossAxisOffset, maxChildCrossAxisExtent, ch ildren.size(), maxAscent)); 1412 lineContexts.append(LineContext(crossAxisOffset, maxChildCrossAxisExtent, ch ildren.size(), maxAscent));
1410 crossAxisOffset += maxChildCrossAxisExtent; 1413 crossAxisOffset += maxChildCrossAxisExtent;
1411 } 1414 }
1412 1415
1413 void LayoutFlexibleBox::layoutColumnReverse(const OrderedFlexItemList& children, LayoutUnit crossAxisOffset, LayoutUnit availableFreeSpace) 1416 void LayoutFlexibleBox::layoutColumnReverse(const OrderedFlexItemList& children, LayoutUnit crossAxisOffset, LayoutUnit availableFreeSpace)
1414 { 1417 {
1415 ContentPosition position = styleRef().resolvedJustifyContentPosition(normalV alueBehavior()); 1418 ContentPosition position = styleRef().resolvedJustifyContentPosition(content AlignmentNormalBehavior());
1416 ContentDistributionType distribution = styleRef().resolvedJustifyContentDist ribution(normalValueBehavior()); 1419 ContentDistributionType distribution = styleRef().resolvedJustifyContentDist ribution(contentAlignmentNormalBehavior());
1417 1420
1418 // This is similar to the logic in layoutAndPlaceChildren, except we place t he children 1421 // This is similar to the logic in layoutAndPlaceChildren, except we place t he children
1419 // starting from the end of the flexbox. We also don't need to layout anythi ng since we're 1422 // starting from the end of the flexbox. We also don't need to layout anythi ng since we're
1420 // just moving the children to a new position. 1423 // just moving the children to a new position.
1421 size_t numberOfChildrenForJustifyContent = numberOfInFlowPositionedChildren( children); 1424 size_t numberOfChildrenForJustifyContent = numberOfInFlowPositionedChildren( children);
1422 LayoutUnit mainAxisOffset = logicalHeight() - flowAwareBorderEnd() - flowAwa rePaddingEnd(); 1425 LayoutUnit mainAxisOffset = logicalHeight() - flowAwareBorderEnd() - flowAwa rePaddingEnd();
1423 mainAxisOffset -= initialJustifyContentOffset(availableFreeSpace, position, distribution, numberOfChildrenForJustifyContent); 1426 mainAxisOffset -= initialJustifyContentOffset(availableFreeSpace, position, distribution, numberOfChildrenForJustifyContent);
1424 mainAxisOffset -= isHorizontalFlow() ? verticalScrollbarWidth() : horizontal ScrollbarHeight(); 1427 mainAxisOffset -= isHorizontalFlow() ? verticalScrollbarWidth() : horizontal ScrollbarHeight();
1425 1428
1426 size_t seenInFlowPositionedChildren = 0; 1429 size_t seenInFlowPositionedChildren = 0;
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
1466 if (alignContentDistribution == ContentDistributionSpaceBetween) 1469 if (alignContentDistribution == ContentDistributionSpaceBetween)
1467 return availableFreeSpace / (numberOfLines - 1); 1470 return availableFreeSpace / (numberOfLines - 1);
1468 if (alignContentDistribution == ContentDistributionSpaceAround || alignC ontentDistribution == ContentDistributionStretch) 1471 if (alignContentDistribution == ContentDistributionSpaceAround || alignC ontentDistribution == ContentDistributionStretch)
1469 return availableFreeSpace / numberOfLines; 1472 return availableFreeSpace / numberOfLines;
1470 } 1473 }
1471 return LayoutUnit(); 1474 return LayoutUnit();
1472 } 1475 }
1473 1476
1474 void LayoutFlexibleBox::alignFlexLines(Vector<LineContext>& lineContexts) 1477 void LayoutFlexibleBox::alignFlexLines(Vector<LineContext>& lineContexts)
1475 { 1478 {
1476 ContentPosition position = styleRef().resolvedAlignContentPosition(normalVal ueBehavior()); 1479 ContentPosition position = styleRef().resolvedAlignContentPosition(contentAl ignmentNormalBehavior());
1477 ContentDistributionType distribution = styleRef().resolvedAlignContentDistri bution(normalValueBehavior()); 1480 ContentDistributionType distribution = styleRef().resolvedAlignContentDistri bution(contentAlignmentNormalBehavior());
1478 1481
1479 // If we have a single line flexbox or a multiline line flexbox with only on e flex line, 1482 // If we have a single line flexbox or a multiline line flexbox with only on e flex line,
1480 // the line height is all the available space. 1483 // the line height is all the available space.
1481 // For flex-direction: row, this means we need to use the height, so we do t his after calling updateLogicalHeight. 1484 // For flex-direction: row, this means we need to use the height, so we do t his after calling updateLogicalHeight.
1482 if (lineContexts.size() == 1) { 1485 if (lineContexts.size() == 1) {
1483 lineContexts[0].crossAxisExtent = crossAxisContentExtent(); 1486 lineContexts[0].crossAxisExtent = crossAxisContentExtent();
1484 return; 1487 return;
1485 } 1488 }
1486 1489
1487 if (position == ContentPositionFlexStart) 1490 if (position == ContentPositionFlexStart)
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
1537 if (style()->flexWrap() == FlexWrapReverse) 1540 if (style()->flexWrap() == FlexWrapReverse)
1538 adjustAlignmentForChild(*child, lineCrossAxisExtent); 1541 adjustAlignmentForChild(*child, lineCrossAxisExtent);
1539 continue; 1542 continue;
1540 } 1543 }
1541 1544
1542 if (updateAutoMarginsInCrossAxis(*child, std::max(LayoutUnit(), avai lableAlignmentSpaceForChild(lineCrossAxisExtent, *child)))) 1545 if (updateAutoMarginsInCrossAxis(*child, std::max(LayoutUnit(), avai lableAlignmentSpaceForChild(lineCrossAxisExtent, *child))))
1543 continue; 1546 continue;
1544 1547
1545 switch (alignmentForChild(*child)) { 1548 switch (alignmentForChild(*child)) {
1546 case ItemPositionAuto: 1549 case ItemPositionAuto:
1550 case ItemPositionNormal:
1547 ASSERT_NOT_REACHED(); 1551 ASSERT_NOT_REACHED();
1548 break; 1552 break;
1549 case ItemPositionStretch: { 1553 case ItemPositionStretch: {
1550 applyStretchAlignmentToChild(*child, lineCrossAxisExtent); 1554 applyStretchAlignmentToChild(*child, lineCrossAxisExtent);
1551 // Since wrap-reverse flips cross start and cross end, strech ch ildren should be aligned with the cross end. 1555 // Since wrap-reverse flips cross start and cross end, strech ch ildren should be aligned with the cross end.
1552 if (style()->flexWrap() == FlexWrapReverse) 1556 if (style()->flexWrap() == FlexWrapReverse)
1553 adjustAlignmentForChild(*child, availableAlignmentSpaceForCh ild(lineCrossAxisExtent, *child)); 1557 adjustAlignmentForChild(*child, availableAlignmentSpaceForCh ild(lineCrossAxisExtent, *child));
1554 break; 1558 break;
1555 } 1559 }
1556 case ItemPositionFlexStart: 1560 case ItemPositionFlexStart:
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
1662 ASSERT(child); 1666 ASSERT(child);
1663 LayoutUnit lineCrossAxisExtent = lineContexts[lineNumber].crossAxisE xtent; 1667 LayoutUnit lineCrossAxisExtent = lineContexts[lineNumber].crossAxisE xtent;
1664 LayoutUnit originalOffset = lineContexts[lineNumber].crossAxisOffset - crossAxisStartEdge; 1668 LayoutUnit originalOffset = lineContexts[lineNumber].crossAxisOffset - crossAxisStartEdge;
1665 LayoutUnit newOffset = contentExtent - originalOffset - lineCrossAxi sExtent; 1669 LayoutUnit newOffset = contentExtent - originalOffset - lineCrossAxi sExtent;
1666 adjustAlignmentForChild(*child, newOffset - originalOffset); 1670 adjustAlignmentForChild(*child, newOffset - originalOffset);
1667 } 1671 }
1668 } 1672 }
1669 } 1673 }
1670 1674
1671 } // namespace blink 1675 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698