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 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 typedef HashSet<int, DefaultHash<int>::Hash, FlexOrderHashTraits> FlexOrderHashSet;
52 class RenderFlexibleBox::TreeOrderIterator {
54 explicit TreeOrderIterator(RenderFlexibleBox* flexibleBox)
55 : m_flexibleBox(flexibleBox)
68 m_currentChild = m_currentChild ? m_currentChild->nextSiblingBox() : m_flexibleBox->firstChildBox();
71 m_flexOrderValues.add(m_currentChild->style()->flexOrder());
73 return m_currentChild;
81 const FlexOrderHashSet& flexOrderValues()
83 return m_flexOrderValues;
87 RenderFlexibleBox* m_flexibleBox;
88 RenderBox* m_currentChild;
89 FlexOrderHashSet m_flexOrderValues;
92 class RenderFlexibleBox::FlexOrderIterator {
94 FlexOrderIterator(RenderFlexibleBox* flexibleBox, const FlexOrderHashSet& flexOrderValues)
95 : m_flexibleBox(flexibleBox)
97 , m_orderValuesIterator(0)
99 copyToVector(flexOrderValues, m_orderValues);
100 std::sort(m_orderValues.begin(), m_orderValues.end());
112 if (!m_currentChild) {
113 if (m_orderValuesIterator == m_orderValues.end())
115 if (m_orderValuesIterator) {
116 ++m_orderValuesIterator;
117 if (m_orderValuesIterator == m_orderValues.end())
120 m_orderValuesIterator = m_orderValues.begin();
122 m_currentChild = m_flexibleBox->firstChildBox();
124 m_currentChild = m_currentChild->nextSiblingBox();
125 } while (!m_currentChild || m_currentChild->style()->flexOrder() != *m_orderValuesIterator);
127 return m_currentChild;
133 m_orderValuesIterator = 0;
137 RenderFlexibleBox* m_flexibleBox;
138 RenderBox* m_currentChild;
139 Vector<int> m_orderValues;
140 Vector<int>::const_iterator m_orderValuesIterator;
144 RenderFlexibleBox::RenderFlexibleBox(Node* node)
147 setChildrenInline(false); // All of our children must be block-level.
150 RenderFlexibleBox::~RenderFlexibleBox()
154 const char* RenderFlexibleBox::renderName() const
156 return "RenderFlexibleBox";
159 void RenderFlexibleBox::layoutBlock(bool relayoutChildren, int, BlockLayoutPass)
161 ASSERT(needsLayout());
163 if (!relayoutChildren && simplifiedLayout())
166 LayoutRepainter repainter(*this, checkForRepaintDuringLayout());
167 LayoutStateMaintainer statePusher(view(), this, IntSize(x(), y()), hasTransform() || hasReflection() || style()->isFlippedBlocksWritingMode());
169 if (inRenderFlowThread()) {
170 // Regions changing widths can force us to relayout our children.
171 if (logicalWidthChangedInRegions())
172 relayoutChildren = true;
174 computeInitialRegionRangeForBlock();
176 IntSize previousSize = size();
179 // We need to call both of these because we grab both crossAxisExtent and mainAxisExtent in layoutFlexItems.
180 computeLogicalWidth();
181 computeLogicalHeight();
185 // For overflow:scroll blocks, ensure we have both scrollbars in place always.
186 if (scrollsOverflow()) {
187 if (style()->overflowX() == OSCROLL)
188 layer()->setHasHorizontalScrollbar(true);
189 if (style()->overflowY() == OSCROLL)
190 layer()->setHasVerticalScrollbar(true);
193 layoutFlexItems(relayoutChildren);
195 LayoutUnit oldClientAfterEdge = clientLogicalBottom();
196 computeLogicalHeight();
198 if (size() != previousSize)
199 relayoutChildren = true;
201 layoutPositionedObjects(relayoutChildren || isRoot());
203 computeRegionRangeForBlock();
205 // FIXME: css3/flexbox/repaint-rtl-column.html seems to repaint more overflow than it needs to.
206 computeOverflow(oldClientAfterEdge);
209 updateLayerTransform();
211 // Update our scroll information if we're overflow:auto/scroll/hidden now that we know if
212 // we overflow or not.
213 if (hasOverflowClip())
214 layer()->updateScrollInfoAfterLayout();
216 repainter.repaintAfterLayout();
218 setNeedsLayout(false);
221 bool RenderFlexibleBox::hasOrthogonalFlow(RenderBox* child) const
223 // FIXME: If the child is a flexbox, then we need to check isHorizontalFlow.
224 return isHorizontalFlow() != child->isHorizontalWritingMode();
227 bool RenderFlexibleBox::isColumnFlow() const
229 return style()->isColumnFlexDirection();
232 bool RenderFlexibleBox::isHorizontalFlow() const
234 if (isHorizontalWritingMode())
235 return !isColumnFlow();
236 return isColumnFlow();
239 bool RenderFlexibleBox::isLeftToRightFlow() const
242 return style()->writingMode() == TopToBottomWritingMode || style()->writingMode() == LeftToRightWritingMode;
243 return style()->isLeftToRightDirection() ^ (style()->flexDirection() == FlowRowReverse);
246 Length RenderFlexibleBox::mainAxisLengthForChild(RenderBox* child) const
248 return isHorizontalFlow() ? child->style()->width() : child->style()->height();
251 Length RenderFlexibleBox::crossAxisLength() const
253 return isHorizontalFlow() ? style()->height() : style()->width();
256 void RenderFlexibleBox::setCrossAxisExtent(LayoutUnit extent)
258 if (isHorizontalFlow())
264 LayoutUnit RenderFlexibleBox::crossAxisExtentForChild(RenderBox* child)
266 return isHorizontalFlow() ? child->height() : child->width();
269 LayoutUnit RenderFlexibleBox::mainAxisExtentForChild(RenderBox* child)
271 return isHorizontalFlow() ? child->width() : child->height();
274 LayoutUnit RenderFlexibleBox::crossAxisExtent() const
276 return isHorizontalFlow() ? height() : width();
279 LayoutUnit RenderFlexibleBox::mainAxisExtent() const
281 return isHorizontalFlow() ? width() : height();
284 LayoutUnit RenderFlexibleBox::crossAxisContentExtent() const
286 return isHorizontalFlow() ? contentHeight() : contentWidth();
289 LayoutUnit RenderFlexibleBox::mainAxisContentExtent() const
291 return isHorizontalFlow() ? contentWidth() : contentHeight();
294 WritingMode RenderFlexibleBox::transformedWritingMode() const
296 WritingMode mode = style()->writingMode();
301 case TopToBottomWritingMode:
302 case BottomToTopWritingMode:
303 return style()->isLeftToRightDirection() ? LeftToRightWritingMode : RightToLeftWritingMode;
304 case LeftToRightWritingMode:
305 case RightToLeftWritingMode:
306 return style()->isLeftToRightDirection() ? TopToBottomWritingMode : BottomToTopWritingMode;
308 ASSERT_NOT_REACHED();
309 return TopToBottomWritingMode;
312 LayoutUnit RenderFlexibleBox::flowAwareBorderStart() const
314 if (isHorizontalFlow())
315 return isLeftToRightFlow() ? borderLeft() : borderRight();
316 return isLeftToRightFlow() ? borderTop() : borderBottom();
319 LayoutUnit RenderFlexibleBox::flowAwareBorderEnd() const
321 if (isHorizontalFlow())
322 return isLeftToRightFlow() ? borderRight() : borderLeft();
323 return isLeftToRightFlow() ? borderBottom() : borderTop();
326 LayoutUnit RenderFlexibleBox::flowAwareBorderBefore() const
328 switch (transformedWritingMode()) {
329 case TopToBottomWritingMode:
331 case BottomToTopWritingMode:
332 return borderBottom();
333 case LeftToRightWritingMode:
335 case RightToLeftWritingMode:
336 return borderRight();
338 ASSERT_NOT_REACHED();
342 LayoutUnit RenderFlexibleBox::crossAxisBorderAndPaddingExtent() const
344 return isHorizontalFlow() ? borderAndPaddingHeight() : borderAndPaddingWidth();
347 LayoutUnit RenderFlexibleBox::flowAwarePaddingStart() const
349 if (isHorizontalFlow())
350 return isLeftToRightFlow() ? paddingLeft() : paddingRight();
351 return isLeftToRightFlow() ? paddingTop() : paddingBottom();
354 LayoutUnit RenderFlexibleBox::flowAwarePaddingEnd() const
356 if (isHorizontalFlow())
357 return isLeftToRightFlow() ? paddingRight() : paddingLeft();
358 return isLeftToRightFlow() ? paddingBottom() : paddingTop();
361 LayoutUnit RenderFlexibleBox::flowAwarePaddingBefore() const
363 switch (transformedWritingMode()) {
364 case TopToBottomWritingMode:
366 case BottomToTopWritingMode:
367 return paddingBottom();
368 case LeftToRightWritingMode:
369 return paddingLeft();
370 case RightToLeftWritingMode:
371 return paddingRight();
373 ASSERT_NOT_REACHED();
377 LayoutUnit RenderFlexibleBox::flowAwareMarginStartForChild(RenderBox* child) const
379 if (isHorizontalFlow())
380 return isLeftToRightFlow() ? child->marginLeft() : child->marginRight();
381 return isLeftToRightFlow() ? child->marginTop() : child->marginBottom();
384 LayoutUnit RenderFlexibleBox::flowAwareMarginEndForChild(RenderBox* child) const
386 if (isHorizontalFlow())
387 return isLeftToRightFlow() ? child->marginRight() : child->marginLeft();
388 return isLeftToRightFlow() ? child->marginBottom() : child->marginTop();
391 LayoutUnit RenderFlexibleBox::flowAwareMarginBeforeForChild(RenderBox* child) const
393 switch (transformedWritingMode()) {
394 case TopToBottomWritingMode:
395 return child->marginTop();
396 case BottomToTopWritingMode:
397 return child->marginBottom();
398 case LeftToRightWritingMode:
399 return child->marginLeft();
400 case RightToLeftWritingMode:
401 return child->marginRight();
403 ASSERT_NOT_REACHED();
407 LayoutUnit RenderFlexibleBox::flowAwareMarginAfterForChild(RenderBox* child) const
409 switch (transformedWritingMode()) {
410 case TopToBottomWritingMode:
411 return child->marginBottom();
412 case BottomToTopWritingMode:
413 return child->marginTop();
414 case LeftToRightWritingMode:
415 return child->marginRight();
416 case RightToLeftWritingMode:
417 return child->marginLeft();
419 ASSERT_NOT_REACHED();
420 return marginBottom();
423 LayoutUnit RenderFlexibleBox::crossAxisMarginExtentForChild(RenderBox* child) const
425 return isHorizontalFlow() ? child->marginTop() + child->marginBottom() : child->marginLeft() + child->marginRight();
428 LayoutUnit RenderFlexibleBox::crossAxisScrollbarExtent() const
430 return isHorizontalFlow() ? horizontalScrollbarHeight() : verticalScrollbarWidth();
433 LayoutPoint RenderFlexibleBox::flowAwareLocationForChild(RenderBox* child) const
435 return isHorizontalFlow() ? child->location() : child->location().transposedPoint();
438 void RenderFlexibleBox::setFlowAwareLocationForChild(RenderBox* child, const LayoutPoint& location)
440 if (isHorizontalFlow())
441 child->setLocation(location);
443 child->setLocation(location.transposedPoint());
446 LayoutUnit RenderFlexibleBox::mainAxisBorderAndPaddingExtentForChild(RenderBox* child) const
448 return isHorizontalFlow() ? child->borderAndPaddingWidth() : child->borderAndPaddingHeight();
451 LayoutUnit RenderFlexibleBox::mainAxisScrollbarExtentForChild(RenderBox* child) const
453 return isHorizontalFlow() ? child->verticalScrollbarWidth() : child->horizontalScrollbarHeight();
456 LayoutUnit RenderFlexibleBox::preferredMainAxisContentExtentForChild(RenderBox* child) const
458 Length mainAxisLength = mainAxisLengthForChild(child);
459 if (mainAxisLength.isAuto()) {
460 LayoutUnit mainAxisExtent = hasOrthogonalFlow(child) ? child->logicalHeight() : child->maxPreferredLogicalWidth();
461 return mainAxisExtent - mainAxisBorderAndPaddingExtentForChild(child) - mainAxisScrollbarExtentForChild(child);
463 return mainAxisLength.calcMinValue(mainAxisContentExtent());
466 void RenderFlexibleBox::layoutFlexItems(bool relayoutChildren)
468 float totalPositiveFlexibility;
469 float totalNegativeFlexibility;
470 TreeOrderIterator treeIterator(this);
472 WTF::Vector<LayoutUnit> preferredSizes;
473 computeMainAxisPreferredSizes(relayoutChildren, treeIterator, preferredSizes, totalPositiveFlexibility, totalNegativeFlexibility);
474 LayoutUnit preferredMainAxisExtent = 0;
475 for (size_t i = 0; i < preferredSizes.size(); ++i)
476 preferredMainAxisExtent += preferredSizes[i];
477 LayoutUnit availableFreeSpace = mainAxisContentExtent() - preferredMainAxisExtent;
479 FlexOrderIterator flexIterator(this, treeIterator.flexOrderValues());
480 InflexibleFlexItemSize inflexibleItems;
481 WTF::Vector<LayoutUnit> childSizes;
482 while (!runFreeSpaceAllocationAlgorithm(flexIterator, availableFreeSpace, totalPositiveFlexibility, totalNegativeFlexibility, inflexibleItems, childSizes)) {
483 ASSERT(totalPositiveFlexibility >= 0 && totalNegativeFlexibility >= 0);
484 ASSERT(inflexibleItems.size() > 0);
487 layoutAndPlaceChildren(flexIterator, childSizes, availableFreeSpace, totalPositiveFlexibility);
490 float RenderFlexibleBox::positiveFlexForChild(RenderBox* child) const
492 return isHorizontalFlow() ? child->style()->flexboxWidthPositiveFlex() : child->style()->flexboxHeightPositiveFlex();
495 float RenderFlexibleBox::negativeFlexForChild(RenderBox* child) const
497 return isHorizontalFlow() ? child->style()->flexboxWidthNegativeFlex() : child->style()->flexboxHeightNegativeFlex();
500 LayoutUnit RenderFlexibleBox::availableAlignmentSpaceForChild(RenderBox* child)
502 LayoutUnit crossContentExtent = crossAxisContentExtent();
503 LayoutUnit childCrossExtent = crossAxisMarginExtentForChild(child) + crossAxisExtentForChild(child);
504 return crossContentExtent - childCrossExtent;
507 LayoutUnit RenderFlexibleBox::marginBoxAscent(RenderBox* child)
509 LayoutUnit ascent = child->firstLineBoxBaseline();
511 ascent = crossAxisExtentForChild(child) + flowAwareMarginAfterForChild(child);
512 return ascent + flowAwareMarginBeforeForChild(child);
515 void RenderFlexibleBox::computeMainAxisPreferredSizes(bool relayoutChildren, TreeOrderIterator& iterator, WTF::Vector<LayoutUnit>& preferredSizes, float& totalPositiveFlexibility, float& totalNegativeFlexibility)
517 totalPositiveFlexibility = totalNegativeFlexibility = 0;
519 LayoutUnit flexboxAvailableContentExtent = mainAxisContentExtent();
520 for (RenderBox* child = iterator.first(); child; child = iterator.next()) {
521 if (child->isPositioned()) {
522 preferredSizes.append(0);
526 child->clearOverrideSize();
527 if (mainAxisLengthForChild(child).isAuto()) {
528 if (!relayoutChildren)
529 child->setChildNeedsLayout(true);
530 child->layoutIfNeeded();
533 LayoutUnit preferredSize = mainAxisBorderAndPaddingExtentForChild(child) + preferredMainAxisContentExtentForChild(child);
535 // We set the margins because we want to make sure 'auto' has a margin
536 // of 0 and because if we're not auto sizing, we don't do a layout that
537 // computes the start/end margins.
538 if (isHorizontalFlow()) {
539 child->setMarginLeft(child->style()->marginLeft().calcMinValue(flexboxAvailableContentExtent));
540 child->setMarginRight(child->style()->marginRight().calcMinValue(flexboxAvailableContentExtent));
541 preferredSize += child->marginLeft() + child->marginRight();
543 child->setMarginTop(child->style()->marginTop().calcMinValue(flexboxAvailableContentExtent));
544 child->setMarginBottom(child->style()->marginBottom().calcMinValue(flexboxAvailableContentExtent));
545 preferredSize += child->marginTop() + child->marginBottom();
548 preferredSizes.append(preferredSize);
550 totalPositiveFlexibility += positiveFlexForChild(child);
551 totalNegativeFlexibility += negativeFlexForChild(child);
555 // Returns true if we successfully ran the algorithm and sized the flex items.
556 bool RenderFlexibleBox::runFreeSpaceAllocationAlgorithm(FlexOrderIterator& iterator, LayoutUnit& availableFreeSpace, float& totalPositiveFlexibility, float& totalNegativeFlexibility, InflexibleFlexItemSize& inflexibleItems, WTF::Vector<LayoutUnit>& childSizes)
560 LayoutUnit flexboxAvailableContentExtent = mainAxisContentExtent();
561 for (RenderBox* child = iterator.first(); child; child = iterator.next()) {
562 if (child->isPositioned()) {
563 childSizes.append(0);
567 LayoutUnit childPreferredSize;
568 if (inflexibleItems.contains(child))
569 childPreferredSize = inflexibleItems.get(child);
571 childPreferredSize = preferredMainAxisContentExtentForChild(child);
572 if (availableFreeSpace > 0 && totalPositiveFlexibility > 0) {
573 childPreferredSize += lroundf(availableFreeSpace * positiveFlexForChild(child) / totalPositiveFlexibility);
575 Length childLogicalMaxWidth = isHorizontalFlow() ? child->style()->maxWidth() : child->style()->maxHeight();
576 if (!childLogicalMaxWidth.isUndefined() && childLogicalMaxWidth.isSpecified() && childPreferredSize > childLogicalMaxWidth.calcValue(flexboxAvailableContentExtent)) {
577 childPreferredSize = childLogicalMaxWidth.calcValue(flexboxAvailableContentExtent);
578 availableFreeSpace -= childPreferredSize - preferredMainAxisContentExtentForChild(child);
579 totalPositiveFlexibility -= positiveFlexForChild(child);
581 inflexibleItems.set(child, childPreferredSize);
584 } else if (availableFreeSpace < 0 && totalNegativeFlexibility > 0) {
585 childPreferredSize += lroundf(availableFreeSpace * negativeFlexForChild(child) / totalNegativeFlexibility);
587 Length childLogicalMinWidth = isHorizontalFlow() ? child->style()->minWidth() : child->style()->minHeight();
588 if (!childLogicalMinWidth.isUndefined() && childLogicalMinWidth.isSpecified() && childPreferredSize < childLogicalMinWidth.calcValue(flexboxAvailableContentExtent)) {
589 childPreferredSize = childLogicalMinWidth.calcValue(flexboxAvailableContentExtent);
590 availableFreeSpace += preferredMainAxisContentExtentForChild(child) - childPreferredSize;
591 totalNegativeFlexibility -= negativeFlexForChild(child);
593 inflexibleItems.set(child, childPreferredSize);
598 childSizes.append(childPreferredSize);
603 static bool hasPackingSpace(LayoutUnit availableFreeSpace, float totalPositiveFlexibility)
605 return availableFreeSpace > 0 && !totalPositiveFlexibility;
608 static LayoutUnit initialPackingOffset(LayoutUnit availableFreeSpace, float totalPositiveFlexibility, EFlexPack flexPack, size_t numberOfChildren)
610 if (hasPackingSpace(availableFreeSpace, totalPositiveFlexibility)) {
611 if (flexPack == PackEnd)
612 return availableFreeSpace;
613 if (flexPack == PackCenter)
614 return availableFreeSpace / 2;
615 if (flexPack == PackDistribute && numberOfChildren)
616 return availableFreeSpace / (2 * numberOfChildren);
621 static LayoutUnit packingSpaceBetweenChildren(LayoutUnit availableFreeSpace, float totalPositiveFlexibility, EFlexPack flexPack, size_t numberOfChildren)
623 if (hasPackingSpace(availableFreeSpace, totalPositiveFlexibility) && numberOfChildren > 1) {
624 if (flexPack == PackJustify)
625 return availableFreeSpace / (numberOfChildren - 1);
626 if (flexPack == PackDistribute)
627 return availableFreeSpace / numberOfChildren;
632 void RenderFlexibleBox::setLogicalOverrideSize(RenderBox* child, LayoutUnit childPreferredSize)
634 // FIXME: Rename setOverrideWidth/setOverrideHeight to setOverrideLogicalWidth/setOverrideLogicalHeight.
635 if (hasOrthogonalFlow(child))
636 child->setOverrideHeight(childPreferredSize);
638 child->setOverrideWidth(childPreferredSize);
641 void RenderFlexibleBox::prepareChildForPositionedLayout(RenderBox* child, LayoutUnit mainAxisOffset, LayoutUnit crossAxisOffset)
643 ASSERT(child->isPositioned());
644 child->containingBlock()->insertPositionedObject(child);
645 RenderLayer* childLayer = child->layer();
646 LayoutUnit inlinePosition = isColumnFlow() ? crossAxisOffset : mainAxisOffset;
647 if (style()->flexDirection() == FlowRowReverse)
648 inlinePosition = mainAxisExtent() - mainAxisOffset;
649 childLayer->setStaticInlinePosition(inlinePosition); // FIXME: Not right for regions.
651 LayoutUnit staticBlockPosition = isColumnFlow() ? mainAxisOffset : crossAxisOffset;
652 if (childLayer->staticBlockPosition() != staticBlockPosition) {
653 childLayer->setStaticBlockPosition(staticBlockPosition);
654 if (child->style()->hasStaticBlockPosition(style()->isHorizontalWritingMode()))
655 child->setChildNeedsLayout(true, false);
659 static EFlexAlign flexAlignForChild(RenderBox* child)
661 EFlexAlign align = child->style()->flexItemAlign();
662 if (align == AlignAuto)
663 return child->parent()->style()->flexAlign();
667 void RenderFlexibleBox::layoutAndPlaceChildren(FlexOrderIterator& iterator, const WTF::Vector<LayoutUnit>& childSizes, LayoutUnit availableFreeSpace, float totalPositiveFlexibility)
669 LayoutUnit mainAxisOffset = flowAwareBorderStart() + flowAwarePaddingStart();
670 mainAxisOffset += initialPackingOffset(availableFreeSpace, totalPositiveFlexibility, style()->flexPack(), childSizes.size());
671 if (style()->flexDirection() == FlowRowReverse)
672 mainAxisOffset += isHorizontalFlow() ? verticalScrollbarWidth() : horizontalScrollbarHeight();
674 LayoutUnit crossAxisOffset = flowAwareBorderBefore() + flowAwarePaddingBefore();
675 LayoutUnit totalMainExtent = mainAxisExtent();
676 LayoutUnit maxAscent = 0, maxDescent = 0; // Used when flex-align: baseline.
677 bool shouldFlipMainAxis = !isColumnFlow() && !isLeftToRightFlow();
679 for (RenderBox* child = iterator.first(); child; child = iterator.next(), ++i) {
680 if (child->isPositioned()) {
681 prepareChildForPositionedLayout(child, mainAxisOffset, crossAxisOffset);
682 mainAxisOffset += packingSpaceBetweenChildren(availableFreeSpace, totalPositiveFlexibility, style()->flexPack(), childSizes.size());
685 LayoutUnit childPreferredSize = childSizes[i] + mainAxisBorderAndPaddingExtentForChild(child);
686 setLogicalOverrideSize(child, childPreferredSize);
687 child->setChildNeedsLayout(true);
688 child->layoutIfNeeded();
690 if (flexAlignForChild(child) == AlignBaseline) {
691 LayoutUnit ascent = marginBoxAscent(child);
692 LayoutUnit descent = (crossAxisMarginExtentForChild(child) + crossAxisExtentForChild(child)) - ascent;
694 maxAscent = std::max(maxAscent, ascent);
695 maxDescent = std::max(maxDescent, descent);
697 if (crossAxisLength().isAuto())
698 setCrossAxisExtent(std::max(crossAxisExtent(), crossAxisBorderAndPaddingExtent() + crossAxisMarginExtentForChild(child) + maxAscent + maxDescent + crossAxisScrollbarExtent()));
699 } else if (crossAxisLength().isAuto())
700 setCrossAxisExtent(std::max(crossAxisExtent(), crossAxisBorderAndPaddingExtent() + crossAxisMarginExtentForChild(child) + crossAxisExtentForChild(child) + crossAxisScrollbarExtent()));
702 mainAxisOffset += flowAwareMarginStartForChild(child);
704 LayoutUnit childMainExtent = mainAxisExtentForChild(child);
705 IntPoint childLocation(shouldFlipMainAxis ? totalMainExtent - mainAxisOffset - childMainExtent : mainAxisOffset,
706 crossAxisOffset + flowAwareMarginBeforeForChild(child));
708 // FIXME: Supporting layout deltas.
709 setFlowAwareLocationForChild(child, childLocation);
710 mainAxisOffset += childMainExtent + flowAwareMarginEndForChild(child);
712 mainAxisOffset += packingSpaceBetweenChildren(availableFreeSpace, totalPositiveFlexibility, style()->flexPack(), childSizes.size());
715 setLogicalHeight(mainAxisOffset + flowAwareBorderEnd() + flowAwarePaddingEnd() + scrollbarLogicalHeight());
718 if (style()->flexDirection() == FlowColumnReverse) {
719 // We have to do an extra pass for column-reverse to reposition the flex items since the start depends
720 // on the height of the flexbox, which we only know after we've positioned all the flex items.
721 computeLogicalHeight();
722 layoutColumnReverse(iterator, childSizes, availableFreeSpace, totalPositiveFlexibility);
725 alignChildren(iterator, maxAscent);
728 void RenderFlexibleBox::layoutColumnReverse(FlexOrderIterator& iterator, const WTF::Vector<LayoutUnit>& childSizes, LayoutUnit availableFreeSpace, float totalPositiveFlexibility)
730 // This is similar to the logic in layoutAndPlaceChildren, except we place the children
731 // starting from the end of the flexbox. We also don't need to layout anything since we're
732 // just moving the children to a new position.
733 LayoutUnit mainAxisOffset = logicalHeight() - flowAwareBorderEnd() - flowAwarePaddingEnd();
734 mainAxisOffset -= initialPackingOffset(availableFreeSpace, totalPositiveFlexibility, style()->flexPack(), childSizes.size());
735 mainAxisOffset -= isHorizontalFlow() ? verticalScrollbarWidth() : horizontalScrollbarHeight();
737 LayoutUnit crossAxisOffset = flowAwareBorderBefore() + flowAwarePaddingBefore();
739 for (RenderBox* child = iterator.first(); child; child = iterator.next(), ++i) {
740 if (child->isPositioned()) {
741 child->layer()->setStaticBlockPosition(mainAxisOffset);
742 mainAxisOffset -= packingSpaceBetweenChildren(availableFreeSpace, totalPositiveFlexibility, style()->flexPack(), childSizes.size());
745 mainAxisOffset -= mainAxisExtentForChild(child) + flowAwareMarginEndForChild(child);
747 LayoutRect oldRect = child->frameRect();
748 setFlowAwareLocationForChild(child, IntPoint(mainAxisOffset, crossAxisOffset + flowAwareMarginBeforeForChild(child)));
749 if (!selfNeedsLayout() && child->checkForRepaintDuringLayout())
750 child->repaintDuringLayoutIfMoved(oldRect);
752 mainAxisOffset -= flowAwareMarginStartForChild(child);
753 mainAxisOffset -= packingSpaceBetweenChildren(availableFreeSpace, totalPositiveFlexibility, style()->flexPack(), childSizes.size());
757 void RenderFlexibleBox::adjustAlignmentForChild(RenderBox* child, LayoutUnit delta)
759 LayoutRect oldRect = child->frameRect();
761 setFlowAwareLocationForChild(child, flowAwareLocationForChild(child) + LayoutSize(0, delta));
763 // If the child moved, we have to repaint it as well as any floating/positioned
764 // descendants. An exception is if we need a layout. In this case, we know we're going to
765 // repaint ourselves (and the child) anyway.
766 if (!selfNeedsLayout() && child->checkForRepaintDuringLayout())
767 child->repaintDuringLayoutIfMoved(oldRect);
770 void RenderFlexibleBox::alignChildren(FlexOrderIterator& iterator, LayoutUnit maxAscent)
772 LayoutUnit crossExtent = crossAxisExtent();
774 for (RenderBox* child = iterator.first(); child; child = iterator.next()) {
775 // direction:rtl + flex-direction:column means the cross-axis direction is flipped.
776 if (!style()->isLeftToRightDirection() && isColumnFlow()) {
777 LayoutPoint location = flowAwareLocationForChild(child);
778 location.setY(crossExtent - crossAxisExtentForChild(child) - location.y());
779 setFlowAwareLocationForChild(child, location);
782 // FIXME: Make sure this does the right thing with column flows.
783 switch (flexAlignForChild(child)) {
785 ASSERT_NOT_REACHED();
788 if (!isColumnFlow() && child->style()->logicalHeight().isAuto()) {
789 LayoutUnit logicalHeightBefore = child->logicalHeight();
790 LayoutUnit stretchedLogicalHeight = child->logicalHeight() + RenderFlexibleBox::availableAlignmentSpaceForChild(child);
791 child->setLogicalHeight(stretchedLogicalHeight);
792 child->computeLogicalHeight();
794 if (child->logicalHeight() != logicalHeightBefore) {
795 child->setOverrideHeight(child->logicalHeight());
796 child->setLogicalHeight(0);
797 child->setChildNeedsLayout(true);
798 child->layoutIfNeeded();
806 adjustAlignmentForChild(child, RenderFlexibleBox::availableAlignmentSpaceForChild(child));
809 adjustAlignmentForChild(child, RenderFlexibleBox::availableAlignmentSpaceForChild(child) / 2);
811 case AlignBaseline: {
812 LayoutUnit ascent = marginBoxAscent(child);
813 adjustAlignmentForChild(child, maxAscent - ascent);