|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Sam Atkins <sam@ladybird.org> |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: BSD-2-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +#include "CSSUnitValue.h" |
| 8 | +#include <LibWeb/Bindings/CSSUnitValuePrototype.h> |
| 9 | +#include <LibWeb/Bindings/Intrinsics.h> |
| 10 | +#include <LibWeb/CSS/Serialize.h> |
| 11 | +#include <LibWeb/WebIDL/ExceptionOr.h> |
| 12 | + |
| 13 | +namespace Web::CSS { |
| 14 | + |
| 15 | +GC_DEFINE_ALLOCATOR(CSSUnitValue); |
| 16 | + |
| 17 | +GC::Ref<CSSUnitValue> CSSUnitValue::create(JS::Realm& realm, double value, FlyString unit) |
| 18 | +{ |
| 19 | + // The type of a CSSUnitValue is the result of creating a type from its unit internal slot. |
| 20 | + // https://drafts.css-houdini.org/css-typed-om-1/#type-of-a-cssunitvalue |
| 21 | + auto numeric_type = NumericType::create_from_unit(unit); |
| 22 | + return realm.create<CSSUnitValue>(realm, value, move(unit), numeric_type.release_value()); |
| 23 | +} |
| 24 | + |
| 25 | +// https://drafts.css-houdini.org/css-typed-om-1/#dom-cssunitvalue-cssunitvalue |
| 26 | +WebIDL::ExceptionOr<GC::Ref<CSSUnitValue>> CSSUnitValue::construct_impl(JS::Realm& realm, double value, FlyString unit) |
| 27 | +{ |
| 28 | + // 1. If creating a type from unit returns failure, throw a TypeError and abort this algorithm. |
| 29 | + auto numeric_type = NumericType::create_from_unit(unit); |
| 30 | + if (!numeric_type.has_value()) |
| 31 | + return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, MUST(String::formatted("Cannot create CSSUnitValue with unrecognized unit '{}'", unit)) }; |
| 32 | + |
| 33 | + // 2. Return a new CSSUnitValue with its value internal slot set to value and its unit set to unit. |
| 34 | + return realm.create<CSSUnitValue>(realm, value, move(unit), numeric_type.release_value()); |
| 35 | +} |
| 36 | + |
| 37 | +CSSUnitValue::CSSUnitValue(JS::Realm& realm, double value, FlyString unit, NumericType type) |
| 38 | + : CSSNumericValue(realm, move(type)) |
| 39 | + , m_value(value) |
| 40 | + // AD-HOC: WPT expects the unit to be lowercase but this doesn't seem to be specified anywhere. |
| 41 | + , m_unit(unit.to_ascii_lowercase()) |
| 42 | +{ |
| 43 | +} |
| 44 | + |
| 45 | +void CSSUnitValue::initialize(JS::Realm& realm) |
| 46 | +{ |
| 47 | + WEB_SET_PROTOTYPE_FOR_INTERFACE(CSSUnitValue); |
| 48 | + Base::initialize(realm); |
| 49 | +} |
| 50 | + |
| 51 | +// https://drafts.css-houdini.org/css-typed-om-1/#dom-cssunitvalue-value |
| 52 | +void CSSUnitValue::set_value(double value) |
| 53 | +{ |
| 54 | + // AD-HOC: No definition: https://github.com/w3c/css-houdini-drafts/issues/1146 |
| 55 | + m_value = value; |
| 56 | +} |
| 57 | + |
| 58 | +// https://drafts.css-houdini.org/css-typed-om-1/#serialize-a-cssunitvalue |
| 59 | +String CSSUnitValue::serialize_unit_value(Optional<double> minimum, Optional<double> maximum) const |
| 60 | +{ |
| 61 | + // To serialize a CSSUnitValue this, with optional arguments minimum, a numeric value, and maximum, a numeric value: |
| 62 | + |
| 63 | + // 1. Let value and unit be this‘s value and unit internal slots. |
| 64 | + |
| 65 | + // 2. Set s to the result of serializing a <number> from value, per CSSOM §6.7.2 Serializing CSS Values. |
| 66 | + StringBuilder s; |
| 67 | + serialize_a_number(s, m_value); |
| 68 | + |
| 69 | + // 3. If unit is: |
| 70 | + // -> "number" |
| 71 | + if (m_unit == "number"_fly_string) { |
| 72 | + // Do nothing. |
| 73 | + } |
| 74 | + // -> "percent" |
| 75 | + else if (m_unit == "percent"_fly_string) { |
| 76 | + // Append "%" to s. |
| 77 | + s.append("%"sv); |
| 78 | + } |
| 79 | + // -> anything else |
| 80 | + else { |
| 81 | + // Append unit to s. |
| 82 | + s.append(m_unit.to_ascii_lowercase()); |
| 83 | + } |
| 84 | + |
| 85 | + // 4. If minimum was passed and this is less than minimum, or if maximum was passed and this is greater than |
| 86 | + // maximum, or either minimum and/or maximum were passed and the relative size of this and minimum/maximum can’t |
| 87 | + // be determined with the available information at this time, prepend "calc(" to s, then append ")" to s. |
| 88 | + if ((minimum.has_value() && m_value < minimum.value()) |
| 89 | + || (maximum.has_value() && m_value > maximum.value())) { |
| 90 | + // FIXME: "or either minimum and/or maximum were passed and the relative size of this and minimum/maximum can’t be determined with the available information at this time" |
| 91 | + return MUST(String::formatted("calc({})", s.string_view())); |
| 92 | + } |
| 93 | + |
| 94 | + // 5. Return s. |
| 95 | + return s.to_string_without_validation(); |
| 96 | +} |
| 97 | + |
| 98 | +} |
0 commit comments