ResolvedCSSStyleDeclaration.cpp 27 KB

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