Flex.cpp 979 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "Flex.h"
  7. #include <LibWeb/CSS/Percentage.h>
  8. namespace Web::CSS {
  9. Flex::Flex(double value, Type type)
  10. : m_type(type)
  11. , m_value(value)
  12. {
  13. }
  14. Flex Flex::make_fr(double value)
  15. {
  16. return { value, Type::Fr };
  17. }
  18. Flex Flex::percentage_of(Percentage const& percentage) const
  19. {
  20. return Flex { percentage.as_fraction() * m_value, m_type };
  21. }
  22. String Flex::to_string() const
  23. {
  24. return MUST(String::formatted("{}fr", to_fr()));
  25. }
  26. double Flex::to_fr() const
  27. {
  28. switch (m_type) {
  29. case Type::Fr:
  30. return m_value;
  31. }
  32. VERIFY_NOT_REACHED();
  33. }
  34. StringView Flex::unit_name() const
  35. {
  36. switch (m_type) {
  37. case Type::Fr:
  38. return "fr"sv;
  39. }
  40. VERIFY_NOT_REACHED();
  41. }
  42. Optional<Flex::Type> Flex::unit_from_name(StringView name)
  43. {
  44. if (name.equals_ignoring_ascii_case("fr"sv))
  45. return Type::Fr;
  46. return {};
  47. }
  48. }