CSSStyleDeclaration.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * Copyright (c) 2018-2022, 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/Document.h>
  9. #include <LibWeb/DOM/Element.h>
  10. #include <LibWeb/HTML/Window.h>
  11. namespace Web::CSS {
  12. CSSStyleDeclaration::CSSStyleDeclaration(HTML::Window& window_object)
  13. : PlatformObject(window_object.cached_web_prototype("CSSStyleDeclaration"))
  14. {
  15. }
  16. PropertyOwningCSSStyleDeclaration* PropertyOwningCSSStyleDeclaration::create(HTML::Window& window_object, Vector<StyleProperty> properties, HashMap<String, StyleProperty> custom_properties)
  17. {
  18. return window_object.heap().allocate<PropertyOwningCSSStyleDeclaration>(window_object.realm(), window_object, move(properties), move(custom_properties));
  19. }
  20. PropertyOwningCSSStyleDeclaration::PropertyOwningCSSStyleDeclaration(HTML::Window& window_object, Vector<StyleProperty> properties, HashMap<String, StyleProperty> custom_properties)
  21. : CSSStyleDeclaration(window_object)
  22. , m_properties(move(properties))
  23. , m_custom_properties(move(custom_properties))
  24. {
  25. }
  26. String PropertyOwningCSSStyleDeclaration::item(size_t index) const
  27. {
  28. if (index >= m_properties.size())
  29. return {};
  30. return CSS::string_from_property_id(m_properties[index].property_id);
  31. }
  32. ElementInlineCSSStyleDeclaration* ElementInlineCSSStyleDeclaration::create(DOM::Element& element, Vector<StyleProperty> properties, HashMap<String, StyleProperty> custom_properties)
  33. {
  34. auto& window_object = element.document().window();
  35. return window_object.heap().allocate<ElementInlineCSSStyleDeclaration>(window_object.realm(), element, move(properties), move(custom_properties));
  36. }
  37. ElementInlineCSSStyleDeclaration::ElementInlineCSSStyleDeclaration(DOM::Element& element, Vector<StyleProperty> properties, HashMap<String, StyleProperty> custom_properties)
  38. : PropertyOwningCSSStyleDeclaration(element.document().window(), move(properties), move(custom_properties))
  39. , m_element(element.make_weak_ptr<DOM::Element>())
  40. {
  41. }
  42. void ElementInlineCSSStyleDeclaration::visit_edges(Cell::Visitor& visitor)
  43. {
  44. Base::visit_edges(visitor);
  45. visitor.visit(m_element.ptr());
  46. }
  47. size_t PropertyOwningCSSStyleDeclaration::length() const
  48. {
  49. return m_properties.size();
  50. }
  51. Optional<StyleProperty> PropertyOwningCSSStyleDeclaration::property(PropertyID property_id) const
  52. {
  53. for (auto& property : m_properties) {
  54. if (property.property_id == property_id)
  55. return property;
  56. }
  57. return {};
  58. }
  59. // https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-setproperty
  60. WebIDL::ExceptionOr<void> PropertyOwningCSSStyleDeclaration::set_property(PropertyID property_id, StringView value, StringView priority)
  61. {
  62. // 1. If the computed flag is set, then throw a NoModificationAllowedError exception.
  63. // NOTE: This is handled by the virtual override in ResolvedCSSStyleDeclaration.
  64. // FIXME: 2. If property is not a custom property, follow these substeps:
  65. // FIXME: 1. Let property be property converted to ASCII lowercase.
  66. // FIXME: 2. If property is not a case-sensitive match for a supported CSS property, then return.
  67. // NOTE: This must be handled before we've turned the property string into a PropertyID.
  68. // 3. If value is the empty string, invoke removeProperty() with property as argument and return.
  69. if (value.is_empty()) {
  70. MUST(remove_property(property_id));
  71. return {};
  72. }
  73. // 4. If priority is not the empty string and is not an ASCII case-insensitive match for the string "important", then return.
  74. if (!priority.is_empty() && !priority.equals_ignoring_case("important"sv))
  75. return {};
  76. // 5. Let component value list be the result of parsing value for property property.
  77. auto component_value_list = parse_css_value(CSS::Parser::ParsingContext {}, value, property_id);
  78. // 6. If component value list is null, then return.
  79. if (!component_value_list)
  80. return {};
  81. // 7. Let updated be false.
  82. bool updated = false;
  83. // FIXME: 8. If property is a shorthand property, then for each longhand property longhand that property maps to, in canonical order, follow these substeps:
  84. // FIXME: 1. Let longhand result be the result of set the CSS declaration longhand with the appropriate value(s) from component value list,
  85. // with the important flag set if priority is not the empty string, and unset otherwise, and with the list of declarations being the declarations.
  86. // FIXME: 2. If longhand result is true, let updated be true.
  87. // 9. Otherwise, let updated be the result of set the CSS declaration property with value component value list,
  88. // with the important flag set if priority is not the empty string, and unset otherwise,
  89. // and with the list of declarations being the declarations.
  90. updated = set_a_css_declaration(property_id, component_value_list.release_nonnull(), !priority.is_empty() ? Important::Yes : Important::No);
  91. // 10. If updated is true, update style attribute for the CSS declaration block.
  92. if (updated)
  93. update_style_attribute();
  94. return {};
  95. }
  96. // https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-removeproperty
  97. WebIDL::ExceptionOr<String> PropertyOwningCSSStyleDeclaration::remove_property(PropertyID property_id)
  98. {
  99. // 1. If the computed flag is set, then throw a NoModificationAllowedError exception.
  100. // NOTE: This is handled by the virtual override in ResolvedCSSStyleDeclaration.
  101. // 2. If property is not a custom property, let property be property converted to ASCII lowercase.
  102. // NOTE: We've already converted it to a PropertyID enum value.
  103. // 3. Let value be the return value of invoking getPropertyValue() with property as argument.
  104. // FIXME: The trip through string_from_property_id() here is silly.
  105. auto value = get_property_value(string_from_property_id(property_id));
  106. // 4. Let removed be false.
  107. bool removed = false;
  108. // FIXME: 5. If property is a shorthand property, for each longhand property longhand that property maps to:
  109. // 1. If longhand is not a property name of a CSS declaration in the declarations, continue.
  110. // 2. Remove that CSS declaration and let removed be true.
  111. // 6. Otherwise, if property is a case-sensitive match for a property name of a CSS declaration in the declarations, remove that CSS declaration and let removed be true.
  112. removed = m_properties.remove_first_matching([&](auto& entry) { return entry.property_id == property_id; });
  113. // 7. If removed is true, Update style attribute for the CSS declaration block.
  114. if (removed)
  115. update_style_attribute();
  116. // 8. Return value.
  117. return value;
  118. }
  119. // https://drafts.csswg.org/cssom/#update-style-attribute-for
  120. void ElementInlineCSSStyleDeclaration::update_style_attribute()
  121. {
  122. // 1. Assert: declaration block’s computed flag is unset.
  123. // NOTE: Unnecessary, only relevant for ResolvedCSSStyleDeclaration.
  124. // 2. Let owner node be declaration block’s owner node.
  125. // 3. If owner node is null, then return.
  126. if (!m_element)
  127. return;
  128. // 4. Set declaration block’s updating flag.
  129. m_updating = true;
  130. // 5. Set an attribute value for owner node using "style" and the result of serializing declaration block.
  131. m_element->set_attribute(HTML::AttributeNames::style, serialized());
  132. // 6. Unset declaration block’s updating flag.
  133. m_updating = false;
  134. }
  135. // https://drafts.csswg.org/cssom/#set-a-css-declaration
  136. bool PropertyOwningCSSStyleDeclaration::set_a_css_declaration(PropertyID property_id, NonnullRefPtr<StyleValue> value, Important important)
  137. {
  138. // FIXME: Handle logical property groups.
  139. for (auto& property : m_properties) {
  140. if (property.property_id == property_id) {
  141. if (property.important == important && *property.value == *value)
  142. return false;
  143. property.value = move(value);
  144. property.important = important;
  145. return true;
  146. }
  147. }
  148. m_properties.append(CSS::StyleProperty {
  149. .important = important,
  150. .property_id = property_id,
  151. .value = move(value),
  152. });
  153. return true;
  154. }
  155. String CSSStyleDeclaration::get_property_value(StringView property_name) const
  156. {
  157. auto property_id = property_id_from_string(property_name);
  158. if (property_id == CSS::PropertyID::Invalid)
  159. return {};
  160. auto maybe_property = property(property_id);
  161. if (!maybe_property.has_value())
  162. return {};
  163. return maybe_property->value->to_string();
  164. }
  165. // https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-getpropertypriority
  166. String CSSStyleDeclaration::get_property_priority(StringView property_name) const
  167. {
  168. auto property_id = property_id_from_string(property_name);
  169. if (property_id == CSS::PropertyID::Invalid)
  170. return {};
  171. auto maybe_property = property(property_id);
  172. if (!maybe_property.has_value())
  173. return {};
  174. return maybe_property->important == Important::Yes ? "important" : "";
  175. }
  176. WebIDL::ExceptionOr<void> CSSStyleDeclaration::set_property(StringView property_name, StringView css_text, StringView priority)
  177. {
  178. auto property_id = property_id_from_string(property_name);
  179. if (property_id == CSS::PropertyID::Invalid)
  180. return {};
  181. return set_property(property_id, css_text, priority);
  182. }
  183. WebIDL::ExceptionOr<String> CSSStyleDeclaration::remove_property(StringView property_name)
  184. {
  185. auto property_id = property_id_from_string(property_name);
  186. if (property_id == CSS::PropertyID::Invalid)
  187. return String::empty();
  188. return remove_property(property_id);
  189. }
  190. String CSSStyleDeclaration::css_text() const
  191. {
  192. TODO();
  193. return "";
  194. }
  195. void CSSStyleDeclaration::set_css_text(StringView)
  196. {
  197. TODO();
  198. }
  199. // https://www.w3.org/TR/cssom/#serialize-a-css-declaration
  200. static String serialize_a_css_declaration(CSS::PropertyID property, String value, Important important)
  201. {
  202. StringBuilder builder;
  203. // 1. Let s be the empty string.
  204. // 2. Append property to s.
  205. builder.append(string_from_property_id(property));
  206. // 3. Append ": " (U+003A U+0020) to s.
  207. builder.append(": "sv);
  208. // 4. Append value to s.
  209. builder.append(value);
  210. // 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.
  211. if (important == Important::Yes)
  212. builder.append(" !important"sv);
  213. // 6. Append ";" (U+003B) to s.
  214. builder.append(';');
  215. // 7. Return s.
  216. return builder.to_string();
  217. }
  218. // https://www.w3.org/TR/cssom/#serialize-a-css-declaration-block
  219. String PropertyOwningCSSStyleDeclaration::serialized() const
  220. {
  221. // 1. Let list be an empty array.
  222. Vector<String> list;
  223. // 2. Let already serialized be an empty array.
  224. HashTable<PropertyID> already_serialized;
  225. // 3. Declaration loop: For each CSS declaration declaration in declaration block’s declarations, follow these substeps:
  226. for (auto& declaration : m_properties) {
  227. // 1. Let property be declaration’s property name.
  228. auto property = declaration.property_id;
  229. // 2. If property is in already serialized, continue with the steps labeled declaration loop.
  230. if (already_serialized.contains(property))
  231. continue;
  232. // FIXME: 3. If property maps to one or more shorthand properties, let shorthands be an array of those shorthand properties, in preferred order.
  233. // FIXME: 4. Shorthand loop: For each shorthand in shorthands, follow these substeps: ...
  234. // 5. Let value be the result of invoking serialize a CSS value of declaration.
  235. auto value = declaration.value->to_string();
  236. // 6. Let serialized declaration be the result of invoking serialize a CSS declaration with property name property, value value,
  237. // and the important flag set if declaration has its important flag set.
  238. auto serialized_declaration = serialize_a_css_declaration(property, move(value), declaration.important);
  239. // 7. Append serialized declaration to list.
  240. list.append(move(serialized_declaration));
  241. // 8. Append property to already serialized.
  242. already_serialized.set(property);
  243. }
  244. // 4. Return list joined with " " (U+0020).
  245. StringBuilder builder;
  246. builder.join(' ', list);
  247. return builder.to_string();
  248. }
  249. static CSS::PropertyID property_id_from_name(StringView name)
  250. {
  251. // FIXME: Perhaps this should go in the code generator.
  252. if (name == "cssFloat"sv)
  253. return CSS::PropertyID::Float;
  254. if (auto property_id = CSS::property_id_from_camel_case_string(name); property_id != CSS::PropertyID::Invalid)
  255. return property_id;
  256. if (auto property_id = CSS::property_id_from_string(name); property_id != CSS::PropertyID::Invalid)
  257. return property_id;
  258. return CSS::PropertyID::Invalid;
  259. }
  260. JS::ThrowCompletionOr<bool> CSSStyleDeclaration::internal_has_property(JS::PropertyKey const& name) const
  261. {
  262. if (!name.is_string())
  263. return Base::internal_has_property(name);
  264. return property_id_from_name(name.to_string()) != CSS::PropertyID::Invalid;
  265. }
  266. JS::ThrowCompletionOr<JS::Value> CSSStyleDeclaration::internal_get(JS::PropertyKey const& name, JS::Value receiver) const
  267. {
  268. if (!name.is_string())
  269. return Base::internal_get(name, receiver);
  270. auto property_id = property_id_from_name(name.to_string());
  271. if (property_id == CSS::PropertyID::Invalid)
  272. return Base::internal_get(name, receiver);
  273. if (auto maybe_property = property(property_id); maybe_property.has_value())
  274. return { js_string(vm(), maybe_property->value->to_string()) };
  275. return { js_string(vm(), String::empty()) };
  276. }
  277. JS::ThrowCompletionOr<bool> CSSStyleDeclaration::internal_set(JS::PropertyKey const& name, JS::Value value, JS::Value receiver)
  278. {
  279. if (!name.is_string())
  280. return Base::internal_set(name, value, receiver);
  281. auto property_id = property_id_from_name(name.to_string());
  282. if (property_id == CSS::PropertyID::Invalid)
  283. return Base::internal_set(name, value, receiver);
  284. auto css_text = TRY(value.to_string(vm()));
  285. set_property(property_id, css_text);
  286. return true;
  287. }
  288. }