CSSStyleDeclaration.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/CSS/CSSStyleDeclaration.h>
  7. #include <LibWeb/CSS/Parser/Parser.h>
  8. #include <LibWeb/DOM/Element.h>
  9. namespace Web::CSS {
  10. PropertyOwningCSSStyleDeclaration::PropertyOwningCSSStyleDeclaration(Vector<StyleProperty> properties, HashMap<String, StyleProperty> custom_properties)
  11. : m_properties(move(properties))
  12. , m_custom_properties(move(custom_properties))
  13. {
  14. }
  15. PropertyOwningCSSStyleDeclaration::~PropertyOwningCSSStyleDeclaration()
  16. {
  17. }
  18. CSSStyleDeclaration::~CSSStyleDeclaration()
  19. {
  20. }
  21. String PropertyOwningCSSStyleDeclaration::item(size_t index) const
  22. {
  23. if (index >= m_properties.size())
  24. return {};
  25. return CSS::string_from_property_id(m_properties[index].property_id);
  26. }
  27. ElementInlineCSSStyleDeclaration::ElementInlineCSSStyleDeclaration(DOM::Element& element)
  28. : PropertyOwningCSSStyleDeclaration({}, {})
  29. , m_element(element.make_weak_ptr<DOM::Element>())
  30. {
  31. }
  32. ElementInlineCSSStyleDeclaration::ElementInlineCSSStyleDeclaration(DOM::Element& element, PropertyOwningCSSStyleDeclaration& declaration)
  33. : PropertyOwningCSSStyleDeclaration(move(declaration.m_properties), move(declaration.m_custom_properties))
  34. , m_element(element.make_weak_ptr<DOM::Element>())
  35. {
  36. }
  37. ElementInlineCSSStyleDeclaration::~ElementInlineCSSStyleDeclaration()
  38. {
  39. }
  40. size_t PropertyOwningCSSStyleDeclaration::length() const
  41. {
  42. return m_properties.size();
  43. }
  44. Optional<StyleProperty> PropertyOwningCSSStyleDeclaration::property(PropertyID property_id) const
  45. {
  46. for (auto& property : m_properties) {
  47. if (property.property_id == property_id)
  48. return property;
  49. }
  50. return {};
  51. }
  52. bool PropertyOwningCSSStyleDeclaration::set_property(PropertyID property_id, StringView css_text)
  53. {
  54. auto new_value = parse_css_value(CSS::ParsingContext {}, css_text, property_id);
  55. if (!new_value) {
  56. m_properties.remove_all_matching([&](auto& entry) {
  57. return entry.property_id == property_id;
  58. });
  59. return false;
  60. }
  61. ScopeGuard style_invalidation_guard = [&] {
  62. auto& declaration = verify_cast<CSS::ElementInlineCSSStyleDeclaration>(*this);
  63. if (auto* element = declaration.element())
  64. element->invalidate_style();
  65. };
  66. // FIXME: I don't think '!important' is being handled correctly here..
  67. for (auto& property : m_properties) {
  68. if (property.property_id == property_id) {
  69. property.value = new_value.release_nonnull();
  70. return true;
  71. }
  72. }
  73. m_properties.append(CSS::StyleProperty {
  74. .property_id = property_id,
  75. .value = new_value.release_nonnull(),
  76. .important = false,
  77. });
  78. return true;
  79. }
  80. String CSSStyleDeclaration::get_property_value(StringView property_name) const
  81. {
  82. auto property_id = property_id_from_string(property_name);
  83. if (property_id == CSS::PropertyID::Invalid)
  84. return {};
  85. auto maybe_property = property(property_id);
  86. if (!maybe_property.has_value())
  87. return {};
  88. return maybe_property->value->to_string();
  89. }
  90. void CSSStyleDeclaration::set_property(StringView property_name, StringView css_text)
  91. {
  92. auto property_id = property_id_from_string(property_name);
  93. if (property_id == CSS::PropertyID::Invalid)
  94. return;
  95. set_property(property_id, css_text);
  96. }
  97. }