CSSStyleDeclaration.cpp 14 KB

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