|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Sam Atkins <sam@ladybird.org> |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: BSD-2-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +#include "CSSNumericValue.h" |
| 8 | +#include <LibWeb/Bindings/CSSNumericValuePrototype.h> |
| 9 | +#include <LibWeb/Bindings/Intrinsics.h> |
| 10 | +#include <LibWeb/CSS/NumericType.h> |
| 11 | +#include <LibWeb/CSS/Serialize.h> |
| 12 | + |
| 13 | +namespace Web::CSS { |
| 14 | + |
| 15 | +GC_DEFINE_ALLOCATOR(CSSNumericValue); |
| 16 | + |
| 17 | +static Bindings::CSSNumericBaseType to_om_numeric_base_type(NumericType::BaseType source) |
| 18 | +{ |
| 19 | + switch (source) { |
| 20 | + case NumericType::BaseType::Length: |
| 21 | + return Bindings::CSSNumericBaseType::Length; |
| 22 | + case NumericType::BaseType::Angle: |
| 23 | + return Bindings::CSSNumericBaseType::Angle; |
| 24 | + case NumericType::BaseType::Time: |
| 25 | + return Bindings::CSSNumericBaseType::Time; |
| 26 | + case NumericType::BaseType::Frequency: |
| 27 | + return Bindings::CSSNumericBaseType::Frequency; |
| 28 | + case NumericType::BaseType::Resolution: |
| 29 | + return Bindings::CSSNumericBaseType::Resolution; |
| 30 | + case NumericType::BaseType::Flex: |
| 31 | + return Bindings::CSSNumericBaseType::Flex; |
| 32 | + case NumericType::BaseType::Percent: |
| 33 | + return Bindings::CSSNumericBaseType::Percent; |
| 34 | + case NumericType::BaseType::__Count: |
| 35 | + VERIFY_NOT_REACHED(); |
| 36 | + } |
| 37 | + VERIFY_NOT_REACHED(); |
| 38 | +} |
| 39 | + |
| 40 | +CSSNumericValue::CSSNumericValue(JS::Realm& realm, NumericType type) |
| 41 | + : CSSStyleValue(realm) |
| 42 | + , m_type(move(type)) |
| 43 | +{ |
| 44 | +} |
| 45 | + |
| 46 | +void CSSNumericValue::initialize(JS::Realm& realm) |
| 47 | +{ |
| 48 | + WEB_SET_PROTOTYPE_FOR_INTERFACE(CSSNumericValue); |
| 49 | + Base::initialize(realm); |
| 50 | +} |
| 51 | + |
| 52 | +// https://drafts.css-houdini.org/css-typed-om-1/#dom-cssnumericvalue-type |
| 53 | +CSSNumericType CSSNumericValue::type_for_bindings() const |
| 54 | +{ |
| 55 | + // 1. Let result be a new CSSNumericType. |
| 56 | + CSSNumericType result {}; |
| 57 | + |
| 58 | + // 2. For each baseType → power in the type of this, |
| 59 | + m_type.for_each_type_and_exponent([&result](NumericType::BaseType base_type, auto power) { |
| 60 | + // 1. If power is not 0, set result[baseType] to power. |
| 61 | + if (power == 0) |
| 62 | + return; |
| 63 | + |
| 64 | + switch (base_type) { |
| 65 | + case NumericType::BaseType::Length: |
| 66 | + result.length = power; |
| 67 | + break; |
| 68 | + case NumericType::BaseType::Angle: |
| 69 | + result.angle = power; |
| 70 | + break; |
| 71 | + case NumericType::BaseType::Time: |
| 72 | + result.time = power; |
| 73 | + break; |
| 74 | + case NumericType::BaseType::Frequency: |
| 75 | + result.frequency = power; |
| 76 | + break; |
| 77 | + case NumericType::BaseType::Resolution: |
| 78 | + result.resolution = power; |
| 79 | + break; |
| 80 | + case NumericType::BaseType::Flex: |
| 81 | + result.flex = power; |
| 82 | + break; |
| 83 | + case NumericType::BaseType::Percent: |
| 84 | + result.percent = power; |
| 85 | + break; |
| 86 | + case NumericType::BaseType::__Count: |
| 87 | + VERIFY_NOT_REACHED(); |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + // 3. If the percent hint of this is not null, |
| 92 | + if (auto percent_hint = m_type.percent_hint(); percent_hint.has_value()) { |
| 93 | + // 1. Set result[percentHint] to the percent hint of this. |
| 94 | + result.percent_hint = to_om_numeric_base_type(percent_hint.value()); |
| 95 | + } |
| 96 | + |
| 97 | + // 4. Return result. |
| 98 | + return result; |
| 99 | +} |
| 100 | + |
| 101 | +// https://drafts.css-houdini.org/css-typed-om-1/#serialize-a-cssnumericvalue |
| 102 | +String CSSNumericValue::to_string(SerializationParams const&) const |
| 103 | +{ |
| 104 | + // To serialize a CSSNumericValue this, given an optional minimum, a numeric value, and optional maximum, a numeric value: |
| 105 | + // FIXME: 1. If this is a CSSUnitValue, serialize a CSSUnitValue from this, passing minimum and maximum. Return the result. |
| 106 | + // FIXME: 2. Otherwise, serialize a CSSMathValue from this, and return the result. |
| 107 | + |
| 108 | + return {}; |
| 109 | +} |
| 110 | + |
| 111 | +} |
0 commit comments