NumericStyleValue.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 ValueComparingNonnullRefPtr<NumericStyleValue> create_float(float value)
  15. {
  16. return adopt_ref(*new NumericStyleValue(value));
  17. }
  18. static ValueComparingNonnullRefPtr<NumericStyleValue> create_integer(i64 value)
  19. {
  20. return adopt_ref(*new NumericStyleValue(value));
  21. }
  22. virtual bool has_length() const override { return to_number() == 0; }
  23. virtual Length to_length() const override { return Length::make_px(0); }
  24. virtual bool has_number() const override { return true; }
  25. virtual float to_number() const override
  26. {
  27. return m_value.visit(
  28. [](float value) { return value; },
  29. [](i64 value) { return (float)value; });
  30. }
  31. virtual bool has_integer() const override { return m_value.has<i64>(); }
  32. virtual float to_integer() const override { return m_value.get<i64>(); }
  33. virtual ErrorOr<String> to_string() const override;
  34. bool properties_equal(NumericStyleValue const& other) const { return m_value == other.m_value; }
  35. private:
  36. explicit NumericStyleValue(Variant<float, i64> value)
  37. : StyleValueWithDefaultOperators(Type::Numeric)
  38. , m_value(move(value))
  39. {
  40. }
  41. Variant<float, i64> m_value { (i64)0 };
  42. };
  43. }