1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
/*
* Copyright (C) 2013 Google Inc. All rights reserved.
* Copyright (C) 2013, 2014 Igalia S.L.
* Copyright (C) 2025 Samuel Weinig <sam@webkit.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include "StyleLengthWrapper.h"
namespace WebCore {
namespace Style {
using namespace CSS::Literals;
// FIXME: Make LengthWrapperBase support additional numeric types in addition to the <length-percentage> one and then replace GridTrackBreadth with that.
struct GridTrackBreadthLength : LengthWrapperBase<LengthPercentage<CSS::Nonnegative>, CSS::Keyword::MinContent, CSS::Keyword::MaxContent, CSS::Keyword::Auto> {
using Base::Base;
};
// <track-breadth> = <length-percentage [0,inf]> | <flex [0,inf]> | min-content | max-content | auto
// https://www.w3.org/TR/css-grid-1/#typedef-track-breadth
class GridTrackBreadth {
public:
using Fixed = GridTrackBreadthLength::Fixed;
using Percentage = GridTrackBreadthLength::Percentage;
using Calc = GridTrackBreadthLength::Calc;
using Flex = Style::Flex<CSS::Nonnegative>;
GridTrackBreadth(Fixed fixed)
: m_length(fixed)
, m_flex(0_css_fr)
, m_type(GridTrackBreadthType::Length)
{
}
GridTrackBreadth(Percentage percent)
: m_length(percent)
, m_flex(0_css_fr)
, m_type(GridTrackBreadthType::Length)
{
}
GridTrackBreadth(GridTrackBreadthLength&& length)
: m_length(WTFMove(length))
, m_flex(0_css_fr)
, m_type(GridTrackBreadthType::Length)
{
}
GridTrackBreadth(Flex flex)
: m_length(0_css_px)
, m_flex(flex)
, m_type(GridTrackBreadthType::Flex)
{
}
GridTrackBreadth(CSS::Keyword::MinContent keyword)
: m_length(keyword)
, m_flex(0_css_fr)
, m_type(GridTrackBreadthType::Length)
{
}
GridTrackBreadth(CSS::Keyword::MaxContent keyword)
: m_length(keyword)
, m_flex(0_css_fr)
, m_type(GridTrackBreadthType::Length)
{
}
GridTrackBreadth(CSS::Keyword::Auto keyword)
: m_length(keyword)
, m_flex(0_css_fr)
, m_type(GridTrackBreadthType::Length)
{
}
GridTrackBreadth(CSS::ValueLiteral<CSS::LengthUnit::Px> literal)
: m_length(literal)
, m_flex(0_css_fr)
, m_type(GridTrackBreadthType::Length)
{
}
GridTrackBreadth(CSS::ValueLiteral<CSS::PercentageUnit::Percentage> literal)
: m_length(literal)
, m_flex(0_css_fr)
, m_type(GridTrackBreadthType::Length)
{
}
GridTrackBreadth(CSS::ValueLiteral<CSS::FlexUnit::Fr> literal)
: m_length(0_css_px)
, m_flex(literal)
, m_type(GridTrackBreadthType::Flex)
{
}
bool isLength() const { return m_type == GridTrackBreadthType::Length; }
bool isFlex() const { return m_type == GridTrackBreadthType::Flex; }
const GridTrackBreadthLength& length() const { ASSERT(isLength()); return m_length; }
Flex flex() const { ASSERT(isFlex()); return m_flex; }
bool isPercentOrCalculated() const { return m_type == GridTrackBreadthType::Length && m_length.isPercentOrCalculated(); }
bool isContentSized() const { return m_type == GridTrackBreadthType::Length && (m_length.isAuto() || m_length.isMinContent() || m_length.isMaxContent()); }
bool isAuto() const { return m_type == GridTrackBreadthType::Length && m_length.isAuto(); }
template<typename... F> decltype(auto) switchOn(F&&... f) const
{
auto visitor = WTF::makeVisitor(std::forward<F>(f)...);
if (isFlex())
return visitor(m_flex);
return WTF::switchOn(m_length, [&](const auto& value) { return visitor(value); });
}
bool operator==(const GridTrackBreadth&) const = default;
private:
GridTrackBreadthLength m_length;
Flex m_flex;
enum class GridTrackBreadthType : bool { Length, Flex };
GridTrackBreadthType m_type;
};
// MARK: - Conversion
template<> struct CSSValueConversion<GridTrackBreadth> { auto operator()(BuilderState&, const CSSPrimitiveValue&) -> GridTrackBreadth; };
// MARK: - Blending
template<> struct Blending<GridTrackBreadth> {
auto blend(const GridTrackBreadth&, const GridTrackBreadth&, const BlendingContext&) -> GridTrackBreadth;
};
} // namespace Style
} // namespace WebCore
DEFINE_VARIANT_LIKE_CONFORMANCE(WebCore::Style::GridTrackBreadthLength)
DEFINE_VARIANT_LIKE_CONFORMANCE(WebCore::Style::GridTrackBreadth)
|