Skip to content

Truly independent web browser

2.6 KB
Raw Blame
/*
 * Copyright (c) 2023-2024, Sam Atkins <[email protected]>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <LibWeb/Export.h>

#include <AK/Array.h>
#include <AK/Optional.h>
#include <AK/String.h>
#include <AK/Utf16FlyString.h>
#include <LibWeb/Forward.h>

namespace Web::CSS::StyleValueFFI {

struct FfiNumericType;

}

namespace Web::CSS {

// https://drafts.css-houdini.org/css-typed-om-1/#cssnumericvalue-type
class WEB_API NumericType {
public:
    enum class BaseType {
        Length,
        Angle,
        Time,
        Frequency,
        Resolution,
        Flex,
        Percent,
        __Count,
    };

    NumericType() = default;
    NumericType(BaseType type, i32 power)
    {
        set_exponent(type, power);
    }

    bool matches_angle(Optional<ValueType> percentages_resolve_as) const { return matches_dimension(BaseType::Angle, percentages_resolve_as); }
    bool matches_length(Optional<ValueType> percentages_resolve_as) const { return matches_dimension(BaseType::Length, percentages_resolve_as); }
    bool matches_length_percentage(Optional<ValueType> percentages_resolve_as) const { return matches_dimension_percentage(BaseType::Length, percentages_resolve_as); }
    bool matches_number(Optional<ValueType> percentages_resolve_as) const;
    bool matches_percentage() const;
    bool matches_resolution(Optional<ValueType> percentages_resolve_as) const { return matches_dimension(BaseType::Resolution, percentages_resolve_as); }

    Optional<i32> const& exponent(BaseType type) const { return m_type_exponents[to_underlying(type)]; }
    void set_exponent(BaseType type, i32 exponent) { m_type_exponents[to_underlying(type)] = exponent; }

    template<typename Callback>
    void for_each_type_and_exponent(Callback callback) const
    {
        for (auto i = 0; i < to_underlying(BaseType::__Count); ++i) {
            if (!m_type_exponents[i].has_value())
                continue;
            auto base_type = static_cast<BaseType>(i);
            callback(base_type, m_type_exponents[i].value());
        }
    }

    Optional<BaseType> const& percent_hint() const { return m_percent_hint; }
    void set_percent_hint(Optional<BaseType> hint) { m_percent_hint = hint; }

    bool operator==(NumericType const& other) const = default;

private:
    bool matches_dimension(BaseType, Optional<ValueType> percentages_resolve_as) const;
    bool matches_dimension_percentage(BaseType, Optional<ValueType> percentages_resolve_as) const;

    Array<Optional<i32>, to_underlying(BaseType::__Count)> m_type_exponents;
    Optional<BaseType> m_percent_hint;
};

}