CSSStyleDeclaration.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. String PropertyOwningCSSStyleDeclaration::item(size_t index) const
  16. {
  17. if (index >= m_properties.size())
  18. return {};
  19. return CSS::string_from_property_id(m_properties[index].property_id);
  20. }
  21. ElementInlineCSSStyleDeclaration::ElementInlineCSSStyleDeclaration(DOM::Element& element)
  22. : PropertyOwningCSSStyleDeclaration({}, {})
  23. , m_element(element.make_weak_ptr<DOM::Element>())
  24. {
  25. }
  26. ElementInlineCSSStyleDeclaration::ElementInlineCSSStyleDeclaration(DOM::Element& element, PropertyOwningCSSStyleDeclaration& declaration)
  27. : PropertyOwningCSSStyleDeclaration(move(declaration.m_properties), move(declaration.m_custom_properties))
  28. , m_element(element.make_weak_ptr<DOM::Element>())
  29. {
  30. }
  31. size_t PropertyOwningCSSStyleDeclaration::length() const
  32. {
  33. return m_properties.size();
  34. }
  35. Optional<StyleProperty> PropertyOwningCSSStyleDeclaration::property(PropertyID property_id) const
  36. {
  37. for (auto& property : m_properties) {
  38. if (property.property_id == property_id)
  39. return property;
  40. }
  41. return {};
  42. }
  43. bool PropertyOwningCSSStyleDeclaration::set_property(PropertyID property_id, StringView css_text)
  44. {
  45. auto new_value = parse_css_value(CSS::ParsingContext {}, css_text, property_id);
  46. if (!new_value) {
  47. m_properties.remove_all_matching([&](auto& entry) {
  48. return entry.property_id == property_id;
  49. });
  50. return false;
  51. }
  52. ScopeGuard style_invalidation_guard = [&] {
  53. auto& declaration = verify_cast<CSS::ElementInlineCSSStyleDeclaration>(*this);
  54. if (auto* element = declaration.element())
  55. element->invalidate_style();
  56. };
  57. // FIXME: I don't think '!important' is being handled correctly here..
  58. for (auto& property : m_properties) {
  59. if (property.property_id == property_id) {
  60. property.value = new_value.release_nonnull();
  61. return true;
  62. }
  63. }
  64. m_properties.append(CSS::StyleProperty {
  65. .important = Important::No,
  66. .property_id = property_id,
  67. .value = new_value.release_nonnull(),
  68. });
  69. return true;
  70. }
  71. String CSSStyleDeclaration::get_property_value(StringView property_name) const
  72. {
  73. auto property_id = property_id_from_string(property_name);
  74. if (property_id == CSS::PropertyID::Invalid)
  75. return {};
  76. auto maybe_property = property(property_id);
  77. if (!maybe_property.has_value())
  78. return {};
  79. return maybe_property->value->to_string();
  80. }
  81. void CSSStyleDeclaration::set_property(StringView property_name, StringView css_text)
  82. {
  83. auto property_id = property_id_from_string(property_name);
  84. if (property_id == CSS::PropertyID::Invalid)
  85. return;
  86. set_property(property_id, css_text);
  87. }
  88. String CSSStyleDeclaration::css_text() const
  89. {
  90. TODO();
  91. return "";
  92. }
  93. void CSSStyleDeclaration::set_css_text(StringView)
  94. {
  95. TODO();
  96. }
  97. // https://www.w3.org/TR/cssom/#serialize-a-css-declaration
  98. static String serialize_a_css_declaration(CSS::PropertyID property, String value, Important important)
  99. {
  100. StringBuilder builder;
  101. // 1. Let s be the empty string.
  102. // 2. Append property to s.
  103. builder.append(string_from_property_id(property));
  104. // 3. Append ": " (U+003A U+0020) to s.
  105. builder.append(": "sv);
  106. // 4. Append value to s.
  107. builder.append(value);
  108. // 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.
  109. if (important == Important::Yes)
  110. builder.append(" !important"sv);
  111. // 6. Append ";" (U+003B) to s.
  112. builder.append(';');
  113. // 7. Return s.
  114. return builder.to_string();
  115. }
  116. // https://www.w3.org/TR/cssom/#serialize-a-css-declaration-block
  117. String PropertyOwningCSSStyleDeclaration::serialized() const
  118. {
  119. // 1. Let list be an empty array.
  120. Vector<String> list;
  121. // 2. Let already serialized be an empty array.
  122. HashTable<PropertyID> already_serialized;
  123. // 3. Declaration loop: For each CSS declaration declaration in declaration block’s declarations, follow these substeps:
  124. for (auto& declaration : m_properties) {
  125. // 1. Let property be declaration’s property name.
  126. auto property = declaration.property_id;
  127. // 2. If property is in already serialized, continue with the steps labeled declaration loop.
  128. if (already_serialized.contains(property))
  129. continue;
  130. // FIXME: 3. If property maps to one or more shorthand properties, let shorthands be an array of those shorthand properties, in preferred order.
  131. // FIXME: 4. Shorthand loop: For each shorthand in shorthands, follow these substeps: ...
  132. // 5. Let value be the result of invoking serialize a CSS value of declaration.
  133. auto value = declaration.value->to_string();
  134. // 6. Let serialized declaration be the result of invoking serialize a CSS declaration with property name property, value value,
  135. // and the important flag set if declaration has its important flag set.
  136. auto serialized_declaration = serialize_a_css_declaration(property, move(value), declaration.important);
  137. // 7. Append serialized declaration to list.
  138. list.append(move(serialized_declaration));
  139. // 8. Append property to already serialized.
  140. already_serialized.set(property);
  141. }
  142. // 4. Return list joined with " " (U+0020).
  143. StringBuilder builder;
  144. builder.join(' ', list);
  145. return builder.to_string();
  146. }
  147. }