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 static LayoutUnit marginLogicalWidthForChild(RenderBox* child, RenderStyle* parentStyle)
119 // A margin has three types: fixed, percentage, and auto (variable).
120 // Auto and percentage margins become 0 when computing min/max width.
121 // Fixed margins can be added in as is.
122 Length marginLeft = child->style()->marginStartUsing(parentStyle);
123 Length marginRight = child->style()->marginEndUsing(parentStyle);
124 LayoutUnit margin = 0;
125 if (marginLeft.isFixed())
126 margin += marginLeft.value();
127 if (marginRight.isFixed())
128 margin += marginRight.value();
132 void RenderFlexibleBox::computePreferredLogicalWidths()
134 ASSERT(preferredLogicalWidthsDirty());
136 RenderStyle* styleToUse = style();
137 if (styleToUse->logicalWidth().isFixed() && styleToUse->logicalWidth().value() > 0)
138 m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth = computeContentBoxLogicalWidth(styleToUse->logicalWidth().value());
140 m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth = 0;
142 for (RenderBox* child = firstChildBox(); child; child = child->nextSiblingBox()) {
143 if (child->isPositioned())
146 LayoutUnit margin = marginLogicalWidthForChild(child, style());
147 bool hasOrthogonalWritingMode = child->isHorizontalWritingMode() != isHorizontalWritingMode();
148 LayoutUnit minPreferredLogicalWidth = hasOrthogonalWritingMode ? child->logicalHeight() : child->minPreferredLogicalWidth();
149 LayoutUnit maxPreferredLogicalWidth = hasOrthogonalWritingMode ? child->logicalHeight() : child->maxPreferredLogicalWidth();
150 minPreferredLogicalWidth += margin;
151 maxPreferredLogicalWidth += margin;
152 if (!isColumnFlow()) {
153 m_minPreferredLogicalWidth += minPreferredLogicalWidth;
154 m_maxPreferredLogicalWidth += maxPreferredLogicalWidth;
156 m_minPreferredLogicalWidth = std::max(minPreferredLogicalWidth, m_minPreferredLogicalWidth);
157 m_maxPreferredLogicalWidth = std::max(maxPreferredLogicalWidth, m_maxPreferredLogicalWidth);
161 m_maxPreferredLogicalWidth = std::max(m_minPreferredLogicalWidth, m_maxPreferredLogicalWidth);
164 LayoutUnit scrollbarWidth = 0;
165 if (hasOverflowClip()) {
166 if (isHorizontalWritingMode() && styleToUse->overflowY() == OSCROLL) {
167 layer()->setHasVerticalScrollbar(true);
168 scrollbarWidth = verticalScrollbarWidth();
169 } else if (!isHorizontalWritingMode() && styleToUse->overflowX() == OSCROLL) {
170 layer()->setHasHorizontalScrollbar(true);
171 scrollbarWidth = horizontalScrollbarHeight();
175 m_maxPreferredLogicalWidth += scrollbarWidth;
176 m_minPreferredLogicalWidth += scrollbarWidth;
178 if (styleToUse->logicalMinWidth().isFixed() && styleToUse->logicalMinWidth().value() > 0) {
179 m_maxPreferredLogicalWidth = std::max(m_maxPreferredLogicalWidth, computeContentBoxLogicalWidth(styleToUse->logicalMinWidth().value()));
180 m_minPreferredLogicalWidth = std::max(m_minPreferredLogicalWidth, computeContentBoxLogicalWidth(styleToUse->logicalMinWidth().value()));
183 if (styleToUse->logicalMaxWidth().isFixed()) {
184 m_maxPreferredLogicalWidth = std::min(m_maxPreferredLogicalWidth, computeContentBoxLogicalWidth(styleToUse->logicalMaxWidth().value()));
185 m_minPreferredLogicalWidth = std::min(m_minPreferredLogicalWidth, computeContentBoxLogicalWidth(styleToUse->logicalMaxWidth().value()));
188 LayoutUnit borderAndPadding = borderAndPaddingLogicalWidth();
189 m_minPreferredLogicalWidth += borderAndPadding;
190 m_maxPreferredLogicalWidth += borderAndPadding;
192 setPreferredLogicalWidthsDirty(false);
195 void RenderFlexibleBox::layoutBlock(bool relayoutChildren, int, BlockLayoutPass)
197 ASSERT(needsLayout());
199 if (!relayoutChildren && simplifiedLayout())
202 LayoutRepainter repainter(*this, checkForRepaintDuringLayout());
203 LayoutStateMaintainer statePusher(view(), this, IntSize(x(), y()), hasTransform() || hasReflection() || style()->isFlippedBlocksWritingMode());
205 if (inRenderFlowThread()) {
206 // Regions changing widths can force us to relayout our children.
207 if (logicalWidthChangedInRegions())
208 relayoutChildren = true;
210 computeInitialRegionRangeForBlock();
212 IntSize previousSize = size();
215 // We need to call both of these because we grab both crossAxisExtent and mainAxisExtent in layoutFlexItems.
216 computeLogicalWidth();
217 computeLogicalHeight();
221 // For overflow:scroll blocks, ensure we have both scrollbars in place always.
222 if (scrollsOverflow()) {
223 if (style()->overflowX() == OSCROLL)
224 layer()->setHasHorizontalScrollbar(true);
225 if (style()->overflowY() == OSCROLL)
226 layer()->setHasVerticalScrollbar(true);
229 layoutFlexItems(relayoutChildren);
231 LayoutUnit oldClientAfterEdge = clientLogicalBottom();
232 computeLogicalHeight();
234 if (size() != previousSize)
235 relayoutChildren = true;
237 layoutPositionedObjects(relayoutChildren || isRoot());
239 computeRegionRangeForBlock();
241 // FIXME: css3/flexbox/repaint-rtl-column.html seems to repaint more overflow than it needs to.
242 computeOverflow(oldClientAfterEdge);
245 updateLayerTransform();
247 // Update our scroll information if we're overflow:auto/scroll/hidden now that we know if
248 // we overflow or not.
249 if (hasOverflowClip())
250 layer()->updateScrollInfoAfterLayout();
252 repainter.repaintAfterLayout();
254 setNeedsLayout(false);
257 bool RenderFlexibleBox::hasOrthogonalFlow(RenderBox* child) const
259 // FIXME: If the child is a flexbox, then we need to check isHorizontalFlow.
260 return isHorizontalFlow() != child->isHorizontalWritingMode();
263 bool RenderFlexibleBox::isColumnFlow() const
265 return style()->isColumnFlexDirection();
268 bool RenderFlexibleBox::isHorizontalFlow() const
270 if (isHorizontalWritingMode())
271 return !isColumnFlow();
272 return isColumnFlow();
275 bool RenderFlexibleBox::isLeftToRightFlow() const
278 return style()->writingMode() == TopToBottomWritingMode || style()->writingMode() == LeftToRightWritingMode;
279 return style()->isLeftToRightDirection() ^ (style()->flexDirection() == FlowRowReverse);
282 Length RenderFlexibleBox::mainAxisLengthForChild(RenderBox* child) const
284 return isHorizontalFlow() ? child->style()->width() : child->style()->height();
287 Length RenderFlexibleBox::crossAxisLength() const
289 return isHorizontalFlow() ? style()->height() : style()->width();
292 void RenderFlexibleBox::setCrossAxisExtent(LayoutUnit extent)
294 if (isHorizontalFlow())
300 LayoutUnit RenderFlexibleBox::crossAxisExtentForChild(RenderBox* child)
302 return isHorizontalFlow() ? child->height() : child->width();
305 LayoutUnit RenderFlexibleBox::mainAxisExtentForChild(RenderBox* child)
307 return isHorizontalFlow() ? child->width() : child->height();
310 LayoutUnit RenderFlexibleBox::crossAxisExtent() const
312 return isHorizontalFlow() ? height() : width();
315 LayoutUnit RenderFlexibleBox::mainAxisExtent() const
317 return isHorizontalFlow() ? width() : height();
320 LayoutUnit RenderFlexibleBox::crossAxisContentExtent() const
322 return isHorizontalFlow() ? contentHeight() : contentWidth();
325 LayoutUnit RenderFlexibleBox::mainAxisContentExtent() const
327 return isHorizontalFlow() ? contentWidth() : contentHeight();
330 WritingMode RenderFlexibleBox::transformedWritingMode() const
332 WritingMode mode = style()->writingMode();
337 case TopToBottomWritingMode:
338 case BottomToTopWritingMode:
339 return style()->isLeftToRightDirection() ? LeftToRightWritingMode : RightToLeftWritingMode;
340 case LeftToRightWritingMode:
341 case RightToLeftWritingMode:
342 return style()->isLeftToRightDirection() ? TopToBottomWritingMode : BottomToTopWritingMode;
344 ASSERT_NOT_REACHED();
345 return TopToBottomWritingMode;
348 LayoutUnit RenderFlexibleBox::flowAwareBorderStart() const
350 if (isHorizontalFlow())
351 return isLeftToRightFlow() ? borderLeft() : borderRight();
352 return isLeftToRightFlow() ? borderTop() : borderBottom();
355 LayoutUnit RenderFlexibleBox::flowAwareBorderEnd() const
357 if (isHorizontalFlow())
358 return isLeftToRightFlow() ? borderRight() : borderLeft();
359 return isLeftToRightFlow() ? borderBottom() : borderTop();
362 LayoutUnit RenderFlexibleBox::flowAwareBorderBefore() const
364 switch (transformedWritingMode()) {
365 case TopToBottomWritingMode:
367 case BottomToTopWritingMode:
368 return borderBottom();
369 case LeftToRightWritingMode:
371 case RightToLeftWritingMode:
372 return borderRight();
374 ASSERT_NOT_REACHED();
378 LayoutUnit RenderFlexibleBox::crossAxisBorderAndPaddingExtent() const
380 return isHorizontalFlow() ? borderAndPaddingHeight() : borderAndPaddingWidth();
383 LayoutUnit RenderFlexibleBox::flowAwarePaddingStart() const
385 if (isHorizontalFlow())
386 return isLeftToRightFlow() ? paddingLeft() : paddingRight();
387 return isLeftToRightFlow() ? paddingTop() : paddingBottom();
390 LayoutUnit RenderFlexibleBox::flowAwarePaddingEnd() const
392 if (isHorizontalFlow())
393 return isLeftToRightFlow() ? paddingRight() : paddingLeft();
394 return isLeftToRightFlow() ? paddingBottom() : paddingTop();
397 LayoutUnit RenderFlexibleBox::flowAwarePaddingBefore() const
399 switch (transformedWritingMode()) {
400 case TopToBottomWritingMode:
402 case BottomToTopWritingMode:
403 return paddingBottom();
404 case LeftToRightWritingMode:
405 return paddingLeft();
406 case RightToLeftWritingMode:
407 return paddingRight();
409 ASSERT_NOT_REACHED();
413 LayoutUnit RenderFlexibleBox::flowAwareMarginStartForChild(RenderBox* child) const
415 if (isHorizontalFlow())
416 return isLeftToRightFlow() ? child->marginLeft() : child->marginRight();
417 return isLeftToRightFlow() ? child->marginTop() : child->marginBottom();
420 LayoutUnit RenderFlexibleBox::flowAwareMarginEndForChild(RenderBox* child) const
422 if (isHorizontalFlow())
423 return isLeftToRightFlow() ? child->marginRight() : child->marginLeft();
424 return isLeftToRightFlow() ? child->marginBottom() : child->marginTop();
427 LayoutUnit RenderFlexibleBox::flowAwareMarginBeforeForChild(RenderBox* child) const
429 switch (transformedWritingMode()) {
430 case TopToBottomWritingMode:
431 return child->marginTop();
432 case BottomToTopWritingMode:
433 return child->marginBottom();
434 case LeftToRightWritingMode:
435 return child->marginLeft();
436 case RightToLeftWritingMode:
437 return child->marginRight();
439 ASSERT_NOT_REACHED();
443 LayoutUnit RenderFlexibleBox::flowAwareMarginAfterForChild(RenderBox* child) const
445 switch (transformedWritingMode()) {
446 case TopToBottomWritingMode:
447 return child->marginBottom();
448 case BottomToTopWritingMode:
449 return child->marginTop();
450 case LeftToRightWritingMode:
451 return child->marginRight();
452 case RightToLeftWritingMode:
453 return child->marginLeft();
455 ASSERT_NOT_REACHED();
456 return marginBottom();
459 LayoutUnit RenderFlexibleBox::crossAxisMarginExtentForChild(RenderBox* child) const
461 return isHorizontalFlow() ? child->marginTop() + child->marginBottom() : child->marginLeft() + child->marginRight();
464 LayoutUnit RenderFlexibleBox::crossAxisScrollbarExtent() const
466 return isHorizontalFlow() ? horizontalScrollbarHeight() : verticalScrollbarWidth();
469 LayoutPoint RenderFlexibleBox::flowAwareLocationForChild(RenderBox* child) const
471 return isHorizontalFlow() ? child->location() : child->location().transposedPoint();
474 void RenderFlexibleBox::setFlowAwareLocationForChild(RenderBox* child, const LayoutPoint& location)
476 if (isHorizontalFlow())
477 child->setLocation(location);
479 child->setLocation(location.transposedPoint());
482 LayoutUnit RenderFlexibleBox::mainAxisBorderAndPaddingExtentForChild(RenderBox* child) const
484 return isHorizontalFlow() ? child->borderAndPaddingWidth() : child->borderAndPaddingHeight();
487 LayoutUnit RenderFlexibleBox::mainAxisScrollbarExtentForChild(RenderBox* child) const
489 return isHorizontalFlow() ? child->verticalScrollbarWidth() : child->horizontalScrollbarHeight();
492 LayoutUnit RenderFlexibleBox::preferredMainAxisContentExtentForChild(RenderBox* child) const
494 Length mainAxisLength = mainAxisLengthForChild(child);
495 if (mainAxisLength.isAuto()) {
496 LayoutUnit mainAxisExtent = hasOrthogonalFlow(child) ? child->logicalHeight() : child->maxPreferredLogicalWidth();
497 return mainAxisExtent - mainAxisBorderAndPaddingExtentForChild(child) - mainAxisScrollbarExtentForChild(child);
499 return mainAxisLength.calcMinValue(mainAxisContentExtent());
502 void RenderFlexibleBox::layoutFlexItems(bool relayoutChildren)
504 FlexOrderHashSet flexOrderValues;
505 computeMainAxisPreferredSizes(relayoutChildren, flexOrderValues);
507 OrderedFlexItemList orderedChildren;
508 LayoutUnit preferredMainAxisExtent;
509 float totalPositiveFlexibility;
510 float totalNegativeFlexibility;
511 FlexOrderIterator flexIterator(this, flexOrderValues);
512 computeFlexOrder(flexIterator, orderedChildren, preferredMainAxisExtent, totalPositiveFlexibility, totalNegativeFlexibility);
514 LayoutUnit availableFreeSpace = mainAxisContentExtent() - preferredMainAxisExtent;
515 InflexibleFlexItemSize inflexibleItems;
516 WTF::Vector<LayoutUnit> childSizes;
517 while (!runFreeSpaceAllocationAlgorithm(orderedChildren, availableFreeSpace, totalPositiveFlexibility, totalNegativeFlexibility, inflexibleItems, childSizes)) {
518 ASSERT(totalPositiveFlexibility >= 0 && totalNegativeFlexibility >= 0);
519 ASSERT(inflexibleItems.size() > 0);
522 layoutAndPlaceChildren(orderedChildren, childSizes, availableFreeSpace);
525 float RenderFlexibleBox::positiveFlexForChild(RenderBox* child) const
527 return isHorizontalFlow() ? child->style()->flexboxWidthPositiveFlex() : child->style()->flexboxHeightPositiveFlex();
530 float RenderFlexibleBox::negativeFlexForChild(RenderBox* child) const
532 return isHorizontalFlow() ? child->style()->flexboxWidthNegativeFlex() : child->style()->flexboxHeightNegativeFlex();
535 LayoutUnit RenderFlexibleBox::availableAlignmentSpaceForChild(RenderBox* child)
537 LayoutUnit crossContentExtent = crossAxisContentExtent();
538 LayoutUnit childCrossExtent = crossAxisMarginExtentForChild(child) + crossAxisExtentForChild(child);
539 return crossContentExtent - childCrossExtent;
542 LayoutUnit RenderFlexibleBox::marginBoxAscent(RenderBox* child)
544 LayoutUnit ascent = child->firstLineBoxBaseline();
546 ascent = crossAxisExtentForChild(child) + flowAwareMarginAfterForChild(child);
547 return ascent + flowAwareMarginBeforeForChild(child);
550 void RenderFlexibleBox::computeMainAxisPreferredSizes(bool relayoutChildren, FlexOrderHashSet& flexOrderValues)
552 LayoutUnit flexboxAvailableContentExtent = mainAxisContentExtent();
553 for (RenderBox* child = firstChildBox(); child; child = child->nextSiblingBox()) {
554 flexOrderValues.add(child->style()->flexOrder());
556 if (child->isPositioned())
559 child->clearOverrideSize();
560 if (mainAxisLengthForChild(child).isAuto()) {
561 if (!relayoutChildren)
562 child->setChildNeedsLayout(true);
563 child->layoutIfNeeded();
566 // We set the margins because we want to make sure 'auto' has a margin
567 // of 0 and because if we're not auto sizing, we don't do a layout that
568 // computes the start/end margins.
569 if (isHorizontalFlow()) {
570 child->setMarginLeft(child->style()->marginLeft().calcMinValue(flexboxAvailableContentExtent));
571 child->setMarginRight(child->style()->marginRight().calcMinValue(flexboxAvailableContentExtent));
573 child->setMarginTop(child->style()->marginTop().calcMinValue(flexboxAvailableContentExtent));
574 child->setMarginBottom(child->style()->marginBottom().calcMinValue(flexboxAvailableContentExtent));
579 void RenderFlexibleBox::computeFlexOrder(FlexOrderIterator& iterator, OrderedFlexItemList& orderedChildren, LayoutUnit& preferredMainAxisExtent, float& totalPositiveFlexibility, float& totalNegativeFlexibility)
581 orderedChildren.clear();
582 preferredMainAxisExtent = 0;
583 totalPositiveFlexibility = totalNegativeFlexibility = 0;
584 for (RenderBox* child = iterator.first(); child; child = iterator.next()) {
585 orderedChildren.append(child);
586 if (child->isPositioned())
589 LayoutUnit childMainAxisExtent = mainAxisBorderAndPaddingExtentForChild(child) + preferredMainAxisContentExtentForChild(child);
590 if (isHorizontalFlow())
591 childMainAxisExtent += child->marginLeft() + child->marginRight();
593 childMainAxisExtent += child->marginTop() + child->marginBottom();
595 // FIXME: When implementing multiline, we would return here if adding
596 // the child's main axis extent would cause us to overflow.
597 preferredMainAxisExtent += childMainAxisExtent;
598 totalPositiveFlexibility += positiveFlexForChild(child);
599 totalNegativeFlexibility += negativeFlexForChild(child);
603 // Returns true if we successfully ran the algorithm and sized the flex items.
604 bool RenderFlexibleBox::runFreeSpaceAllocationAlgorithm(const OrderedFlexItemList& children, LayoutUnit& availableFreeSpace, float& totalPositiveFlexibility, float& totalNegativeFlexibility, InflexibleFlexItemSize& inflexibleItems, WTF::Vector<LayoutUnit>& childSizes)
608 LayoutUnit flexboxAvailableContentExtent = mainAxisContentExtent();
609 for (size_t i = 0; i < children.size(); ++i) {
610 RenderBox* child = children[i];
611 if (child->isPositioned()) {
612 childSizes.append(0);
616 LayoutUnit childPreferredSize;
617 if (inflexibleItems.contains(child))
618 childPreferredSize = inflexibleItems.get(child);
620 childPreferredSize = preferredMainAxisContentExtentForChild(child);
621 if (availableFreeSpace > 0 && totalPositiveFlexibility > 0) {
622 childPreferredSize += lroundf(availableFreeSpace * positiveFlexForChild(child) / totalPositiveFlexibility);
624 Length childLogicalMaxWidth = isHorizontalFlow() ? child->style()->maxWidth() : child->style()->maxHeight();
625 if (!childLogicalMaxWidth.isUndefined() && childLogicalMaxWidth.isSpecified() && childPreferredSize > childLogicalMaxWidth.calcValue(flexboxAvailableContentExtent)) {
626 childPreferredSize = childLogicalMaxWidth.calcValue(flexboxAvailableContentExtent);
627 availableFreeSpace -= childPreferredSize - preferredMainAxisContentExtentForChild(child);
628 totalPositiveFlexibility -= positiveFlexForChild(child);
630 inflexibleItems.set(child, childPreferredSize);
633 } else if (availableFreeSpace < 0 && totalNegativeFlexibility > 0) {
634 childPreferredSize += lroundf(availableFreeSpace * negativeFlexForChild(child) / totalNegativeFlexibility);
636 Length childLogicalMinWidth = isHorizontalFlow() ? child->style()->minWidth() : child->style()->minHeight();
637 if (!childLogicalMinWidth.isUndefined() && childLogicalMinWidth.isSpecified() && childPreferredSize < childLogicalMinWidth.calcValue(flexboxAvailableContentExtent)) {
638 childPreferredSize = childLogicalMinWidth.calcValue(flexboxAvailableContentExtent);
639 availableFreeSpace += preferredMainAxisContentExtentForChild(child) - childPreferredSize;
640 totalNegativeFlexibility -= negativeFlexForChild(child);
642 inflexibleItems.set(child, childPreferredSize);
647 childSizes.append(childPreferredSize);
652 static LayoutUnit initialPackingOffset(LayoutUnit availableFreeSpace, EFlexPack flexPack, size_t numberOfChildren)
654 if (availableFreeSpace > 0) {
655 if (flexPack == PackEnd)
656 return availableFreeSpace;
657 if (flexPack == PackCenter)
658 return availableFreeSpace / 2;
659 if (flexPack == PackDistribute && numberOfChildren)
660 return availableFreeSpace / (2 * numberOfChildren);
661 } else if (availableFreeSpace < 0) {
662 if (flexPack == PackCenter || flexPack == PackDistribute)
663 return availableFreeSpace / 2;
668 static LayoutUnit packingSpaceBetweenChildren(LayoutUnit availableFreeSpace, EFlexPack flexPack, size_t numberOfChildren)
670 if (availableFreeSpace > 0 && numberOfChildren > 1) {
671 if (flexPack == PackJustify)
672 return availableFreeSpace / (numberOfChildren - 1);
673 if (flexPack == PackDistribute)
674 return availableFreeSpace / numberOfChildren;
679 void RenderFlexibleBox::setLogicalOverrideSize(RenderBox* child, LayoutUnit childPreferredSize)
681 // FIXME: Rename setOverrideWidth/setOverrideHeight to setOverrideLogicalWidth/setOverrideLogicalHeight.
682 if (hasOrthogonalFlow(child))
683 child->setOverrideHeight(childPreferredSize);
685 child->setOverrideWidth(childPreferredSize);
688 void RenderFlexibleBox::prepareChildForPositionedLayout(RenderBox* child, LayoutUnit mainAxisOffset, LayoutUnit crossAxisOffset)
690 ASSERT(child->isPositioned());
691 child->containingBlock()->insertPositionedObject(child);
692 RenderLayer* childLayer = child->layer();
693 LayoutUnit inlinePosition = isColumnFlow() ? crossAxisOffset : mainAxisOffset;
694 if (style()->flexDirection() == FlowRowReverse)
695 inlinePosition = mainAxisExtent() - mainAxisOffset;
696 childLayer->setStaticInlinePosition(inlinePosition); // FIXME: Not right for regions.
698 LayoutUnit staticBlockPosition = isColumnFlow() ? mainAxisOffset : crossAxisOffset;
699 if (childLayer->staticBlockPosition() != staticBlockPosition) {
700 childLayer->setStaticBlockPosition(staticBlockPosition);
701 if (child->style()->hasStaticBlockPosition(style()->isHorizontalWritingMode()))
702 child->setChildNeedsLayout(true, false);
706 static EFlexAlign flexAlignForChild(RenderBox* child)
708 EFlexAlign align = child->style()->flexItemAlign();
709 if (align == AlignAuto)
710 return child->parent()->style()->flexAlign();
714 void RenderFlexibleBox::layoutAndPlaceChildren(const OrderedFlexItemList& children, const WTF::Vector<LayoutUnit>& childSizes, LayoutUnit availableFreeSpace)
716 LayoutUnit mainAxisOffset = flowAwareBorderStart() + flowAwarePaddingStart();
717 mainAxisOffset += initialPackingOffset(availableFreeSpace, style()->flexPack(), childSizes.size());
718 if (style()->flexDirection() == FlowRowReverse)
719 mainAxisOffset += isHorizontalFlow() ? verticalScrollbarWidth() : horizontalScrollbarHeight();
721 LayoutUnit crossAxisOffset = flowAwareBorderBefore() + flowAwarePaddingBefore();
722 LayoutUnit totalMainExtent = mainAxisExtent();
723 LayoutUnit maxAscent = 0, maxDescent = 0; // Used when flex-align: baseline.
724 bool shouldFlipMainAxis = !isColumnFlow() && !isLeftToRightFlow();
725 for (size_t i = 0; i < children.size(); ++i) {
726 RenderBox* child = children[i];
727 if (child->isPositioned()) {
728 prepareChildForPositionedLayout(child, mainAxisOffset, crossAxisOffset);
729 mainAxisOffset += packingSpaceBetweenChildren(availableFreeSpace, style()->flexPack(), childSizes.size());
732 LayoutUnit childPreferredSize = childSizes[i] + mainAxisBorderAndPaddingExtentForChild(child);
733 setLogicalOverrideSize(child, childPreferredSize);
734 child->setChildNeedsLayout(true);
735 child->layoutIfNeeded();
737 if (flexAlignForChild(child) == AlignBaseline) {
738 LayoutUnit ascent = marginBoxAscent(child);
739 LayoutUnit descent = (crossAxisMarginExtentForChild(child) + crossAxisExtentForChild(child)) - ascent;
741 maxAscent = std::max(maxAscent, ascent);
742 maxDescent = std::max(maxDescent, descent);
744 if (crossAxisLength().isAuto())
745 setCrossAxisExtent(std::max(crossAxisExtent(), crossAxisBorderAndPaddingExtent() + crossAxisMarginExtentForChild(child) + maxAscent + maxDescent + crossAxisScrollbarExtent()));
746 } else if (crossAxisLength().isAuto())
747 setCrossAxisExtent(std::max(crossAxisExtent(), crossAxisBorderAndPaddingExtent() + crossAxisMarginExtentForChild(child) + crossAxisExtentForChild(child) + crossAxisScrollbarExtent()));
749 mainAxisOffset += flowAwareMarginStartForChild(child);
751 LayoutUnit childMainExtent = mainAxisExtentForChild(child);
752 IntPoint childLocation(shouldFlipMainAxis ? totalMainExtent - mainAxisOffset - childMainExtent : mainAxisOffset,
753 crossAxisOffset + flowAwareMarginBeforeForChild(child));
755 // FIXME: Supporting layout deltas.
756 setFlowAwareLocationForChild(child, childLocation);
757 mainAxisOffset += childMainExtent + flowAwareMarginEndForChild(child);
759 mainAxisOffset += packingSpaceBetweenChildren(availableFreeSpace, style()->flexPack(), childSizes.size());
762 setLogicalHeight(mainAxisOffset + flowAwareBorderEnd() + flowAwarePaddingEnd() + scrollbarLogicalHeight());
765 if (style()->flexDirection() == FlowColumnReverse) {
766 // We have to do an extra pass for column-reverse to reposition the flex items since the start depends
767 // on the height of the flexbox, which we only know after we've positioned all the flex items.
768 computeLogicalHeight();
769 layoutColumnReverse(children, childSizes, availableFreeSpace);
772 alignChildren(children, maxAscent);
775 void RenderFlexibleBox::layoutColumnReverse(const OrderedFlexItemList& children, const WTF::Vector<LayoutUnit>& childSizes, LayoutUnit availableFreeSpace)
777 // This is similar to the logic in layoutAndPlaceChildren, except we place the children
778 // starting from the end of the flexbox. We also don't need to layout anything since we're
779 // just moving the children to a new position.
780 LayoutUnit mainAxisOffset = logicalHeight() - flowAwareBorderEnd() - flowAwarePaddingEnd();
781 mainAxisOffset -= initialPackingOffset(availableFreeSpace, style()->flexPack(), childSizes.size());
782 mainAxisOffset -= isHorizontalFlow() ? verticalScrollbarWidth() : horizontalScrollbarHeight();
784 LayoutUnit crossAxisOffset = flowAwareBorderBefore() + flowAwarePaddingBefore();
785 for (size_t i = 0; i < children.size(); ++i) {
786 RenderBox* child = children[i];
787 if (child->isPositioned()) {
788 child->layer()->setStaticBlockPosition(mainAxisOffset);
789 mainAxisOffset -= packingSpaceBetweenChildren(availableFreeSpace, style()->flexPack(), childSizes.size());
792 mainAxisOffset -= mainAxisExtentForChild(child) + flowAwareMarginEndForChild(child);
794 LayoutRect oldRect = child->frameRect();
795 setFlowAwareLocationForChild(child, IntPoint(mainAxisOffset, crossAxisOffset + flowAwareMarginBeforeForChild(child)));
796 if (!selfNeedsLayout() && child->checkForRepaintDuringLayout())
797 child->repaintDuringLayoutIfMoved(oldRect);
799 mainAxisOffset -= flowAwareMarginStartForChild(child);
800 mainAxisOffset -= packingSpaceBetweenChildren(availableFreeSpace, style()->flexPack(), childSizes.size());
804 void RenderFlexibleBox::adjustAlignmentForChild(RenderBox* child, LayoutUnit delta)
806 LayoutRect oldRect = child->frameRect();
808 setFlowAwareLocationForChild(child, flowAwareLocationForChild(child) + LayoutSize(0, delta));
810 // If the child moved, we have to repaint it as well as any floating/positioned
811 // descendants. An exception is if we need a layout. In this case, we know we're going to
812 // repaint ourselves (and the child) anyway.
813 if (!selfNeedsLayout() && child->checkForRepaintDuringLayout())
814 child->repaintDuringLayoutIfMoved(oldRect);
817 void RenderFlexibleBox::alignChildren(const OrderedFlexItemList& children, LayoutUnit maxAscent)
819 LayoutUnit crossExtent = crossAxisExtent();
821 for (size_t i = 0; i < children.size(); ++i) {
822 RenderBox* child = children[i];
823 // direction:rtl + flex-direction:column means the cross-axis direction is flipped.
824 if (!style()->isLeftToRightDirection() && isColumnFlow()) {
825 LayoutPoint location = flowAwareLocationForChild(child);
826 location.setY(crossExtent - crossAxisExtentForChild(child) - location.y());
827 setFlowAwareLocationForChild(child, location);
830 // FIXME: Make sure this does the right thing with column flows.
831 switch (flexAlignForChild(child)) {
833 ASSERT_NOT_REACHED();
836 if (!isColumnFlow() && child->style()->logicalHeight().isAuto()) {
837 LayoutUnit logicalHeightBefore = child->logicalHeight();
838 LayoutUnit stretchedLogicalHeight = child->logicalHeight() + RenderFlexibleBox::availableAlignmentSpaceForChild(child);
839 child->setLogicalHeight(stretchedLogicalHeight);
840 child->computeLogicalHeight();
842 if (child->logicalHeight() != logicalHeightBefore) {
843 child->setOverrideHeight(child->logicalHeight());
844 child->setLogicalHeight(0);
845 child->setChildNeedsLayout(true);
846 child->layoutIfNeeded();
854 adjustAlignmentForChild(child, RenderFlexibleBox::availableAlignmentSpaceForChild(child));
857 adjustAlignmentForChild(child, RenderFlexibleBox::availableAlignmentSpaceForChild(child) / 2);
859 case AlignBaseline: {
860 LayoutUnit ascent = marginBoxAscent(child);
861 adjustAlignmentForChild(child, maxAscent - ascent);