CSSStyleDeclaration.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. .important = false,
  75. .property_id = property_id,
  76. .value = new_value.release_nonnull(),
  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. String CSSStyleDeclaration::css_text() const
  98. {
  99. TODO();
  100. return "";
  101. }
  102. void CSSStyleDeclaration::set_css_text(StringView)
  103. {
  104. TODO();
  105. }
  106. // https://www.w3.org/TR/cssom/#serialize-a-css-declaration
  107. static String serialize_a_css_declaration(CSS::PropertyID property, String value, bool important)
  108. {
  109. StringBuilder builder;
  110. // 1. Let s be the empty string.
  111. // 2. Append property to s.
  112. builder.append(string_from_property_id(property));
  113. // 3. Append ": " (U+003A U+0020) to s.
  114. builder.append(": "sv);
  115. // 4. Append value to s.
  116. builder.append(value);
  117. // 5. If the important flag is set, append " !important" (U+0020 U+0021 U+0069 U+006D U+0070 U+006F U+0072 U+0074 U+0061 U+006E U+0074) to s.
  118. if (important)
  119. builder.append(" !important"sv);
  120. // 6. Append ";" (U+003B) to s.
  121. builder.append(';');
  122. // 7. Return s.
  123. return builder.to_string();
  124. }
  125. // https://www.w3.org/TR/cssom/#serialize-a-css-declaration-block
  126. String PropertyOwningCSSStyleDeclaration::serialized() const
  127. {
  128. // 1. Let list be an empty array.
  129. Vector<String> list;
  130. // 2. Let already serialized be an empty array.
  131. HashTable<PropertyID> already_serialized;
  132. // 3. Declaration loop: For each CSS declaration declaration in declaration block’s declarations, follow these substeps:
  133. for (auto& declaration : m_properties) {
  134. // 1. Let property be declaration’s property name.
  135. auto property = declaration.property_id;
  136. // 2. If property is in already serialized, continue with the steps labeled declaration loop.
  137. if (already_serialized.contains(property))
  138. continue;
  139. // FIXME: 3. If property maps to one or more shorthand properties, let shorthands be an array of those shorthand properties, in preferred order.
  140. // FIXME: 4. Shorthand loop: For each shorthand in shorthands, follow these substeps: ...
  141. // 5. Let value be the result of invoking serialize a CSS value of declaration.
  142. auto value = declaration.value->to_string();
  143. // 6. Let serialized declaration be the result of invoking serialize a CSS declaration with property name property, value value,
  144. // and the important flag set if declaration has its important flag set.
  145. auto serialized_declaration = serialize_a_css_declaration(property, move(value), declaration.important);
  146. // 7. Append serialized declaration to list.
  147. list.append(move(serialized_declaration));
  148. // 8. Append property to already serialized.
  149. already_serialized.set(property);
  150. }
  151. // 4. Return list joined with " " (U+0020).
  152. StringBuilder builder;
  153. builder.join(' ', list);
  154. return builder.to_string();
  155. }
  156. }