blob: 30fb60ad79621718f22be82b5eaa164432c10414 [file] [log] [blame]
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "core/layout/ng/ng_length_utils.h"
#include "core/layout/ng/ng_constraint_space.h"
#include "core/style/ComputedStyle.h"
#include "platform/LayoutUnit.h"
#include "platform/Length.h"
namespace blink {
// TODO(layout-ng):
// - Handle border-box correctly
// - positioned and/or replaced calculations
// - Handle margins for fill-available and width: auto
LayoutUnit resolveInlineLength(const NGConstraintSpace& constraintSpace,
const Length& length,
LengthResolveType type) {
// TODO(layout-ng): Handle min/max/fit-content
DCHECK(!length.isMaxSizeNone());
if (type == LengthResolveType::MinSize && length.isAuto())
return LayoutUnit();
return valueForLength(length, constraintSpace.ContainerSize().inlineSize);
}
LayoutUnit resolveBlockLength(const NGConstraintSpace& constraintSpace,
const Length& length,
LayoutUnit contentSize,
LengthResolveType type) {
DCHECK(!length.isMaxSizeNone());
if (type == LengthResolveType::MinSize && length.isAuto())
return LayoutUnit();
if (length.isAuto())
return contentSize;
if (length.isMinContent() || length.isMaxContent() || length.isFitContent())
return contentSize;
return valueForLength(length, constraintSpace.ContainerSize().blockSize);
}
LayoutUnit computeInlineSizeForFragment(
const NGConstraintSpace& constraintSpace,
const ComputedStyle& style) {
if (constraintSpace.fixedInlineSize())
return constraintSpace.ContainerSize().inlineSize;
LayoutUnit extent = resolveInlineLength(constraintSpace, style.logicalWidth(),
LengthResolveType::ContentSize);
Length maxLength = style.logicalMaxWidth();
if (!maxLength.isMaxSizeNone()) {
LayoutUnit max = resolveInlineLength(constraintSpace, maxLength,
LengthResolveType::MaxSize);
extent = std::min(extent, max);
}
LayoutUnit min = resolveInlineLength(constraintSpace, style.logicalMinWidth(),
LengthResolveType::MinSize);
extent = std::max(extent, min);
if (style.boxSizing() == BoxSizingContentBox) {
// TODO(layout-ng): Compute border/padding size and add it
}
return extent;
}
LayoutUnit computeBlockSizeForFragment(const NGConstraintSpace& constraintSpace,
const ComputedStyle& style,
LayoutUnit contentSize) {
if (constraintSpace.fixedBlockSize())
return constraintSpace.ContainerSize().blockSize;
LayoutUnit extent =
resolveBlockLength(constraintSpace, style.logicalHeight(), contentSize,
LengthResolveType::ContentSize);
Length maxLength = style.logicalMaxHeight();
if (!maxLength.isMaxSizeNone()) {
LayoutUnit max = resolveBlockLength(constraintSpace, maxLength, contentSize,
LengthResolveType::MaxSize);
extent = std::min(extent, max);
}
LayoutUnit min = resolveBlockLength(constraintSpace, style.logicalMinHeight(),
contentSize, LengthResolveType::MinSize);
extent = std::max(extent, min);
if (style.boxSizing() == BoxSizingContentBox) {
// TODO(layout-ng): Compute border/padding size and add it
}
return extent;
}
} // namespace blink