diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt index d5393b0410..36eca3d26d 100644 --- a/Userland/Libraries/LibWeb/CMakeLists.txt +++ b/Userland/Libraries/LibWeb/CMakeLists.txt @@ -26,6 +26,7 @@ set(SOURCES CSS/CSSKeyframesRule.cpp CSS/CSSFontFaceRule.cpp CSS/CSSMediaRule.cpp + CSS/CSSNumericType.cpp CSS/CSSRule.cpp CSS/CSSRuleList.cpp CSS/CSSStyleDeclaration.cpp diff --git a/Userland/Libraries/LibWeb/CSS/CSSNumericType.cpp b/Userland/Libraries/LibWeb/CSS/CSSNumericType.cpp new file mode 100644 index 0000000000..04cbba79c5 --- /dev/null +++ b/Userland/Libraries/LibWeb/CSS/CSSNumericType.cpp @@ -0,0 +1,432 @@ +/* + * Copyright (c) 2023, Sam Atkins + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include "CSSNumericType.h" +#include +#include +#include +#include +#include +#include +#include + +namespace Web::CSS { + +Optional CSSNumericType::base_type_from_value_type(ValueType value_type) +{ + switch (value_type) { + case ValueType::Angle: + return BaseType::Angle; + case ValueType::Frequency: + return BaseType::Frequency; + case ValueType::Length: + return BaseType::Length; + case ValueType::Percentage: + return BaseType::Percent; + case ValueType::Resolution: + return BaseType::Resolution; + case ValueType::Time: + return BaseType::Time; + + case ValueType::Color: + case ValueType::CustomIdent: + case ValueType::FilterValueList: + case ValueType::Image: + case ValueType::Integer: + case ValueType::Paint: + case ValueType::Number: + case ValueType::Position: + case ValueType::Ratio: + case ValueType::Rect: + case ValueType::String: + case ValueType::Url: + return {}; + } + + VERIFY_NOT_REACHED(); +} + +// https://drafts.css-houdini.org/css-typed-om-1/#cssnumericvalue-create-a-type +Optional CSSNumericType::create_from_unit(StringView unit) +{ + // To create a type from a string unit, follow the appropriate branch of the following: + + // unit is "number" + if (unit == "number"sv) { + // Return «[ ]» (empty map) + return CSSNumericType {}; + } + + // unit is "percent" + if (unit == "percent"sv) { + // Return «[ "percent" → 1 ]» + return CSSNumericType { BaseType::Percent, 1 }; + } + + // unit is a unit + if (Length::unit_from_name(unit).has_value()) { + // Return «[ "length" → 1 ]» + return CSSNumericType { BaseType::Length, 1 }; + } + + // unit is an unit + if (Angle::unit_from_name(unit).has_value()) { + // Return «[ "angle" → 1 ]» + return CSSNumericType { BaseType::Angle, 1 }; + } + + // unit is a