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"
41 // Normally, -1 and 0 are not valid in a HashSet, but these are relatively likely flex-order values. Instead,
42 // we make the two smallest int values invalid flex-order values (in the css parser code we clamp them to
44 struct RenderFlexibleBox::FlexOrderHashTraits : WTF::GenericHashTraits<int> {
45 static const bool emptyValueIsZero = false;
46 static int emptyValue() { return std::numeric_limits<int>::min(); }
47 static void constructDeletedValue(int& slot) { slot = std::numeric_limits<int>::min() + 1; }
48 static bool isDeletedValue(int value) { return value == std::numeric_limits<int>::min() + 1; }
51 class RenderFlexibleBox::FlexOrderIterator {
53 FlexOrderIterator(RenderFlexibleBox* flexibleBox, const FlexOrderHashSet& flexOrderValues)
54 : m_flexibleBox(flexibleBox)
56 , m_orderValuesIterator(0)
58 copyToVector(flexOrderValues, m_orderValues);
59 std::sort(m_orderValues.begin(), m_orderValues.end());
63 RenderBox* currentChild() { return m_currentChild; }
74 if (!m_currentChild) {
75 if (m_orderValuesIterator == m_orderValues.end())
77 if (m_orderValuesIterator) {
78 ++m_orderValuesIterator;
79 if (m_orderValuesIterator == m_orderValues.end())
82 m_orderValuesIterator = m_orderValues.begin();
84 m_currentChild = m_flexibleBox->firstChildBox();
86 m_currentChild = m_currentChild->nextSiblingBox();
87 } while (!m_currentChild || m_currentChild->style()->flexOrder() != *m_orderValuesIterator);
89 return m_currentChild;
95 m_orderValuesIterator = 0;
99 RenderFlexibleBox* m_flexibleBox;
100 RenderBox* m_currentChild;
101 Vector<int> m_orderValues;
102 Vector<int>::const_iterator m_orderValuesIterator;
105 struct RenderFlexibleBox::LineContext {
106 LineContext(LayoutUnit crossAxisOffset, LayoutUnit crossAxisExtent, size_t numberOfChildren, LayoutUnit maxAscent)
107 : crossAxisOffset(crossAxisOffset)
108 , crossAxisExtent(crossAxisExtent)
109 , numberOfChildren(numberOfChildren)
110 , maxAscent(maxAscent)
114 LayoutUnit crossAxisOffset;
115 LayoutUnit crossAxisExtent;
116 size_t numberOfChildren;
117 LayoutUnit maxAscent;
121 RenderFlexibleBox::RenderFlexibleBox(Node* node)
124 setChildrenInline(false); // All of our children must be block-level.
127 RenderFlexibleBox::~RenderFlexibleBox()
131 const char* RenderFlexibleBox::renderName() const
133 return "RenderFlexibleBox";
136 static LayoutUnit marginLogicalWidthForChild(RenderBox* child, RenderStyle* parentStyle)
138 // A margin has three types: fixed, percentage, and auto (variable).
139 // Auto and percentage margins become 0 when computing min/max width.
140 // Fixed margins can be added in as is.
141 Length marginLeft = child->style()->marginStartUsing(parentStyle);
142 Length marginRight = child->style()->marginEndUsing(parentStyle);
143 LayoutUnit margin = 0;
144 if (marginLeft.isFixed())
145 margin += marginLeft.value();
146 if (marginRight.isFixed())
147 margin += marginRight.value();
151 void RenderFlexibleBox::computePreferredLogicalWidths()
153 ASSERT(preferredLogicalWidthsDirty());
155 RenderStyle* styleToUse = style();
156 if (styleToUse->logicalWidth().isFixed() && styleToUse->logicalWidth().value() > 0)
157 m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth = computeContentBoxLogicalWidth(styleToUse->logicalWidth().value());
159 m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth = 0;
161 for (RenderBox* child = firstChildBox(); child; child = child->nextSiblingBox()) {
162 if (child->isPositioned())
165 LayoutUnit margin = marginLogicalWidthForChild(child, style());
166 bool hasOrthogonalWritingMode = child->isHorizontalWritingMode() != isHorizontalWritingMode();
167 LayoutUnit minPreferredLogicalWidth = hasOrthogonalWritingMode ? child->logicalHeight() : child->minPreferredLogicalWidth();
168 LayoutUnit maxPreferredLogicalWidth = hasOrthogonalWritingMode ? child->logicalHeight() : child->maxPreferredLogicalWidth();
169 minPreferredLogicalWidth += margin;
170 maxPreferredLogicalWidth += margin;
171 if (!isColumnFlow()) {
172 m_maxPreferredLogicalWidth += maxPreferredLogicalWidth;
174 // For multiline, the min preferred width is if you put a break between each item.
175 m_minPreferredLogicalWidth = std::max(m_minPreferredLogicalWidth, minPreferredLogicalWidth);
177 m_minPreferredLogicalWidth += minPreferredLogicalWidth;
179 m_minPreferredLogicalWidth = std::max(minPreferredLogicalWidth, m_minPreferredLogicalWidth);
181 // For multiline, the max preferred width is if you put a break between each item.
182 m_maxPreferredLogicalWidth += maxPreferredLogicalWidth;
184 m_maxPreferredLogicalWidth = std::max(maxPreferredLogicalWidth, m_maxPreferredLogicalWidth);
188 m_maxPreferredLogicalWidth = std::max(m_minPreferredLogicalWidth, m_maxPreferredLogicalWidth);
191 LayoutUnit scrollbarWidth = 0;
192 if (hasOverflowClip()) {
193 if (isHorizontalWritingMode() && styleToUse->overflowY() == OSCROLL) {
194 layer()->setHasVerticalScrollbar(true);
195 scrollbarWidth = verticalScrollbarWidth();
196 } else if (!isHorizontalWritingMode() && styleToUse->overflowX() == OSCROLL) {
197 layer()->setHasHorizontalScrollbar(true);
198 scrollbarWidth = horizontalScrollbarHeight();
202 m_maxPreferredLogicalWidth += scrollbarWidth;
203 m_minPreferredLogicalWidth += scrollbarWidth;
205 if (styleToUse->logicalMinWidth().isFixed() && styleToUse->logicalMinWidth().value() > 0) {
206 m_maxPreferredLogicalWidth = std::max(m_maxPreferredLogicalWidth, computeContentBoxLogicalWidth(styleToUse->logicalMinWidth().value()));
207 m_minPreferredLogicalWidth = std::max(m_minPreferredLogicalWidth, computeContentBoxLogicalWidth(styleToUse->logicalMinWidth().value()));
210 if (styleToUse->logicalMaxWidth().isFixed()) {
211 m_maxPreferredLogicalWidth = std::min(m_maxPreferredLogicalWidth, computeContentBoxLogicalWidth(styleToUse->logicalMaxWidth().value()));
212 m_minPreferredLogicalWidth = std::min(m_minPreferredLogicalWidth, computeContentBoxLogicalWidth(styleToUse->logicalMaxWidth().value()));
215 LayoutUnit borderAndPadding = borderAndPaddingLogicalWidth();
216 m_minPreferredLogicalWidth += borderAndPadding;
217 m_maxPreferredLogicalWidth += borderAndPadding;
219 setPreferredLogicalWidthsDirty(false);
222 void RenderFlexibleBox::layoutBlock(bool relayoutChildren, int, BlockLayoutPass)
224 ASSERT(needsLayout());
226 if (!relayoutChildren && simplifiedLayout())
229 LayoutRepainter repainter(*this, checkForRepaintDuringLayout());
230 LayoutStateMaintainer statePusher(view(), this, locationOffset(), hasTransform() || hasReflection() || style()->isFlippedBlocksWritingMode());
232 if (inRenderFlowThread()) {
233 // Regions changing widths can force us to relayout our children.
234 if (logicalWidthChangedInRegions())
235 relayoutChildren = true;
237 computeInitialRegionRangeForBlock();
239 IntSize previousSize = size();
242 // We need to call both of these because we grab both crossAxisExtent and mainAxisExtent in layoutFlexItems.
243 computeLogicalWidth();
244 computeLogicalHeight();
248 // For overflow:scroll blocks, ensure we have both scrollbars in place always.
249 if (scrollsOverflow()) {
250 if (style()->overflowX() == OSCROLL)
251 layer()->setHasHorizontalScrollbar(true);
252 if (style()->overflowY() == OSCROLL)
253 layer()->setHasVerticalScrollbar(true);
256 layoutFlexItems(relayoutChildren);
258 LayoutUnit oldClientAfterEdge = clientLogicalBottom();
259 computeLogicalHeight();
261 if (size() != previousSize)
262 relayoutChildren = true;
264 layoutPositionedObjects(relayoutChildren || isRoot());
266 computeRegionRangeForBlock();
268 // FIXME: css3/flexbox/repaint-rtl-column.html seems to repaint more overflow than it needs to.
269 computeOverflow(oldClientAfterEdge);
272 updateLayerTransform();
274 // Update our scroll information if we're overflow:auto/scroll/hidden now that we know if
275 // we overflow or not.
276 updateScrollInfoAfterLayout();
278 repainter.repaintAfterLayout();
280 setNeedsLayout(false);
283 bool RenderFlexibleBox::hasOrthogonalFlow(RenderBox* child) const
285 // FIXME: If the child is a flexbox, then we need to check isHorizontalFlow.
286 return isHorizontalFlow() != child->isHorizontalWritingMode();
289 bool RenderFlexibleBox::isColumnFlow() const
291 return style()->isColumnFlexDirection();
294 bool RenderFlexibleBox::isHorizontalFlow() const
296 if (isHorizontalWritingMode())
297 return !isColumnFlow();
298 return isColumnFlow();
301 bool RenderFlexibleBox::isLeftToRightFlow() const
304 return style()->writingMode() == TopToBottomWritingMode || style()->writingMode() == LeftToRightWritingMode;
305 return style()->isLeftToRightDirection() ^ (style()->flexDirection() == FlowRowReverse);
308 bool RenderFlexibleBox::isMultiline() const
310 return style()->flexWrap() != FlexWrapNone;
313 Length RenderFlexibleBox::mainAxisLengthForChild(RenderBox* child) const
315 return isHorizontalFlow() ? child->style()->width() : child->style()->height();
318 Length RenderFlexibleBox::crossAxisLength() const
320 return isHorizontalFlow() ? style()->height() : style()->width();
323 void RenderFlexibleBox::setCrossAxisExtent(LayoutUnit extent)
325 if (isHorizontalFlow())
331 LayoutUnit RenderFlexibleBox::crossAxisExtentForChild(RenderBox* child)
333 return isHorizontalFlow() ? child->height() : child->width();
336 LayoutUnit RenderFlexibleBox::mainAxisExtentForChild(RenderBox* child)
338 return isHorizontalFlow() ? child->width() : child->height();
341 LayoutUnit RenderFlexibleBox::crossAxisExtent() const
343 return isHorizontalFlow() ? height() : width();
346 LayoutUnit RenderFlexibleBox::mainAxisExtent() const
348 return isHorizontalFlow() ? width() : height();
351 LayoutUnit RenderFlexibleBox::crossAxisContentExtent() const
353 return isHorizontalFlow() ? contentHeight() : contentWidth();
356 LayoutUnit RenderFlexibleBox::mainAxisContentExtent() const
358 return isHorizontalFlow() ? contentWidth() : contentHeight();
361 WritingMode RenderFlexibleBox::transformedWritingMode() const
363 WritingMode mode = style()->writingMode();
368 case TopToBottomWritingMode:
369 case BottomToTopWritingMode:
370 return style()->isLeftToRightDirection() ? LeftToRightWritingMode : RightToLeftWritingMode;
371 case LeftToRightWritingMode:
372 case RightToLeftWritingMode:
373 return style()->isLeftToRightDirection() ? TopToBottomWritingMode : BottomToTopWritingMode;
375 ASSERT_NOT_REACHED();
376 return TopToBottomWritingMode;
379 LayoutUnit RenderFlexibleBox::flowAwareBorderStart() const
381 if (isHorizontalFlow())
382 return isLeftToRightFlow() ? borderLeft() : borderRight();
383 return isLeftToRightFlow() ? borderTop() : borderBottom();
386 LayoutUnit RenderFlexibleBox::flowAwareBorderEnd() const
388 if (isHorizontalFlow())
389 return isLeftToRightFlow() ? borderRight() : borderLeft();
390 return isLeftToRightFlow() ? borderBottom() : borderTop();
393 LayoutUnit RenderFlexibleBox::flowAwareBorderBefore() const
395 switch (transformedWritingMode()) {
396 case TopToBottomWritingMode:
398 case BottomToTopWritingMode:
399 return borderBottom();
400 case LeftToRightWritingMode:
402 case RightToLeftWritingMode:
403 return borderRight();
405 ASSERT_NOT_REACHED();
409 LayoutUnit RenderFlexibleBox::flowAwareBorderAfter() const
411 switch (transformedWritingMode()) {
412 case TopToBottomWritingMode:
413 return borderBottom();
414 case BottomToTopWritingMode:
416 case LeftToRightWritingMode:
417 return borderRight();
418 case RightToLeftWritingMode:
421 ASSERT_NOT_REACHED();
425 LayoutUnit RenderFlexibleBox::flowAwarePaddingStart() const
427 if (isHorizontalFlow())
428 return isLeftToRightFlow() ? paddingLeft() : paddingRight();
429 return isLeftToRightFlow() ? paddingTop() : paddingBottom();
432 LayoutUnit RenderFlexibleBox::flowAwarePaddingEnd() const
434 if (isHorizontalFlow())
435 return isLeftToRightFlow() ? paddingRight() : paddingLeft();
436 return isLeftToRightFlow() ? paddingBottom() : paddingTop();
439 LayoutUnit RenderFlexibleBox::flowAwarePaddingBefore() const
441 switch (transformedWritingMode()) {
442 case TopToBottomWritingMode:
444 case BottomToTopWritingMode:
445 return paddingBottom();
446 case LeftToRightWritingMode:
447 return paddingLeft();
448 case RightToLeftWritingMode:
449 return paddingRight();
451 ASSERT_NOT_REACHED();
455 LayoutUnit RenderFlexibleBox::flowAwarePaddingAfter() const
457 switch (transformedWritingMode()) {
458 case TopToBottomWritingMode:
459 return paddingBottom();
460 case BottomToTopWritingMode:
462 case LeftToRightWritingMode:
463 return paddingRight();
464 case RightToLeftWritingMode:
465 return paddingLeft();
467 ASSERT_NOT_REACHED();
471 LayoutUnit RenderFlexibleBox::flowAwareMarginStartForChild(RenderBox* child) const
473 if (isHorizontalFlow())
474 return isLeftToRightFlow() ? child->marginLeft() : child->marginRight();
475 return isLeftToRightFlow() ? child->marginTop() : child->marginBottom();
478 LayoutUnit RenderFlexibleBox::flowAwareMarginEndForChild(RenderBox* child) const
480 if (isHorizontalFlow())
481 return isLeftToRightFlow() ? child->marginRight() : child->marginLeft();
482 return isLeftToRightFlow() ? child->marginBottom() : child->marginTop();
485 LayoutUnit RenderFlexibleBox::flowAwareMarginBeforeForChild(RenderBox* child) const
487 switch (transformedWritingMode()) {
488 case TopToBottomWritingMode:
489 return child->marginTop();
490 case BottomToTopWritingMode:
491 return child->marginBottom();
492 case LeftToRightWritingMode:
493 return child->marginLeft();
494 case RightToLeftWritingMode:
495 return child->marginRight();
497 ASSERT_NOT_REACHED();
501 LayoutUnit RenderFlexibleBox::flowAwareMarginAfterForChild(RenderBox* child) const
503 switch (transformedWritingMode()) {
504 case TopToBottomWritingMode:
505 return child->marginBottom();
506 case BottomToTopWritingMode:
507 return child->marginTop();
508 case LeftToRightWritingMode:
509 return child->marginRight();
510 case RightToLeftWritingMode:
511 return child->marginLeft();
513 ASSERT_NOT_REACHED();
514 return marginBottom();
517 LayoutUnit RenderFlexibleBox::crossAxisMarginExtentForChild(RenderBox* child) const
519 return isHorizontalFlow() ? child->marginHeight() : child->marginWidth();
522 LayoutUnit RenderFlexibleBox::crossAxisScrollbarExtent() const
524 return isHorizontalFlow() ? horizontalScrollbarHeight() : verticalScrollbarWidth();
527 LayoutPoint RenderFlexibleBox::flowAwareLocationForChild(RenderBox* child) const
529 return isHorizontalFlow() ? child->location() : child->location().transposedPoint();
532 void RenderFlexibleBox::setFlowAwareLocationForChild(RenderBox* child, const LayoutPoint& location)
534 if (isHorizontalFlow())
535 child->setLocation(location);
537 child->setLocation(location.transposedPoint());
540 LayoutUnit RenderFlexibleBox::mainAxisBorderAndPaddingExtentForChild(RenderBox* child) const
542 return isHorizontalFlow() ? child->borderAndPaddingWidth() : child->borderAndPaddingHeight();
545 LayoutUnit RenderFlexibleBox::mainAxisScrollbarExtentForChild(RenderBox* child) const
547 return isHorizontalFlow() ? child->verticalScrollbarWidth() : child->horizontalScrollbarHeight();
550 LayoutUnit RenderFlexibleBox::preferredMainAxisContentExtentForChild(RenderBox* child) const
552 Length mainAxisLength = mainAxisLengthForChild(child);
553 if (mainAxisLength.isAuto()) {
554 LayoutUnit mainAxisExtent = hasOrthogonalFlow(child) ? child->logicalHeight() : child->maxPreferredLogicalWidth();
555 return mainAxisExtent - mainAxisBorderAndPaddingExtentForChild(child);
557 return miminumValueForLength(mainAxisLength, mainAxisContentExtent());
560 LayoutUnit RenderFlexibleBox::computeAvailableFreeSpace(LayoutUnit preferredMainAxisExtent)
563 return mainAxisContentExtent() - preferredMainAxisExtent;
565 if (hasOverrideHeight())
566 return overrideHeight();
568 LayoutUnit heightResult = computeContentLogicalHeightUsing(style()->logicalHeight());
569 if (heightResult == -1)
570 heightResult = preferredMainAxisExtent;
571 LayoutUnit minHeight = computeContentLogicalHeightUsing(style()->logicalMinHeight()); // Leave as -1 if unset.
572 LayoutUnit maxHeight = style()->logicalMaxHeight().isUndefined() ? heightResult : computeContentLogicalHeightUsing(style()->logicalMaxHeight());
574 maxHeight = heightResult;
575 heightResult = std::min(maxHeight, heightResult);
576 heightResult = std::max(minHeight, heightResult);
578 return heightResult - preferredMainAxisExtent;
581 void RenderFlexibleBox::layoutFlexItems(bool relayoutChildren)
583 FlexOrderHashSet flexOrderValues;
584 computeMainAxisPreferredSizes(relayoutChildren, flexOrderValues);
586 OrderedFlexItemList orderedChildren;
587 LayoutUnit preferredMainAxisExtent;
588 float totalPositiveFlexibility;
589 float totalNegativeFlexibility;
590 LayoutUnit minMaxAppliedMainAxisExtent;
591 WTF::Vector<LineContext> lineContexts;
592 FlexOrderIterator flexIterator(this, flexOrderValues);
594 LayoutUnit crossAxisOffset = flowAwareBorderBefore() + flowAwarePaddingBefore();
595 while (computeNextFlexLine(flexIterator, orderedChildren, preferredMainAxisExtent, totalPositiveFlexibility, totalNegativeFlexibility, minMaxAppliedMainAxisExtent)) {
596 LayoutUnit availableFreeSpace = computeAvailableFreeSpace(preferredMainAxisExtent);
597 FlexSign flexSign = (minMaxAppliedMainAxisExtent < preferredMainAxisExtent + availableFreeSpace) ? PositiveFlexibility : NegativeFlexibility;
598 InflexibleFlexItemSize inflexibleItems;
599 WTF::Vector<LayoutUnit> childSizes;
600 while (!resolveFlexibleLengths(flexSign, orderedChildren, availableFreeSpace, totalPositiveFlexibility, totalNegativeFlexibility, inflexibleItems, childSizes)) {
601 ASSERT(totalPositiveFlexibility >= 0 && totalNegativeFlexibility >= 0);
602 ASSERT(inflexibleItems.size() > 0);
605 layoutAndPlaceChildren(crossAxisOffset, orderedChildren, childSizes, availableFreeSpace, lineContexts);
608 alignChildren(flexIterator, lineContexts);
610 if (style()->flexWrap() == FlexWrapReverse)
611 flipForWrapReverse(flexIterator, lineContexts);
613 // direction:rtl + flex-direction:column means the cross-axis direction is flipped.
614 flipForRightToLeftColumn(flexIterator);
617 float RenderFlexibleBox::positiveFlexForChild(RenderBox* child) const
619 return isHorizontalFlow() ? child->style()->flexboxWidthPositiveFlex() : child->style()->flexboxHeightPositiveFlex();
622 float RenderFlexibleBox::negativeFlexForChild(RenderBox* child) const
624 return isHorizontalFlow() ? child->style()->flexboxWidthNegativeFlex() : child->style()->flexboxHeightNegativeFlex();
627 LayoutUnit RenderFlexibleBox::availableAlignmentSpaceForChild(LayoutUnit lineCrossAxisExtent, RenderBox* child)
629 LayoutUnit childCrossExtent = crossAxisMarginExtentForChild(child) + crossAxisExtentForChild(child);
630 return lineCrossAxisExtent - childCrossExtent;
633 LayoutUnit RenderFlexibleBox::marginBoxAscentForChild(RenderBox* child)
635 LayoutUnit ascent = child->firstLineBoxBaseline();
637 ascent = crossAxisExtentForChild(child) + flowAwareMarginAfterForChild(child);
638 return ascent + flowAwareMarginBeforeForChild(child);
641 void RenderFlexibleBox::computeMainAxisPreferredSizes(bool relayoutChildren, FlexOrderHashSet& flexOrderValues)
643 LayoutUnit flexboxAvailableContentExtent = mainAxisContentExtent();
644 for (RenderBox* child = firstChildBox(); child; child = child->nextSiblingBox()) {
645 flexOrderValues.add(child->style()->flexOrder());
647 if (child->isPositioned())
650 child->clearOverrideSize();
651 if (mainAxisLengthForChild(child).isAuto()) {
652 if (!relayoutChildren)
653 child->setChildNeedsLayout(true);
654 child->layoutIfNeeded();
657 // We set the margins because we want to make sure 'auto' has a margin
658 // of 0 and because if we're not auto sizing, we don't do a layout that
659 // computes the start/end margins.
660 if (isHorizontalFlow()) {
661 child->setMarginLeft(miminumValueForLength(child->style()->marginLeft(), flexboxAvailableContentExtent));
662 child->setMarginRight(miminumValueForLength(child->style()->marginRight(), flexboxAvailableContentExtent));
664 child->setMarginTop(miminumValueForLength(child->style()->marginTop(), flexboxAvailableContentExtent));
665 child->setMarginBottom(miminumValueForLength(child->style()->marginBottom(), flexboxAvailableContentExtent));
670 LayoutUnit RenderFlexibleBox::lineBreakLength()
673 return mainAxisContentExtent();
675 LayoutUnit height = computeContentLogicalHeightUsing(style()->logicalHeight());
677 height = std::numeric_limits<LayoutUnit>::max();
678 LayoutUnit maxHeight = computeContentLogicalHeightUsing(style()->logicalMaxHeight());
680 height = std::min(height, maxHeight);
684 bool RenderFlexibleBox::computeNextFlexLine(FlexOrderIterator& iterator, OrderedFlexItemList& orderedChildren, LayoutUnit& preferredMainAxisExtent, float& totalPositiveFlexibility, float& totalNegativeFlexibility, LayoutUnit& minMaxAppliedMainAxisExtent)
686 orderedChildren.clear();
687 preferredMainAxisExtent = 0;
688 totalPositiveFlexibility = totalNegativeFlexibility = 0;
689 minMaxAppliedMainAxisExtent = 0;
691 if (!iterator.currentChild())
694 LayoutUnit flexboxAvailableContentExtent = mainAxisContentExtent();
695 LayoutUnit lineBreak = lineBreakLength();
697 for (RenderBox* child = iterator.currentChild(); child; child = iterator.next()) {
698 if (child->isPositioned()) {
699 orderedChildren.append(child);
703 LayoutUnit childMainAxisExtent = preferredMainAxisContentExtentForChild(child);
704 LayoutUnit childMainAxisMarginBoxExtent = mainAxisBorderAndPaddingExtentForChild(child) + childMainAxisExtent;
705 childMainAxisMarginBoxExtent += isHorizontalFlow() ? child->marginWidth() : child->marginHeight();
707 if (isMultiline() && preferredMainAxisExtent + childMainAxisMarginBoxExtent > lineBreak && orderedChildren.size() > 0)
709 orderedChildren.append(child);
710 preferredMainAxisExtent += childMainAxisMarginBoxExtent;
711 totalPositiveFlexibility += positiveFlexForChild(child);
712 totalNegativeFlexibility += negativeFlexForChild(child);
714 LayoutUnit childMinMaxAppliedMainAxisExtent = childMainAxisExtent;
715 // FIXME: valueForLength isn't quite right in quirks mode: percentage heights should check parents until a value is found.
716 // https://bugs.webkit.org/show_bug.cgi?id=81809
717 Length maxLength = isHorizontalFlow() ? child->style()->maxWidth() : child->style()->maxHeight();
718 if (maxLength.isSpecified() && childMinMaxAppliedMainAxisExtent > valueForLength(maxLength, flexboxAvailableContentExtent))
719 childMinMaxAppliedMainAxisExtent = valueForLength(maxLength, flexboxAvailableContentExtent);
720 Length minLength = isHorizontalFlow() ? child->style()->minWidth() : child->style()->minHeight();
721 if (minLength.isSpecified() && childMinMaxAppliedMainAxisExtent < valueForLength(minLength, flexboxAvailableContentExtent))
722 childMinMaxAppliedMainAxisExtent = valueForLength(minLength, flexboxAvailableContentExtent);
723 minMaxAppliedMainAxisExtent += childMinMaxAppliedMainAxisExtent - childMainAxisExtent + childMainAxisMarginBoxExtent;
728 // Returns true if we successfully ran the algorithm and sized the flex items.
729 bool RenderFlexibleBox::resolveFlexibleLengths(FlexSign, const OrderedFlexItemList& children, LayoutUnit& availableFreeSpace, float& totalPositiveFlexibility, float& totalNegativeFlexibility, InflexibleFlexItemSize& inflexibleItems, WTF::Vector<LayoutUnit>& childSizes)
733 LayoutUnit flexboxAvailableContentExtent = mainAxisContentExtent();
734 for (size_t i = 0; i < children.size(); ++i) {
735 RenderBox* child = children[i];
736 if (child->isPositioned()) {
737 childSizes.append(0);
741 LayoutUnit childPreferredSize;
742 if (inflexibleItems.contains(child))
743 childPreferredSize = inflexibleItems.get(child);
745 childPreferredSize = preferredMainAxisContentExtentForChild(child);
746 if (availableFreeSpace > 0 && totalPositiveFlexibility > 0) {
747 childPreferredSize += lroundf(availableFreeSpace * positiveFlexForChild(child) / totalPositiveFlexibility);
749 Length childLogicalMaxWidth = isHorizontalFlow() ? child->style()->maxWidth() : child->style()->maxHeight();
750 if (childLogicalMaxWidth.isSpecified() && childPreferredSize > valueForLength(childLogicalMaxWidth, flexboxAvailableContentExtent)) {
751 childPreferredSize = valueForLength(childLogicalMaxWidth, flexboxAvailableContentExtent);
752 availableFreeSpace -= childPreferredSize - preferredMainAxisContentExtentForChild(child);
753 totalPositiveFlexibility -= positiveFlexForChild(child);
755 inflexibleItems.set(child, childPreferredSize);
758 } else if (availableFreeSpace < 0 && totalNegativeFlexibility > 0) {
759 childPreferredSize += lroundf(availableFreeSpace * negativeFlexForChild(child) / totalNegativeFlexibility);
761 Length childLogicalMinWidth = isHorizontalFlow() ? child->style()->minWidth() : child->style()->minHeight();
762 if (childLogicalMinWidth.isSpecified() && childPreferredSize < valueForLength(childLogicalMinWidth, flexboxAvailableContentExtent)) {
763 childPreferredSize = valueForLength(childLogicalMinWidth, flexboxAvailableContentExtent);
764 availableFreeSpace += preferredMainAxisContentExtentForChild(child) - childPreferredSize;
765 totalNegativeFlexibility -= negativeFlexForChild(child);
767 inflexibleItems.set(child, childPreferredSize);
772 childSizes.append(childPreferredSize);
777 static LayoutUnit initialPackingOffset(LayoutUnit availableFreeSpace, EFlexPack flexPack, size_t numberOfChildren)
779 if (availableFreeSpace > 0) {
780 if (flexPack == PackEnd)
781 return availableFreeSpace;
782 if (flexPack == PackCenter)
783 return availableFreeSpace / 2;
784 if (flexPack == PackDistribute && numberOfChildren)
785 return availableFreeSpace / (2 * numberOfChildren);
786 } else if (availableFreeSpace < 0) {
787 if (flexPack == PackCenter || flexPack == PackDistribute)
788 return availableFreeSpace / 2;
793 static LayoutUnit packingSpaceBetweenChildren(LayoutUnit availableFreeSpace, EFlexPack flexPack, size_t numberOfChildren)
795 if (availableFreeSpace > 0 && numberOfChildren > 1) {
796 if (flexPack == PackJustify)
797 return availableFreeSpace / (numberOfChildren - 1);
798 if (flexPack == PackDistribute)
799 return availableFreeSpace / numberOfChildren;
804 void RenderFlexibleBox::setLogicalOverrideSize(RenderBox* child, LayoutUnit childPreferredSize)
806 // FIXME: Rename setOverrideWidth/setOverrideHeight to setOverrideLogicalWidth/setOverrideLogicalHeight.
807 if (hasOrthogonalFlow(child))
808 child->setOverrideHeight(childPreferredSize);
810 child->setOverrideWidth(childPreferredSize);
813 void RenderFlexibleBox::prepareChildForPositionedLayout(RenderBox* child, LayoutUnit mainAxisOffset, LayoutUnit crossAxisOffset)
815 ASSERT(child->isPositioned());
816 child->containingBlock()->insertPositionedObject(child);
817 RenderLayer* childLayer = child->layer();
818 LayoutUnit inlinePosition = isColumnFlow() ? crossAxisOffset : mainAxisOffset;
819 if (style()->flexDirection() == FlowRowReverse)
820 inlinePosition = mainAxisExtent() - mainAxisOffset;
821 childLayer->setStaticInlinePosition(inlinePosition); // FIXME: Not right for regions.
823 LayoutUnit staticBlockPosition = isColumnFlow() ? mainAxisOffset : crossAxisOffset;
824 if (childLayer->staticBlockPosition() != staticBlockPosition) {
825 childLayer->setStaticBlockPosition(staticBlockPosition);
826 if (child->style()->hasStaticBlockPosition(style()->isHorizontalWritingMode()))
827 child->setChildNeedsLayout(true, false);
831 static EFlexAlign flexAlignForChild(RenderBox* child)
833 EFlexAlign align = child->style()->flexItemAlign();
834 if (align == AlignAuto)
835 align = child->parent()->style()->flexAlign();
837 if (child->parent()->style()->flexWrap() == FlexWrapReverse) {
838 if (align == AlignStart)
840 else if (align == AlignEnd)
847 void RenderFlexibleBox::layoutAndPlaceChildren(LayoutUnit& crossAxisOffset, const OrderedFlexItemList& children, const WTF::Vector<LayoutUnit>& childSizes, LayoutUnit availableFreeSpace, WTF::Vector<LineContext>& lineContexts)
849 LayoutUnit mainAxisOffset = flowAwareBorderStart() + flowAwarePaddingStart();
850 mainAxisOffset += initialPackingOffset(availableFreeSpace, style()->flexPack(), childSizes.size());
851 if (style()->flexDirection() == FlowRowReverse)
852 mainAxisOffset += isHorizontalFlow() ? verticalScrollbarWidth() : horizontalScrollbarHeight();
854 LayoutUnit totalMainExtent = mainAxisExtent();
855 LayoutUnit maxAscent = 0, maxDescent = 0; // Used when flex-align: baseline.
856 LayoutUnit maxChildCrossAxisExtent = 0;
857 bool shouldFlipMainAxis = !isColumnFlow() && !isLeftToRightFlow();
858 for (size_t i = 0; i < children.size(); ++i) {
859 RenderBox* child = children[i];
860 if (child->isPositioned()) {
861 prepareChildForPositionedLayout(child, mainAxisOffset, crossAxisOffset);
862 mainAxisOffset += packingSpaceBetweenChildren(availableFreeSpace, style()->flexPack(), childSizes.size());
865 LayoutUnit childPreferredSize = childSizes[i] + mainAxisBorderAndPaddingExtentForChild(child);
866 setLogicalOverrideSize(child, childPreferredSize);
867 child->setChildNeedsLayout(true);
868 child->layoutIfNeeded();
870 LayoutUnit childCrossAxisMarginBoxExtent;
871 if (flexAlignForChild(child) == AlignBaseline) {
872 LayoutUnit ascent = marginBoxAscentForChild(child);
873 LayoutUnit descent = (crossAxisMarginExtentForChild(child) + crossAxisExtentForChild(child)) - ascent;
875 maxAscent = std::max(maxAscent, ascent);
876 maxDescent = std::max(maxDescent, descent);
878 childCrossAxisMarginBoxExtent = maxAscent + maxDescent;
880 childCrossAxisMarginBoxExtent = crossAxisExtentForChild(child) + crossAxisMarginExtentForChild(child);
881 if (!isColumnFlow() && style()->logicalHeight().isAuto())
882 setLogicalHeight(std::max(logicalHeight(), crossAxisOffset + flowAwareBorderAfter() + flowAwarePaddingAfter() + childCrossAxisMarginBoxExtent + crossAxisScrollbarExtent()));
883 maxChildCrossAxisExtent = std::max(maxChildCrossAxisExtent, childCrossAxisMarginBoxExtent);
885 mainAxisOffset += flowAwareMarginStartForChild(child);
887 LayoutUnit childMainExtent = mainAxisExtentForChild(child);
888 IntPoint childLocation(shouldFlipMainAxis ? totalMainExtent - mainAxisOffset - childMainExtent : mainAxisOffset,
889 crossAxisOffset + flowAwareMarginBeforeForChild(child));
891 // FIXME: Supporting layout deltas.
892 setFlowAwareLocationForChild(child, childLocation);
893 mainAxisOffset += childMainExtent + flowAwareMarginEndForChild(child);
895 mainAxisOffset += packingSpaceBetweenChildren(availableFreeSpace, style()->flexPack(), childSizes.size());
899 setLogicalHeight(mainAxisOffset + flowAwareBorderEnd() + flowAwarePaddingEnd() + scrollbarLogicalHeight());
901 if (style()->flexDirection() == FlowColumnReverse) {
902 // We have to do an extra pass for column-reverse to reposition the flex items since the start depends
903 // on the height of the flexbox, which we only know after we've positioned all the flex items.
904 computeLogicalHeight();
905 layoutColumnReverse(children, childSizes, crossAxisOffset, availableFreeSpace);
908 LayoutUnit lineCrossAxisExtent = isMultiline() ? maxChildCrossAxisExtent : crossAxisContentExtent();
909 lineContexts.append(LineContext(crossAxisOffset, lineCrossAxisExtent, children.size(), maxAscent));
910 crossAxisOffset += lineCrossAxisExtent;
913 void RenderFlexibleBox::layoutColumnReverse(const OrderedFlexItemList& children, const WTF::Vector<LayoutUnit>& childSizes, LayoutUnit crossAxisOffset, LayoutUnit availableFreeSpace)
915 // This is similar to the logic in layoutAndPlaceChildren, except we place the children
916 // starting from the end of the flexbox. We also don't need to layout anything since we're
917 // just moving the children to a new position.
918 LayoutUnit mainAxisOffset = logicalHeight() - flowAwareBorderEnd() - flowAwarePaddingEnd();
919 mainAxisOffset -= initialPackingOffset(availableFreeSpace, style()->flexPack(), childSizes.size());
920 mainAxisOffset -= isHorizontalFlow() ? verticalScrollbarWidth() : horizontalScrollbarHeight();
922 for (size_t i = 0; i < children.size(); ++i) {
923 RenderBox* child = children[i];
924 if (child->isPositioned()) {
925 child->layer()->setStaticBlockPosition(mainAxisOffset);
926 mainAxisOffset -= packingSpaceBetweenChildren(availableFreeSpace, style()->flexPack(), childSizes.size());
929 mainAxisOffset -= mainAxisExtentForChild(child) + flowAwareMarginEndForChild(child);
931 LayoutRect oldRect = child->frameRect();
932 setFlowAwareLocationForChild(child, IntPoint(mainAxisOffset, crossAxisOffset + flowAwareMarginBeforeForChild(child)));
933 if (!selfNeedsLayout() && child->checkForRepaintDuringLayout())
934 child->repaintDuringLayoutIfMoved(oldRect);
936 mainAxisOffset -= flowAwareMarginStartForChild(child);
937 mainAxisOffset -= packingSpaceBetweenChildren(availableFreeSpace, style()->flexPack(), childSizes.size());
941 void RenderFlexibleBox::adjustAlignmentForChild(RenderBox* child, LayoutUnit delta)
943 LayoutRect oldRect = child->frameRect();
945 setFlowAwareLocationForChild(child, flowAwareLocationForChild(child) + LayoutSize(0, delta));
947 // If the child moved, we have to repaint it as well as any floating/positioned
948 // descendants. An exception is if we need a layout. In this case, we know we're going to
949 // repaint ourselves (and the child) anyway.
950 if (!selfNeedsLayout() && child->checkForRepaintDuringLayout())
951 child->repaintDuringLayoutIfMoved(oldRect);
954 void RenderFlexibleBox::alignChildren(FlexOrderIterator& iterator, const WTF::Vector<LineContext>& lineContexts)
956 // Keep track of the space between the baseline edge and the after edge of the box for each line.
957 WTF::Vector<LayoutUnit> minMarginAfterBaselines;
959 RenderBox* child = iterator.first();
960 for (size_t lineNumber = 0; lineNumber < lineContexts.size(); ++lineNumber) {
961 LayoutUnit minMarginAfterBaseline = std::numeric_limits<LayoutUnit>::max();
962 LayoutUnit lineCrossAxisExtent = lineContexts[lineNumber].crossAxisExtent;
963 LayoutUnit maxAscent = lineContexts[lineNumber].maxAscent;
965 for (size_t childNumber = 0; childNumber < lineContexts[lineNumber].numberOfChildren; ++childNumber, child = iterator.next()) {
967 switch (flexAlignForChild(child)) {
969 ASSERT_NOT_REACHED();
972 applyStretchAlignmentToChild(child, lineCrossAxisExtent);
973 // Since wrap-reverse flips cross start and cross end, strech children should be aligned with the cross end.
974 if (style()->flexWrap() == FlexWrapReverse)
975 adjustAlignmentForChild(child, availableAlignmentSpaceForChild(lineCrossAxisExtent, child));
981 adjustAlignmentForChild(child, availableAlignmentSpaceForChild(lineCrossAxisExtent, child));
984 adjustAlignmentForChild(child, availableAlignmentSpaceForChild(lineCrossAxisExtent, child) / 2);
986 case AlignBaseline: {
987 LayoutUnit ascent = marginBoxAscentForChild(child);
988 LayoutUnit startOffset = maxAscent - ascent;
989 adjustAlignmentForChild(child, startOffset);
991 if (style()->flexWrap() == FlexWrapReverse)
992 minMarginAfterBaseline = std::min(minMarginAfterBaseline, availableAlignmentSpaceForChild(lineCrossAxisExtent, child) - startOffset);
997 minMarginAfterBaselines.append(minMarginAfterBaseline);
1000 if (style()->flexWrap() != FlexWrapReverse)
1003 // wrap-reverse flips the cross axis start and end. For baseline alignment, this means we
1004 // need to align the after edge of baseline elements with the after edge of the flex line.
1005 child = iterator.first();
1006 for (size_t lineNumber = 0; lineNumber < lineContexts.size(); ++lineNumber) {
1007 LayoutUnit minMarginAfterBaseline = minMarginAfterBaselines[lineNumber];
1008 for (size_t childNumber = 0; childNumber < lineContexts[lineNumber].numberOfChildren; ++childNumber, child = iterator.next()) {
1010 if (flexAlignForChild(child) == AlignBaseline && minMarginAfterBaseline)
1011 adjustAlignmentForChild(child, minMarginAfterBaseline);
1016 void RenderFlexibleBox::applyStretchAlignmentToChild(RenderBox* child, LayoutUnit lineCrossAxisExtent)
1018 if (!isColumnFlow() && child->style()->logicalHeight().isAuto()) {
1019 LayoutUnit logicalHeightBefore = child->logicalHeight();
1020 LayoutUnit stretchedLogicalHeight = child->logicalHeight() + availableAlignmentSpaceForChild(lineCrossAxisExtent, child);
1021 if (stretchedLogicalHeight < logicalHeightBefore)
1024 child->setLogicalHeight(stretchedLogicalHeight);
1025 child->computeLogicalHeight();
1027 if (child->logicalHeight() != logicalHeightBefore) {
1028 child->setOverrideHeight(child->logicalHeight());
1029 child->setLogicalHeight(0);
1030 child->setChildNeedsLayout(true);
1031 child->layoutIfNeeded();
1033 } else if (isColumnFlow() && child->style()->logicalWidth().isAuto() && isMultiline()) {
1034 // FIXME: Handle min-width and max-width.
1035 LayoutUnit childWidth = lineCrossAxisExtent - crossAxisMarginExtentForChild(child);
1036 child->setOverrideWidth(std::max(0, childWidth));
1037 child->setChildNeedsLayout(true);
1038 child->layoutIfNeeded();
1042 void RenderFlexibleBox::flipForRightToLeftColumn(FlexOrderIterator& iterator)
1044 if (style()->isLeftToRightDirection() || !isColumnFlow())
1047 LayoutUnit crossExtent = crossAxisExtent();
1048 for (RenderBox* child = iterator.first(); child; child = iterator.next()) {
1049 LayoutPoint location = flowAwareLocationForChild(child);
1050 location.setY(crossExtent - crossAxisExtentForChild(child) - location.y());
1051 setFlowAwareLocationForChild(child, location);
1055 void RenderFlexibleBox::flipForWrapReverse(FlexOrderIterator& iterator, const WTF::Vector<LineContext>& lineContexts)
1057 if (!isColumnFlow())
1058 computeLogicalHeight();
1060 LayoutUnit contentExtent = crossAxisContentExtent();
1061 RenderBox* child = iterator.first();
1062 for (size_t lineNumber = 0; lineNumber < lineContexts.size(); ++lineNumber) {
1063 for (size_t childNumber = 0; childNumber < lineContexts[lineNumber].numberOfChildren; ++childNumber, child = iterator.next()) {
1065 LayoutPoint location = flowAwareLocationForChild(child);
1066 LayoutUnit lineCrossAxisExtent = lineContexts[lineNumber].crossAxisExtent;
1067 LayoutUnit originalOffset = lineContexts[lineNumber].crossAxisOffset - lineContexts[0].crossAxisOffset;
1068 LayoutUnit newOffset = contentExtent - originalOffset - lineCrossAxisExtent;
1069 location.setY(location.y() + newOffset - originalOffset);
1071 LayoutRect oldRect = child->frameRect();
1072 setFlowAwareLocationForChild(child, location);
1073 if (!selfNeedsLayout() && child->checkForRepaintDuringLayout())
1074 child->repaintDuringLayoutIfMoved(oldRect);