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

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: Created 4 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 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 942 matching lines...) Expand 10 before | Expand all | Expand 10 after
1212 LayoutUnit staticBlockPosition = isColumnFlow() ? mainAxisOffset : crossAxis Offset; 1215 LayoutUnit staticBlockPosition = isColumnFlow() ? mainAxisOffset : crossAxis Offset;
1213 if (childLayer->staticBlockPosition() != staticBlockPosition) { 1216 if (childLayer->staticBlockPosition() != staticBlockPosition) {
1214 childLayer->setStaticBlockPosition(staticBlockPosition); 1217 childLayer->setStaticBlockPosition(staticBlockPosition);
1215 if (child.style()->hasStaticBlockPosition(style()->isHorizontalWritingMo de())) 1218 if (child.style()->hasStaticBlockPosition(style()->isHorizontalWritingMo de()))
1216 child.setChildNeedsLayout(MarkOnlyThis); 1219 child.setChildNeedsLayout(MarkOnlyThis);
1217 } 1220 }
1218 } 1221 }
1219 1222
1220 ItemPosition LayoutFlexibleBox::alignmentForChild(const LayoutBox& child) const 1223 ItemPosition LayoutFlexibleBox::alignmentForChild(const LayoutBox& child) const
1221 { 1224 {
1222 ItemPosition align = ComputedStyle::resolveAlignment(styleRef(), child.style Ref(), ItemPositionStretch); 1225 ItemPosition align = child.styleRef().resolvedAlignSelf(styleRef(), selfAlig nmentNormalBehavior).position();
1223 1226
1224 if (align == ItemPositionBaseline && hasOrthogonalFlow(child)) 1227 if (align == ItemPositionBaseline && hasOrthogonalFlow(child))
1225 align = ItemPositionFlexStart; 1228 align = ItemPositionFlexStart;
1226 1229
1227 if (style()->flexWrap() == FlexWrapReverse) { 1230 if (style()->flexWrap() == FlexWrapReverse) {
1228 if (align == ItemPositionFlexStart) 1231 if (align == ItemPositionFlexStart)
1229 align = ItemPositionFlexEnd; 1232 align = ItemPositionFlexEnd;
1230 else if (align == ItemPositionFlexEnd) 1233 else if (align == ItemPositionFlexEnd)
1231 align = ItemPositionFlexStart; 1234 align = ItemPositionFlexStart;
1232 } 1235 }
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
1305 { 1308 {
1306 if (isHorizontalFlow()) 1309 if (isHorizontalFlow())
1307 return child.styleRef().overflowY(); 1310 return child.styleRef().overflowY();
1308 return child.styleRef().overflowX(); 1311 return child.styleRef().overflowX();
1309 } 1312 }
1310 1313
1311 void LayoutFlexibleBox::layoutAndPlaceChildren(LayoutUnit& crossAxisOffset, cons t OrderedFlexItemList& children, const Vector<LayoutUnit, 16>& childSizes, Layou tUnit availableFreeSpace, bool relayoutChildren, SubtreeLayoutScope& layoutScope , Vector<LineContext>& lineContexts) 1314 void LayoutFlexibleBox::layoutAndPlaceChildren(LayoutUnit& crossAxisOffset, cons t OrderedFlexItemList& children, const Vector<LayoutUnit, 16>& childSizes, Layou tUnit availableFreeSpace, bool relayoutChildren, SubtreeLayoutScope& layoutScope , Vector<LineContext>& lineContexts)
1312 { 1315 {
1313 ASSERT(childSizes.size() == children.size()); 1316 ASSERT(childSizes.size() == children.size());
1314 1317
1315 ContentPosition position = styleRef().resolvedJustifyContentPosition(normalV alueBehavior()); 1318 ContentPosition position = styleRef().resolvedJustifyContentPosition(content AlignmentNormalBehavior());
1316 ContentDistributionType distribution = styleRef().resolvedJustifyContentDist ribution(normalValueBehavior()); 1319 ContentDistributionType distribution = styleRef().resolvedJustifyContentDist ribution(contentAlignmentNormalBehavior());
1317 1320
1318 size_t numberOfChildrenForJustifyContent = numberOfInFlowPositionedChildren( children); 1321 size_t numberOfChildrenForJustifyContent = numberOfInFlowPositionedChildren( children);
1319 LayoutUnit autoMarginOffset = autoMarginOffsetInMainAxis(children, available FreeSpace); 1322 LayoutUnit autoMarginOffset = autoMarginOffsetInMainAxis(children, available FreeSpace);
1320 LayoutUnit mainAxisOffset = flowAwareBorderStart() + flowAwarePaddingStart() ; 1323 LayoutUnit mainAxisOffset = flowAwareBorderStart() + flowAwarePaddingStart() ;
1321 mainAxisOffset += initialJustifyContentOffset(availableFreeSpace, position, distribution, numberOfChildrenForJustifyContent); 1324 mainAxisOffset += initialJustifyContentOffset(availableFreeSpace, position, distribution, numberOfChildrenForJustifyContent);
1322 if (style()->flexDirection() == FlowRowReverse) 1325 if (style()->flexDirection() == FlowRowReverse)
1323 mainAxisOffset += isHorizontalFlow() ? verticalScrollbarWidth() : horizo ntalScrollbarHeight(); 1326 mainAxisOffset += isHorizontalFlow() ? verticalScrollbarWidth() : horizo ntalScrollbarHeight();
1324 1327
1325 LayoutUnit totalMainExtent = mainAxisExtent(); 1328 LayoutUnit totalMainExtent = mainAxisExtent();
1326 LayoutUnit maxAscent, maxDescent; // Used when align-items: baseline. 1329 LayoutUnit maxAscent, maxDescent; // Used when align-items: baseline.
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
1399 } 1402 }
1400 1403
1401 if (m_numberOfInFlowChildrenOnFirstLine == -1) 1404 if (m_numberOfInFlowChildrenOnFirstLine == -1)
1402 m_numberOfInFlowChildrenOnFirstLine = seenInFlowPositionedChildren; 1405 m_numberOfInFlowChildrenOnFirstLine = seenInFlowPositionedChildren;
1403 lineContexts.append(LineContext(crossAxisOffset, maxChildCrossAxisExtent, ch ildren.size(), maxAscent)); 1406 lineContexts.append(LineContext(crossAxisOffset, maxChildCrossAxisExtent, ch ildren.size(), maxAscent));
1404 crossAxisOffset += maxChildCrossAxisExtent; 1407 crossAxisOffset += maxChildCrossAxisExtent;
1405 } 1408 }
1406 1409
1407 void LayoutFlexibleBox::layoutColumnReverse(const OrderedFlexItemList& children, LayoutUnit crossAxisOffset, LayoutUnit availableFreeSpace) 1410 void LayoutFlexibleBox::layoutColumnReverse(const OrderedFlexItemList& children, LayoutUnit crossAxisOffset, LayoutUnit availableFreeSpace)
1408 { 1411 {
1409 ContentPosition position = styleRef().resolvedJustifyContentPosition(normalV alueBehavior()); 1412 ContentPosition position = styleRef().resolvedJustifyContentPosition(content AlignmentNormalBehavior());
1410 ContentDistributionType distribution = styleRef().resolvedJustifyContentDist ribution(normalValueBehavior()); 1413 ContentDistributionType distribution = styleRef().resolvedJustifyContentDist ribution(contentAlignmentNormalBehavior());
1411 1414
1412 // This is similar to the logic in layoutAndPlaceChildren, except we place t he children 1415 // This is similar to the logic in layoutAndPlaceChildren, except we place t he children
1413 // starting from the end of the flexbox. We also don't need to layout anythi ng since we're 1416 // starting from the end of the flexbox. We also don't need to layout anythi ng since we're
1414 // just moving the children to a new position. 1417 // just moving the children to a new position.
1415 size_t numberOfChildrenForJustifyContent = numberOfInFlowPositionedChildren( children); 1418 size_t numberOfChildrenForJustifyContent = numberOfInFlowPositionedChildren( children);
1416 LayoutUnit mainAxisOffset = logicalHeight() - flowAwareBorderEnd() - flowAwa rePaddingEnd(); 1419 LayoutUnit mainAxisOffset = logicalHeight() - flowAwareBorderEnd() - flowAwa rePaddingEnd();
1417 mainAxisOffset -= initialJustifyContentOffset(availableFreeSpace, position, distribution, numberOfChildrenForJustifyContent); 1420 mainAxisOffset -= initialJustifyContentOffset(availableFreeSpace, position, distribution, numberOfChildrenForJustifyContent);
1418 mainAxisOffset -= isHorizontalFlow() ? verticalScrollbarWidth() : horizontal ScrollbarHeight(); 1421 mainAxisOffset -= isHorizontalFlow() ? verticalScrollbarWidth() : horizontal ScrollbarHeight();
1419 1422
1420 size_t seenInFlowPositionedChildren = 0; 1423 size_t seenInFlowPositionedChildren = 0;
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
1460 if (alignContentDistribution == ContentDistributionSpaceBetween) 1463 if (alignContentDistribution == ContentDistributionSpaceBetween)
1461 return availableFreeSpace / (numberOfLines - 1); 1464 return availableFreeSpace / (numberOfLines - 1);
1462 if (alignContentDistribution == ContentDistributionSpaceAround || alignC ontentDistribution == ContentDistributionStretch) 1465 if (alignContentDistribution == ContentDistributionSpaceAround || alignC ontentDistribution == ContentDistributionStretch)
1463 return availableFreeSpace / numberOfLines; 1466 return availableFreeSpace / numberOfLines;
1464 } 1467 }
1465 return LayoutUnit(); 1468 return LayoutUnit();
1466 } 1469 }
1467 1470
1468 void LayoutFlexibleBox::alignFlexLines(Vector<LineContext>& lineContexts) 1471 void LayoutFlexibleBox::alignFlexLines(Vector<LineContext>& lineContexts)
1469 { 1472 {
1470 ContentPosition position = styleRef().resolvedAlignContentPosition(normalVal ueBehavior()); 1473 ContentPosition position = styleRef().resolvedAlignContentPosition(contentAl ignmentNormalBehavior());
1471 ContentDistributionType distribution = styleRef().resolvedAlignContentDistri bution(normalValueBehavior()); 1474 ContentDistributionType distribution = styleRef().resolvedAlignContentDistri bution(contentAlignmentNormalBehavior());
1472 1475
1473 // If we have a single line flexbox or a multiline line flexbox with only on e flex line, 1476 // If we have a single line flexbox or a multiline line flexbox with only on e flex line,
1474 // the line height is all the available space. 1477 // the line height is all the available space.
1475 // For flex-direction: row, this means we need to use the height, so we do t his after calling updateLogicalHeight. 1478 // For flex-direction: row, this means we need to use the height, so we do t his after calling updateLogicalHeight.
1476 if (lineContexts.size() == 1) { 1479 if (lineContexts.size() == 1) {
1477 lineContexts[0].crossAxisExtent = crossAxisContentExtent(); 1480 lineContexts[0].crossAxisExtent = crossAxisContentExtent();
1478 return; 1481 return;
1479 } 1482 }
1480 1483
1481 if (position == ContentPositionFlexStart) 1484 if (position == ContentPositionFlexStart)
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
1531 if (style()->flexWrap() == FlexWrapReverse) 1534 if (style()->flexWrap() == FlexWrapReverse)
1532 adjustAlignmentForChild(*child, lineCrossAxisExtent); 1535 adjustAlignmentForChild(*child, lineCrossAxisExtent);
1533 continue; 1536 continue;
1534 } 1537 }
1535 1538
1536 if (updateAutoMarginsInCrossAxis(*child, std::max(LayoutUnit(), avai lableAlignmentSpaceForChild(lineCrossAxisExtent, *child)))) 1539 if (updateAutoMarginsInCrossAxis(*child, std::max(LayoutUnit(), avai lableAlignmentSpaceForChild(lineCrossAxisExtent, *child))))
1537 continue; 1540 continue;
1538 1541
1539 switch (alignmentForChild(*child)) { 1542 switch (alignmentForChild(*child)) {
1540 case ItemPositionAuto: 1543 case ItemPositionAuto:
1544 case ItemPositionNormal:
1541 ASSERT_NOT_REACHED(); 1545 ASSERT_NOT_REACHED();
1542 break; 1546 break;
1543 case ItemPositionStretch: { 1547 case ItemPositionStretch: {
1544 applyStretchAlignmentToChild(*child, lineCrossAxisExtent); 1548 applyStretchAlignmentToChild(*child, lineCrossAxisExtent);
1545 // Since wrap-reverse flips cross start and cross end, strech ch ildren should be aligned with the cross end. 1549 // Since wrap-reverse flips cross start and cross end, strech ch ildren should be aligned with the cross end.
1546 if (style()->flexWrap() == FlexWrapReverse) 1550 if (style()->flexWrap() == FlexWrapReverse)
1547 adjustAlignmentForChild(*child, availableAlignmentSpaceForCh ild(lineCrossAxisExtent, *child)); 1551 adjustAlignmentForChild(*child, availableAlignmentSpaceForCh ild(lineCrossAxisExtent, *child));
1548 break; 1552 break;
1549 } 1553 }
1550 case ItemPositionFlexStart: 1554 case ItemPositionFlexStart:
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
1656 ASSERT(child); 1660 ASSERT(child);
1657 LayoutUnit lineCrossAxisExtent = lineContexts[lineNumber].crossAxisE xtent; 1661 LayoutUnit lineCrossAxisExtent = lineContexts[lineNumber].crossAxisE xtent;
1658 LayoutUnit originalOffset = lineContexts[lineNumber].crossAxisOffset - crossAxisStartEdge; 1662 LayoutUnit originalOffset = lineContexts[lineNumber].crossAxisOffset - crossAxisStartEdge;
1659 LayoutUnit newOffset = contentExtent - originalOffset - lineCrossAxi sExtent; 1663 LayoutUnit newOffset = contentExtent - originalOffset - lineCrossAxi sExtent;
1660 adjustAlignmentForChild(*child, newOffset - originalOffset); 1664 adjustAlignmentForChild(*child, newOffset - originalOffset);
1661 } 1665 }
1662 } 1666 }
1663 } 1667 }
1664 1668
1665 } // namespace blink 1669 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698