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;
120 struct RenderFlexibleBox::Violation {
121 Violation(RenderBox* child, LayoutUnit childSize)
123 , childSize(childSize)
128 LayoutUnit childSize;
132 RenderFlexibleBox::RenderFlexibleBox(Node* node)
135 setChildrenInline(false); // All of our children must be block-level.
138 RenderFlexibleBox::~RenderFlexibleBox()
142 const char* RenderFlexibleBox::renderName() const
144 return "RenderFlexibleBox";
147 static LayoutUnit marginLogicalWidthForChild(RenderBox* child, RenderStyle* parentStyle)
149 // A margin has three types: fixed, percentage, and auto (variable).
150 // Auto and percentage margins become 0 when computing min/max width.
151 // Fixed margins can be added in as is.
152 Length marginLeft = child->style()->marginStartUsing(parentStyle);
153 Length marginRight = child->style()->marginEndUsing(parentStyle);
154 LayoutUnit margin = 0;
155 if (marginLeft.isFixed())
156 margin += marginLeft.value();
157 if (marginRight.isFixed())
158 margin += marginRight.value();
162 void RenderFlexibleBox::computePreferredLogicalWidths()
164 ASSERT(preferredLogicalWidthsDirty());
166 RenderStyle* styleToUse = style();
167 if (styleToUse->logicalWidth().isFixed() && styleToUse->logicalWidth().value() > 0)
168 m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth = computeContentBoxLogicalWidth(styleToUse->logicalWidth().value());
170 m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth = 0;
172 for (RenderBox* child = firstChildBox(); child; child = child->nextSiblingBox()) {
173 if (child->isPositioned())
176 LayoutUnit margin = marginLogicalWidthForChild(child, style());
177 bool hasOrthogonalWritingMode = child->isHorizontalWritingMode() != isHorizontalWritingMode();
178 LayoutUnit minPreferredLogicalWidth = hasOrthogonalWritingMode ? child->logicalHeight() : child->minPreferredLogicalWidth();
179 LayoutUnit maxPreferredLogicalWidth = hasOrthogonalWritingMode ? child->logicalHeight() : child->maxPreferredLogicalWidth();
180 minPreferredLogicalWidth += margin;
181 maxPreferredLogicalWidth += margin;
182 if (!isColumnFlow()) {
183 m_maxPreferredLogicalWidth += maxPreferredLogicalWidth;
185 // For multiline, the min preferred width is if you put a break between each item.
186 m_minPreferredLogicalWidth = std::max(m_minPreferredLogicalWidth, minPreferredLogicalWidth);
188 m_minPreferredLogicalWidth += minPreferredLogicalWidth;
190 m_minPreferredLogicalWidth = std::max(minPreferredLogicalWidth, m_minPreferredLogicalWidth);
192 // For multiline, the max preferred width is if you put a break between each item.
193 m_maxPreferredLogicalWidth += maxPreferredLogicalWidth;
195 m_maxPreferredLogicalWidth = std::max(maxPreferredLogicalWidth, m_maxPreferredLogicalWidth);
199 m_maxPreferredLogicalWidth = std::max(m_minPreferredLogicalWidth, m_maxPreferredLogicalWidth);
202 LayoutUnit scrollbarWidth = 0;
203 if (hasOverflowClip()) {
204 if (isHorizontalWritingMode() && styleToUse->overflowY() == OSCROLL) {
205 layer()->setHasVerticalScrollbar(true);
206 scrollbarWidth = verticalScrollbarWidth();
207 } else if (!isHorizontalWritingMode() && styleToUse->overflowX() == OSCROLL) {
208 layer()->setHasHorizontalScrollbar(true);
209 scrollbarWidth = horizontalScrollbarHeight();
213 m_maxPreferredLogicalWidth += scrollbarWidth;
214 m_minPreferredLogicalWidth += scrollbarWidth;
216 if (styleToUse->logicalMinWidth().isFixed() && styleToUse->logicalMinWidth().value() > 0) {
217 m_maxPreferredLogicalWidth = std::max(m_maxPreferredLogicalWidth, computeContentBoxLogicalWidth(styleToUse->logicalMinWidth().value()));
218 m_minPreferredLogicalWidth = std::max(m_minPreferredLogicalWidth, computeContentBoxLogicalWidth(styleToUse->logicalMinWidth().value()));
221 if (styleToUse->logicalMaxWidth().isFixed()) {
222 m_maxPreferredLogicalWidth = std::min(m_maxPreferredLogicalWidth, computeContentBoxLogicalWidth(styleToUse->logicalMaxWidth().value()));
223 m_minPreferredLogicalWidth = std::min(m_minPreferredLogicalWidth, computeContentBoxLogicalWidth(styleToUse->logicalMaxWidth().value()));
226 LayoutUnit borderAndPadding = borderAndPaddingLogicalWidth();
227 m_minPreferredLogicalWidth += borderAndPadding;
228 m_maxPreferredLogicalWidth += borderAndPadding;
230 setPreferredLogicalWidthsDirty(false);
233 void RenderFlexibleBox::layoutBlock(bool relayoutChildren, int, BlockLayoutPass)
235 ASSERT(needsLayout());
237 if (!relayoutChildren && simplifiedLayout())
240 LayoutRepainter repainter(*this, checkForRepaintDuringLayout());
241 LayoutStateMaintainer statePusher(view(), this, locationOffset(), hasTransform() || hasReflection() || style()->isFlippedBlocksWritingMode());
243 if (inRenderFlowThread()) {
244 // Regions changing widths can force us to relayout our children.
245 if (logicalWidthChangedInRegions())
246 relayoutChildren = true;
248 computeInitialRegionRangeForBlock();
250 IntSize previousSize = size();
253 computeLogicalWidth();
257 // For overflow:scroll blocks, ensure we have both scrollbars in place always.
258 if (scrollsOverflow()) {
259 if (style()->overflowX() == OSCROLL)
260 layer()->setHasHorizontalScrollbar(true);
261 if (style()->overflowY() == OSCROLL)
262 layer()->setHasVerticalScrollbar(true);
265 WTF::Vector<LineContext> lineContexts;
266 FlexOrderHashSet flexOrderValues;
267 computeMainAxisPreferredSizes(relayoutChildren, flexOrderValues);
268 FlexOrderIterator flexIterator(this, flexOrderValues);
269 layoutFlexItems(relayoutChildren, flexIterator, lineContexts);
271 LayoutUnit oldClientAfterEdge = clientLogicalBottom();
272 computeLogicalHeight();
273 repositionLogicalHeightDependentFlexItems(flexIterator, lineContexts, oldClientAfterEdge);
275 if (size() != previousSize)
276 relayoutChildren = true;
278 layoutPositionedObjects(relayoutChildren || isRoot());
280 computeRegionRangeForBlock();
282 // FIXME: css3/flexbox/repaint-rtl-column.html seems to repaint more overflow than it needs to.
283 computeOverflow(oldClientAfterEdge);
286 updateLayerTransform();
288 // Update our scroll information if we're overflow:auto/scroll/hidden now that we know if
289 // we overflow or not.
290 updateScrollInfoAfterLayout();
292 repainter.repaintAfterLayout();
294 setNeedsLayout(false);
297 void RenderFlexibleBox::repositionLogicalHeightDependentFlexItems(FlexOrderIterator& iterator, WTF::Vector<LineContext>& lineContexts, LayoutUnit& oldClientAfterEdge)
299 // If we have a single line flexbox, the line height is all the available space.
300 // For flex-direction: row, this means we need to use the height, so we do this after calling computeLogicalHeight.
301 if (!isMultiline() && lineContexts.size() == 1)
302 lineContexts[0].crossAxisExtent = crossAxisContentExtent();
303 alignChildren(iterator, lineContexts);
305 if (style()->flexWrap() == FlexWrapReverse) {
306 if (isHorizontalFlow())
307 oldClientAfterEdge = clientLogicalBottom();
308 flipForWrapReverse(iterator, lineContexts);
311 // direction:rtl + flex-direction:column means the cross-axis direction is flipped.
312 flipForRightToLeftColumn(iterator);
315 bool RenderFlexibleBox::hasOrthogonalFlow(RenderBox* child) const
317 // FIXME: If the child is a flexbox, then we need to check isHorizontalFlow.
318 return isHorizontalFlow() != child->isHorizontalWritingMode();
321 bool RenderFlexibleBox::isColumnFlow() const
323 return style()->isColumnFlexDirection();
326 bool RenderFlexibleBox::isHorizontalFlow() const
328 if (isHorizontalWritingMode())
329 return !isColumnFlow();
330 return isColumnFlow();
333 bool RenderFlexibleBox::isLeftToRightFlow() const
336 return style()->writingMode() == TopToBottomWritingMode || style()->writingMode() == LeftToRightWritingMode;
337 return style()->isLeftToRightDirection() ^ (style()->flexDirection() == FlowRowReverse);
340 bool RenderFlexibleBox::isMultiline() const
342 return style()->flexWrap() != FlexWrapNone;
345 Length RenderFlexibleBox::mainAxisLengthForChild(RenderBox* child) const
347 return isHorizontalFlow() ? child->style()->width() : child->style()->height();
350 Length RenderFlexibleBox::crossAxisLength() const
352 return isHorizontalFlow() ? style()->height() : style()->width();
355 void RenderFlexibleBox::setCrossAxisExtent(LayoutUnit extent)
357 if (isHorizontalFlow())
363 LayoutUnit RenderFlexibleBox::crossAxisExtentForChild(RenderBox* child)
365 return isHorizontalFlow() ? child->height() : child->width();
368 LayoutUnit RenderFlexibleBox::mainAxisExtentForChild(RenderBox* child)
370 return isHorizontalFlow() ? child->width() : child->height();
373 LayoutUnit RenderFlexibleBox::crossAxisExtent() const
375 return isHorizontalFlow() ? height() : width();
378 LayoutUnit RenderFlexibleBox::mainAxisExtent() const
380 return isHorizontalFlow() ? width() : height();
383 LayoutUnit RenderFlexibleBox::crossAxisContentExtent() const
385 return isHorizontalFlow() ? contentHeight() : contentWidth();
388 LayoutUnit RenderFlexibleBox::mainAxisContentExtent() const
390 return isHorizontalFlow() ? contentWidth() : contentHeight();
393 WritingMode RenderFlexibleBox::transformedWritingMode() const
395 WritingMode mode = style()->writingMode();
400 case TopToBottomWritingMode:
401 case BottomToTopWritingMode:
402 return style()->isLeftToRightDirection() ? LeftToRightWritingMode : RightToLeftWritingMode;
403 case LeftToRightWritingMode:
404 case RightToLeftWritingMode:
405 return style()->isLeftToRightDirection() ? TopToBottomWritingMode : BottomToTopWritingMode;
407 ASSERT_NOT_REACHED();
408 return TopToBottomWritingMode;
411 LayoutUnit RenderFlexibleBox::flowAwareBorderStart() const
413 if (isHorizontalFlow())
414 return isLeftToRightFlow() ? borderLeft() : borderRight();
415 return isLeftToRightFlow() ? borderTop() : borderBottom();
418 LayoutUnit RenderFlexibleBox::flowAwareBorderEnd() const
420 if (isHorizontalFlow())
421 return isLeftToRightFlow() ? borderRight() : borderLeft();
422 return isLeftToRightFlow() ? borderBottom() : borderTop();
425 LayoutUnit RenderFlexibleBox::flowAwareBorderBefore() const
427 switch (transformedWritingMode()) {
428 case TopToBottomWritingMode:
430 case BottomToTopWritingMode:
431 return borderBottom();
432 case LeftToRightWritingMode:
434 case RightToLeftWritingMode:
435 return borderRight();
437 ASSERT_NOT_REACHED();
441 LayoutUnit RenderFlexibleBox::flowAwareBorderAfter() const
443 switch (transformedWritingMode()) {
444 case TopToBottomWritingMode:
445 return borderBottom();
446 case BottomToTopWritingMode:
448 case LeftToRightWritingMode:
449 return borderRight();
450 case RightToLeftWritingMode:
453 ASSERT_NOT_REACHED();
457 LayoutUnit RenderFlexibleBox::flowAwarePaddingStart() const
459 if (isHorizontalFlow())
460 return isLeftToRightFlow() ? paddingLeft() : paddingRight();
461 return isLeftToRightFlow() ? paddingTop() : paddingBottom();
464 LayoutUnit RenderFlexibleBox::flowAwarePaddingEnd() const
466 if (isHorizontalFlow())
467 return isLeftToRightFlow() ? paddingRight() : paddingLeft();
468 return isLeftToRightFlow() ? paddingBottom() : paddingTop();
471 LayoutUnit RenderFlexibleBox::flowAwarePaddingBefore() const
473 switch (transformedWritingMode()) {
474 case TopToBottomWritingMode:
476 case BottomToTopWritingMode:
477 return paddingBottom();
478 case LeftToRightWritingMode:
479 return paddingLeft();
480 case RightToLeftWritingMode:
481 return paddingRight();
483 ASSERT_NOT_REACHED();
487 LayoutUnit RenderFlexibleBox::flowAwarePaddingAfter() const
489 switch (transformedWritingMode()) {
490 case TopToBottomWritingMode:
491 return paddingBottom();
492 case BottomToTopWritingMode:
494 case LeftToRightWritingMode:
495 return paddingRight();
496 case RightToLeftWritingMode:
497 return paddingLeft();
499 ASSERT_NOT_REACHED();
503 LayoutUnit RenderFlexibleBox::flowAwareMarginStartForChild(RenderBox* child) const
505 if (isHorizontalFlow())
506 return isLeftToRightFlow() ? child->marginLeft() : child->marginRight();
507 return isLeftToRightFlow() ? child->marginTop() : child->marginBottom();
510 LayoutUnit RenderFlexibleBox::flowAwareMarginEndForChild(RenderBox* child) const
512 if (isHorizontalFlow())
513 return isLeftToRightFlow() ? child->marginRight() : child->marginLeft();
514 return isLeftToRightFlow() ? child->marginBottom() : child->marginTop();
517 LayoutUnit RenderFlexibleBox::flowAwareMarginBeforeForChild(RenderBox* child) const
519 switch (transformedWritingMode()) {
520 case TopToBottomWritingMode:
521 return child->marginTop();
522 case BottomToTopWritingMode:
523 return child->marginBottom();
524 case LeftToRightWritingMode:
525 return child->marginLeft();
526 case RightToLeftWritingMode:
527 return child->marginRight();
529 ASSERT_NOT_REACHED();
533 LayoutUnit RenderFlexibleBox::flowAwareMarginAfterForChild(RenderBox* child) const
535 switch (transformedWritingMode()) {
536 case TopToBottomWritingMode:
537 return child->marginBottom();
538 case BottomToTopWritingMode:
539 return child->marginTop();
540 case LeftToRightWritingMode:
541 return child->marginRight();
542 case RightToLeftWritingMode:
543 return child->marginLeft();
545 ASSERT_NOT_REACHED();
546 return marginBottom();
549 LayoutUnit RenderFlexibleBox::crossAxisMarginExtentForChild(RenderBox* child) const
551 return isHorizontalFlow() ? child->marginHeight() : child->marginWidth();
554 LayoutUnit RenderFlexibleBox::crossAxisScrollbarExtent() const
556 return isHorizontalFlow() ? horizontalScrollbarHeight() : verticalScrollbarWidth();
559 LayoutPoint RenderFlexibleBox::flowAwareLocationForChild(RenderBox* child) const
561 return isHorizontalFlow() ? child->location() : child->location().transposedPoint();
564 void RenderFlexibleBox::setFlowAwareLocationForChild(RenderBox* child, const LayoutPoint& location)
566 if (isHorizontalFlow())
567 child->setLocation(location);
569 child->setLocation(location.transposedPoint());
572 LayoutUnit RenderFlexibleBox::mainAxisBorderAndPaddingExtentForChild(RenderBox* child) const
574 return isHorizontalFlow() ? child->borderAndPaddingWidth() : child->borderAndPaddingHeight();
577 LayoutUnit RenderFlexibleBox::mainAxisScrollbarExtentForChild(RenderBox* child) const
579 return isHorizontalFlow() ? child->verticalScrollbarWidth() : child->horizontalScrollbarHeight();
582 LayoutUnit RenderFlexibleBox::preferredMainAxisContentExtentForChild(RenderBox* child) const
584 Length mainAxisLength = mainAxisLengthForChild(child);
585 if (mainAxisLength.isAuto()) {
586 LayoutUnit mainAxisExtent = hasOrthogonalFlow(child) ? child->logicalHeight() : child->maxPreferredLogicalWidth();
587 return mainAxisExtent - mainAxisBorderAndPaddingExtentForChild(child);
589 return miminumValueForLength(mainAxisLength, mainAxisContentExtent());
592 LayoutUnit RenderFlexibleBox::computeAvailableFreeSpace(LayoutUnit preferredMainAxisExtent)
595 return mainAxisContentExtent() - preferredMainAxisExtent;
597 if (hasOverrideHeight())
598 return overrideHeight();
600 LayoutUnit heightResult = computeContentLogicalHeightUsing(style()->logicalHeight());
601 if (heightResult == -1)
602 heightResult = preferredMainAxisExtent;
603 LayoutUnit minHeight = computeContentLogicalHeightUsing(style()->logicalMinHeight()); // Leave as -1 if unset.
604 LayoutUnit maxHeight = style()->logicalMaxHeight().isUndefined() ? heightResult : computeContentLogicalHeightUsing(style()->logicalMaxHeight());
606 maxHeight = heightResult;
607 heightResult = std::min(maxHeight, heightResult);
608 heightResult = std::max(minHeight, heightResult);
610 return heightResult - preferredMainAxisExtent;
613 void RenderFlexibleBox::layoutFlexItems(bool relayoutChildren, FlexOrderIterator& iterator, WTF::Vector<LineContext>& lineContexts)
615 OrderedFlexItemList orderedChildren;
616 LayoutUnit preferredMainAxisExtent;
617 float totalPositiveFlexibility;
618 float totalNegativeFlexibility;
619 LayoutUnit minMaxAppliedMainAxisExtent;
621 LayoutUnit crossAxisOffset = flowAwareBorderBefore() + flowAwarePaddingBefore();
622 while (computeNextFlexLine(iterator, orderedChildren, preferredMainAxisExtent, totalPositiveFlexibility, totalNegativeFlexibility, minMaxAppliedMainAxisExtent)) {
623 LayoutUnit availableFreeSpace = computeAvailableFreeSpace(preferredMainAxisExtent);
624 FlexSign flexSign = (minMaxAppliedMainAxisExtent < preferredMainAxisExtent + availableFreeSpace) ? PositiveFlexibility : NegativeFlexibility;
625 InflexibleFlexItemSize inflexibleItems;
626 WTF::Vector<LayoutUnit> childSizes;
627 while (!resolveFlexibleLengths(flexSign, orderedChildren, availableFreeSpace, totalPositiveFlexibility, totalNegativeFlexibility, inflexibleItems, childSizes)) {
628 ASSERT(totalPositiveFlexibility >= 0 && totalNegativeFlexibility >= 0);
629 ASSERT(inflexibleItems.size() > 0);
632 layoutAndPlaceChildren(crossAxisOffset, orderedChildren, childSizes, availableFreeSpace, lineContexts);
636 float RenderFlexibleBox::positiveFlexForChild(RenderBox* child) const
638 return isHorizontalFlow() ? child->style()->flexboxWidthPositiveFlex() : child->style()->flexboxHeightPositiveFlex();
641 float RenderFlexibleBox::negativeFlexForChild(RenderBox* child) const
643 return isHorizontalFlow() ? child->style()->flexboxWidthNegativeFlex() : child->style()->flexboxHeightNegativeFlex();
646 LayoutUnit RenderFlexibleBox::availableAlignmentSpaceForChild(LayoutUnit lineCrossAxisExtent, RenderBox* child)
648 LayoutUnit childCrossExtent = crossAxisMarginExtentForChild(child) + crossAxisExtentForChild(child);
649 return lineCrossAxisExtent - childCrossExtent;
652 LayoutUnit RenderFlexibleBox::marginBoxAscentForChild(RenderBox* child)
654 LayoutUnit ascent = child->firstLineBoxBaseline();
656 ascent = crossAxisExtentForChild(child) + flowAwareMarginAfterForChild(child);
657 return ascent + flowAwareMarginBeforeForChild(child);
660 void RenderFlexibleBox::computeMainAxisPreferredSizes(bool relayoutChildren, FlexOrderHashSet& flexOrderValues)
662 LayoutUnit flexboxAvailableContentExtent = mainAxisContentExtent();
663 for (RenderBox* child = firstChildBox(); child; child = child->nextSiblingBox()) {
664 flexOrderValues.add(child->style()->flexOrder());
666 if (child->isPositioned())
669 child->clearOverrideSize();
670 if (mainAxisLengthForChild(child).isAuto()) {
671 if (!relayoutChildren)
672 child->setChildNeedsLayout(true);
673 child->layoutIfNeeded();
676 // We set the margins because we want to make sure 'auto' has a margin
677 // of 0 and because if we're not auto sizing, we don't do a layout that
678 // computes the start/end margins.
679 if (isHorizontalFlow()) {
680 child->setMarginLeft(miminumValueForLength(child->style()->marginLeft(), flexboxAvailableContentExtent));
681 child->setMarginRight(miminumValueForLength(child->style()->marginRight(), flexboxAvailableContentExtent));
683 child->setMarginTop(miminumValueForLength(child->style()->marginTop(), flexboxAvailableContentExtent));
684 child->setMarginBottom(miminumValueForLength(child->style()->marginBottom(), flexboxAvailableContentExtent));
689 LayoutUnit RenderFlexibleBox::lineBreakLength()
692 return mainAxisContentExtent();
694 LayoutUnit height = computeContentLogicalHeightUsing(style()->logicalHeight());
696 height = std::numeric_limits<LayoutUnit>::max();
697 LayoutUnit maxHeight = computeContentLogicalHeightUsing(style()->logicalMaxHeight());
699 height = std::min(height, maxHeight);
703 LayoutUnit RenderFlexibleBox::adjustChildSizeForMinAndMax(RenderBox* child, LayoutUnit childSize, LayoutUnit flexboxAvailableContentExtent)
705 Length max = isHorizontalFlow() ? child->style()->maxWidth() : child->style()->maxHeight();
706 Length min = isHorizontalFlow() ? child->style()->minWidth() : child->style()->minHeight();
707 // FIXME: valueForLength isn't quite right in quirks mode: percentage heights should check parents until a value is found.
708 // https://bugs.webkit.org/show_bug.cgi?id=81809
709 if (max.isSpecified() && childSize > valueForLength(max, flexboxAvailableContentExtent))
710 childSize = valueForLength(max, flexboxAvailableContentExtent);
711 if (min.isSpecified() && childSize < valueForLength(min, flexboxAvailableContentExtent))
712 childSize = valueForLength(min, flexboxAvailableContentExtent);
716 bool RenderFlexibleBox::computeNextFlexLine(FlexOrderIterator& iterator, OrderedFlexItemList& orderedChildren, LayoutUnit& preferredMainAxisExtent, float& totalPositiveFlexibility, float& totalNegativeFlexibility, LayoutUnit& minMaxAppliedMainAxisExtent)
718 orderedChildren.clear();
719 preferredMainAxisExtent = 0;
720 totalPositiveFlexibility = totalNegativeFlexibility = 0;
721 minMaxAppliedMainAxisExtent = 0;
723 if (!iterator.currentChild())
726 LayoutUnit flexboxAvailableContentExtent = mainAxisContentExtent();
727 LayoutUnit lineBreak = lineBreakLength();
729 for (RenderBox* child = iterator.currentChild(); child; child = iterator.next()) {
730 if (child->isPositioned()) {
731 orderedChildren.append(child);
735 LayoutUnit childMainAxisExtent = preferredMainAxisContentExtentForChild(child);
736 LayoutUnit childMainAxisMarginBoxExtent = mainAxisBorderAndPaddingExtentForChild(child) + childMainAxisExtent;
737 childMainAxisMarginBoxExtent += isHorizontalFlow() ? child->marginWidth() : child->marginHeight();
739 if (isMultiline() && preferredMainAxisExtent + childMainAxisMarginBoxExtent > lineBreak && orderedChildren.size() > 0)
741 orderedChildren.append(child);
742 preferredMainAxisExtent += childMainAxisMarginBoxExtent;
743 totalPositiveFlexibility += positiveFlexForChild(child);
744 totalNegativeFlexibility += negativeFlexForChild(child);
746 LayoutUnit childMinMaxAppliedMainAxisExtent = adjustChildSizeForMinAndMax(child, childMainAxisExtent, flexboxAvailableContentExtent);
747 minMaxAppliedMainAxisExtent += childMinMaxAppliedMainAxisExtent - childMainAxisExtent + childMainAxisMarginBoxExtent;
752 void RenderFlexibleBox::freezeViolations(const WTF::Vector<Violation>& violations, LayoutUnit& availableFreeSpace, float& totalPositiveFlexibility, float& totalNegativeFlexibility, InflexibleFlexItemSize& inflexibleItems)
754 for (size_t i = 0; i < violations.size(); ++i) {
755 RenderBox* child = violations[i].child;
756 LayoutUnit childSize = violations[i].childSize;
757 availableFreeSpace -= childSize - preferredMainAxisContentExtentForChild(child);
758 totalPositiveFlexibility -= positiveFlexForChild(child);
759 totalNegativeFlexibility -= negativeFlexForChild(child);
760 inflexibleItems.set(child, childSize);
764 // Returns true if we successfully ran the algorithm and sized the flex items.
765 bool RenderFlexibleBox::resolveFlexibleLengths(FlexSign flexSign, const OrderedFlexItemList& children, LayoutUnit& availableFreeSpace, float& totalPositiveFlexibility, float& totalNegativeFlexibility, InflexibleFlexItemSize& inflexibleItems, WTF::Vector<LayoutUnit>& childSizes)
769 LayoutUnit flexboxAvailableContentExtent = mainAxisContentExtent();
770 LayoutUnit totalViolation = 0;
771 WTF::Vector<Violation> minViolations;
772 WTF::Vector<Violation> maxViolations;
773 for (size_t i = 0; i < children.size(); ++i) {
774 RenderBox* child = children[i];
775 if (child->isPositioned()) {
776 childSizes.append(0);
780 if (inflexibleItems.contains(child))
781 childSizes.append(inflexibleItems.get(child));
783 LayoutUnit childSize = preferredMainAxisContentExtentForChild(child);
784 if (availableFreeSpace > 0 && totalPositiveFlexibility > 0 && flexSign == PositiveFlexibility)
785 childSize += lroundf(availableFreeSpace * positiveFlexForChild(child) / totalPositiveFlexibility);
786 else if (availableFreeSpace < 0 && totalNegativeFlexibility > 0 && flexSign == NegativeFlexibility)
787 childSize += lroundf(availableFreeSpace * negativeFlexForChild(child) / totalNegativeFlexibility);
789 LayoutUnit adjustedChildSize = adjustChildSizeForMinAndMax(child, childSize, flexboxAvailableContentExtent);
790 childSizes.append(adjustedChildSize);
792 LayoutUnit violation = adjustedChildSize - childSize;
794 minViolations.append(Violation(child, adjustedChildSize));
795 else if (violation < 0)
796 maxViolations.append(Violation(child, adjustedChildSize));
797 totalViolation += violation;
802 freezeViolations(totalViolation < 0 ? maxViolations : minViolations, availableFreeSpace, totalPositiveFlexibility, totalNegativeFlexibility, inflexibleItems);
803 return !totalViolation;
806 static LayoutUnit initialPackingOffset(LayoutUnit availableFreeSpace, EFlexPack flexPack, size_t numberOfChildren)
808 if (availableFreeSpace > 0) {
809 if (flexPack == PackEnd)
810 return availableFreeSpace;
811 if (flexPack == PackCenter)
812 return availableFreeSpace / 2;
813 if (flexPack == PackDistribute && numberOfChildren)
814 return availableFreeSpace / (2 * numberOfChildren);
815 } else if (availableFreeSpace < 0) {
816 if (flexPack == PackCenter || flexPack == PackDistribute)
817 return availableFreeSpace / 2;
822 static LayoutUnit packingSpaceBetweenChildren(LayoutUnit availableFreeSpace, EFlexPack flexPack, size_t numberOfChildren)
824 if (availableFreeSpace > 0 && numberOfChildren > 1) {
825 if (flexPack == PackJustify)
826 return availableFreeSpace / (numberOfChildren - 1);
827 if (flexPack == PackDistribute)
828 return availableFreeSpace / numberOfChildren;
833 void RenderFlexibleBox::setLogicalOverrideSize(RenderBox* child, LayoutUnit childPreferredSize)
835 // FIXME: Rename setOverrideWidth/setOverrideHeight to setOverrideLogicalWidth/setOverrideLogicalHeight.
836 if (hasOrthogonalFlow(child))
837 child->setOverrideHeight(childPreferredSize);
839 child->setOverrideWidth(childPreferredSize);
842 void RenderFlexibleBox::prepareChildForPositionedLayout(RenderBox* child, LayoutUnit mainAxisOffset, LayoutUnit crossAxisOffset)
844 ASSERT(child->isPositioned());
845 child->containingBlock()->insertPositionedObject(child);
846 RenderLayer* childLayer = child->layer();
847 LayoutUnit inlinePosition = isColumnFlow() ? crossAxisOffset : mainAxisOffset;
848 if (style()->flexDirection() == FlowRowReverse)
849 inlinePosition = mainAxisExtent() - mainAxisOffset;
850 childLayer->setStaticInlinePosition(inlinePosition); // FIXME: Not right for regions.
852 LayoutUnit staticBlockPosition = isColumnFlow() ? mainAxisOffset : crossAxisOffset;
853 if (childLayer->staticBlockPosition() != staticBlockPosition) {
854 childLayer->setStaticBlockPosition(staticBlockPosition);
855 if (child->style()->hasStaticBlockPosition(style()->isHorizontalWritingMode()))
856 child->setChildNeedsLayout(true, false);
860 static EFlexAlign flexAlignForChild(RenderBox* child)
862 EFlexAlign align = child->style()->flexItemAlign();
863 if (align == AlignAuto)
864 align = child->parent()->style()->flexAlign();
866 if (child->parent()->style()->flexWrap() == FlexWrapReverse) {
867 if (align == AlignStart)
869 else if (align == AlignEnd)
876 void RenderFlexibleBox::layoutAndPlaceChildren(LayoutUnit& crossAxisOffset, const OrderedFlexItemList& children, const WTF::Vector<LayoutUnit>& childSizes, LayoutUnit availableFreeSpace, WTF::Vector<LineContext>& lineContexts)
878 ASSERT(childSizes.size() == children.size());
879 LayoutUnit mainAxisOffset = flowAwareBorderStart() + flowAwarePaddingStart();
880 mainAxisOffset += initialPackingOffset(availableFreeSpace, style()->flexPack(), childSizes.size());
881 if (style()->flexDirection() == FlowRowReverse)
882 mainAxisOffset += isHorizontalFlow() ? verticalScrollbarWidth() : horizontalScrollbarHeight();
884 LayoutUnit totalMainExtent = mainAxisExtent();
885 LayoutUnit maxAscent = 0, maxDescent = 0; // Used when flex-align: baseline.
886 LayoutUnit maxChildCrossAxisExtent = 0;
887 bool shouldFlipMainAxis = !isColumnFlow() && !isLeftToRightFlow();
888 for (size_t i = 0; i < children.size(); ++i) {
889 RenderBox* child = children[i];
890 if (child->isPositioned()) {
891 prepareChildForPositionedLayout(child, mainAxisOffset, crossAxisOffset);
892 mainAxisOffset += packingSpaceBetweenChildren(availableFreeSpace, style()->flexPack(), childSizes.size());
895 LayoutUnit childPreferredSize = childSizes[i] + mainAxisBorderAndPaddingExtentForChild(child);
896 setLogicalOverrideSize(child, childPreferredSize);
897 child->setChildNeedsLayout(true);
898 child->layoutIfNeeded();
900 LayoutUnit childCrossAxisMarginBoxExtent;
901 if (flexAlignForChild(child) == AlignBaseline) {
902 LayoutUnit ascent = marginBoxAscentForChild(child);
903 LayoutUnit descent = (crossAxisMarginExtentForChild(child) + crossAxisExtentForChild(child)) - ascent;
905 maxAscent = std::max(maxAscent, ascent);
906 maxDescent = std::max(maxDescent, descent);
908 childCrossAxisMarginBoxExtent = maxAscent + maxDescent;
910 childCrossAxisMarginBoxExtent = crossAxisExtentForChild(child) + crossAxisMarginExtentForChild(child);
911 if (!isColumnFlow() && style()->logicalHeight().isAuto())
912 setLogicalHeight(std::max(logicalHeight(), crossAxisOffset + flowAwareBorderAfter() + flowAwarePaddingAfter() + childCrossAxisMarginBoxExtent + crossAxisScrollbarExtent()));
913 maxChildCrossAxisExtent = std::max(maxChildCrossAxisExtent, childCrossAxisMarginBoxExtent);
915 mainAxisOffset += flowAwareMarginStartForChild(child);
917 LayoutUnit childMainExtent = mainAxisExtentForChild(child);
918 IntPoint childLocation(shouldFlipMainAxis ? totalMainExtent - mainAxisOffset - childMainExtent : mainAxisOffset,
919 crossAxisOffset + flowAwareMarginBeforeForChild(child));
921 // FIXME: Supporting layout deltas.
922 setFlowAwareLocationForChild(child, childLocation);
923 mainAxisOffset += childMainExtent + flowAwareMarginEndForChild(child);
925 mainAxisOffset += packingSpaceBetweenChildren(availableFreeSpace, style()->flexPack(), childSizes.size());
929 setLogicalHeight(mainAxisOffset + flowAwareBorderEnd() + flowAwarePaddingEnd() + scrollbarLogicalHeight());
931 if (style()->flexDirection() == FlowColumnReverse) {
932 // We have to do an extra pass for column-reverse to reposition the flex items since the start depends
933 // on the height of the flexbox, which we only know after we've positioned all the flex items.
934 computeLogicalHeight();
935 layoutColumnReverse(children, childSizes, crossAxisOffset, availableFreeSpace);
938 lineContexts.append(LineContext(crossAxisOffset, maxChildCrossAxisExtent, children.size(), maxAscent));
939 crossAxisOffset += maxChildCrossAxisExtent;
942 void RenderFlexibleBox::layoutColumnReverse(const OrderedFlexItemList& children, const WTF::Vector<LayoutUnit>& childSizes, LayoutUnit crossAxisOffset, LayoutUnit availableFreeSpace)
944 // This is similar to the logic in layoutAndPlaceChildren, except we place the children
945 // starting from the end of the flexbox. We also don't need to layout anything since we're
946 // just moving the children to a new position.
947 LayoutUnit mainAxisOffset = logicalHeight() - flowAwareBorderEnd() - flowAwarePaddingEnd();
948 mainAxisOffset -= initialPackingOffset(availableFreeSpace, style()->flexPack(), childSizes.size());
949 mainAxisOffset -= isHorizontalFlow() ? verticalScrollbarWidth() : horizontalScrollbarHeight();
951 for (size_t i = 0; i < children.size(); ++i) {
952 RenderBox* child = children[i];
953 if (child->isPositioned()) {
954 child->layer()->setStaticBlockPosition(mainAxisOffset);
955 mainAxisOffset -= packingSpaceBetweenChildren(availableFreeSpace, style()->flexPack(), childSizes.size());
958 mainAxisOffset -= mainAxisExtentForChild(child) + flowAwareMarginEndForChild(child);
960 LayoutRect oldRect = child->frameRect();
961 setFlowAwareLocationForChild(child, IntPoint(mainAxisOffset, crossAxisOffset + flowAwareMarginBeforeForChild(child)));
962 if (!selfNeedsLayout() && child->checkForRepaintDuringLayout())
963 child->repaintDuringLayoutIfMoved(oldRect);
965 mainAxisOffset -= flowAwareMarginStartForChild(child);
966 mainAxisOffset -= packingSpaceBetweenChildren(availableFreeSpace, style()->flexPack(), childSizes.size());
970 void RenderFlexibleBox::adjustAlignmentForChild(RenderBox* child, LayoutUnit delta)
972 LayoutRect oldRect = child->frameRect();
974 setFlowAwareLocationForChild(child, flowAwareLocationForChild(child) + LayoutSize(0, delta));
976 // If the child moved, we have to repaint it as well as any floating/positioned
977 // descendants. An exception is if we need a layout. In this case, we know we're going to
978 // repaint ourselves (and the child) anyway.
979 if (!selfNeedsLayout() && child->checkForRepaintDuringLayout())
980 child->repaintDuringLayoutIfMoved(oldRect);
983 void RenderFlexibleBox::alignChildren(FlexOrderIterator& iterator, const WTF::Vector<LineContext>& lineContexts)
985 // Keep track of the space between the baseline edge and the after edge of the box for each line.
986 WTF::Vector<LayoutUnit> minMarginAfterBaselines;
988 RenderBox* child = iterator.first();
989 for (size_t lineNumber = 0; lineNumber < lineContexts.size(); ++lineNumber) {
990 LayoutUnit minMarginAfterBaseline = std::numeric_limits<LayoutUnit>::max();
991 LayoutUnit lineCrossAxisExtent = lineContexts[lineNumber].crossAxisExtent;
992 LayoutUnit maxAscent = lineContexts[lineNumber].maxAscent;
994 for (size_t childNumber = 0; childNumber < lineContexts[lineNumber].numberOfChildren; ++childNumber, child = iterator.next()) {
996 switch (flexAlignForChild(child)) {
998 ASSERT_NOT_REACHED();
1000 case AlignStretch: {
1001 applyStretchAlignmentToChild(child, lineCrossAxisExtent);
1002 // Since wrap-reverse flips cross start and cross end, strech children should be aligned with the cross end.
1003 if (style()->flexWrap() == FlexWrapReverse)
1004 adjustAlignmentForChild(child, availableAlignmentSpaceForChild(lineCrossAxisExtent, child));
1010 adjustAlignmentForChild(child, availableAlignmentSpaceForChild(lineCrossAxisExtent, child));
1013 adjustAlignmentForChild(child, availableAlignmentSpaceForChild(lineCrossAxisExtent, child) / 2);
1015 case AlignBaseline: {
1016 LayoutUnit ascent = marginBoxAscentForChild(child);
1017 LayoutUnit startOffset = maxAscent - ascent;
1018 adjustAlignmentForChild(child, startOffset);
1020 if (style()->flexWrap() == FlexWrapReverse)
1021 minMarginAfterBaseline = std::min(minMarginAfterBaseline, availableAlignmentSpaceForChild(lineCrossAxisExtent, child) - startOffset);
1026 minMarginAfterBaselines.append(minMarginAfterBaseline);
1029 if (style()->flexWrap() != FlexWrapReverse)
1032 // wrap-reverse flips the cross axis start and end. For baseline alignment, this means we
1033 // need to align the after edge of baseline elements with the after edge of the flex line.
1034 child = iterator.first();
1035 for (size_t lineNumber = 0; lineNumber < lineContexts.size(); ++lineNumber) {
1036 LayoutUnit minMarginAfterBaseline = minMarginAfterBaselines[lineNumber];
1037 for (size_t childNumber = 0; childNumber < lineContexts[lineNumber].numberOfChildren; ++childNumber, child = iterator.next()) {
1039 if (flexAlignForChild(child) == AlignBaseline && minMarginAfterBaseline)
1040 adjustAlignmentForChild(child, minMarginAfterBaseline);
1045 void RenderFlexibleBox::applyStretchAlignmentToChild(RenderBox* child, LayoutUnit lineCrossAxisExtent)
1047 if (!isColumnFlow() && child->style()->logicalHeight().isAuto()) {
1048 LayoutUnit logicalHeightBefore = child->logicalHeight();
1049 LayoutUnit stretchedLogicalHeight = child->logicalHeight() + availableAlignmentSpaceForChild(lineCrossAxisExtent, child);
1050 if (stretchedLogicalHeight < logicalHeightBefore)
1053 child->setLogicalHeight(stretchedLogicalHeight);
1054 child->computeLogicalHeight();
1056 if (child->logicalHeight() != logicalHeightBefore) {
1057 child->setOverrideHeight(child->logicalHeight());
1058 child->setLogicalHeight(0);
1059 child->setChildNeedsLayout(true);
1060 child->layoutIfNeeded();
1062 } else if (isColumnFlow() && child->style()->logicalWidth().isAuto() && isMultiline()) {
1063 // FIXME: Handle min-width and max-width.
1064 LayoutUnit childWidth = lineCrossAxisExtent - crossAxisMarginExtentForChild(child);
1065 child->setOverrideWidth(std::max(0, childWidth));
1066 child->setChildNeedsLayout(true);
1067 child->layoutIfNeeded();
1071 void RenderFlexibleBox::flipForRightToLeftColumn(FlexOrderIterator& iterator)
1073 if (style()->isLeftToRightDirection() || !isColumnFlow())
1076 LayoutUnit crossExtent = crossAxisExtent();
1077 for (RenderBox* child = iterator.first(); child; child = iterator.next()) {
1078 LayoutPoint location = flowAwareLocationForChild(child);
1079 location.setY(crossExtent - crossAxisExtentForChild(child) - location.y());
1080 setFlowAwareLocationForChild(child, location);
1084 void RenderFlexibleBox::flipForWrapReverse(FlexOrderIterator& iterator, const WTF::Vector<LineContext>& lineContexts)
1086 LayoutUnit contentExtent = crossAxisContentExtent();
1087 RenderBox* child = iterator.first();
1088 for (size_t lineNumber = 0; lineNumber < lineContexts.size(); ++lineNumber) {
1089 for (size_t childNumber = 0; childNumber < lineContexts[lineNumber].numberOfChildren; ++childNumber, child = iterator.next()) {
1091 LayoutPoint location = flowAwareLocationForChild(child);
1092 LayoutUnit lineCrossAxisExtent = lineContexts[lineNumber].crossAxisExtent;
1093 LayoutUnit originalOffset = lineContexts[lineNumber].crossAxisOffset - lineContexts[0].crossAxisOffset;
1094 LayoutUnit newOffset = contentExtent - originalOffset - lineCrossAxisExtent;
1095 location.setY(location.y() + newOffset - originalOffset);
1097 LayoutRect oldRect = child->frameRect();
1098 setFlowAwareLocationForChild(child, location);
1099 if (!selfNeedsLayout() && child->checkForRepaintDuringLayout())
1100 child->repaintDuringLayoutIfMoved(oldRect);