CSSStyleDeclaration.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /*
  2. * Copyright (c) 2018-2024, Andreas Kling <andreas@ladybird.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/CSS/StyleComputer.h>
  12. #include <LibWeb/CSS/StyleValues/ImageStyleValue.h>
  13. #include <LibWeb/DOM/Document.h>
  14. #include <LibWeb/DOM/Element.h>
  15. #include <LibWeb/Infra/Strings.h>
  16. namespace Web::CSS {
  17. GC_DEFINE_ALLOCATOR(CSSStyleDeclaration);
  18. GC_DEFINE_ALLOCATOR(PropertyOwningCSSStyleDeclaration);
  19. GC_DEFINE_ALLOCATOR(ElementInlineCSSStyleDeclaration);
  20. CSSStyleDeclaration::CSSStyleDeclaration(JS::Realm& realm)
  21. : PlatformObject(realm)
  22. {
  23. m_legacy_platform_object_flags = LegacyPlatformObjectFlags {
  24. .supports_indexed_properties = true,
  25. };
  26. }
  27. void CSSStyleDeclaration::initialize(JS::Realm& realm)
  28. {
  29. Base::initialize(realm);
  30. WEB_SET_PROTOTYPE_FOR_INTERFACE(CSSStyleDeclaration);
  31. }
  32. GC::Ptr<CSSRule> CSSStyleDeclaration::parent_rule() const
  33. {
  34. return nullptr;
  35. }
  36. GC::Ref<PropertyOwningCSSStyleDeclaration> PropertyOwningCSSStyleDeclaration::create(JS::Realm& realm, Vector<StyleProperty> properties, HashMap<FlyString, StyleProperty> custom_properties)
  37. {
  38. return realm.create<PropertyOwningCSSStyleDeclaration>(realm, move(properties), move(custom_properties));
  39. }
  40. PropertyOwningCSSStyleDeclaration::PropertyOwningCSSStyleDeclaration(JS::Realm& realm, Vector<StyleProperty> properties, HashMap<FlyString, StyleProperty> custom_properties)
  41. : CSSStyleDeclaration(realm)
  42. , m_properties(move(properties))
  43. , m_custom_properties(move(custom_properties))
  44. {
  45. }
  46. void PropertyOwningCSSStyleDeclaration::visit_edges(Cell::Visitor& visitor)
  47. {
  48. Base::visit_edges(visitor);
  49. visitor.visit(m_parent_rule);
  50. for (auto& property : m_properties) {
  51. if (property.value->is_image())
  52. property.value->as_image().visit_edges(visitor);
  53. }
  54. }
  55. String PropertyOwningCSSStyleDeclaration::item(size_t index) const
  56. {
  57. if (index >= m_properties.size())
  58. return {};
  59. return CSS::string_from_property_id(m_properties[index].property_id).to_string();
  60. }
  61. GC::Ref<ElementInlineCSSStyleDeclaration> ElementInlineCSSStyleDeclaration::create(DOM::Element& element, Vector<StyleProperty> properties, HashMap<FlyString, StyleProperty> custom_properties)
  62. {
  63. auto& realm = element.realm();
  64. return realm.create<ElementInlineCSSStyleDeclaration>(element, move(properties), move(custom_properties));
  65. }
  66. ElementInlineCSSStyleDeclaration::ElementInlineCSSStyleDeclaration(DOM::Element& element, Vector<StyleProperty> properties, HashMap<FlyString, StyleProperty> custom_properties)
  67. : PropertyOwningCSSStyleDeclaration(element.realm(), move(properties), move(custom_properties))
  68. , m_element(element.make_weak_ptr<DOM::Element>())
  69. {
  70. }
  71. void ElementInlineCSSStyleDeclaration::visit_edges(Cell::Visitor& visitor)
  72. {
  73. Base::visit_edges(visitor);
  74. visitor.visit(m_element);
  75. }
  76. size_t PropertyOwningCSSStyleDeclaration::length() const
  77. {
  78. return m_properties.size();
  79. }
  80. Optional<StyleProperty> PropertyOwningCSSStyleDeclaration::property(PropertyID property_id) const
  81. {
  82. for (auto& property : m_properties) {
  83. if (property.property_id == property_id)
  84. return property;
  85. }
  86. return {};
  87. }
  88. // https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-setproperty
  89. WebIDL::ExceptionOr<void> PropertyOwningCSSStyleDeclaration::set_property(StringView property_name, StringView value, StringView priority)
  90. {
  91. auto maybe_property_id = property_id_from_string(property_name);
  92. if (!maybe_property_id.has_value())
  93. return {};
  94. auto property_id = maybe_property_id.value();
  95. // 1. If the computed flag is set, then throw a NoModificationAllowedError exception.
  96. // NOTE: This is handled by the virtual override in ResolvedCSSStyleDeclaration.
  97. // FIXME: 2. If property is not a custom property, follow these substeps:
  98. // FIXME: 1. Let property be property converted to ASCII lowercase.
  99. // FIXME: 2. If property is not a case-sensitive match for a supported CSS property, then return.
  100. // NOTE: This must be handled before we've turned the property string into a PropertyID.
  101. // 3. If value is the empty string, invoke removeProperty() with property as argument and return.
  102. if (value.is_empty()) {
  103. MUST(remove_property(property_name));
  104. return {};
  105. }
  106. // 4. If priority is not the empty string and is not an ASCII case-insensitive match for the string "important", then return.
  107. if (!priority.is_empty() && !Infra::is_ascii_case_insensitive_match(priority, "important"sv))
  108. return {};
  109. // 5. Let component value list be the result of parsing value for property property.
  110. auto component_value_list = is<ElementInlineCSSStyleDeclaration>(this)
  111. ? parse_css_value(CSS::Parser::ParsingContext { static_cast<ElementInlineCSSStyleDeclaration&>(*this).element()->document() }, value, property_id)
  112. : parse_css_value(CSS::Parser::ParsingContext { realm() }, value, property_id);
  113. // 6. If component value list is null, then return.
  114. if (!component_value_list)
  115. return {};
  116. // 7. Let updated be false.
  117. bool updated = false;
  118. // 8. If property is a shorthand property,
  119. if (property_is_shorthand(property_id)) {
  120. // then for each longhand property longhand that property maps to, in canonical order, follow these substeps:
  121. StyleComputer::for_each_property_expanding_shorthands(property_id, *component_value_list, StyleComputer::AllowUnresolved::Yes, [this, &updated, priority](PropertyID longhand_property_id, CSSStyleValue const& longhand_value) {
  122. // 1. Let longhand result be the result of set the CSS declaration longhand with the appropriate value(s) from component value list,
  123. // with the important flag set if priority is not the empty string, and unset otherwise, and with the list of declarations being the declarations.
  124. // 2. If longhand result is true, let updated be true.
  125. updated |= set_a_css_declaration(longhand_property_id, longhand_value, !priority.is_empty() ? Important::Yes : Important::No);
  126. });
  127. }
  128. // 9. Otherwise,
  129. else {
  130. if (property_id == PropertyID::Custom) {
  131. auto custom_name = FlyString::from_utf8_without_validation(property_name.bytes());
  132. StyleProperty style_property {
  133. .important = !priority.is_empty() ? Important::Yes : Important::No,
  134. .property_id = property_id,
  135. .value = component_value_list.release_nonnull(),
  136. .custom_name = custom_name,
  137. };
  138. m_custom_properties.set(custom_name, style_property);
  139. updated = true;
  140. } else {
  141. // let updated be the result of set the CSS declaration property with value component value list,
  142. // with the important flag set if priority is not the empty string, and unset otherwise,
  143. // and with the list of declarations being the declarations.
  144. updated = set_a_css_declaration(property_id, *component_value_list, !priority.is_empty() ? Important::Yes : Important::No);
  145. }
  146. }
  147. // 10. If updated is true, update style attribute for the CSS declaration block.
  148. if (updated)
  149. update_style_attribute();
  150. return {};
  151. }
  152. // https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-removeproperty
  153. WebIDL::ExceptionOr<String> PropertyOwningCSSStyleDeclaration::remove_property(StringView property_name)
  154. {
  155. auto property_id = property_id_from_string(property_name);
  156. if (!property_id.has_value())
  157. return String {};
  158. // 1. If the computed flag is set, then throw a NoModificationAllowedError exception.
  159. // NOTE: This is handled by the virtual override in ResolvedCSSStyleDeclaration.
  160. // 2. If property is not a custom property, let property be property converted to ASCII lowercase.
  161. // NOTE: We've already converted it to a PropertyID enum value.
  162. // 3. Let value be the return value of invoking getPropertyValue() with property as argument.
  163. auto value = get_property_value(property_name);
  164. // 4. Let removed be false.
  165. bool removed = false;
  166. // FIXME: 5. If property is a shorthand property, for each longhand property longhand that property maps to:
  167. // 1. If longhand is not a property name of a CSS declaration in the declarations, continue.
  168. // 2. Remove that CSS declaration and let removed be true.
  169. // 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.
  170. if (property_id == PropertyID::Custom) {
  171. auto custom_name = FlyString::from_utf8_without_validation(property_name.bytes());
  172. removed = m_custom_properties.remove(custom_name);
  173. } else {
  174. removed = m_properties.remove_first_matching([&](auto& entry) { return entry.property_id == property_id; });
  175. }
  176. // 7. If removed is true, Update style attribute for the CSS declaration block.
  177. if (removed)
  178. update_style_attribute();
  179. // 8. Return value.
  180. return value;
  181. }
  182. // https://drafts.csswg.org/cssom/#update-style-attribute-for
  183. void ElementInlineCSSStyleDeclaration::update_style_attribute()
  184. {
  185. // 1. Assert: declaration block’s computed flag is unset.
  186. // NOTE: Unnecessary, only relevant for ResolvedCSSStyleDeclaration.
  187. // 2. Let owner node be declaration block’s owner node.
  188. // 3. If owner node is null, then return.
  189. if (!m_element)
  190. return;
  191. // 4. Set declaration block’s updating flag.
  192. m_updating = true;
  193. // 5. Set an attribute value for owner node using "style" and the result of serializing declaration block.
  194. MUST(m_element->set_attribute(HTML::AttributeNames::style, serialized()));
  195. // 6. Unset declaration block’s updating flag.
  196. m_updating = false;
  197. }
  198. // https://drafts.csswg.org/cssom/#set-a-css-declaration
  199. bool PropertyOwningCSSStyleDeclaration::set_a_css_declaration(PropertyID property_id, NonnullRefPtr<CSSStyleValue const> value, Important important)
  200. {
  201. // FIXME: Handle logical property groups.
  202. for (auto& property : m_properties) {
  203. if (property.property_id == property_id) {
  204. if (property.important == important && *property.value == *value)
  205. return false;
  206. property.value = move(value);
  207. property.important = important;
  208. return true;
  209. }
  210. }
  211. m_properties.append(CSS::StyleProperty {
  212. .important = important,
  213. .property_id = property_id,
  214. .value = move(value),
  215. });
  216. return true;
  217. }
  218. // https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-getpropertyvalue
  219. String CSSStyleDeclaration::get_property_value(StringView property_name) const
  220. {
  221. auto property_id = property_id_from_string(property_name);
  222. if (!property_id.has_value())
  223. return {};
  224. if (property_id.value() == PropertyID::Custom) {
  225. auto maybe_custom_property = custom_property(FlyString::from_utf8_without_validation(property_name.bytes()));
  226. if (maybe_custom_property.has_value()) {
  227. return maybe_custom_property.value().value->to_string();
  228. }
  229. return {};
  230. }
  231. // 2. If property is a shorthand property, then follow these substeps:
  232. if (property_is_shorthand(property_id.value())) {
  233. // 1. Let list be a new empty array.
  234. StringBuilder list;
  235. Optional<Important> last_important_flag;
  236. // 2. For each longhand property longhand that property maps to, in canonical order, follow these substeps:
  237. for (auto longhand_property_id : longhands_for_shorthand(property_id.value())) {
  238. // 1. If longhand is a case-sensitive match for a property name of a CSS declaration in the declarations, let declaration be that CSS declaration, or null otherwise.
  239. auto declaration = property(longhand_property_id);
  240. // 2. If declaration is null, then return the empty string.
  241. if (!declaration.has_value())
  242. return {};
  243. // 3. Append the declaration to list.
  244. if (!list.is_empty())
  245. list.append(' ');
  246. list.append(declaration->value->to_string());
  247. if (last_important_flag.has_value() && declaration->important != *last_important_flag)
  248. return {};
  249. last_important_flag = declaration->important;
  250. }
  251. // 3. If important flags of all declarations in list are same, then return the serialization of list.
  252. return list.to_string_without_validation();
  253. // 4. Return the empty string.
  254. // NOTE: This is handled by the loop.
  255. }
  256. auto maybe_property = property(property_id.value());
  257. if (!maybe_property.has_value())
  258. return {};
  259. return maybe_property->value->to_string();
  260. }
  261. // https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-getpropertypriority
  262. StringView CSSStyleDeclaration::get_property_priority(StringView property_name) const
  263. {
  264. auto property_id = property_id_from_string(property_name);
  265. if (!property_id.has_value())
  266. return {};
  267. if (property_id.value() == PropertyID::Custom) {
  268. auto maybe_custom_property = custom_property(FlyString::from_utf8_without_validation(property_name.bytes()));
  269. if (!maybe_custom_property.has_value())
  270. return {};
  271. return maybe_custom_property.value().important == Important::Yes ? "important"sv : ""sv;
  272. }
  273. auto maybe_property = property(property_id.value());
  274. if (!maybe_property.has_value())
  275. return {};
  276. return maybe_property->important == Important::Yes ? "important"sv : ""sv;
  277. }
  278. WebIDL::ExceptionOr<void> CSSStyleDeclaration::set_property(PropertyID property_id, StringView css_text, StringView priority)
  279. {
  280. return set_property(string_from_property_id(property_id), css_text, priority);
  281. }
  282. WebIDL::ExceptionOr<String> CSSStyleDeclaration::remove_property(PropertyID property_name)
  283. {
  284. return remove_property(string_from_property_id(property_name));
  285. }
  286. // https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-csstext
  287. String CSSStyleDeclaration::css_text() const
  288. {
  289. // 1. If the computed flag is set, then return the empty string.
  290. // NOTE: See ResolvedCSSStyleDeclaration::serialized()
  291. // 2. Return the result of serializing the declarations.
  292. return serialized();
  293. }
  294. // https://drafts.csswg.org/cssom/#dom-cssstyleproperties-cssfloat
  295. String CSSStyleDeclaration::css_float() const
  296. {
  297. // The cssFloat attribute, on getting, must return the result of invoking getPropertyValue() with float as argument.
  298. return get_property_value("float"sv);
  299. }
  300. WebIDL::ExceptionOr<void> CSSStyleDeclaration::set_css_float(StringView value)
  301. {
  302. // On setting, the attribute must invoke setProperty() with float as first argument, as second argument the given value,
  303. // and no third argument. Any exceptions thrown must be re-thrown.
  304. return set_property("float"sv, value, ""sv);
  305. }
  306. Optional<JS::Value> CSSStyleDeclaration::item_value(size_t index) const
  307. {
  308. auto value = item(index);
  309. if (value.is_empty())
  310. return {};
  311. return JS::PrimitiveString::create(vm(), value);
  312. }
  313. // https://www.w3.org/TR/cssom/#serialize-a-css-declaration
  314. static String serialize_a_css_declaration(CSS::PropertyID property, StringView value, Important important)
  315. {
  316. StringBuilder builder;
  317. // 1. Let s be the empty string.
  318. // 2. Append property to s.
  319. builder.append(string_from_property_id(property));
  320. // 3. Append ": " (U+003A U+0020) to s.
  321. builder.append(": "sv);
  322. // 4. Append value to s.
  323. builder.append(value);
  324. // 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.
  325. if (important == Important::Yes)
  326. builder.append(" !important"sv);
  327. // 6. Append ";" (U+003B) to s.
  328. builder.append(';');
  329. // 7. Return s.
  330. return MUST(builder.to_string());
  331. }
  332. // https://www.w3.org/TR/cssom/#serialize-a-css-declaration-block
  333. String PropertyOwningCSSStyleDeclaration::serialized() const
  334. {
  335. // 1. Let list be an empty array.
  336. Vector<String> list;
  337. // 2. Let already serialized be an empty array.
  338. HashTable<PropertyID> already_serialized;
  339. // NOTE: The spec treats custom properties the same as any other property, and expects the above loop to handle them.
  340. // However, our implementation separates them from regular properties, so we need to handle them separately here.
  341. // FIXME: Is the relative order of custom properties and regular properties supposed to be preserved?
  342. for (auto& declaration : m_custom_properties) {
  343. // 1. Let property be declaration’s property name.
  344. auto const& property = declaration.key;
  345. // 2. If property is in already serialized, continue with the steps labeled declaration loop.
  346. // NOTE: It is never in already serialized, as there are no shorthands for custom properties.
  347. // 3. If property maps to one or more shorthand properties, let shorthands be an array of those shorthand properties, in preferred order.
  348. // NOTE: There are no shorthands for custom properties.
  349. // 4. Shorthand loop: For each shorthand in shorthands, follow these substeps: ...
  350. // NOTE: There are no shorthands for custom properties.
  351. // 5. Let value be the result of invoking serialize a CSS value of declaration.
  352. auto value = declaration.value.value->to_string();
  353. // 6. Let serialized declaration be the result of invoking serialize a CSS declaration with property name property, value value,
  354. // and the important flag set if declaration has its important flag set.
  355. // NOTE: We have to inline this here as the actual implementation does not accept custom properties.
  356. String serialized_declaration = [&] {
  357. // https://www.w3.org/TR/cssom/#serialize-a-css-declaration
  358. StringBuilder builder;
  359. // 1. Let s be the empty string.
  360. // 2. Append property to s.
  361. builder.append(property);
  362. // 3. Append ": " (U+003A U+0020) to s.
  363. builder.append(": "sv);
  364. // 4. Append value to s.
  365. builder.append(value);
  366. // 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.
  367. if (declaration.value.important == Important::Yes)
  368. builder.append(" !important"sv);
  369. // 6. Append ";" (U+003B) to s.
  370. builder.append(';');
  371. // 7. Return s.
  372. return MUST(builder.to_string());
  373. }();
  374. // 7. Append serialized declaration to list.
  375. list.append(move(serialized_declaration));
  376. // 8. Append property to already serialized.
  377. // NOTE: We don't need to do this, as we don't have shorthands for custom properties.
  378. }
  379. // 3. Declaration loop: For each CSS declaration declaration in declaration block’s declarations, follow these substeps:
  380. for (auto& declaration : m_properties) {
  381. // 1. Let property be declaration’s property name.
  382. auto property = declaration.property_id;
  383. // 2. If property is in already serialized, continue with the steps labeled declaration loop.
  384. if (already_serialized.contains(property))
  385. continue;
  386. // FIXME: 3. If property maps to one or more shorthand properties, let shorthands be an array of those shorthand properties, in preferred order.
  387. // FIXME: 4. Shorthand loop: For each shorthand in shorthands, follow these substeps: ...
  388. // 5. Let value be the result of invoking serialize a CSS value of declaration.
  389. auto value = declaration.value->to_string();
  390. // 6. Let serialized declaration be the result of invoking serialize a CSS declaration with property name property, value value,
  391. // and the important flag set if declaration has its important flag set.
  392. auto serialized_declaration = serialize_a_css_declaration(property, move(value), declaration.important);
  393. // 7. Append serialized declaration to list.
  394. list.append(move(serialized_declaration));
  395. // 8. Append property to already serialized.
  396. already_serialized.set(property);
  397. }
  398. // 4. Return list joined with " " (U+0020).
  399. StringBuilder builder;
  400. builder.join(' ', list);
  401. return MUST(builder.to_string());
  402. }
  403. WebIDL::ExceptionOr<void> PropertyOwningCSSStyleDeclaration::set_css_text(StringView css_text)
  404. {
  405. dbgln("(STUBBED) PropertyOwningCSSStyleDeclaration::set_css_text(css_text='{}')", css_text);
  406. return {};
  407. }
  408. void PropertyOwningCSSStyleDeclaration::empty_the_declarations()
  409. {
  410. m_properties.clear();
  411. m_custom_properties.clear();
  412. }
  413. void PropertyOwningCSSStyleDeclaration::set_the_declarations(Vector<StyleProperty> properties, HashMap<FlyString, StyleProperty> custom_properties)
  414. {
  415. m_properties = move(properties);
  416. m_custom_properties = move(custom_properties);
  417. }
  418. void ElementInlineCSSStyleDeclaration::set_declarations_from_text(StringView css_text)
  419. {
  420. // FIXME: What do we do if the element is null?
  421. if (!m_element) {
  422. dbgln("FIXME: Returning from ElementInlineCSSStyleDeclaration::declarations_from_text as m_element is null.");
  423. return;
  424. }
  425. empty_the_declarations();
  426. auto style = parse_css_style_attribute(CSS::Parser::ParsingContext(m_element->document()), css_text, *m_element.ptr());
  427. set_the_declarations(style->properties(), style->custom_properties());
  428. }
  429. // https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-csstext
  430. WebIDL::ExceptionOr<void> ElementInlineCSSStyleDeclaration::set_css_text(StringView css_text)
  431. {
  432. // FIXME: What do we do if the element is null?
  433. if (!m_element) {
  434. dbgln("FIXME: Returning from ElementInlineCSSStyleDeclaration::set_css_text as m_element is null.");
  435. return {};
  436. }
  437. // 1. If the computed flag is set, then throw a NoModificationAllowedError exception.
  438. // NOTE: See ResolvedCSSStyleDeclaration.
  439. // 2. Empty the declarations.
  440. // 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.
  441. set_declarations_from_text(css_text);
  442. // 4. Update style attribute for the CSS declaration block.
  443. update_style_attribute();
  444. return {};
  445. }
  446. GC::Ptr<CSSRule> PropertyOwningCSSStyleDeclaration::parent_rule() const
  447. {
  448. return m_parent_rule;
  449. }
  450. void PropertyOwningCSSStyleDeclaration::set_parent_rule(GC::Ref<CSSRule> rule)
  451. {
  452. m_parent_rule = rule;
  453. }
  454. }