NumericStyleValue.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
  4. * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
  5. * Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #pragma once
  10. #include <LibWeb/CSS/StyleValue.h>
  11. namespace Web::CSS {
  12. class NumericStyleValue : public StyleValueWithDefaultOperators<NumericStyleValue> {
  13. public:
  14. static ErrorOr<ValueComparingNonnullRefPtr<NumericStyleValue>> create_float(float value)
  15. {
  16. return adopt_nonnull_ref_or_enomem(new (nothrow) NumericStyleValue(value));
  17. }
  18. static ErrorOr<ValueComparingNonnullRefPtr<NumericStyleValue>> create_integer(i64 value)
  19. {
  20. return adopt_nonnull_ref_or_enomem(new (nothrow) NumericStyleValue(value));
  21. }
  22. virtual bool has_length() const override { return number() == 0; }
  23. virtual Length to_length() const override { return Length::make_px(0); }
  24. float number() const
  25. {
  26. return m_value.visit(
  27. [](float value) { return value; },
  28. [](i64 value) { return (float)value; });
  29. }
  30. bool has_integer() const { return m_value.has<i64>(); }
  31. float integer() const { return m_value.get<i64>(); }
  32. virtual ErrorOr<String> to_string() const override;
  33. bool properties_equal(NumericStyleValue const& other) const { return m_value == other.m_value; }
  34. private:
  35. explicit NumericStyleValue(Variant<float, i64> value)
  36. : StyleValueWithDefaultOperators(Type::Numeric)
  37. , m_value(move(value))
  38. {
  39. }
  40. Variant<float, i64> m_value { (i64)0 };
  41. };
  42. }