Display.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/CSS/Display.h>
  7. namespace Web::CSS {
  8. String Display::to_string() const
  9. {
  10. StringBuilder builder;
  11. switch (m_type) {
  12. case Type::OutsideAndInside:
  13. // NOTE: Following the precedence rules of “most backwards-compatible, then shortest”,
  14. // serialization of equivalent display values uses the “Short display” column.
  15. if (*this == Display::from_short(Display::Short::Block))
  16. return "block"_string;
  17. if (*this == Display::from_short(Display::Short::FlowRoot))
  18. return "flow-root"_string;
  19. if (*this == Display::from_short(Display::Short::Inline))
  20. return "inline"_string;
  21. if (*this == Display::from_short(Display::Short::InlineBlock))
  22. return "inline-block"_string;
  23. if (*this == Display::from_short(Display::Short::RunIn))
  24. return "run-in"_string;
  25. if (*this == Display::from_short(Display::Short::ListItem))
  26. return "list-item"_string;
  27. if (*this == Display::from_short(Display::Short::Flex))
  28. return "flex"_string;
  29. if (*this == Display::from_short(Display::Short::InlineFlex))
  30. return "inline-flex"_string;
  31. if (*this == Display::from_short(Display::Short::Grid))
  32. return "grid"_string;
  33. if (*this == Display::from_short(Display::Short::InlineGrid))
  34. return "inline-grid"_string;
  35. if (*this == Display::from_short(Display::Short::Ruby))
  36. return "ruby"_string;
  37. if (*this == Display::from_short(Display::Short::Table))
  38. return "table"_string;
  39. if (*this == Display::from_short(Display::Short::InlineTable))
  40. return "inline-table"_string;
  41. {
  42. Vector<StringView, 3> parts;
  43. if (!(m_value.outside_inside.outside == DisplayOutside::Block && m_value.outside_inside.inside == DisplayInside::FlowRoot))
  44. parts.append(CSS::to_string(m_value.outside_inside.outside));
  45. if (m_value.outside_inside.inside != DisplayInside::Flow)
  46. parts.append(CSS::to_string(m_value.outside_inside.inside));
  47. if (m_value.outside_inside.list_item == ListItem::Yes)
  48. parts.append("list-item"sv);
  49. builder.join(' ', parts);
  50. }
  51. break;
  52. case Type::Internal:
  53. builder.append(CSS::to_string(m_value.internal));
  54. break;
  55. case Type::Box:
  56. builder.append(CSS::to_string(m_value.box));
  57. break;
  58. };
  59. return MUST(builder.to_string());
  60. }
  61. }