2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "RenderFlexibleBox.h"
34 #include "LayoutRepainter.h"
35 #include "RenderLayer.h"
36 #include "RenderView.h"
40 // Normally, -1 and 0 are not valid in a HashSet, but these are relatively likely flex-order values. Instead,
41 // we make the two smallest int values invalid flex-order values (in the css parser code we clamp them to
43 struct RenderFlexibleBox::FlexOrderHashTraits : WTF::GenericHashTraits<int> {
44 static const bool emptyValueIsZero = false;
45 static int emptyValue() { return std::numeric_limits<int>::min(); }
46 static void constructDeletedValue(int& slot) { slot = std::numeric_limits<int>::min() + 1; }
47 static bool isDeletedValue(int value) { return value == std::numeric_limits<int>::min() + 1; }
50 class RenderFlexibleBox::FlexOrderIterator {
52 FlexOrderIterator(RenderFlexibleBox* flexibleBox, const FlexOrderHashSet& flexOrderValues)
53 : m_flexibleBox(flexibleBox)
55 , m_orderValuesIterator(0)
57 copyToVector(flexOrderValues, m_orderValues);
58 std::sort(m_orderValues.begin(), m_orderValues.end());
70 if (!m_currentChild) {
71 if (m_orderValuesIterator == m_orderValues.end())
73 if (m_orderValuesIterator) {
74 ++m_orderValuesIterator;
75 if (m_orderValuesIterator == m_orderValues.end())
78 m_orderValuesIterator = m_orderValues.begin();
80 m_currentChild = m_flexibleBox->firstChildBox();
82 m_currentChild = m_currentChild->nextSiblingBox();
83 } while (!m_currentChild || m_currentChild->style()->flexOrder() != *m_orderValuesIterator);
85 return m_currentChild;
91 m_orderValuesIterator = 0;
95 RenderFlexibleBox* m_flexibleBox;
96 RenderBox* m_currentChild;
97 Vector<int> m_orderValues;
98 Vector<int>::const_iterator m_orderValuesIterator;
102 RenderFlexibleBox::RenderFlexibleBox(Node* node)
105 setChildrenInline(false); // All of our children must be block-level.
108 RenderFlexibleBox::~RenderFlexibleBox()
112 const char* RenderFlexibleBox::renderName() const
114 return "RenderFlexibleBox";
117 void RenderFlexibleBox::layoutBlock(bool relayoutChildren, int, BlockLayoutPass)
119 ASSERT(needsLayout());
121 if (!relayoutChildren && simplifiedLayout())
124 LayoutRepainter repainter(*this, checkForRepaintDuringLayout());
125 LayoutStateMaintainer statePusher(view(), this, IntSize(x(), y()), hasTransform() || hasReflection() || style()->isFlippedBlocksWritingMode());
127 if (inRenderFlowThread()) {
128 // Regions changing widths can force us to relayout our children.
129 if (logicalWidthChangedInRegions())
130 relayoutChildren = true;
132 computeInitialRegionRangeForBlock();
134 IntSize previousSize = size();
137 // We need to call both of these because we grab both crossAxisExtent and mainAxisExtent in layoutFlexItems.
138 computeLogicalWidth();
139 computeLogicalHeight();
143 // For overflow:scroll blocks, ensure we have both scrollbars in place always.
144 if (scrollsOverflow()) {
145 if (style()->overflowX() == OSCROLL)
146 layer()->setHasHorizontalScrollbar(true);
147 if (style()->overflowY() == OSCROLL)
148 layer()->setHasVerticalScrollbar(true);
151 layoutFlexItems(relayoutChildren);
153 LayoutUnit oldClientAfterEdge = clientLogicalBottom();
154 computeLogicalHeight();
156 if (size() != previousSize)
157 relayoutChildren = true;
159 layoutPositionedObjects(relayoutChildren || isRoot());
161 computeRegionRangeForBlock();
163 // FIXME: css3/flexbox/repaint-rtl-column.html seems to repaint more overflow than it needs to.
164 computeOverflow(oldClientAfterEdge);
167 updateLayerTransform();
169 // Update our scroll information if we're overflow:auto/scroll/hidden now that we know if
170 // we overflow or not.
171 if (hasOverflowClip())
172 layer()->updateScrollInfoAfterLayout();
174 repainter.repaintAfterLayout();
176 setNeedsLayout(false);
179 bool RenderFlexibleBox::hasOrthogonalFlow(RenderBox* child) const
181 // FIXME: If the child is a flexbox, then we need to check isHorizontalFlow.
182 return isHorizontalFlow() != child->isHorizontalWritingMode();
185 bool RenderFlexibleBox::isColumnFlow() const
187 return style()->isColumnFlexDirection();
190 bool RenderFlexibleBox::isHorizontalFlow() const
192 if (isHorizontalWritingMode())
193 return !isColumnFlow();
194 return isColumnFlow();
197 bool RenderFlexibleBox::isLeftToRightFlow() const
200 return style()->writingMode() == TopToBottomWritingMode || style()->writingMode() == LeftToRightWritingMode;
201 return style()->isLeftToRightDirection() ^ (style()->flexDirection() == FlowRowReverse);
204 Length RenderFlexibleBox::mainAxisLengthForChild(RenderBox* child) const
206 return isHorizontalFlow() ? child->style()->width() : child->style()->height();
209 Length RenderFlexibleBox::crossAxisLength() const
211 return isHorizontalFlow() ? style()->height() : style()->width();
214 void RenderFlexibleBox::setCrossAxisExtent(LayoutUnit extent)
216 if (isHorizontalFlow())
222 LayoutUnit RenderFlexibleBox::crossAxisExtentForChild(RenderBox* child)
224 return isHorizontalFlow() ? child->height() : child->width();
227 LayoutUnit RenderFlexibleBox::mainAxisExtentForChild(RenderBox* child)
229 return isHorizontalFlow() ? child->width() : child->height();
232 LayoutUnit RenderFlexibleBox::crossAxisExtent() const
234 return isHorizontalFlow() ? height() : width();
237 LayoutUnit RenderFlexibleBox::mainAxisExtent() const
239 return isHorizontalFlow() ? width() : height();
242 LayoutUnit RenderFlexibleBox::crossAxisContentExtent() const
244 return isHorizontalFlow() ? contentHeight() : contentWidth();
247 LayoutUnit RenderFlexibleBox::mainAxisContentExtent() const
249 return isHorizontalFlow() ? contentWidth() : contentHeight();
252 WritingMode RenderFlexibleBox::transformedWritingMode() const
254 WritingMode mode = style()->writingMode();
259 case TopToBottomWritingMode:
260 case BottomToTopWritingMode:
261 return style()->isLeftToRightDirection() ? LeftToRightWritingMode : RightToLeftWritingMode;
262 case LeftToRightWritingMode:
263 case RightToLeftWritingMode:
264 return style()->isLeftToRightDirection() ? TopToBottomWritingMode : BottomToTopWritingMode;
266 ASSERT_NOT_REACHED();
267 return TopToBottomWritingMode;
270 LayoutUnit RenderFlexibleBox::flowAwareBorderStart() const
272 if (isHorizontalFlow())
273 return isLeftToRightFlow() ? borderLeft() : borderRight();
274 return isLeftToRightFlow() ? borderTop() : borderBottom();
277 LayoutUnit RenderFlexibleBox::flowAwareBorderEnd() const
279 if (isHorizontalFlow())
280 return isLeftToRightFlow() ? borderRight() : borderLeft();
281 return isLeftToRightFlow() ? borderBottom() : borderTop();
284 LayoutUnit RenderFlexibleBox::flowAwareBorderBefore() const
286 switch (transformedWritingMode()) {
287 case TopToBottomWritingMode:
289 case BottomToTopWritingMode:
290 return borderBottom();
291 case LeftToRightWritingMode:
293 case RightToLeftWritingMode:
294 return borderRight();
296 ASSERT_NOT_REACHED();
300 LayoutUnit RenderFlexibleBox::crossAxisBorderAndPaddingExtent() const
302 return isHorizontalFlow() ? borderAndPaddingHeight() : borderAndPaddingWidth();
305 LayoutUnit RenderFlexibleBox::flowAwarePaddingStart() const
307 if (isHorizontalFlow())
308 return isLeftToRightFlow() ? paddingLeft() : paddingRight();
309 return isLeftToRightFlow() ? paddingTop() : paddingBottom();
312 LayoutUnit RenderFlexibleBox::flowAwarePaddingEnd() const
314 if (isHorizontalFlow())
315 return isLeftToRightFlow() ? paddingRight() : paddingLeft();
316 return isLeftToRightFlow() ? paddingBottom() : paddingTop();
319 LayoutUnit RenderFlexibleBox::flowAwarePaddingBefore() const
321 switch (transformedWritingMode()) {
322 case TopToBottomWritingMode:
324 case BottomToTopWritingMode:
325 return paddingBottom();
326 case LeftToRightWritingMode:
327 return paddingLeft();
328 case RightToLeftWritingMode:
329 return paddingRight();
331 ASSERT_NOT_REACHED();
335 LayoutUnit RenderFlexibleBox::flowAwareMarginStartForChild(RenderBox* child) const
337 if (isHorizontalFlow())
338 return isLeftToRightFlow() ? child->marginLeft() : child->marginRight();
339 return isLeftToRightFlow() ? child->marginTop() : child->marginBottom();
342 LayoutUnit RenderFlexibleBox::flowAwareMarginEndForChild(RenderBox* child) const
344 if (isHorizontalFlow())
345 return isLeftToRightFlow() ? child->marginRight() : child->marginLeft();
346 return isLeftToRightFlow() ? child->marginBottom() : child->marginTop();
349 LayoutUnit RenderFlexibleBox::flowAwareMarginBeforeForChild(RenderBox* child) const
351 switch (transformedWritingMode()) {
352 case TopToBottomWritingMode:
353 return child->marginTop();
354 case BottomToTopWritingMode:
355 return child->marginBottom();
356 case LeftToRightWritingMode:
357 return child->marginLeft();
358 case RightToLeftWritingMode:
359 return child->marginRight();
361 ASSERT_NOT_REACHED();
365 LayoutUnit RenderFlexibleBox::flowAwareMarginAfterForChild(RenderBox* child) const
367 switch (transformedWritingMode()) {
368 case TopToBottomWritingMode:
369 return child->marginBottom();
370 case BottomToTopWritingMode:
371 return child->marginTop();
372 case LeftToRightWritingMode:
373 return child->marginRight();
374 case RightToLeftWritingMode:
375 return child->marginLeft();
377 ASSERT_NOT_REACHED();
378 return marginBottom();
381 LayoutUnit RenderFlexibleBox::crossAxisMarginExtentForChild(RenderBox* child) const
383 return isHorizontalFlow() ? child->marginTop() + child->marginBottom() : child->marginLeft() + child->marginRight();
386 LayoutUnit RenderFlexibleBox::crossAxisScrollbarExtent() const
388 return isHorizontalFlow() ? horizontalScrollbarHeight() : verticalScrollbarWidth();
391 LayoutPoint RenderFlexibleBox::flowAwareLocationForChild(RenderBox* child) const
393 return isHorizontalFlow() ? child->location() : child->location().transposedPoint();
396 void RenderFlexibleBox::setFlowAwareLocationForChild(RenderBox* child, const LayoutPoint& location)
398 if (isHorizontalFlow())
399 child->setLocation(location);
401 child->setLocation(location.transposedPoint());
404 LayoutUnit RenderFlexibleBox::mainAxisBorderAndPaddingExtentForChild(RenderBox* child) const
406 return isHorizontalFlow() ? child->borderAndPaddingWidth() : child->borderAndPaddingHeight();
409 LayoutUnit RenderFlexibleBox::mainAxisScrollbarExtentForChild(RenderBox* child) const
411 return isHorizontalFlow() ? child->verticalScrollbarWidth() : child->horizontalScrollbarHeight();
414 LayoutUnit RenderFlexibleBox::preferredMainAxisContentExtentForChild(RenderBox* child) const
416 Length mainAxisLength = mainAxisLengthForChild(child);
417 if (mainAxisLength.isAuto()) {
418 LayoutUnit mainAxisExtent = hasOrthogonalFlow(child) ? child->logicalHeight() : child->maxPreferredLogicalWidth();
419 return mainAxisExtent - mainAxisBorderAndPaddingExtentForChild(child) - mainAxisScrollbarExtentForChild(child);
421 return mainAxisLength.calcMinValue(mainAxisContentExtent());
424 void RenderFlexibleBox::layoutFlexItems(bool relayoutChildren)
426 FlexOrderHashSet flexOrderValues;
427 computeMainAxisPreferredSizes(relayoutChildren, flexOrderValues);
429 OrderedFlexItemList orderedChildren;
430 LayoutUnit preferredMainAxisExtent;
431 float totalPositiveFlexibility;
432 float totalNegativeFlexibility;
433 FlexOrderIterator flexIterator(this, flexOrderValues);
434 computeFlexOrder(flexIterator, orderedChildren, preferredMainAxisExtent, totalPositiveFlexibility, totalNegativeFlexibility);
436 LayoutUnit availableFreeSpace = mainAxisContentExtent() - preferredMainAxisExtent;
437 InflexibleFlexItemSize inflexibleItems;
438 WTF::Vector<LayoutUnit> childSizes;
439 while (!runFreeSpaceAllocationAlgorithm(orderedChildren, availableFreeSpace, totalPositiveFlexibility, totalNegativeFlexibility, inflexibleItems, childSizes)) {
440 ASSERT(totalPositiveFlexibility >= 0 && totalNegativeFlexibility >= 0);
441 ASSERT(inflexibleItems.size() > 0);
444 layoutAndPlaceChildren(orderedChildren, childSizes, availableFreeSpace);
447 float RenderFlexibleBox::positiveFlexForChild(RenderBox* child) const
449 return isHorizontalFlow() ? child->style()->flexboxWidthPositiveFlex() : child->style()->flexboxHeightPositiveFlex();
452 float RenderFlexibleBox::negativeFlexForChild(RenderBox* child) const
454 return isHorizontalFlow() ? child->style()->flexboxWidthNegativeFlex() : child->style()->flexboxHeightNegativeFlex();
457 LayoutUnit RenderFlexibleBox::availableAlignmentSpaceForChild(RenderBox* child)
459 LayoutUnit crossContentExtent = crossAxisContentExtent();
460 LayoutUnit childCrossExtent = crossAxisMarginExtentForChild(child) + crossAxisExtentForChild(child);
461 return crossContentExtent - childCrossExtent;
464 LayoutUnit RenderFlexibleBox::marginBoxAscent(RenderBox* child)
466 LayoutUnit ascent = child->firstLineBoxBaseline();
468 ascent = crossAxisExtentForChild(child) + flowAwareMarginAfterForChild(child);
469 return ascent + flowAwareMarginBeforeForChild(child);
472 void RenderFlexibleBox::computeMainAxisPreferredSizes(bool relayoutChildren, FlexOrderHashSet& flexOrderValues)
474 LayoutUnit flexboxAvailableContentExtent = mainAxisContentExtent();
475 for (RenderBox* child = firstChildBox(); child; child = child->nextSiblingBox()) {
476 flexOrderValues.add(child->style()->flexOrder());
478 if (child->isPositioned())
481 child->clearOverrideSize();
482 if (mainAxisLengthForChild(child).isAuto()) {
483 if (!relayoutChildren)
484 child->setChildNeedsLayout(true);
485 child->layoutIfNeeded();
488 // We set the margins because we want to make sure 'auto' has a margin
489 // of 0 and because if we're not auto sizing, we don't do a layout that
490 // computes the start/end margins.
491 if (isHorizontalFlow()) {
492 child->setMarginLeft(child->style()->marginLeft().calcMinValue(flexboxAvailableContentExtent));
493 child->setMarginRight(child->style()->marginRight().calcMinValue(flexboxAvailableContentExtent));
495 child->setMarginTop(child->style()->marginTop().calcMinValue(flexboxAvailableContentExtent));
496 child->setMarginBottom(child->style()->marginBottom().calcMinValue(flexboxAvailableContentExtent));
501 void RenderFlexibleBox::computeFlexOrder(FlexOrderIterator& iterator, OrderedFlexItemList& orderedChildren, LayoutUnit& preferredMainAxisExtent, float& totalPositiveFlexibility, float& totalNegativeFlexibility)
503 orderedChildren.clear();
504 preferredMainAxisExtent = 0;
505 totalPositiveFlexibility = totalNegativeFlexibility = 0;
506 for (RenderBox* child = iterator.first(); child; child = iterator.next()) {
507 orderedChildren.append(child);
508 if (child->isPositioned())
511 LayoutUnit childMainAxisExtent = mainAxisBorderAndPaddingExtentForChild(child) + preferredMainAxisContentExtentForChild(child);
512 if (isHorizontalFlow())
513 childMainAxisExtent += child->marginLeft() + child->marginRight();
515 childMainAxisExtent += child->marginTop() + child->marginBottom();
517 // FIXME: When implementing multiline, we would return here if adding
518 // the child's main axis extent would cause us to overflow.
519 preferredMainAxisExtent += childMainAxisExtent;
520 totalPositiveFlexibility += positiveFlexForChild(child);
521 totalNegativeFlexibility += negativeFlexForChild(child);
525 // Returns true if we successfully ran the algorithm and sized the flex items.
526 bool RenderFlexibleBox::runFreeSpaceAllocationAlgorithm(const OrderedFlexItemList& children, LayoutUnit& availableFreeSpace, float& totalPositiveFlexibility, float& totalNegativeFlexibility, InflexibleFlexItemSize& inflexibleItems, WTF::Vector<LayoutUnit>& childSizes)
530 LayoutUnit flexboxAvailableContentExtent = mainAxisContentExtent();
531 for (size_t i = 0; i < children.size(); ++i) {
532 RenderBox* child = children[i];
533 if (child->isPositioned()) {
534 childSizes.append(0);
538 LayoutUnit childPreferredSize;
539 if (inflexibleItems.contains(child))
540 childPreferredSize = inflexibleItems.get(child);
542 childPreferredSize = preferredMainAxisContentExtentForChild(child);
543 if (availableFreeSpace > 0 && totalPositiveFlexibility > 0) {
544 childPreferredSize += lroundf(availableFreeSpace * positiveFlexForChild(child) / totalPositiveFlexibility);
546 Length childLogicalMaxWidth = isHorizontalFlow() ? child->style()->maxWidth() : child->style()->maxHeight();
547 if (!childLogicalMaxWidth.isUndefined() && childLogicalMaxWidth.isSpecified() && childPreferredSize > childLogicalMaxWidth.calcValue(flexboxAvailableContentExtent)) {
548 childPreferredSize = childLogicalMaxWidth.calcValue(flexboxAvailableContentExtent);
549 availableFreeSpace -= childPreferredSize - preferredMainAxisContentExtentForChild(child);
550 totalPositiveFlexibility -= positiveFlexForChild(child);
552 inflexibleItems.set(child, childPreferredSize);
555 } else if (availableFreeSpace < 0 && totalNegativeFlexibility > 0) {
556 childPreferredSize += lroundf(availableFreeSpace * negativeFlexForChild(child) / totalNegativeFlexibility);
558 Length childLogicalMinWidth = isHorizontalFlow() ? child->style()->minWidth() : child->style()->minHeight();
559 if (!childLogicalMinWidth.isUndefined() && childLogicalMinWidth.isSpecified() && childPreferredSize < childLogicalMinWidth.calcValue(flexboxAvailableContentExtent)) {
560 childPreferredSize = childLogicalMinWidth.calcValue(flexboxAvailableContentExtent);
561 availableFreeSpace += preferredMainAxisContentExtentForChild(child) - childPreferredSize;
562 totalNegativeFlexibility -= negativeFlexForChild(child);
564 inflexibleItems.set(child, childPreferredSize);
569 childSizes.append(childPreferredSize);
574 static LayoutUnit initialPackingOffset(LayoutUnit availableFreeSpace, EFlexPack flexPack, size_t numberOfChildren)
576 if (availableFreeSpace > 0) {
577 if (flexPack == PackEnd)
578 return availableFreeSpace;
579 if (flexPack == PackCenter)
580 return availableFreeSpace / 2;
581 if (flexPack == PackDistribute && numberOfChildren)
582 return availableFreeSpace / (2 * numberOfChildren);
583 } else if (availableFreeSpace < 0) {
584 if (flexPack == PackCenter || flexPack == PackDistribute)
585 return availableFreeSpace / 2;
590 static LayoutUnit packingSpaceBetweenChildren(LayoutUnit availableFreeSpace, EFlexPack flexPack, size_t numberOfChildren)
592 if (availableFreeSpace > 0 && numberOfChildren > 1) {
593 if (flexPack == PackJustify)
594 return availableFreeSpace / (numberOfChildren - 1);
595 if (flexPack == PackDistribute)
596 return availableFreeSpace / numberOfChildren;
601 void RenderFlexibleBox::setLogicalOverrideSize(RenderBox* child, LayoutUnit childPreferredSize)
603 // FIXME: Rename setOverrideWidth/setOverrideHeight to setOverrideLogicalWidth/setOverrideLogicalHeight.
604 if (hasOrthogonalFlow(child))
605 child->setOverrideHeight(childPreferredSize);
607 child->setOverrideWidth(childPreferredSize);
610 void RenderFlexibleBox::prepareChildForPositionedLayout(RenderBox* child, LayoutUnit mainAxisOffset, LayoutUnit crossAxisOffset)
612 ASSERT(child->isPositioned());
613 child->containingBlock()->insertPositionedObject(child);
614 RenderLayer* childLayer = child->layer();
615 LayoutUnit inlinePosition = isColumnFlow() ? crossAxisOffset : mainAxisOffset;
616 if (style()->flexDirection() == FlowRowReverse)
617 inlinePosition = mainAxisExtent() - mainAxisOffset;
618 childLayer->setStaticInlinePosition(inlinePosition); // FIXME: Not right for regions.
620 LayoutUnit staticBlockPosition = isColumnFlow() ? mainAxisOffset : crossAxisOffset;
621 if (childLayer->staticBlockPosition() != staticBlockPosition) {
622 childLayer->setStaticBlockPosition(staticBlockPosition);
623 if (child->style()->hasStaticBlockPosition(style()->isHorizontalWritingMode()))
624 child->setChildNeedsLayout(true, false);
628 static EFlexAlign flexAlignForChild(RenderBox* child)
630 EFlexAlign align = child->style()->flexItemAlign();
631 if (align == AlignAuto)
632 return child->parent()->style()->flexAlign();
636 void RenderFlexibleBox::layoutAndPlaceChildren(const OrderedFlexItemList& children, const WTF::Vector<LayoutUnit>& childSizes, LayoutUnit availableFreeSpace)
638 LayoutUnit mainAxisOffset = flowAwareBorderStart() + flowAwarePaddingStart();
639 mainAxisOffset += initialPackingOffset(availableFreeSpace, style()->flexPack(), childSizes.size());
640 if (style()->flexDirection() == FlowRowReverse)
641 mainAxisOffset += isHorizontalFlow() ? verticalScrollbarWidth() : horizontalScrollbarHeight();
643 LayoutUnit crossAxisOffset = flowAwareBorderBefore() + flowAwarePaddingBefore();
644 LayoutUnit totalMainExtent = mainAxisExtent();
645 LayoutUnit maxAscent = 0, maxDescent = 0; // Used when flex-align: baseline.
646 bool shouldFlipMainAxis = !isColumnFlow() && !isLeftToRightFlow();
647 for (size_t i = 0; i < children.size(); ++i) {
648 RenderBox* child = children[i];
649 if (child->isPositioned()) {
650 prepareChildForPositionedLayout(child, mainAxisOffset, crossAxisOffset);
651 mainAxisOffset += packingSpaceBetweenChildren(availableFreeSpace, style()->flexPack(), childSizes.size());
654 LayoutUnit childPreferredSize = childSizes[i] + mainAxisBorderAndPaddingExtentForChild(child);
655 setLogicalOverrideSize(child, childPreferredSize);
656 child->setChildNeedsLayout(true);
657 child->layoutIfNeeded();
659 if (flexAlignForChild(child) == AlignBaseline) {
660 LayoutUnit ascent = marginBoxAscent(child);
661 LayoutUnit descent = (crossAxisMarginExtentForChild(child) + crossAxisExtentForChild(child)) - ascent;
663 maxAscent = std::max(maxAscent, ascent);
664 maxDescent = std::max(maxDescent, descent);
666 if (crossAxisLength().isAuto())
667 setCrossAxisExtent(std::max(crossAxisExtent(), crossAxisBorderAndPaddingExtent() + crossAxisMarginExtentForChild(child) + maxAscent + maxDescent + crossAxisScrollbarExtent()));
668 } else if (crossAxisLength().isAuto())
669 setCrossAxisExtent(std::max(crossAxisExtent(), crossAxisBorderAndPaddingExtent() + crossAxisMarginExtentForChild(child) + crossAxisExtentForChild(child) + crossAxisScrollbarExtent()));
671 mainAxisOffset += flowAwareMarginStartForChild(child);
673 LayoutUnit childMainExtent = mainAxisExtentForChild(child);
674 IntPoint childLocation(shouldFlipMainAxis ? totalMainExtent - mainAxisOffset - childMainExtent : mainAxisOffset,
675 crossAxisOffset + flowAwareMarginBeforeForChild(child));
677 // FIXME: Supporting layout deltas.
678 setFlowAwareLocationForChild(child, childLocation);
679 mainAxisOffset += childMainExtent + flowAwareMarginEndForChild(child);
681 mainAxisOffset += packingSpaceBetweenChildren(availableFreeSpace, style()->flexPack(), childSizes.size());
684 setLogicalHeight(mainAxisOffset + flowAwareBorderEnd() + flowAwarePaddingEnd() + scrollbarLogicalHeight());
687 if (style()->flexDirection() == FlowColumnReverse) {
688 // We have to do an extra pass for column-reverse to reposition the flex items since the start depends
689 // on the height of the flexbox, which we only know after we've positioned all the flex items.
690 computeLogicalHeight();
691 layoutColumnReverse(children, childSizes, availableFreeSpace);
694 alignChildren(children, maxAscent);
697 void RenderFlexibleBox::layoutColumnReverse(const OrderedFlexItemList& children, const WTF::Vector<LayoutUnit>& childSizes, LayoutUnit availableFreeSpace)
699 // This is similar to the logic in layoutAndPlaceChildren, except we place the children
700 // starting from the end of the flexbox. We also don't need to layout anything since we're
701 // just moving the children to a new position.
702 LayoutUnit mainAxisOffset = logicalHeight() - flowAwareBorderEnd() - flowAwarePaddingEnd();
703 mainAxisOffset -= initialPackingOffset(availableFreeSpace, style()->flexPack(), childSizes.size());
704 mainAxisOffset -= isHorizontalFlow() ? verticalScrollbarWidth() : horizontalScrollbarHeight();
706 LayoutUnit crossAxisOffset = flowAwareBorderBefore() + flowAwarePaddingBefore();
707 for (size_t i = 0; i < children.size(); ++i) {
708 RenderBox* child = children[i];
709 if (child->isPositioned()) {
710 child->layer()->setStaticBlockPosition(mainAxisOffset);
711 mainAxisOffset -= packingSpaceBetweenChildren(availableFreeSpace, style()->flexPack(), childSizes.size());
714 mainAxisOffset -= mainAxisExtentForChild(child) + flowAwareMarginEndForChild(child);
716 LayoutRect oldRect = child->frameRect();
717 setFlowAwareLocationForChild(child, IntPoint(mainAxisOffset, crossAxisOffset + flowAwareMarginBeforeForChild(child)));
718 if (!selfNeedsLayout() && child->checkForRepaintDuringLayout())
719 child->repaintDuringLayoutIfMoved(oldRect);
721 mainAxisOffset -= flowAwareMarginStartForChild(child);
722 mainAxisOffset -= packingSpaceBetweenChildren(availableFreeSpace, style()->flexPack(), childSizes.size());
726 void RenderFlexibleBox::adjustAlignmentForChild(RenderBox* child, LayoutUnit delta)
728 LayoutRect oldRect = child->frameRect();
730 setFlowAwareLocationForChild(child, flowAwareLocationForChild(child) + LayoutSize(0, delta));
732 // If the child moved, we have to repaint it as well as any floating/positioned
733 // descendants. An exception is if we need a layout. In this case, we know we're going to
734 // repaint ourselves (and the child) anyway.
735 if (!selfNeedsLayout() && child->checkForRepaintDuringLayout())
736 child->repaintDuringLayoutIfMoved(oldRect);
739 void RenderFlexibleBox::alignChildren(const OrderedFlexItemList& children, LayoutUnit maxAscent)
741 LayoutUnit crossExtent = crossAxisExtent();
743 for (size_t i = 0; i < children.size(); ++i) {
744 RenderBox* child = children[i];
745 // direction:rtl + flex-direction:column means the cross-axis direction is flipped.
746 if (!style()->isLeftToRightDirection() && isColumnFlow()) {
747 LayoutPoint location = flowAwareLocationForChild(child);
748 location.setY(crossExtent - crossAxisExtentForChild(child) - location.y());
749 setFlowAwareLocationForChild(child, location);
752 // FIXME: Make sure this does the right thing with column flows.
753 switch (flexAlignForChild(child)) {
755 ASSERT_NOT_REACHED();
758 if (!isColumnFlow() && child->style()->logicalHeight().isAuto()) {
759 LayoutUnit logicalHeightBefore = child->logicalHeight();
760 LayoutUnit stretchedLogicalHeight = child->logicalHeight() + RenderFlexibleBox::availableAlignmentSpaceForChild(child);
761 child->setLogicalHeight(stretchedLogicalHeight);
762 child->computeLogicalHeight();
764 if (child->logicalHeight() != logicalHeightBefore) {
765 child->setOverrideHeight(child->logicalHeight());
766 child->setLogicalHeight(0);
767 child->setChildNeedsLayout(true);
768 child->layoutIfNeeded();
776 adjustAlignmentForChild(child, RenderFlexibleBox::availableAlignmentSpaceForChild(child));
779 adjustAlignmentForChild(child, RenderFlexibleBox::availableAlignmentSpaceForChild(child) / 2);
781 case AlignBaseline: {
782 LayoutUnit ascent = marginBoxAscent(child);
783 adjustAlignmentForChild(child, maxAscent - ascent);