CSSStyleDeclaration.cpp 5.7 KB

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