ComputedCSSStyleDeclaration.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/CSS/ComputedCSSStyleDeclaration.h>
  7. #include <LibWeb/CSS/StyleResolver.h>
  8. #include <LibWeb/DOM/Document.h>
  9. #include <LibWeb/DOM/Element.h>
  10. namespace Web::CSS {
  11. ComputedCSSStyleDeclaration::ComputedCSSStyleDeclaration(DOM::Element& element)
  12. : m_element(element)
  13. {
  14. }
  15. ComputedCSSStyleDeclaration::~ComputedCSSStyleDeclaration()
  16. {
  17. }
  18. size_t ComputedCSSStyleDeclaration::length() const
  19. {
  20. return 0;
  21. }
  22. String ComputedCSSStyleDeclaration::item(size_t index) const
  23. {
  24. (void)index;
  25. return {};
  26. }
  27. static CSS::ValueID to_css_value_id(CSS::Display value)
  28. {
  29. switch (value) {
  30. case CSS::Display::None:
  31. return CSS::ValueID::None;
  32. case CSS::Display::Block:
  33. return CSS::ValueID::Block;
  34. case CSS::Display::Inline:
  35. return CSS::ValueID::Inline;
  36. case CSS::Display::InlineBlock:
  37. return CSS::ValueID::InlineBlock;
  38. case CSS::Display::ListItem:
  39. return CSS::ValueID::ListItem;
  40. case CSS::Display::Table:
  41. return CSS::ValueID::Table;
  42. case CSS::Display::TableRow:
  43. return CSS::ValueID::TableRow;
  44. case CSS::Display::TableCell:
  45. return CSS::ValueID::TableCell;
  46. case CSS::Display::TableHeaderGroup:
  47. return CSS::ValueID::TableHeaderGroup;
  48. case CSS::Display::TableRowGroup:
  49. return CSS::ValueID::TableRowGroup;
  50. case CSS::Display::TableFooterGroup:
  51. return CSS::ValueID::TableFooterGroup;
  52. case CSS::Display::TableColumn:
  53. return CSS::ValueID::TableColumn;
  54. case CSS::Display::TableColumnGroup:
  55. return CSS::ValueID::TableColumnGroup;
  56. case CSS::Display::TableCaption:
  57. return CSS::ValueID::TableCaption;
  58. case CSS::Display::Flex:
  59. return CSS::ValueID::Flex;
  60. }
  61. VERIFY_NOT_REACHED();
  62. }
  63. Optional<StyleProperty> ComputedCSSStyleDeclaration::property(PropertyID property_id) const
  64. {
  65. const_cast<DOM::Document&>(m_element->document()).force_layout();
  66. if (!m_element->layout_node()) {
  67. auto style = m_element->document().style_resolver().resolve_style(const_cast<DOM::Element&>(*m_element));
  68. if (auto maybe_property = style->property(property_id); maybe_property.has_value()) {
  69. return StyleProperty {
  70. .property_id = property_id,
  71. .value = maybe_property.release_value(),
  72. };
  73. }
  74. return {};
  75. }
  76. auto& layout_node = *m_element->layout_node();
  77. switch (property_id) {
  78. case CSS::PropertyID::Color:
  79. return StyleProperty {
  80. .property_id = property_id,
  81. .value = ColorStyleValue::create(layout_node.computed_values().color()),
  82. };
  83. case CSS::PropertyID::Display: {
  84. return StyleProperty {
  85. .property_id = property_id,
  86. .value = IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().display())),
  87. };
  88. }
  89. default:
  90. dbgln("FIXME: Computed style for the '{}' property was requested", string_from_property_id(property_id));
  91. return {};
  92. }
  93. }
  94. bool ComputedCSSStyleDeclaration::set_property(PropertyID, StringView)
  95. {
  96. return false;
  97. }
  98. }