ResolvedCSSStyleDeclaration.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*
  2. * Copyright (c) 2021-2023, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
  4. * Copyright (c) 2022-2023, Sam Atkins <atkinssj@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/Debug.h>
  9. #include <AK/Format.h>
  10. #include <AK/NonnullRefPtr.h>
  11. #include <LibWeb/CSS/Enums.h>
  12. #include <LibWeb/CSS/ResolvedCSSStyleDeclaration.h>
  13. #include <LibWeb/CSS/StyleComputer.h>
  14. #include <LibWeb/CSS/StyleValues/BackgroundRepeatStyleValue.h>
  15. #include <LibWeb/CSS/StyleValues/BackgroundSizeStyleValue.h>
  16. #include <LibWeb/CSS/StyleValues/BorderRadiusStyleValue.h>
  17. #include <LibWeb/CSS/StyleValues/CalculatedStyleValue.h>
  18. #include <LibWeb/CSS/StyleValues/ColorStyleValue.h>
  19. #include <LibWeb/CSS/StyleValues/EdgeStyleValue.h>
  20. #include <LibWeb/CSS/StyleValues/GridTrackPlacementStyleValue.h>
  21. #include <LibWeb/CSS/StyleValues/GridTrackSizeListStyleValue.h>
  22. #include <LibWeb/CSS/StyleValues/IdentifierStyleValue.h>
  23. #include <LibWeb/CSS/StyleValues/InitialStyleValue.h>
  24. #include <LibWeb/CSS/StyleValues/IntegerStyleValue.h>
  25. #include <LibWeb/CSS/StyleValues/LengthStyleValue.h>
  26. #include <LibWeb/CSS/StyleValues/NumberStyleValue.h>
  27. #include <LibWeb/CSS/StyleValues/PercentageStyleValue.h>
  28. #include <LibWeb/CSS/StyleValues/PositionStyleValue.h>
  29. #include <LibWeb/CSS/StyleValues/RatioStyleValue.h>
  30. #include <LibWeb/CSS/StyleValues/RectStyleValue.h>
  31. #include <LibWeb/CSS/StyleValues/ShadowStyleValue.h>
  32. #include <LibWeb/CSS/StyleValues/ShorthandStyleValue.h>
  33. #include <LibWeb/CSS/StyleValues/StyleValueList.h>
  34. #include <LibWeb/CSS/StyleValues/TimeStyleValue.h>
  35. #include <LibWeb/CSS/StyleValues/TransformationStyleValue.h>
  36. #include <LibWeb/CSS/StyleValues/URLStyleValue.h>
  37. #include <LibWeb/DOM/Document.h>
  38. #include <LibWeb/DOM/Element.h>
  39. #include <LibWeb/Layout/Viewport.h>
  40. #include <LibWeb/Painting/PaintableBox.h>
  41. #include <LibWeb/Painting/StackingContext.h>
  42. #include <LibWeb/Painting/ViewportPaintable.h>
  43. namespace Web::CSS {
  44. JS::NonnullGCPtr<ResolvedCSSStyleDeclaration> ResolvedCSSStyleDeclaration::create(DOM::Element& element)
  45. {
  46. return element.realm().heap().allocate<ResolvedCSSStyleDeclaration>(element.realm(), element);
  47. }
  48. ResolvedCSSStyleDeclaration::ResolvedCSSStyleDeclaration(DOM::Element& element)
  49. : CSSStyleDeclaration(element.realm())
  50. , m_element(element)
  51. {
  52. }
  53. void ResolvedCSSStyleDeclaration::visit_edges(Cell::Visitor& visitor)
  54. {
  55. Base::visit_edges(visitor);
  56. visitor.visit(m_element.ptr());
  57. }
  58. size_t ResolvedCSSStyleDeclaration::length() const
  59. {
  60. return 0;
  61. }
  62. String ResolvedCSSStyleDeclaration::item(size_t index) const
  63. {
  64. (void)index;
  65. return {};
  66. }
  67. static NonnullRefPtr<StyleValue const> style_value_for_background_property(Layout::NodeWithStyle const& layout_node, Function<NonnullRefPtr<StyleValue const>(BackgroundLayerData const&)> callback, Function<NonnullRefPtr<StyleValue const>()> default_value)
  68. {
  69. auto const& background_layers = layout_node.background_layers();
  70. if (background_layers.is_empty())
  71. return default_value();
  72. if (background_layers.size() == 1)
  73. return callback(background_layers.first());
  74. StyleValueVector values;
  75. values.ensure_capacity(background_layers.size());
  76. for (auto const& layer : background_layers)
  77. values.unchecked_append(callback(layer));
  78. return StyleValueList::create(move(values), StyleValueList::Separator::Comma);
  79. }
  80. static NonnullRefPtr<StyleValue const> style_value_for_length_percentage(LengthPercentage const& length_percentage)
  81. {
  82. if (length_percentage.is_auto())
  83. return IdentifierStyleValue::create(ValueID::Auto);
  84. if (length_percentage.is_percentage())
  85. return PercentageStyleValue::create(length_percentage.percentage());
  86. if (length_percentage.is_length())
  87. return LengthStyleValue::create(length_percentage.length());
  88. return length_percentage.calculated();
  89. }
  90. static NonnullRefPtr<StyleValue const> style_value_for_size(Size const& size)
  91. {
  92. if (size.is_none())
  93. return IdentifierStyleValue::create(ValueID::None);
  94. if (size.is_percentage())
  95. return PercentageStyleValue::create(size.percentage());
  96. if (size.is_length())
  97. return LengthStyleValue::create(size.length());
  98. if (size.is_auto())
  99. return IdentifierStyleValue::create(ValueID::Auto);
  100. if (size.is_calculated())
  101. return size.calculated();
  102. if (size.is_min_content())
  103. return IdentifierStyleValue::create(ValueID::MinContent);
  104. if (size.is_max_content())
  105. return IdentifierStyleValue::create(ValueID::MaxContent);
  106. // FIXME: Support fit-content(<length>)
  107. if (size.is_fit_content())
  108. return IdentifierStyleValue::create(ValueID::FitContent);
  109. TODO();
  110. }
  111. static NonnullRefPtr<StyleValue const> style_value_for_sided_shorthand(ValueComparingNonnullRefPtr<StyleValue const> top, ValueComparingNonnullRefPtr<StyleValue const> right, ValueComparingNonnullRefPtr<StyleValue const> bottom, ValueComparingNonnullRefPtr<StyleValue const> left)
  112. {
  113. bool top_and_bottom_same = top == bottom;
  114. bool left_and_right_same = left == right;
  115. if (top_and_bottom_same && left_and_right_same && top == left)
  116. return top;
  117. if (top_and_bottom_same && left_and_right_same)
  118. return StyleValueList::create(StyleValueVector { move(top), move(right) }, StyleValueList::Separator::Space);
  119. if (left_and_right_same)
  120. return StyleValueList::create(StyleValueVector { move(top), move(right), move(bottom) }, StyleValueList::Separator::Space);
  121. return StyleValueList::create(StyleValueVector { move(top), move(right), move(bottom), move(left) }, StyleValueList::Separator::Space);
  122. }
  123. enum class LogicalSide {
  124. BlockStart,
  125. BlockEnd,
  126. InlineStart,
  127. InlineEnd,
  128. };
  129. static RefPtr<StyleValue const> style_value_for_length_box_logical_side(Layout::NodeWithStyle const&, LengthBox const& box, LogicalSide logical_side)
  130. {
  131. // FIXME: Actually determine the logical sides based on layout_node's writing-mode and direction.
  132. switch (logical_side) {
  133. case LogicalSide::BlockStart:
  134. return style_value_for_length_percentage(box.top());
  135. case LogicalSide::BlockEnd:
  136. return style_value_for_length_percentage(box.bottom());
  137. case LogicalSide::InlineStart:
  138. return style_value_for_length_percentage(box.left());
  139. case LogicalSide::InlineEnd:
  140. return style_value_for_length_percentage(box.right());
  141. }
  142. VERIFY_NOT_REACHED();
  143. }
  144. RefPtr<StyleValue const> ResolvedCSSStyleDeclaration::style_value_for_property(Layout::NodeWithStyle const& layout_node, PropertyID property_id) const
  145. {
  146. // A limited number of properties have special rules for producing their "resolved value".
  147. // We also have to manually construct shorthands from their longhands here.
  148. // Everything else uses the computed value.
  149. // https://www.w3.org/TR/cssom-1/#resolved-values
  150. // The resolved value for a given longhand property can be determined as follows:
  151. switch (property_id) {
  152. // -> background-color
  153. // FIXME: -> border-block-end-color
  154. // FIXME: -> border-block-start-color
  155. // -> border-bottom-color
  156. // FIXME: -> border-inline-end-color
  157. // FIXME: -> border-inline-start-color
  158. // -> border-left-color
  159. // -> border-right-color
  160. // -> border-top-color
  161. // FIXME: -> box-shadow
  162. // FIXME: -> caret-color
  163. // -> color
  164. // -> outline-color
  165. // -> A resolved value special case property like color defined in another specification
  166. // The resolved value is the used value.
  167. case PropertyID::BackgroundColor:
  168. return ColorStyleValue::create(layout_node.computed_values().background_color());
  169. case PropertyID::BorderBottomColor:
  170. return ColorStyleValue::create(layout_node.computed_values().border_bottom().color);
  171. case PropertyID::BorderLeftColor:
  172. return ColorStyleValue::create(layout_node.computed_values().border_left().color);
  173. case PropertyID::BorderRightColor:
  174. return ColorStyleValue::create(layout_node.computed_values().border_right().color);
  175. case PropertyID::BorderTopColor:
  176. return ColorStyleValue::create(layout_node.computed_values().border_top().color);
  177. case PropertyID::Color:
  178. return ColorStyleValue::create(layout_node.computed_values().color());
  179. case PropertyID::OutlineColor:
  180. return ColorStyleValue::create(layout_node.computed_values().outline_color());
  181. case PropertyID::TextDecorationColor:
  182. return ColorStyleValue::create(layout_node.computed_values().text_decoration_color());
  183. // -> line-height
  184. // The resolved value is normal if the computed value is normal, or the used value otherwise.
  185. case PropertyID::LineHeight: {
  186. auto line_height = static_cast<DOM::Element const&>(*layout_node.dom_node()).computed_css_values()->property(property_id);
  187. if (line_height->is_identifier() && line_height->to_identifier() == ValueID::Normal)
  188. return line_height;
  189. return LengthStyleValue::create(Length::make_px(layout_node.line_height()));
  190. }
  191. // FIXME: -> block-size
  192. // -> height
  193. // FIXME: -> inline-size
  194. // -> margin-block-end
  195. // -> margin-block-start
  196. // -> margin-bottom
  197. // -> margin-inline-end
  198. // -> margin-inline-start
  199. // -> margin-left
  200. // -> margin-right
  201. // -> margin-top
  202. // -> padding-block-end
  203. // -> padding-block-start
  204. // -> padding-bottom
  205. // -> padding-inline-end
  206. // -> padding-inline-start
  207. // -> padding-left
  208. // -> padding-right
  209. // -> padding-top
  210. // -> width
  211. // -> A resolved value special case property like height defined in another specification
  212. // FIXME: If the property applies to the element or pseudo-element and the resolved value of the
  213. // display property is not none or contents, then the resolved value is the used value.
  214. // Otherwise the resolved value is the computed value.
  215. case PropertyID::Height:
  216. return style_value_for_size(layout_node.computed_values().height());
  217. case PropertyID::MarginBlockEnd:
  218. return style_value_for_length_box_logical_side(layout_node, layout_node.computed_values().margin(), LogicalSide::BlockEnd);
  219. case PropertyID::MarginBlockStart:
  220. return style_value_for_length_box_logical_side(layout_node, layout_node.computed_values().margin(), LogicalSide::BlockStart);
  221. case PropertyID::MarginBottom:
  222. return style_value_for_length_percentage(layout_node.computed_values().margin().bottom());
  223. case PropertyID::MarginInlineEnd:
  224. return style_value_for_length_box_logical_side(layout_node, layout_node.computed_values().margin(), LogicalSide::InlineEnd);
  225. case PropertyID::MarginInlineStart:
  226. return style_value_for_length_box_logical_side(layout_node, layout_node.computed_values().margin(), LogicalSide::InlineStart);
  227. case PropertyID::MarginLeft:
  228. return style_value_for_length_percentage(layout_node.computed_values().margin().left());
  229. case PropertyID::MarginRight:
  230. return style_value_for_length_percentage(layout_node.computed_values().margin().right());
  231. case PropertyID::MarginTop:
  232. return style_value_for_length_percentage(layout_node.computed_values().margin().top());
  233. case PropertyID::PaddingBlockEnd:
  234. return style_value_for_length_box_logical_side(layout_node, layout_node.computed_values().padding(), LogicalSide::BlockEnd);
  235. case PropertyID::PaddingBlockStart:
  236. return style_value_for_length_box_logical_side(layout_node, layout_node.computed_values().padding(), LogicalSide::BlockStart);
  237. case PropertyID::PaddingBottom:
  238. return style_value_for_length_percentage(layout_node.computed_values().padding().bottom());
  239. case PropertyID::PaddingInlineEnd:
  240. return style_value_for_length_box_logical_side(layout_node, layout_node.computed_values().padding(), LogicalSide::InlineEnd);
  241. case PropertyID::PaddingInlineStart:
  242. return style_value_for_length_box_logical_side(layout_node, layout_node.computed_values().padding(), LogicalSide::InlineStart);
  243. case PropertyID::PaddingLeft:
  244. return style_value_for_length_percentage(layout_node.computed_values().padding().left());
  245. case PropertyID::PaddingRight:
  246. return style_value_for_length_percentage(layout_node.computed_values().padding().right());
  247. case PropertyID::PaddingTop:
  248. return style_value_for_length_percentage(layout_node.computed_values().padding().top());
  249. case PropertyID::Width:
  250. return style_value_for_size(layout_node.computed_values().width());
  251. // -> bottom
  252. // -> left
  253. // -> inset-block-end
  254. // -> inset-block-start
  255. // -> inset-inline-end
  256. // -> inset-inline-start
  257. // -> right
  258. // -> top
  259. // -> A resolved value special case property like top defined in another specification
  260. // FIXME: If the property applies to a positioned element and the resolved value of the display property is not
  261. // none or contents, and the property is not over-constrained, then the resolved value is the used value.
  262. // Otherwise the resolved value is the computed value.
  263. case PropertyID::Bottom:
  264. return style_value_for_length_percentage(layout_node.computed_values().inset().bottom());
  265. case PropertyID::InsetBlockEnd:
  266. return style_value_for_length_box_logical_side(layout_node, layout_node.computed_values().inset(), LogicalSide::BlockEnd);
  267. case PropertyID::InsetBlockStart:
  268. return style_value_for_length_box_logical_side(layout_node, layout_node.computed_values().inset(), LogicalSide::BlockStart);
  269. case PropertyID::InsetInlineEnd:
  270. return style_value_for_length_box_logical_side(layout_node, layout_node.computed_values().inset(), LogicalSide::InlineEnd);
  271. case PropertyID::InsetInlineStart:
  272. return style_value_for_length_box_logical_side(layout_node, layout_node.computed_values().inset(), LogicalSide::InlineStart);
  273. case PropertyID::Left:
  274. return style_value_for_length_percentage(layout_node.computed_values().inset().left());
  275. case PropertyID::Right:
  276. return style_value_for_length_percentage(layout_node.computed_values().inset().right());
  277. case PropertyID::Top:
  278. return style_value_for_length_percentage(layout_node.computed_values().inset().top());
  279. // -> A resolved value special case property defined in another specification
  280. // As defined in the relevant specification.
  281. case PropertyID::Transform: {
  282. // NOTE: The computed value for `transform` serializes as a single `matrix(...)` value, instead of
  283. // the original list of transform functions. So, we produce a StyleValue for that.
  284. // https://www.w3.org/TR/css-transforms-1/#serialization-of-the-computed-value
  285. // FIXME: Computing values should happen in the StyleComputer!
  286. auto transformations = layout_node.computed_values().transformations();
  287. if (transformations.is_empty())
  288. return IdentifierStyleValue::create(ValueID::None);
  289. // The transform matrix is held by the StackingContext, so we need to make sure we have one first.
  290. auto const* viewport = layout_node.document().paintable();
  291. VERIFY(viewport);
  292. const_cast<Painting::ViewportPaintable&>(*viewport).build_stacking_context_tree_if_needed();
  293. VERIFY(layout_node.paintable());
  294. auto const& paintable_box = verify_cast<Painting::PaintableBox const>(layout_node.paintable());
  295. VERIFY(paintable_box->stacking_context());
  296. // FIXME: This needs to serialize to matrix3d if the transformation matrix is a 3D matrix.
  297. // https://w3c.github.io/csswg-drafts/css-transforms-2/#serialization-of-the-computed-value
  298. auto affine_matrix = paintable_box->stacking_context()->affine_transform_matrix();
  299. StyleValueVector parameters;
  300. parameters.ensure_capacity(6);
  301. parameters.unchecked_append(NumberStyleValue::create(affine_matrix.a()));
  302. parameters.unchecked_append(NumberStyleValue::create(affine_matrix.b()));
  303. parameters.unchecked_append(NumberStyleValue::create(affine_matrix.c()));
  304. parameters.unchecked_append(NumberStyleValue::create(affine_matrix.d()));
  305. parameters.unchecked_append(NumberStyleValue::create(affine_matrix.e()));
  306. parameters.unchecked_append(NumberStyleValue::create(affine_matrix.f()));
  307. NonnullRefPtr<StyleValue> matrix_function = TransformationStyleValue::create(TransformFunction::Matrix, move(parameters));
  308. // Elsewhere we always store the transform property's value as a StyleValueList of TransformationStyleValues,
  309. // so this is just for consistency.
  310. StyleValueVector matrix_functions { matrix_function };
  311. return StyleValueList::create(move(matrix_functions), StyleValueList::Separator::Space);
  312. }
  313. // -> Any other property
  314. // The resolved value is the computed value.
  315. // NOTE: This is handled inside the `default` case.
  316. // NOTE: Everything below is a shorthand that requires some manual construction.
  317. case PropertyID::BackgroundPosition:
  318. return style_value_for_background_property(
  319. layout_node,
  320. [](auto& layer) -> NonnullRefPtr<StyleValue> {
  321. return PositionStyleValue::create(
  322. EdgeStyleValue::create(layer.position_edge_x, layer.position_offset_x),
  323. EdgeStyleValue::create(layer.position_edge_y, layer.position_offset_y));
  324. },
  325. []() -> NonnullRefPtr<StyleValue> {
  326. return PositionStyleValue::create(
  327. EdgeStyleValue::create(PositionEdge::Left, Percentage(0)),
  328. EdgeStyleValue::create(PositionEdge::Top, Percentage(0)));
  329. });
  330. case PropertyID::Border: {
  331. auto width = style_value_for_property(layout_node, PropertyID::BorderWidth);
  332. auto style = style_value_for_property(layout_node, PropertyID::BorderStyle);
  333. auto color = style_value_for_property(layout_node, PropertyID::BorderColor);
  334. // `border` only has a reasonable value if all four sides are the same.
  335. if (width->is_value_list() || style->is_value_list() || color->is_value_list())
  336. return nullptr;
  337. return ShorthandStyleValue::create(property_id,
  338. { PropertyID::BorderWidth, PropertyID::BorderStyle, PropertyID::BorderColor },
  339. { width.release_nonnull(), style.release_nonnull(), color.release_nonnull() });
  340. }
  341. case PropertyID::BorderColor: {
  342. auto top = style_value_for_property(layout_node, PropertyID::BorderTopColor);
  343. auto right = style_value_for_property(layout_node, PropertyID::BorderRightColor);
  344. auto bottom = style_value_for_property(layout_node, PropertyID::BorderBottomColor);
  345. auto left = style_value_for_property(layout_node, PropertyID::BorderLeftColor);
  346. return style_value_for_sided_shorthand(top.release_nonnull(), right.release_nonnull(), bottom.release_nonnull(), left.release_nonnull());
  347. }
  348. case PropertyID::BorderStyle: {
  349. auto top = style_value_for_property(layout_node, PropertyID::BorderTopStyle);
  350. auto right = style_value_for_property(layout_node, PropertyID::BorderRightStyle);
  351. auto bottom = style_value_for_property(layout_node, PropertyID::BorderBottomStyle);
  352. auto left = style_value_for_property(layout_node, PropertyID::BorderLeftStyle);
  353. return style_value_for_sided_shorthand(top.release_nonnull(), right.release_nonnull(), bottom.release_nonnull(), left.release_nonnull());
  354. }
  355. case PropertyID::BorderWidth: {
  356. auto top = style_value_for_property(layout_node, PropertyID::BorderTopWidth);
  357. auto right = style_value_for_property(layout_node, PropertyID::BorderRightWidth);
  358. auto bottom = style_value_for_property(layout_node, PropertyID::BorderBottomWidth);
  359. auto left = style_value_for_property(layout_node, PropertyID::BorderLeftWidth);
  360. return style_value_for_sided_shorthand(top.release_nonnull(), right.release_nonnull(), bottom.release_nonnull(), left.release_nonnull());
  361. }
  362. case PropertyID::Margin: {
  363. auto top = style_value_for_property(layout_node, PropertyID::MarginTop);
  364. auto right = style_value_for_property(layout_node, PropertyID::MarginRight);
  365. auto bottom = style_value_for_property(layout_node, PropertyID::MarginBottom);
  366. auto left = style_value_for_property(layout_node, PropertyID::MarginLeft);
  367. return style_value_for_sided_shorthand(top.release_nonnull(), right.release_nonnull(), bottom.release_nonnull(), left.release_nonnull());
  368. }
  369. case PropertyID::Padding: {
  370. auto top = style_value_for_property(layout_node, PropertyID::PaddingTop);
  371. auto right = style_value_for_property(layout_node, PropertyID::PaddingRight);
  372. auto bottom = style_value_for_property(layout_node, PropertyID::PaddingBottom);
  373. auto left = style_value_for_property(layout_node, PropertyID::PaddingLeft);
  374. return style_value_for_sided_shorthand(top.release_nonnull(), right.release_nonnull(), bottom.release_nonnull(), left.release_nonnull());
  375. }
  376. case PropertyID::Invalid:
  377. return IdentifierStyleValue::create(ValueID::Invalid);
  378. case PropertyID::Custom:
  379. dbgln_if(LIBWEB_CSS_DEBUG, "Computed style for custom properties was requested (?)");
  380. return nullptr;
  381. default:
  382. if (!property_is_shorthand(property_id))
  383. return static_cast<DOM::Element const&>(*layout_node.dom_node()).computed_css_values()->property(property_id);
  384. // Handle shorthands in a generic way
  385. auto longhand_ids = longhands_for_shorthand(property_id);
  386. StyleValueVector longhand_values;
  387. longhand_values.ensure_capacity(longhand_ids.size());
  388. for (auto longhand_id : longhand_ids)
  389. longhand_values.append(style_value_for_property(layout_node, longhand_id).release_nonnull());
  390. return ShorthandStyleValue::create(property_id, move(longhand_ids), move(longhand_values));
  391. }
  392. }
  393. Optional<StyleProperty> ResolvedCSSStyleDeclaration::property(PropertyID property_id) const
  394. {
  395. // https://www.w3.org/TR/cssom-1/#dom-window-getcomputedstyle
  396. // NOTE: This is a partial enforcement of step 5 ("If elt is connected, ...")
  397. if (!m_element->is_connected())
  398. return {};
  399. if (property_affects_layout(property_id)) {
  400. const_cast<DOM::Document&>(m_element->document()).update_layout();
  401. } else {
  402. // FIXME: If we had a way to update style for a single element, this would be a good place to use it.
  403. const_cast<DOM::Document&>(m_element->document()).update_style();
  404. }
  405. if (!m_element->layout_node()) {
  406. auto style_or_error = m_element->document().style_computer().compute_style(const_cast<DOM::Element&>(*m_element));
  407. if (style_or_error.is_error()) {
  408. dbgln("ResolvedCSSStyleDeclaration::property style computer failed");
  409. return {};
  410. }
  411. auto style = style_or_error.release_value();
  412. // FIXME: This is a stopgap until we implement shorthand -> longhand conversion.
  413. auto value = style->maybe_null_property(property_id);
  414. if (!value) {
  415. dbgln("FIXME: ResolvedCSSStyleDeclaration::property(property_id=0x{:x}) No value for property ID in newly computed style case.", to_underlying(property_id));
  416. return {};
  417. }
  418. return StyleProperty {
  419. .property_id = property_id,
  420. .value = value.release_nonnull(),
  421. };
  422. }
  423. auto& layout_node = *m_element->layout_node();
  424. auto value = style_value_for_property(layout_node, property_id);
  425. if (!value)
  426. return {};
  427. return StyleProperty {
  428. .property_id = property_id,
  429. .value = value.release_nonnull(),
  430. };
  431. }
  432. // https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-setproperty
  433. WebIDL::ExceptionOr<void> ResolvedCSSStyleDeclaration::set_property(PropertyID, StringView, StringView)
  434. {
  435. // 1. If the computed flag is set, then throw a NoModificationAllowedError exception.
  436. return WebIDL::NoModificationAllowedError::create(realm(), "Cannot modify properties in result of getComputedStyle()"_fly_string);
  437. }
  438. // https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-removeproperty
  439. WebIDL::ExceptionOr<String> ResolvedCSSStyleDeclaration::remove_property(PropertyID)
  440. {
  441. // 1. If the computed flag is set, then throw a NoModificationAllowedError exception.
  442. return WebIDL::NoModificationAllowedError::create(realm(), "Cannot remove properties from result of getComputedStyle()"_fly_string);
  443. }
  444. DeprecatedString ResolvedCSSStyleDeclaration::serialized() const
  445. {
  446. // https://www.w3.org/TR/cssom/#dom-cssstyledeclaration-csstext
  447. // If the computed flag is set, then return the empty string.
  448. // NOTE: ResolvedCSSStyleDeclaration is something you would only get from window.getComputedStyle(),
  449. // which returns what the spec calls "resolved style". The "computed flag" is always set here.
  450. return DeprecatedString::empty();
  451. }
  452. // https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-csstext
  453. WebIDL::ExceptionOr<void> ResolvedCSSStyleDeclaration::set_css_text(StringView)
  454. {
  455. // 1. If the computed flag is set, then throw a NoModificationAllowedError exception.
  456. return WebIDL::NoModificationAllowedError::create(realm(), "Cannot modify properties in result of getComputedStyle()"_fly_string);
  457. }
  458. }