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, LayoutUnit)
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 LayoutSize 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(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 if (hasOverflowClip())
291 layer()->updateScrollInfoAfterLayout();
293 repainter.repaintAfterLayout();
295 setNeedsLayout(false);
298 void RenderFlexibleBox::repositionLogicalHeightDependentFlexItems(FlexOrderIterator& iterator, WTF::Vector<LineContext>& lineContexts, LayoutUnit& oldClientAfterEdge)
300 LayoutUnit crossAxisStartEdge = lineContexts.isEmpty() ? ZERO_LAYOUT_UNIT : lineContexts[0].crossAxisOffset;
301 packFlexLines(iterator, lineContexts);
303 // If we have a single line flexbox, the line height is all the available space.
304 // For flex-direction: row, this means we need to use the height, so we do this after calling computeLogicalHeight.
305 if (!isMultiline() && lineContexts.size() == 1)
306 lineContexts[0].crossAxisExtent = crossAxisContentExtent();
307 alignChildren(iterator, lineContexts);
309 if (style()->flexWrap() == FlexWrapReverse) {
310 if (isHorizontalFlow())
311 oldClientAfterEdge = clientLogicalBottom();
312 flipForWrapReverse(iterator, lineContexts, crossAxisStartEdge);
315 // direction:rtl + flex-direction:column means the cross-axis direction is flipped.
316 flipForRightToLeftColumn(iterator);
319 bool RenderFlexibleBox::hasOrthogonalFlow(RenderBox* child) const
321 // FIXME: If the child is a flexbox, then we need to check isHorizontalFlow.
322 return isHorizontalFlow() != child->isHorizontalWritingMode();
325 bool RenderFlexibleBox::isColumnFlow() const
327 return style()->isColumnFlexDirection();
330 bool RenderFlexibleBox::isHorizontalFlow() const
332 if (isHorizontalWritingMode())
333 return !isColumnFlow();
334 return isColumnFlow();
337 bool RenderFlexibleBox::isLeftToRightFlow() const
340 return style()->writingMode() == TopToBottomWritingMode || style()->writingMode() == LeftToRightWritingMode;
341 return style()->isLeftToRightDirection() ^ (style()->flexDirection() == FlowRowReverse);
344 bool RenderFlexibleBox::isMultiline() const
346 return style()->flexWrap() != FlexWrapNone;
349 Length RenderFlexibleBox::preferredLengthForChild(RenderBox* child) const
351 Length flexLength = child->style()->flexPreferredSize();
352 if (flexLength.isAuto())
353 flexLength = isHorizontalFlow() ? child->style()->width() : child->style()->height();
357 Length RenderFlexibleBox::crossAxisLength() const
359 return isHorizontalFlow() ? style()->height() : style()->width();
362 void RenderFlexibleBox::setCrossAxisExtent(LayoutUnit extent)
364 if (isHorizontalFlow())
370 LayoutUnit RenderFlexibleBox::crossAxisExtentForChild(RenderBox* child)
372 return isHorizontalFlow() ? child->height() : child->width();
375 LayoutUnit RenderFlexibleBox::mainAxisExtentForChild(RenderBox* child)
377 return isHorizontalFlow() ? child->width() : child->height();
380 LayoutUnit RenderFlexibleBox::crossAxisExtent() const
382 return isHorizontalFlow() ? height() : width();
385 LayoutUnit RenderFlexibleBox::mainAxisExtent() const
387 return isHorizontalFlow() ? width() : height();
390 LayoutUnit RenderFlexibleBox::crossAxisContentExtent() const
392 return isHorizontalFlow() ? contentHeight() : contentWidth();
395 LayoutUnit RenderFlexibleBox::mainAxisContentExtent() const
397 return isHorizontalFlow() ? contentWidth() : contentHeight();
400 WritingMode RenderFlexibleBox::transformedWritingMode() const
402 WritingMode mode = style()->writingMode();
407 case TopToBottomWritingMode:
408 case BottomToTopWritingMode:
409 return style()->isLeftToRightDirection() ? LeftToRightWritingMode : RightToLeftWritingMode;
410 case LeftToRightWritingMode:
411 case RightToLeftWritingMode:
412 return style()->isLeftToRightDirection() ? TopToBottomWritingMode : BottomToTopWritingMode;
414 ASSERT_NOT_REACHED();
415 return TopToBottomWritingMode;
418 LayoutUnit RenderFlexibleBox::flowAwareBorderStart() const
420 if (isHorizontalFlow())
421 return isLeftToRightFlow() ? borderLeft() : borderRight();
422 return isLeftToRightFlow() ? borderTop() : borderBottom();
425 LayoutUnit RenderFlexibleBox::flowAwareBorderEnd() const
427 if (isHorizontalFlow())
428 return isLeftToRightFlow() ? borderRight() : borderLeft();
429 return isLeftToRightFlow() ? borderBottom() : borderTop();
432 LayoutUnit RenderFlexibleBox::flowAwareBorderBefore() const
434 switch (transformedWritingMode()) {
435 case TopToBottomWritingMode:
437 case BottomToTopWritingMode:
438 return borderBottom();
439 case LeftToRightWritingMode:
441 case RightToLeftWritingMode:
442 return borderRight();
444 ASSERT_NOT_REACHED();
448 LayoutUnit RenderFlexibleBox::flowAwareBorderAfter() const
450 switch (transformedWritingMode()) {
451 case TopToBottomWritingMode:
452 return borderBottom();
453 case BottomToTopWritingMode:
455 case LeftToRightWritingMode:
456 return borderRight();
457 case RightToLeftWritingMode:
460 ASSERT_NOT_REACHED();
464 LayoutUnit RenderFlexibleBox::flowAwarePaddingStart() const
466 if (isHorizontalFlow())
467 return isLeftToRightFlow() ? paddingLeft() : paddingRight();
468 return isLeftToRightFlow() ? paddingTop() : paddingBottom();
471 LayoutUnit RenderFlexibleBox::flowAwarePaddingEnd() const
473 if (isHorizontalFlow())
474 return isLeftToRightFlow() ? paddingRight() : paddingLeft();
475 return isLeftToRightFlow() ? paddingBottom() : paddingTop();
478 LayoutUnit RenderFlexibleBox::flowAwarePaddingBefore() const
480 switch (transformedWritingMode()) {
481 case TopToBottomWritingMode:
483 case BottomToTopWritingMode:
484 return paddingBottom();
485 case LeftToRightWritingMode:
486 return paddingLeft();
487 case RightToLeftWritingMode:
488 return paddingRight();
490 ASSERT_NOT_REACHED();
494 LayoutUnit RenderFlexibleBox::flowAwarePaddingAfter() const
496 switch (transformedWritingMode()) {
497 case TopToBottomWritingMode:
498 return paddingBottom();
499 case BottomToTopWritingMode:
501 case LeftToRightWritingMode:
502 return paddingRight();
503 case RightToLeftWritingMode:
504 return paddingLeft();
506 ASSERT_NOT_REACHED();
510 LayoutUnit RenderFlexibleBox::flowAwareMarginStartForChild(RenderBox* child) const
512 if (isHorizontalFlow())
513 return isLeftToRightFlow() ? child->marginLeft() : child->marginRight();
514 return isLeftToRightFlow() ? child->marginTop() : child->marginBottom();
517 LayoutUnit RenderFlexibleBox::flowAwareMarginEndForChild(RenderBox* child) const
519 if (isHorizontalFlow())
520 return isLeftToRightFlow() ? child->marginRight() : child->marginLeft();
521 return isLeftToRightFlow() ? child->marginBottom() : child->marginTop();
524 LayoutUnit RenderFlexibleBox::flowAwareMarginBeforeForChild(RenderBox* child) const
526 switch (transformedWritingMode()) {
527 case TopToBottomWritingMode:
528 return child->marginTop();
529 case BottomToTopWritingMode:
530 return child->marginBottom();
531 case LeftToRightWritingMode:
532 return child->marginLeft();
533 case RightToLeftWritingMode:
534 return child->marginRight();
536 ASSERT_NOT_REACHED();
540 LayoutUnit RenderFlexibleBox::flowAwareMarginAfterForChild(RenderBox* child) const
542 switch (transformedWritingMode()) {
543 case TopToBottomWritingMode:
544 return child->marginBottom();
545 case BottomToTopWritingMode:
546 return child->marginTop();
547 case LeftToRightWritingMode:
548 return child->marginRight();
549 case RightToLeftWritingMode:
550 return child->marginLeft();
552 ASSERT_NOT_REACHED();
553 return marginBottom();
556 LayoutUnit RenderFlexibleBox::crossAxisMarginExtentForChild(RenderBox* child) const
558 return isHorizontalFlow() ? child->marginHeight() : child->marginWidth();
561 LayoutUnit RenderFlexibleBox::crossAxisScrollbarExtent() const
563 return isHorizontalFlow() ? horizontalScrollbarHeight() : verticalScrollbarWidth();
566 LayoutPoint RenderFlexibleBox::flowAwareLocationForChild(RenderBox* child) const
568 return isHorizontalFlow() ? child->location() : child->location().transposedPoint();
571 void RenderFlexibleBox::setFlowAwareLocationForChild(RenderBox* child, const LayoutPoint& location)
573 if (isHorizontalFlow())
574 child->setLocation(location);
576 child->setLocation(location.transposedPoint());
579 LayoutUnit RenderFlexibleBox::mainAxisBorderAndPaddingExtentForChild(RenderBox* child) const
581 return isHorizontalFlow() ? child->borderAndPaddingWidth() : child->borderAndPaddingHeight();
584 LayoutUnit RenderFlexibleBox::mainAxisScrollbarExtentForChild(RenderBox* child) const
586 return isHorizontalFlow() ? child->verticalScrollbarWidth() : child->horizontalScrollbarHeight();
589 LayoutUnit RenderFlexibleBox::preferredMainAxisContentExtentForChild(RenderBox* child) const
591 Length mainAxisLength = preferredLengthForChild(child);
592 if (mainAxisLength.isAuto()) {
593 LayoutUnit mainAxisExtent = hasOrthogonalFlow(child) ? child->logicalHeight() : child->maxPreferredLogicalWidth();
594 return mainAxisExtent - mainAxisBorderAndPaddingExtentForChild(child);
596 return minimumValueForLength(mainAxisLength, mainAxisContentExtent(), view());
599 LayoutUnit RenderFlexibleBox::computeAvailableFreeSpace(LayoutUnit preferredMainAxisExtent)
601 LayoutUnit contentExtent = 0;
603 contentExtent = mainAxisContentExtent();
604 else if (hasOverrideHeight())
605 contentExtent = overrideHeight() - (logicalHeight() - contentLogicalHeight());
607 LayoutUnit heightResult = computeContentLogicalHeightUsing(style()->logicalHeight());
608 if (heightResult == -1)
609 heightResult = preferredMainAxisExtent;
610 LayoutUnit minHeight = computeContentLogicalHeightUsing(style()->logicalMinHeight()); // Leave as -1 if unset.
611 LayoutUnit maxHeight = style()->logicalMaxHeight().isUndefined() ? heightResult : computeContentLogicalHeightUsing(style()->logicalMaxHeight());
613 maxHeight = heightResult;
614 heightResult = std::min(maxHeight, heightResult);
615 heightResult = std::max(minHeight, heightResult);
616 contentExtent = heightResult;
619 return contentExtent - preferredMainAxisExtent;
622 void RenderFlexibleBox::layoutFlexItems(FlexOrderIterator& iterator, WTF::Vector<LineContext>& lineContexts)
624 OrderedFlexItemList orderedChildren;
625 LayoutUnit preferredMainAxisExtent;
626 float totalPositiveFlexibility;
627 float totalNegativeFlexibility;
628 LayoutUnit minMaxAppliedMainAxisExtent;
630 LayoutUnit crossAxisOffset = flowAwareBorderBefore() + flowAwarePaddingBefore();
631 while (computeNextFlexLine(iterator, orderedChildren, preferredMainAxisExtent, totalPositiveFlexibility, totalNegativeFlexibility, minMaxAppliedMainAxisExtent)) {
632 LayoutUnit availableFreeSpace = computeAvailableFreeSpace(preferredMainAxisExtent);
633 FlexSign flexSign = (minMaxAppliedMainAxisExtent < preferredMainAxisExtent + availableFreeSpace) ? PositiveFlexibility : NegativeFlexibility;
634 InflexibleFlexItemSize inflexibleItems;
635 WTF::Vector<LayoutUnit> childSizes;
636 while (!resolveFlexibleLengths(flexSign, orderedChildren, availableFreeSpace, totalPositiveFlexibility, totalNegativeFlexibility, inflexibleItems, childSizes)) {
637 ASSERT(totalPositiveFlexibility >= 0 && totalNegativeFlexibility >= 0);
638 ASSERT(inflexibleItems.size() > 0);
641 layoutAndPlaceChildren(crossAxisOffset, orderedChildren, childSizes, availableFreeSpace, lineContexts);
645 LayoutUnit RenderFlexibleBox::autoMarginOffsetInMainAxis(const OrderedFlexItemList& children, LayoutUnit& availableFreeSpace)
647 if (availableFreeSpace <= 0)
650 int numberOfAutoMargins = 0;
651 bool isHorizontal = isHorizontalFlow();
652 for (size_t i = 0; i < children.size(); ++i) {
653 RenderBox* child = children[i];
654 if (child->isPositioned())
657 if (child->style()->marginLeft().isAuto())
658 ++numberOfAutoMargins;
659 if (child->style()->marginRight().isAuto())
660 ++numberOfAutoMargins;
662 if (child->style()->marginTop().isAuto())
663 ++numberOfAutoMargins;
664 if (child->style()->marginBottom().isAuto())
665 ++numberOfAutoMargins;
668 if (!numberOfAutoMargins)
671 LayoutUnit sizeOfAutoMargin = availableFreeSpace / numberOfAutoMargins;
672 availableFreeSpace = 0;
673 return sizeOfAutoMargin;
676 void RenderFlexibleBox::updateAutoMarginsInMainAxis(RenderBox* child, LayoutUnit autoMarginOffset)
678 if (isHorizontalFlow()) {
679 if (child->style()->marginLeft().isAuto())
680 child->setMarginLeft(autoMarginOffset);
681 if (child->style()->marginRight().isAuto())
682 child->setMarginRight(autoMarginOffset);
684 if (child->style()->marginTop().isAuto())
685 child->setMarginTop(autoMarginOffset);
686 if (child->style()->marginBottom().isAuto())
687 child->setMarginBottom(autoMarginOffset);
691 bool RenderFlexibleBox::hasAutoMarginsInCrossAxis(RenderBox* child)
693 if (isHorizontalFlow())
694 return child->style()->marginTop().isAuto() || child->style()->marginBottom().isAuto();
695 return child->style()->marginLeft().isAuto() || child->style()->marginRight().isAuto();
698 LayoutUnit RenderFlexibleBox::availableAlignmentSpaceForChild(LayoutUnit lineCrossAxisExtent, RenderBox* child)
700 LayoutUnit childCrossExtent = crossAxisMarginExtentForChild(child) + crossAxisExtentForChild(child);
701 return lineCrossAxisExtent - childCrossExtent;
704 bool RenderFlexibleBox::updateAutoMarginsInCrossAxis(RenderBox* child, LayoutUnit availableAlignmentSpace)
706 bool isHorizontal = isHorizontalFlow();
707 Length start = isHorizontal ? child->style()->marginTop() : child->style()->marginLeft();
708 Length end = isHorizontal ? child->style()->marginBottom() : child->style()->marginRight();
709 if (start.isAuto() && end.isAuto()) {
710 adjustAlignmentForChild(child, availableAlignmentSpace / 2);
712 child->setMarginTop(availableAlignmentSpace / 2);
713 child->setMarginBottom(availableAlignmentSpace / 2);
715 child->setMarginLeft(availableAlignmentSpace / 2);
716 child->setMarginRight(availableAlignmentSpace / 2);
720 if (start.isAuto()) {
721 adjustAlignmentForChild(child, availableAlignmentSpace);
723 child->setMarginTop(availableAlignmentSpace);
725 child->setMarginLeft(availableAlignmentSpace);
730 child->setMarginBottom(availableAlignmentSpace);
732 child->setMarginRight(availableAlignmentSpace);
738 LayoutUnit RenderFlexibleBox::marginBoxAscentForChild(RenderBox* child)
740 LayoutUnit ascent = child->firstLineBoxBaseline();
742 ascent = crossAxisExtentForChild(child) + flowAwareMarginAfterForChild(child);
743 return ascent + flowAwareMarginBeforeForChild(child);
746 void RenderFlexibleBox::computeMainAxisPreferredSizes(bool relayoutChildren, FlexOrderHashSet& flexOrderValues)
748 LayoutUnit flexboxAvailableContentExtent = mainAxisContentExtent();
749 RenderView* renderView = view();
750 for (RenderBox* child = firstChildBox(); child; child = child->nextSiblingBox()) {
751 flexOrderValues.add(child->style()->flexOrder());
753 if (child->isPositioned())
756 child->clearOverrideSize();
757 if (preferredLengthForChild(child).isAuto()) {
758 if (!relayoutChildren)
759 child->setChildNeedsLayout(true);
760 child->layoutIfNeeded();
763 // Before running the flex algorithm, 'auto' has a margin of 0.
764 // Also, if we're not auto sizing, we don't do a layout that computes the start/end margins.
765 if (isHorizontalFlow()) {
766 child->setMarginLeft(minimumValueForLength(child->style()->marginLeft(), flexboxAvailableContentExtent, renderView));
767 child->setMarginRight(minimumValueForLength(child->style()->marginRight(), flexboxAvailableContentExtent, renderView));
769 child->setMarginTop(minimumValueForLength(child->style()->marginTop(), flexboxAvailableContentExtent, renderView));
770 child->setMarginBottom(minimumValueForLength(child->style()->marginBottom(), flexboxAvailableContentExtent, renderView));
775 LayoutUnit RenderFlexibleBox::lineBreakLength()
778 return mainAxisContentExtent();
780 LayoutUnit height = computeContentLogicalHeightUsing(style()->logicalHeight());
782 height = MAX_LAYOUT_UNIT;
783 LayoutUnit maxHeight = computeContentLogicalHeightUsing(style()->logicalMaxHeight());
785 height = std::min(height, maxHeight);
789 LayoutUnit RenderFlexibleBox::adjustChildSizeForMinAndMax(RenderBox* child, LayoutUnit childSize, LayoutUnit flexboxAvailableContentExtent)
791 Length max = isHorizontalFlow() ? child->style()->maxWidth() : child->style()->maxHeight();
792 Length min = isHorizontalFlow() ? child->style()->minWidth() : child->style()->minHeight();
793 RenderView* renderView = view();
794 // FIXME: valueForLength isn't quite right in quirks mode: percentage heights should check parents until a value is found.
795 // https://bugs.webkit.org/show_bug.cgi?id=81809
796 if (max.isSpecified() && childSize > valueForLength(max, flexboxAvailableContentExtent, renderView))
797 childSize = valueForLength(max, flexboxAvailableContentExtent, renderView);
798 if (min.isSpecified() && childSize < valueForLength(min, flexboxAvailableContentExtent, renderView))
799 childSize = valueForLength(min, flexboxAvailableContentExtent, renderView);
803 bool RenderFlexibleBox::computeNextFlexLine(FlexOrderIterator& iterator, OrderedFlexItemList& orderedChildren, LayoutUnit& preferredMainAxisExtent, float& totalPositiveFlexibility, float& totalNegativeFlexibility, LayoutUnit& minMaxAppliedMainAxisExtent)
805 orderedChildren.clear();
806 preferredMainAxisExtent = 0;
807 totalPositiveFlexibility = totalNegativeFlexibility = 0;
808 minMaxAppliedMainAxisExtent = 0;
810 if (!iterator.currentChild())
813 LayoutUnit flexboxAvailableContentExtent = mainAxisContentExtent();
814 LayoutUnit lineBreak = lineBreakLength();
816 for (RenderBox* child = iterator.currentChild(); child; child = iterator.next()) {
817 if (child->isPositioned()) {
818 orderedChildren.append(child);
822 LayoutUnit childMainAxisExtent = preferredMainAxisContentExtentForChild(child);
823 LayoutUnit childMainAxisMarginBoxExtent = mainAxisBorderAndPaddingExtentForChild(child) + childMainAxisExtent;
824 childMainAxisMarginBoxExtent += isHorizontalFlow() ? child->marginWidth() : child->marginHeight();
826 if (isMultiline() && preferredMainAxisExtent + childMainAxisMarginBoxExtent > lineBreak && orderedChildren.size() > 0)
828 orderedChildren.append(child);
829 preferredMainAxisExtent += childMainAxisMarginBoxExtent;
830 totalPositiveFlexibility += child->style()->positiveFlex();
831 totalNegativeFlexibility += child->style()->negativeFlex();
833 LayoutUnit childMinMaxAppliedMainAxisExtent = adjustChildSizeForMinAndMax(child, childMainAxisExtent, flexboxAvailableContentExtent);
834 minMaxAppliedMainAxisExtent += childMinMaxAppliedMainAxisExtent - childMainAxisExtent + childMainAxisMarginBoxExtent;
839 void RenderFlexibleBox::freezeViolations(const WTF::Vector<Violation>& violations, LayoutUnit& availableFreeSpace, float& totalPositiveFlexibility, float& totalNegativeFlexibility, InflexibleFlexItemSize& inflexibleItems)
841 for (size_t i = 0; i < violations.size(); ++i) {
842 RenderBox* child = violations[i].child;
843 LayoutUnit childSize = violations[i].childSize;
844 availableFreeSpace -= childSize - preferredMainAxisContentExtentForChild(child);
845 totalPositiveFlexibility -= child->style()->positiveFlex();
846 totalNegativeFlexibility -= child->style()->negativeFlex();
847 inflexibleItems.set(child, childSize);
851 // Returns true if we successfully ran the algorithm and sized the flex items.
852 bool RenderFlexibleBox::resolveFlexibleLengths(FlexSign flexSign, const OrderedFlexItemList& children, LayoutUnit& availableFreeSpace, float& totalPositiveFlexibility, float& totalNegativeFlexibility, InflexibleFlexItemSize& inflexibleItems, WTF::Vector<LayoutUnit>& childSizes)
855 LayoutUnit flexboxAvailableContentExtent = mainAxisContentExtent();
856 LayoutUnit totalViolation = 0;
857 LayoutUnit usedFreeSpace = 0;
858 WTF::Vector<Violation> minViolations;
859 WTF::Vector<Violation> maxViolations;
860 for (size_t i = 0; i < children.size(); ++i) {
861 RenderBox* child = children[i];
862 if (child->isPositioned()) {
863 childSizes.append(0);
867 if (inflexibleItems.contains(child))
868 childSizes.append(inflexibleItems.get(child));
870 LayoutUnit preferredChildSize = preferredMainAxisContentExtentForChild(child);
871 LayoutUnit childSize = preferredChildSize;
872 if (availableFreeSpace > 0 && totalPositiveFlexibility > 0 && flexSign == PositiveFlexibility)
873 childSize += lroundf(availableFreeSpace * child->style()->positiveFlex() / totalPositiveFlexibility);
874 else if (availableFreeSpace < 0 && totalNegativeFlexibility > 0 && flexSign == NegativeFlexibility)
875 childSize += lroundf(availableFreeSpace * child->style()->negativeFlex() / totalNegativeFlexibility);
877 LayoutUnit adjustedChildSize = adjustChildSizeForMinAndMax(child, childSize, flexboxAvailableContentExtent);
878 childSizes.append(adjustedChildSize);
879 usedFreeSpace += adjustedChildSize - preferredChildSize;
881 LayoutUnit violation = adjustedChildSize - childSize;
883 minViolations.append(Violation(child, adjustedChildSize));
884 else if (violation < 0)
885 maxViolations.append(Violation(child, adjustedChildSize));
886 totalViolation += violation;
891 freezeViolations(totalViolation < 0 ? maxViolations : minViolations, availableFreeSpace, totalPositiveFlexibility, totalNegativeFlexibility, inflexibleItems);
893 availableFreeSpace -= usedFreeSpace;
895 return !totalViolation;
898 static LayoutUnit initialPackingOffset(LayoutUnit availableFreeSpace, EFlexPack flexPack, unsigned numberOfChildren)
900 if (flexPack == PackEnd)
901 return availableFreeSpace;
902 if (flexPack == PackCenter)
903 return availableFreeSpace / 2;
904 if (flexPack == PackDistribute) {
905 if (availableFreeSpace > 0 && numberOfChildren)
906 return availableFreeSpace / (2 * numberOfChildren);
907 if (availableFreeSpace < 0)
908 return availableFreeSpace / 2;
913 static LayoutUnit packingSpaceBetweenChildren(LayoutUnit availableFreeSpace, EFlexPack flexPack, unsigned numberOfChildren)
915 if (availableFreeSpace > 0 && numberOfChildren > 1) {
916 if (flexPack == PackJustify)
917 return availableFreeSpace / (numberOfChildren - 1);
918 if (flexPack == PackDistribute)
919 return availableFreeSpace / numberOfChildren;
924 void RenderFlexibleBox::setLogicalOverrideSize(RenderBox* child, LayoutUnit childPreferredSize)
926 // FIXME: Rename setOverrideWidth/setOverrideHeight to setOverrideLogicalWidth/setOverrideLogicalHeight.
927 if (hasOrthogonalFlow(child))
928 child->setOverrideHeight(childPreferredSize);
930 child->setOverrideWidth(childPreferredSize);
933 void RenderFlexibleBox::prepareChildForPositionedLayout(RenderBox* child, LayoutUnit mainAxisOffset, LayoutUnit crossAxisOffset)
935 ASSERT(child->isPositioned());
936 child->containingBlock()->insertPositionedObject(child);
937 RenderLayer* childLayer = child->layer();
938 LayoutUnit inlinePosition = isColumnFlow() ? crossAxisOffset : mainAxisOffset;
939 if (style()->flexDirection() == FlowRowReverse)
940 inlinePosition = mainAxisExtent() - mainAxisOffset;
941 childLayer->setStaticInlinePosition(inlinePosition); // FIXME: Not right for regions.
943 LayoutUnit staticBlockPosition = isColumnFlow() ? mainAxisOffset : crossAxisOffset;
944 if (childLayer->staticBlockPosition() != staticBlockPosition) {
945 childLayer->setStaticBlockPosition(staticBlockPosition);
946 if (child->style()->hasStaticBlockPosition(style()->isHorizontalWritingMode()))
947 child->setChildNeedsLayout(true, MarkOnlyThis);
951 static EFlexAlign flexAlignForChild(RenderBox* child)
953 EFlexAlign align = child->style()->flexItemAlign();
954 if (align == AlignAuto)
955 align = child->parent()->style()->flexAlign();
957 if (child->parent()->style()->flexWrap() == FlexWrapReverse) {
958 if (align == AlignStart)
960 else if (align == AlignEnd)
967 void RenderFlexibleBox::layoutAndPlaceChildren(LayoutUnit& crossAxisOffset, const OrderedFlexItemList& children, const WTF::Vector<LayoutUnit>& childSizes, LayoutUnit availableFreeSpace, WTF::Vector<LineContext>& lineContexts)
969 ASSERT(childSizes.size() == children.size());
971 LayoutUnit autoMarginOffset = autoMarginOffsetInMainAxis(children, availableFreeSpace);
972 LayoutUnit mainAxisOffset = flowAwareBorderStart() + flowAwarePaddingStart();
973 mainAxisOffset += initialPackingOffset(availableFreeSpace, style()->flexPack(), childSizes.size());
974 if (style()->flexDirection() == FlowRowReverse)
975 mainAxisOffset += isHorizontalFlow() ? verticalScrollbarWidth() : horizontalScrollbarHeight();
977 LayoutUnit totalMainExtent = mainAxisExtent();
978 LayoutUnit maxAscent = 0, maxDescent = 0; // Used when flex-align: baseline.
979 LayoutUnit maxChildCrossAxisExtent = 0;
980 bool shouldFlipMainAxis = !isColumnFlow() && !isLeftToRightFlow();
981 for (size_t i = 0; i < children.size(); ++i) {
982 RenderBox* child = children[i];
983 if (child->isPositioned()) {
984 prepareChildForPositionedLayout(child, mainAxisOffset, crossAxisOffset);
985 mainAxisOffset += packingSpaceBetweenChildren(availableFreeSpace, style()->flexPack(), childSizes.size());
988 LayoutUnit childPreferredSize = childSizes[i] + mainAxisBorderAndPaddingExtentForChild(child);
989 setLogicalOverrideSize(child, childPreferredSize);
990 child->setChildNeedsLayout(true);
991 child->layoutIfNeeded();
993 updateAutoMarginsInMainAxis(child, autoMarginOffset);
995 LayoutUnit childCrossAxisMarginBoxExtent;
996 if (flexAlignForChild(child) == AlignBaseline && !hasAutoMarginsInCrossAxis(child)) {
997 LayoutUnit ascent = marginBoxAscentForChild(child);
998 LayoutUnit descent = (crossAxisMarginExtentForChild(child) + crossAxisExtentForChild(child)) - ascent;
1000 maxAscent = std::max(maxAscent, ascent);
1001 maxDescent = std::max(maxDescent, descent);
1003 childCrossAxisMarginBoxExtent = maxAscent + maxDescent;
1005 childCrossAxisMarginBoxExtent = crossAxisExtentForChild(child) + crossAxisMarginExtentForChild(child);
1006 if (!isColumnFlow() && style()->logicalHeight().isAuto())
1007 setLogicalHeight(std::max(logicalHeight(), crossAxisOffset + flowAwareBorderAfter() + flowAwarePaddingAfter() + childCrossAxisMarginBoxExtent + crossAxisScrollbarExtent()));
1008 maxChildCrossAxisExtent = std::max(maxChildCrossAxisExtent, childCrossAxisMarginBoxExtent);
1010 mainAxisOffset += flowAwareMarginStartForChild(child);
1012 LayoutUnit childMainExtent = mainAxisExtentForChild(child);
1013 IntPoint childLocation(shouldFlipMainAxis ? totalMainExtent - mainAxisOffset - childMainExtent : mainAxisOffset,
1014 crossAxisOffset + flowAwareMarginBeforeForChild(child));
1016 // FIXME: Supporting layout deltas.
1017 setFlowAwareLocationForChild(child, childLocation);
1018 mainAxisOffset += childMainExtent + flowAwareMarginEndForChild(child);
1020 mainAxisOffset += packingSpaceBetweenChildren(availableFreeSpace, style()->flexPack(), childSizes.size());
1024 setLogicalHeight(mainAxisOffset + flowAwareBorderEnd() + flowAwarePaddingEnd() + scrollbarLogicalHeight());
1026 if (style()->flexDirection() == FlowColumnReverse) {
1027 // We have to do an extra pass for column-reverse to reposition the flex items since the start depends
1028 // on the height of the flexbox, which we only know after we've positioned all the flex items.
1029 computeLogicalHeight();
1030 layoutColumnReverse(children, childSizes, crossAxisOffset, availableFreeSpace);
1033 lineContexts.append(LineContext(crossAxisOffset, maxChildCrossAxisExtent, children.size(), maxAscent));
1034 crossAxisOffset += maxChildCrossAxisExtent;
1037 void RenderFlexibleBox::layoutColumnReverse(const OrderedFlexItemList& children, const WTF::Vector<LayoutUnit>& childSizes, LayoutUnit crossAxisOffset, LayoutUnit availableFreeSpace)
1039 // This is similar to the logic in layoutAndPlaceChildren, except we place the children
1040 // starting from the end of the flexbox. We also don't need to layout anything since we're
1041 // just moving the children to a new position.
1042 LayoutUnit mainAxisOffset = logicalHeight() - flowAwareBorderEnd() - flowAwarePaddingEnd();
1043 mainAxisOffset -= initialPackingOffset(availableFreeSpace, style()->flexPack(), childSizes.size());
1044 mainAxisOffset -= isHorizontalFlow() ? verticalScrollbarWidth() : horizontalScrollbarHeight();
1046 for (size_t i = 0; i < children.size(); ++i) {
1047 RenderBox* child = children[i];
1048 if (child->isPositioned()) {
1049 child->layer()->setStaticBlockPosition(mainAxisOffset);
1050 mainAxisOffset -= packingSpaceBetweenChildren(availableFreeSpace, style()->flexPack(), childSizes.size());
1053 mainAxisOffset -= mainAxisExtentForChild(child) + flowAwareMarginEndForChild(child);
1055 LayoutRect oldRect = child->frameRect();
1056 setFlowAwareLocationForChild(child, IntPoint(mainAxisOffset, crossAxisOffset + flowAwareMarginBeforeForChild(child)));
1057 if (!selfNeedsLayout() && child->checkForRepaintDuringLayout())
1058 child->repaintDuringLayoutIfMoved(oldRect);
1060 mainAxisOffset -= flowAwareMarginStartForChild(child);
1061 mainAxisOffset -= packingSpaceBetweenChildren(availableFreeSpace, style()->flexPack(), childSizes.size());
1065 static LayoutUnit initialLinePackingOffset(LayoutUnit availableFreeSpace, EFlexLinePack linePack, unsigned numberOfLines)
1067 if (linePack == LinePackEnd)
1068 return availableFreeSpace;
1069 if (linePack == LinePackCenter)
1070 return availableFreeSpace / 2;
1071 if (linePack == LinePackDistribute) {
1072 if (availableFreeSpace > 0 && numberOfLines)
1073 return availableFreeSpace / (2 * numberOfLines);
1074 if (availableFreeSpace < 0)
1075 return availableFreeSpace / 2;
1080 static LayoutUnit linePackingSpaceBetweenChildren(LayoutUnit availableFreeSpace, EFlexLinePack linePack, unsigned numberOfLines)
1082 if (availableFreeSpace > 0 && numberOfLines > 1) {
1083 if (linePack == LinePackJustify)
1084 return availableFreeSpace / (numberOfLines - 1);
1085 if (linePack == LinePackDistribute || linePack == LinePackStretch)
1086 return availableFreeSpace / numberOfLines;
1091 void RenderFlexibleBox::packFlexLines(FlexOrderIterator& iterator, WTF::Vector<LineContext>& lineContexts)
1093 if (!isMultiline() || style()->flexLinePack() == LinePackStart)
1096 LayoutUnit availableCrossAxisSpace = crossAxisContentExtent();
1097 for (size_t i = 0; i < lineContexts.size(); ++i)
1098 availableCrossAxisSpace -= lineContexts[i].crossAxisExtent;
1100 RenderBox* child = iterator.first();
1101 LayoutUnit lineOffset = initialLinePackingOffset(availableCrossAxisSpace, style()->flexLinePack(), lineContexts.size());
1102 for (unsigned lineNumber = 0; lineNumber < lineContexts.size(); ++lineNumber) {
1103 lineContexts[lineNumber].crossAxisOffset += lineOffset;
1104 for (size_t childNumber = 0; childNumber < lineContexts[lineNumber].numberOfChildren; ++childNumber, child = iterator.next())
1105 adjustAlignmentForChild(child, lineOffset);
1107 if (style()->flexLinePack() == LinePackStretch && availableCrossAxisSpace > 0)
1108 lineContexts[lineNumber].crossAxisExtent += availableCrossAxisSpace / static_cast<unsigned>(lineContexts.size());
1110 lineOffset += linePackingSpaceBetweenChildren(availableCrossAxisSpace, style()->flexLinePack(), lineContexts.size());
1114 void RenderFlexibleBox::adjustAlignmentForChild(RenderBox* child, LayoutUnit delta)
1116 LayoutRect oldRect = child->frameRect();
1118 setFlowAwareLocationForChild(child, flowAwareLocationForChild(child) + LayoutSize(0, delta));
1120 // If the child moved, we have to repaint it as well as any floating/positioned
1121 // descendants. An exception is if we need a layout. In this case, we know we're going to
1122 // repaint ourselves (and the child) anyway.
1123 if (!selfNeedsLayout() && child->checkForRepaintDuringLayout())
1124 child->repaintDuringLayoutIfMoved(oldRect);
1127 void RenderFlexibleBox::alignChildren(FlexOrderIterator& iterator, const WTF::Vector<LineContext>& lineContexts)
1129 // Keep track of the space between the baseline edge and the after edge of the box for each line.
1130 WTF::Vector<LayoutUnit> minMarginAfterBaselines;
1132 RenderBox* child = iterator.first();
1133 for (size_t lineNumber = 0; lineNumber < lineContexts.size(); ++lineNumber) {
1134 LayoutUnit minMarginAfterBaseline = MAX_LAYOUT_UNIT;
1135 LayoutUnit lineCrossAxisExtent = lineContexts[lineNumber].crossAxisExtent;
1136 LayoutUnit maxAscent = lineContexts[lineNumber].maxAscent;
1138 for (size_t childNumber = 0; childNumber < lineContexts[lineNumber].numberOfChildren; ++childNumber, child = iterator.next()) {
1140 if (updateAutoMarginsInCrossAxis(child, availableAlignmentSpaceForChild(lineCrossAxisExtent, child)))
1143 switch (flexAlignForChild(child)) {
1145 ASSERT_NOT_REACHED();
1147 case AlignStretch: {
1148 applyStretchAlignmentToChild(child, lineCrossAxisExtent);
1149 // Since wrap-reverse flips cross start and cross end, strech children should be aligned with the cross end.
1150 if (style()->flexWrap() == FlexWrapReverse)
1151 adjustAlignmentForChild(child, availableAlignmentSpaceForChild(lineCrossAxisExtent, child));
1157 adjustAlignmentForChild(child, availableAlignmentSpaceForChild(lineCrossAxisExtent, child));
1160 adjustAlignmentForChild(child, availableAlignmentSpaceForChild(lineCrossAxisExtent, child) / 2);
1162 case AlignBaseline: {
1163 LayoutUnit ascent = marginBoxAscentForChild(child);
1164 LayoutUnit startOffset = maxAscent - ascent;
1165 adjustAlignmentForChild(child, startOffset);
1167 if (style()->flexWrap() == FlexWrapReverse)
1168 minMarginAfterBaseline = std::min(minMarginAfterBaseline, availableAlignmentSpaceForChild(lineCrossAxisExtent, child) - startOffset);
1173 minMarginAfterBaselines.append(minMarginAfterBaseline);
1176 if (style()->flexWrap() != FlexWrapReverse)
1179 // wrap-reverse flips the cross axis start and end. For baseline alignment, this means we
1180 // need to align the after edge of baseline elements with the after edge of the flex line.
1181 child = iterator.first();
1182 for (size_t lineNumber = 0; lineNumber < lineContexts.size(); ++lineNumber) {
1183 LayoutUnit minMarginAfterBaseline = minMarginAfterBaselines[lineNumber];
1184 for (size_t childNumber = 0; childNumber < lineContexts[lineNumber].numberOfChildren; ++childNumber, child = iterator.next()) {
1186 if (flexAlignForChild(child) == AlignBaseline && !hasAutoMarginsInCrossAxis(child) && minMarginAfterBaseline)
1187 adjustAlignmentForChild(child, minMarginAfterBaseline);
1192 void RenderFlexibleBox::applyStretchAlignmentToChild(RenderBox* child, LayoutUnit lineCrossAxisExtent)
1194 if (!isColumnFlow() && child->style()->logicalHeight().isAuto()) {
1195 LayoutUnit logicalHeightBefore = child->logicalHeight();
1196 LayoutUnit stretchedLogicalHeight = child->logicalHeight() + availableAlignmentSpaceForChild(lineCrossAxisExtent, child);
1197 if (stretchedLogicalHeight < logicalHeightBefore)
1200 child->setLogicalHeight(stretchedLogicalHeight);
1201 child->computeLogicalHeight();
1203 if (child->logicalHeight() != logicalHeightBefore) {
1204 child->setOverrideHeight(child->logicalHeight());
1205 child->setLogicalHeight(0);
1206 child->setChildNeedsLayout(true);
1207 child->layoutIfNeeded();
1209 } else if (isColumnFlow() && child->style()->logicalWidth().isAuto() && isMultiline()) {
1210 // FIXME: Handle min-width and max-width.
1211 LayoutUnit childWidth = lineCrossAxisExtent - crossAxisMarginExtentForChild(child);
1212 child->setOverrideWidth(std::max(ZERO_LAYOUT_UNIT, childWidth));
1213 child->setChildNeedsLayout(true);
1214 child->layoutIfNeeded();
1218 void RenderFlexibleBox::flipForRightToLeftColumn(FlexOrderIterator& iterator)
1220 if (style()->isLeftToRightDirection() || !isColumnFlow())
1223 LayoutUnit crossExtent = crossAxisExtent();
1224 for (RenderBox* child = iterator.first(); child; child = iterator.next()) {
1225 LayoutPoint location = flowAwareLocationForChild(child);
1226 location.setY(crossExtent - crossAxisExtentForChild(child) - location.y());
1227 setFlowAwareLocationForChild(child, location);
1231 void RenderFlexibleBox::flipForWrapReverse(FlexOrderIterator& iterator, const WTF::Vector<LineContext>& lineContexts, LayoutUnit crossAxisStartEdge)
1233 LayoutUnit contentExtent = crossAxisContentExtent();
1234 RenderBox* child = iterator.first();
1235 for (size_t lineNumber = 0; lineNumber < lineContexts.size(); ++lineNumber) {
1236 for (size_t childNumber = 0; childNumber < lineContexts[lineNumber].numberOfChildren; ++childNumber, child = iterator.next()) {
1238 LayoutPoint location = flowAwareLocationForChild(child);
1239 LayoutUnit lineCrossAxisExtent = lineContexts[lineNumber].crossAxisExtent;
1240 LayoutUnit originalOffset = lineContexts[lineNumber].crossAxisOffset - crossAxisStartEdge;
1241 LayoutUnit newOffset = contentExtent - originalOffset - lineCrossAxisExtent;
1242 location.setY(location.y() + newOffset - originalOffset);
1244 LayoutRect oldRect = child->frameRect();
1245 setFlowAwareLocationForChild(child, location);
1246 if (!selfNeedsLayout() && child->checkForRepaintDuringLayout())
1247 child->repaintDuringLayoutIfMoved(oldRect);