CSSStyleDeclaration.cpp 17 KB

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