CSSStyleDeclaration.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. // FIXME: What are we supposed to do if we can't parse it?
  56. if (!new_value)
  57. return false;
  58. ScopeGuard style_invalidation_guard = [&] {
  59. auto& declaration = verify_cast<CSS::ElementInlineCSSStyleDeclaration>(*this);
  60. if (auto* element = declaration.element())
  61. element->invalidate_style();
  62. };
  63. // FIXME: I don't think '!important' is being handled correctly here..
  64. for (auto& property : m_properties) {
  65. if (property.property_id == property_id) {
  66. property.value = new_value.release_nonnull();
  67. return true;
  68. }
  69. }
  70. m_properties.append(CSS::StyleProperty {
  71. .property_id = property_id,
  72. .value = new_value.release_nonnull(),
  73. .important = false,
  74. });
  75. return true;
  76. }
  77. String CSSStyleDeclaration::get_property_value(StringView property_name) const
  78. {
  79. auto property_id = property_id_from_string(property_name);
  80. if (property_id == CSS::PropertyID::Invalid)
  81. return {};
  82. auto maybe_property = property(property_id);
  83. if (!maybe_property.has_value())
  84. return {};
  85. return maybe_property->value->to_string();
  86. }
  87. }