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

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: Applied suggested 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 1031 matching lines...) Expand 10 before | Expand all | Expand 10 after
1301 LayoutUnit staticBlockPosition = isColumnFlow() ? mainAxisOffset : crossAxis Offset; 1304 LayoutUnit staticBlockPosition = isColumnFlow() ? mainAxisOffset : crossAxis Offset;
1302 if (childLayer->staticBlockPosition() != staticBlockPosition) { 1305 if (childLayer->staticBlockPosition() != staticBlockPosition) {
1303 childLayer->setStaticBlockPosition(staticBlockPosition); 1306 childLayer->setStaticBlockPosition(staticBlockPosition);
1304 if (child.style()->hasStaticBlockPosition(style()->isHorizontalWritingMo de())) 1307 if (child.style()->hasStaticBlockPosition(style()->isHorizontalWritingMo de()))
1305 child.setChildNeedsLayout(MarkOnlyThis); 1308 child.setChildNeedsLayout(MarkOnlyThis);
1306 } 1309 }
1307 } 1310 }
1308 1311
1309 ItemPosition LayoutFlexibleBox::alignmentForChild(const LayoutBox& child) const 1312 ItemPosition LayoutFlexibleBox::alignmentForChild(const LayoutBox& child) const
1310 { 1313 {
1311 ItemPosition align = ComputedStyle::resolveAlignment(styleRef(), child.style Ref(), ItemPositionStretch); 1314 ItemPosition align = child.styleRef().resolvedAlignSelf(styleRef(), selfAlig nmentNormalBehavior).position();
1312 1315
1313 if (align == ItemPositionBaseline && hasOrthogonalFlow(child)) 1316 if (align == ItemPositionBaseline && hasOrthogonalFlow(child))
1314 align = ItemPositionFlexStart; 1317 align = ItemPositionFlexStart;
1315 1318
1316 if (style()->flexWrap() == FlexWrapReverse) { 1319 if (style()->flexWrap() == FlexWrapReverse) {
1317 if (align == ItemPositionFlexStart) 1320 if (align == ItemPositionFlexStart)
1318 align = ItemPositionFlexEnd; 1321 align = ItemPositionFlexEnd;
1319 else if (align == ItemPositionFlexEnd) 1322 else if (align == ItemPositionFlexEnd)
1320 align = ItemPositionFlexStart; 1323 align = ItemPositionFlexStart;
1321 } 1324 }
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
1394 { 1397 {
1395 if (isHorizontalFlow()) 1398 if (isHorizontalFlow())
1396 return child.styleRef().overflowY(); 1399 return child.styleRef().overflowY();
1397 return child.styleRef().overflowX(); 1400 return child.styleRef().overflowX();
1398 } 1401 }
1399 1402
1400 void LayoutFlexibleBox::layoutAndPlaceChildren(LayoutUnit& crossAxisOffset, cons t OrderedFlexItemList& children, const Vector<LayoutUnit, 16>& childSizes, Layou tUnit availableFreeSpace, bool relayoutChildren, SubtreeLayoutScope& layoutScope , Vector<LineContext>& lineContexts) 1403 void LayoutFlexibleBox::layoutAndPlaceChildren(LayoutUnit& crossAxisOffset, cons t OrderedFlexItemList& children, const Vector<LayoutUnit, 16>& childSizes, Layou tUnit availableFreeSpace, bool relayoutChildren, SubtreeLayoutScope& layoutScope , Vector<LineContext>& lineContexts)
1401 { 1404 {
1402 ASSERT(childSizes.size() == children.size()); 1405 ASSERT(childSizes.size() == children.size());
1403 1406
1404 ContentPosition position = styleRef().resolvedJustifyContentPosition(normalV alueBehavior()); 1407 ContentPosition position = styleRef().resolvedJustifyContentPosition(content AlignmentNormalBehavior());
1405 ContentDistributionType distribution = styleRef().resolvedJustifyContentDist ribution(normalValueBehavior()); 1408 ContentDistributionType distribution = styleRef().resolvedJustifyContentDist ribution(contentAlignmentNormalBehavior());
1406 1409
1407 size_t numberOfChildrenForJustifyContent = numberOfInFlowPositionedChildren( children); 1410 size_t numberOfChildrenForJustifyContent = numberOfInFlowPositionedChildren( children);
1408 LayoutUnit autoMarginOffset = autoMarginOffsetInMainAxis(children, available FreeSpace); 1411 LayoutUnit autoMarginOffset = autoMarginOffsetInMainAxis(children, available FreeSpace);
1409 LayoutUnit mainAxisOffset = flowAwareBorderStart() + flowAwarePaddingStart() ; 1412 LayoutUnit mainAxisOffset = flowAwareBorderStart() + flowAwarePaddingStart() ;
1410 mainAxisOffset += initialJustifyContentOffset(availableFreeSpace, position, distribution, numberOfChildrenForJustifyContent); 1413 mainAxisOffset += initialJustifyContentOffset(availableFreeSpace, position, distribution, numberOfChildrenForJustifyContent);
1411 if (style()->flexDirection() == FlowRowReverse) 1414 if (style()->flexDirection() == FlowRowReverse)
1412 mainAxisOffset += isHorizontalFlow() ? verticalScrollbarWidth() : horizo ntalScrollbarHeight(); 1415 mainAxisOffset += isHorizontalFlow() ? verticalScrollbarWidth() : horizo ntalScrollbarHeight();
1413 1416
1414 LayoutUnit totalMainExtent = mainAxisExtent(); 1417 LayoutUnit totalMainExtent = mainAxisExtent();
1415 LayoutUnit maxAscent, maxDescent; // Used when align-items: baseline. 1418 LayoutUnit maxAscent, maxDescent; // Used when align-items: baseline.
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
1488 } 1491 }
1489 1492
1490 if (m_numberOfInFlowChildrenOnFirstLine == -1) 1493 if (m_numberOfInFlowChildrenOnFirstLine == -1)
1491 m_numberOfInFlowChildrenOnFirstLine = seenInFlowPositionedChildren; 1494 m_numberOfInFlowChildrenOnFirstLine = seenInFlowPositionedChildren;
1492 lineContexts.append(LineContext(crossAxisOffset, maxChildCrossAxisExtent, ch ildren.size(), maxAscent)); 1495 lineContexts.append(LineContext(crossAxisOffset, maxChildCrossAxisExtent, ch ildren.size(), maxAscent));
1493 crossAxisOffset += maxChildCrossAxisExtent; 1496 crossAxisOffset += maxChildCrossAxisExtent;
1494 } 1497 }
1495 1498
1496 void LayoutFlexibleBox::layoutColumnReverse(const OrderedFlexItemList& children, LayoutUnit crossAxisOffset, LayoutUnit availableFreeSpace) 1499 void LayoutFlexibleBox::layoutColumnReverse(const OrderedFlexItemList& children, LayoutUnit crossAxisOffset, LayoutUnit availableFreeSpace)
1497 { 1500 {
1498 ContentPosition position = styleRef().resolvedJustifyContentPosition(normalV alueBehavior()); 1501 ContentPosition position = styleRef().resolvedJustifyContentPosition(content AlignmentNormalBehavior());
1499 ContentDistributionType distribution = styleRef().resolvedJustifyContentDist ribution(normalValueBehavior()); 1502 ContentDistributionType distribution = styleRef().resolvedJustifyContentDist ribution(contentAlignmentNormalBehavior());
1500 1503
1501 // This is similar to the logic in layoutAndPlaceChildren, except we place t he children 1504 // This is similar to the logic in layoutAndPlaceChildren, except we place t he children
1502 // starting from the end of the flexbox. We also don't need to layout anythi ng since we're 1505 // starting from the end of the flexbox. We also don't need to layout anythi ng since we're
1503 // just moving the children to a new position. 1506 // just moving the children to a new position.
1504 size_t numberOfChildrenForJustifyContent = numberOfInFlowPositionedChildren( children); 1507 size_t numberOfChildrenForJustifyContent = numberOfInFlowPositionedChildren( children);
1505 LayoutUnit mainAxisOffset = logicalHeight() - flowAwareBorderEnd() - flowAwa rePaddingEnd(); 1508 LayoutUnit mainAxisOffset = logicalHeight() - flowAwareBorderEnd() - flowAwa rePaddingEnd();
1506 mainAxisOffset -= initialJustifyContentOffset(availableFreeSpace, position, distribution, numberOfChildrenForJustifyContent); 1509 mainAxisOffset -= initialJustifyContentOffset(availableFreeSpace, position, distribution, numberOfChildrenForJustifyContent);
1507 mainAxisOffset -= isHorizontalFlow() ? verticalScrollbarWidth() : horizontal ScrollbarHeight(); 1510 mainAxisOffset -= isHorizontalFlow() ? verticalScrollbarWidth() : horizontal ScrollbarHeight();
1508 1511
1509 size_t seenInFlowPositionedChildren = 0; 1512 size_t seenInFlowPositionedChildren = 0;
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
1549 if (alignContentDistribution == ContentDistributionSpaceBetween) 1552 if (alignContentDistribution == ContentDistributionSpaceBetween)
1550 return availableFreeSpace / (numberOfLines - 1); 1553 return availableFreeSpace / (numberOfLines - 1);
1551 if (alignContentDistribution == ContentDistributionSpaceAround || alignC ontentDistribution == ContentDistributionStretch) 1554 if (alignContentDistribution == ContentDistributionSpaceAround || alignC ontentDistribution == ContentDistributionStretch)
1552 return availableFreeSpace / numberOfLines; 1555 return availableFreeSpace / numberOfLines;
1553 } 1556 }
1554 return LayoutUnit(); 1557 return LayoutUnit();
1555 } 1558 }
1556 1559
1557 void LayoutFlexibleBox::alignFlexLines(Vector<LineContext>& lineContexts) 1560 void LayoutFlexibleBox::alignFlexLines(Vector<LineContext>& lineContexts)
1558 { 1561 {
1559 ContentPosition position = styleRef().resolvedAlignContentPosition(normalVal ueBehavior()); 1562 ContentPosition position = styleRef().resolvedAlignContentPosition(contentAl ignmentNormalBehavior());
1560 ContentDistributionType distribution = styleRef().resolvedAlignContentDistri bution(normalValueBehavior()); 1563 ContentDistributionType distribution = styleRef().resolvedAlignContentDistri bution(contentAlignmentNormalBehavior());
1561 1564
1562 // If we have a single line flexbox or a multiline line flexbox with only on e flex line, 1565 // If we have a single line flexbox or a multiline line flexbox with only on e flex line,
1563 // the line height is all the available space. 1566 // the line height is all the available space.
1564 // For flex-direction: row, this means we need to use the height, so we do t his after calling updateLogicalHeight. 1567 // For flex-direction: row, this means we need to use the height, so we do t his after calling updateLogicalHeight.
1565 if (lineContexts.size() == 1) { 1568 if (lineContexts.size() == 1) {
1566 lineContexts[0].crossAxisExtent = crossAxisContentExtent(); 1569 lineContexts[0].crossAxisExtent = crossAxisContentExtent();
1567 return; 1570 return;
1568 } 1571 }
1569 1572
1570 if (position == ContentPositionFlexStart) 1573 if (position == ContentPositionFlexStart)
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
1620 if (style()->flexWrap() == FlexWrapReverse) 1623 if (style()->flexWrap() == FlexWrapReverse)
1621 adjustAlignmentForChild(*child, lineCrossAxisExtent); 1624 adjustAlignmentForChild(*child, lineCrossAxisExtent);
1622 continue; 1625 continue;
1623 } 1626 }
1624 1627
1625 if (updateAutoMarginsInCrossAxis(*child, std::max(LayoutUnit(), avai lableAlignmentSpaceForChild(lineCrossAxisExtent, *child)))) 1628 if (updateAutoMarginsInCrossAxis(*child, std::max(LayoutUnit(), avai lableAlignmentSpaceForChild(lineCrossAxisExtent, *child))))
1626 continue; 1629 continue;
1627 1630
1628 switch (alignmentForChild(*child)) { 1631 switch (alignmentForChild(*child)) {
1629 case ItemPositionAuto: 1632 case ItemPositionAuto:
1633 case ItemPositionNormal:
1630 ASSERT_NOT_REACHED(); 1634 ASSERT_NOT_REACHED();
1631 break; 1635 break;
1632 case ItemPositionStretch: { 1636 case ItemPositionStretch: {
1633 applyStretchAlignmentToChild(*child, lineCrossAxisExtent); 1637 applyStretchAlignmentToChild(*child, lineCrossAxisExtent);
1634 // Since wrap-reverse flips cross start and cross end, strech ch ildren should be aligned with the cross end. 1638 // Since wrap-reverse flips cross start and cross end, strech ch ildren should be aligned with the cross end.
1635 if (style()->flexWrap() == FlexWrapReverse) 1639 if (style()->flexWrap() == FlexWrapReverse)
1636 adjustAlignmentForChild(*child, availableAlignmentSpaceForCh ild(lineCrossAxisExtent, *child)); 1640 adjustAlignmentForChild(*child, availableAlignmentSpaceForCh ild(lineCrossAxisExtent, *child));
1637 break; 1641 break;
1638 } 1642 }
1639 case ItemPositionFlexStart: 1643 case ItemPositionFlexStart:
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
1745 ASSERT(child); 1749 ASSERT(child);
1746 LayoutUnit lineCrossAxisExtent = lineContexts[lineNumber].crossAxisE xtent; 1750 LayoutUnit lineCrossAxisExtent = lineContexts[lineNumber].crossAxisE xtent;
1747 LayoutUnit originalOffset = lineContexts[lineNumber].crossAxisOffset - crossAxisStartEdge; 1751 LayoutUnit originalOffset = lineContexts[lineNumber].crossAxisOffset - crossAxisStartEdge;
1748 LayoutUnit newOffset = contentExtent - originalOffset - lineCrossAxi sExtent; 1752 LayoutUnit newOffset = contentExtent - originalOffset - lineCrossAxi sExtent;
1749 adjustAlignmentForChild(*child, newOffset - originalOffset); 1753 adjustAlignmentForChild(*child, newOffset - originalOffset);
1750 } 1754 }
1751 } 1755 }
1752 } 1756 }
1753 1757
1754 } // namespace blink 1758 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698