ResolvedCSSStyleDeclaration.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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/GridTrackPlacementShorthandStyleValue.h>
  21. #include <LibWeb/CSS/StyleValues/GridTrackPlacementStyleValue.h>
  22. #include <LibWeb/CSS/StyleValues/GridTrackSizeListShorthandStyleValue.h>
  23. #include <LibWeb/CSS/StyleValues/GridTrackSizeListStyleValue.h>
  24. #include <LibWeb/CSS/StyleValues/IdentifierStyleValue.h>
  25. #include <LibWeb/CSS/StyleValues/InitialStyleValue.h>
  26. #include <LibWeb/CSS/StyleValues/IntegerStyleValue.h>
  27. #include <LibWeb/CSS/StyleValues/LengthStyleValue.h>
  28. #include <LibWeb/CSS/StyleValues/NumberStyleValue.h>
  29. #include <LibWeb/CSS/StyleValues/PercentageStyleValue.h>
  30. #include <LibWeb/CSS/StyleValues/PositionStyleValue.h>
  31. #include <LibWeb/CSS/StyleValues/RatioStyleValue.h>
  32. #include <LibWeb/CSS/StyleValues/RectStyleValue.h>
  33. #include <LibWeb/CSS/StyleValues/ShadowStyleValue.h>
  34. #include <LibWeb/CSS/StyleValues/ShorthandStyleValue.h>
  35. #include <LibWeb/CSS/StyleValues/StyleValueList.h>
  36. #include <LibWeb/CSS/StyleValues/TextDecorationStyleValue.h>
  37. #include <LibWeb/CSS/StyleValues/TimeStyleValue.h>
  38. #include <LibWeb/CSS/StyleValues/TransformationStyleValue.h>
  39. #include <LibWeb/CSS/StyleValues/URLStyleValue.h>
  40. #include <LibWeb/DOM/Document.h>
  41. #include <LibWeb/DOM/Element.h>
  42. #include <LibWeb/Layout/Viewport.h>
  43. #include <LibWeb/Painting/PaintableBox.h>
  44. #include <LibWeb/Painting/StackingContext.h>
  45. #include <LibWeb/Painting/ViewportPaintable.h>
  46. namespace Web::CSS {
  47. JS::NonnullGCPtr<ResolvedCSSStyleDeclaration> ResolvedCSSStyleDeclaration::create(DOM::Element& element)
  48. {
  49. return element.realm().heap().allocate<ResolvedCSSStyleDeclaration>(element.realm(), element);
  50. }
  51. ResolvedCSSStyleDeclaration::ResolvedCSSStyleDeclaration(DOM::Element& element)
  52. : CSSStyleDeclaration(element.realm())
  53. , m_element(element)
  54. {
  55. }
  56. void ResolvedCSSStyleDeclaration::visit_edges(Cell::Visitor& visitor)
  57. {
  58. Base::visit_edges(visitor);
  59. visitor.visit(m_element.ptr());
  60. }
  61. size_t ResolvedCSSStyleDeclaration::length() const
  62. {
  63. return 0;
  64. }
  65. String ResolvedCSSStyleDeclaration::item(size_t index) const
  66. {
  67. (void)index;
  68. return {};
  69. }
  70. 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)
  71. {
  72. auto const& background_layers = layout_node.background_layers();
  73. if (background_layers.is_empty())
  74. return default_value();
  75. if (background_layers.size() == 1)
  76. return callback(background_layers.first());
  77. StyleValueVector values;
  78. values.ensure_capacity(background_layers.size());
  79. for (auto const& layer : background_layers)
  80. values.unchecked_append(callback(layer));
  81. return StyleValueList::create(move(values), StyleValueList::Separator::Comma);
  82. }
  83. static NonnullRefPtr<StyleValue const> value_or_default(Optional<StyleProperty> property, NonnullRefPtr<StyleValue> default_style)
  84. {
  85. if (property.has_value())
  86. return property.value().value;
  87. return default_style;
  88. }
  89. static NonnullRefPtr<StyleValue const> style_value_for_length_percentage(LengthPercentage const& length_percentage)
  90. {
  91. if (length_percentage.is_auto())
  92. return IdentifierStyleValue::create(ValueID::Auto);
  93. if (length_percentage.is_percentage())
  94. return PercentageStyleValue::create(length_percentage.percentage());
  95. if (length_percentage.is_length())
  96. return LengthStyleValue::create(length_percentage.length());
  97. return length_percentage.calculated();
  98. }
  99. static NonnullRefPtr<StyleValue const> style_value_for_size(Size const& size)
  100. {
  101. if (size.is_none())
  102. return IdentifierStyleValue::create(ValueID::None);
  103. if (size.is_percentage())
  104. return PercentageStyleValue::create(size.percentage());
  105. if (size.is_length())
  106. return LengthStyleValue::create(size.length());
  107. if (size.is_auto())
  108. return IdentifierStyleValue::create(ValueID::Auto);
  109. if (size.is_calculated())
  110. return size.calculated();
  111. if (size.is_min_content())
  112. return IdentifierStyleValue::create(ValueID::MinContent);
  113. if (size.is_max_content())
  114. return IdentifierStyleValue::create(ValueID::MaxContent);
  115. // FIXME: Support fit-content(<length>)
  116. if (size.is_fit_content())
  117. return IdentifierStyleValue::create(ValueID::FitContent);
  118. TODO();
  119. }
  120. 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)
  121. {
  122. bool top_and_bottom_same = top == bottom;
  123. bool left_and_right_same = left == right;
  124. if (top_and_bottom_same && left_and_right_same && top == left)
  125. return top;
  126. if (top_and_bottom_same && left_and_right_same)
  127. return StyleValueList::create(StyleValueVector { move(top), move(right) }, StyleValueList::Separator::Space);
  128. if (left_and_right_same)
  129. return StyleValueList::create(StyleValueVector { move(top), move(right), move(bottom) }, StyleValueList::Separator::Space);
  130. return StyleValueList::create(StyleValueVector { move(top), move(right), move(bottom), move(left) }, StyleValueList::Separator::Space);
  131. }
  132. RefPtr<StyleValue const> ResolvedCSSStyleDeclaration::style_value_for_property(Layout::NodeWithStyle const& layout_node, PropertyID property_id) const
  133. {
  134. // A limited number of properties have special rules for producing their "resolved value".
  135. // We also have to manually construct shorthands from their longhands here.
  136. // Everything else uses the computed value.
  137. // https://www.w3.org/TR/cssom-1/#resolved-values
  138. switch (property_id) {
  139. case PropertyID::Background: {
  140. return ShorthandStyleValue::create(property_id,
  141. { PropertyID::BackgroundColor, PropertyID::BackgroundImage, PropertyID::BackgroundPosition, PropertyID::BackgroundSize, PropertyID::BackgroundRepeat, PropertyID::BackgroundAttachment, PropertyID::BackgroundOrigin, PropertyID::BackgroundClip },
  142. { value_or_default(property(PropertyID::BackgroundColor), InitialStyleValue::the()),
  143. value_or_default(property(PropertyID::BackgroundImage), IdentifierStyleValue::create(ValueID::None)),
  144. value_or_default(property(PropertyID::BackgroundPosition), PositionStyleValue::create(EdgeStyleValue::create(PositionEdge::Left, Length::make_px(0)), EdgeStyleValue::create(PositionEdge::Top, Length::make_px(0)))),
  145. value_or_default(property(PropertyID::BackgroundSize), IdentifierStyleValue::create(ValueID::Auto)),
  146. value_or_default(property(PropertyID::BackgroundRepeat), BackgroundRepeatStyleValue::create(Repeat::Repeat, Repeat::Repeat)),
  147. value_or_default(property(PropertyID::BackgroundAttachment), IdentifierStyleValue::create(ValueID::Scroll)),
  148. value_or_default(property(PropertyID::BackgroundOrigin), IdentifierStyleValue::create(ValueID::PaddingBox)),
  149. value_or_default(property(PropertyID::BackgroundClip), IdentifierStyleValue::create(ValueID::BorderBox)) });
  150. }
  151. case PropertyID::BackgroundColor:
  152. return ColorStyleValue::create(layout_node.computed_values().background_color());
  153. case PropertyID::BackgroundPosition:
  154. return style_value_for_background_property(
  155. layout_node,
  156. [](auto& layer) -> NonnullRefPtr<StyleValue> {
  157. return PositionStyleValue::create(
  158. EdgeStyleValue::create(layer.position_edge_x, layer.position_offset_x),
  159. EdgeStyleValue::create(layer.position_edge_y, layer.position_offset_y));
  160. },
  161. []() -> NonnullRefPtr<StyleValue> {
  162. return PositionStyleValue::create(
  163. EdgeStyleValue::create(PositionEdge::Left, Percentage(0)),
  164. EdgeStyleValue::create(PositionEdge::Top, Percentage(0)));
  165. });
  166. case PropertyID::Border: {
  167. auto top = layout_node.computed_values().border_top();
  168. auto right = layout_node.computed_values().border_right();
  169. auto bottom = layout_node.computed_values().border_bottom();
  170. auto left = layout_node.computed_values().border_left();
  171. // `border` only has a reasonable value if all four sides are the same.
  172. if (top != right || top != bottom || top != left)
  173. return nullptr;
  174. auto width = LengthStyleValue::create(Length::make_px(top.width));
  175. auto style = IdentifierStyleValue::create(to_value_id(top.line_style));
  176. auto color = ColorStyleValue::create(top.color);
  177. return ShorthandStyleValue::create(property_id,
  178. { PropertyID::BorderWidth, PropertyID::BorderStyle, PropertyID::BorderColor },
  179. { width, style, color });
  180. }
  181. case PropertyID::BorderBottom: {
  182. auto border = layout_node.computed_values().border_bottom();
  183. auto width = LengthStyleValue::create(Length::make_px(border.width));
  184. auto style = IdentifierStyleValue::create(to_value_id(border.line_style));
  185. auto color = ColorStyleValue::create(border.color);
  186. return ShorthandStyleValue::create(property_id,
  187. { PropertyID::BorderBottomWidth, PropertyID::BorderBottomStyle, PropertyID::BorderBottomColor },
  188. { width, style, color });
  189. }
  190. case PropertyID::BorderBottomColor:
  191. return ColorStyleValue::create(layout_node.computed_values().border_bottom().color);
  192. case PropertyID::BorderColor: {
  193. auto top = ColorStyleValue::create(layout_node.computed_values().border_top().color);
  194. auto right = ColorStyleValue::create(layout_node.computed_values().border_right().color);
  195. auto bottom = ColorStyleValue::create(layout_node.computed_values().border_bottom().color);
  196. auto left = ColorStyleValue::create(layout_node.computed_values().border_left().color);
  197. return style_value_for_sided_shorthand(top, right, bottom, left);
  198. }
  199. case PropertyID::BorderLeft: {
  200. auto border = layout_node.computed_values().border_left();
  201. auto width = LengthStyleValue::create(Length::make_px(border.width));
  202. auto style = IdentifierStyleValue::create(to_value_id(border.line_style));
  203. auto color = ColorStyleValue::create(border.color);
  204. return ShorthandStyleValue::create(property_id,
  205. { PropertyID::BorderLeftWidth, PropertyID::BorderLeftStyle, PropertyID::BorderLeftColor },
  206. { width, style, color });
  207. }
  208. case PropertyID::BorderLeftColor:
  209. return ColorStyleValue::create(layout_node.computed_values().border_left().color);
  210. case PropertyID::BorderRadius: {
  211. auto maybe_top_left_radius = property(PropertyID::BorderTopLeftRadius);
  212. auto maybe_top_right_radius = property(PropertyID::BorderTopRightRadius);
  213. auto maybe_bottom_left_radius = property(PropertyID::BorderBottomLeftRadius);
  214. auto maybe_bottom_right_radius = property(PropertyID::BorderBottomRightRadius);
  215. RefPtr<BorderRadiusStyleValue const> top_left_radius, top_right_radius, bottom_left_radius, bottom_right_radius;
  216. if (maybe_top_left_radius.has_value()) {
  217. VERIFY(maybe_top_left_radius.value().value->is_border_radius());
  218. top_left_radius = maybe_top_left_radius.value().value->as_border_radius();
  219. }
  220. if (maybe_top_right_radius.has_value()) {
  221. VERIFY(maybe_top_right_radius.value().value->is_border_radius());
  222. top_right_radius = maybe_top_right_radius.value().value->as_border_radius();
  223. }
  224. if (maybe_bottom_left_radius.has_value()) {
  225. VERIFY(maybe_bottom_left_radius.value().value->is_border_radius());
  226. bottom_left_radius = maybe_bottom_left_radius.value().value->as_border_radius();
  227. }
  228. if (maybe_bottom_right_radius.has_value()) {
  229. VERIFY(maybe_bottom_right_radius.value().value->is_border_radius());
  230. bottom_right_radius = maybe_bottom_right_radius.value().value->as_border_radius();
  231. }
  232. return ShorthandStyleValue::create(property_id,
  233. { PropertyID::BorderTopLeftRadius, PropertyID::BorderTopRightRadius, PropertyID::BorderBottomRightRadius, PropertyID::BorderBottomLeftRadius },
  234. { top_left_radius.release_nonnull(), top_right_radius.release_nonnull(), bottom_right_radius.release_nonnull(), bottom_left_radius.release_nonnull() });
  235. }
  236. case PropertyID::BorderRight: {
  237. auto border = layout_node.computed_values().border_right();
  238. auto width = LengthStyleValue::create(Length::make_px(border.width));
  239. auto style = IdentifierStyleValue::create(to_value_id(border.line_style));
  240. auto color = ColorStyleValue::create(border.color);
  241. return ShorthandStyleValue::create(property_id,
  242. { PropertyID::BorderRightWidth, PropertyID::BorderRightStyle, PropertyID::BorderRightColor },
  243. { width, style, color });
  244. }
  245. case PropertyID::BorderRightColor:
  246. return ColorStyleValue::create(layout_node.computed_values().border_right().color);
  247. case PropertyID::BorderStyle: {
  248. auto top = IdentifierStyleValue::create(to_value_id(layout_node.computed_values().border_top().line_style));
  249. auto right = IdentifierStyleValue::create(to_value_id(layout_node.computed_values().border_right().line_style));
  250. auto bottom = IdentifierStyleValue::create(to_value_id(layout_node.computed_values().border_bottom().line_style));
  251. auto left = IdentifierStyleValue::create(to_value_id(layout_node.computed_values().border_left().line_style));
  252. return style_value_for_sided_shorthand(top, right, bottom, left);
  253. }
  254. case PropertyID::BorderTop: {
  255. auto border = layout_node.computed_values().border_top();
  256. auto width = LengthStyleValue::create(Length::make_px(border.width));
  257. auto style = IdentifierStyleValue::create(to_value_id(border.line_style));
  258. auto color = ColorStyleValue::create(border.color);
  259. return ShorthandStyleValue::create(property_id,
  260. { PropertyID::BorderTopWidth, PropertyID::BorderTopStyle, PropertyID::BorderTopColor },
  261. { width, style, color });
  262. }
  263. case PropertyID::BorderTopColor:
  264. return ColorStyleValue::create(layout_node.computed_values().border_top().color);
  265. case PropertyID::BorderWidth: {
  266. auto top = LengthStyleValue::create(Length::make_px(layout_node.computed_values().border_top().width));
  267. auto right = LengthStyleValue::create(Length::make_px(layout_node.computed_values().border_right().width));
  268. auto bottom = LengthStyleValue::create(Length::make_px(layout_node.computed_values().border_bottom().width));
  269. auto left = LengthStyleValue::create(Length::make_px(layout_node.computed_values().border_left().width));
  270. return style_value_for_sided_shorthand(top, right, bottom, left);
  271. }
  272. case PropertyID::Bottom:
  273. return style_value_for_length_percentage(layout_node.computed_values().inset().bottom());
  274. case PropertyID::Color:
  275. return ColorStyleValue::create(layout_node.computed_values().color());
  276. case PropertyID::GridArea: {
  277. auto maybe_grid_row_start = property(PropertyID::GridRowStart);
  278. auto maybe_grid_column_start = property(PropertyID::GridColumnStart);
  279. auto maybe_grid_row_end = property(PropertyID::GridRowEnd);
  280. auto maybe_grid_column_end = property(PropertyID::GridColumnEnd);
  281. RefPtr<GridTrackPlacementStyleValue const> grid_row_start, grid_column_start, grid_row_end, grid_column_end;
  282. if (maybe_grid_row_start.has_value()) {
  283. VERIFY(maybe_grid_row_start.value().value->is_grid_track_placement());
  284. grid_row_start = maybe_grid_row_start.value().value->as_grid_track_placement();
  285. }
  286. if (maybe_grid_column_start.has_value()) {
  287. VERIFY(maybe_grid_column_start.value().value->is_grid_track_placement());
  288. grid_column_start = maybe_grid_column_start.value().value->as_grid_track_placement();
  289. }
  290. if (maybe_grid_row_end.has_value()) {
  291. VERIFY(maybe_grid_row_end.value().value->is_grid_track_placement());
  292. grid_row_end = maybe_grid_row_end.value().value->as_grid_track_placement();
  293. }
  294. if (maybe_grid_column_end.has_value()) {
  295. VERIFY(maybe_grid_column_end.value().value->is_grid_track_placement());
  296. grid_column_end = maybe_grid_column_end.value().value->as_grid_track_placement();
  297. }
  298. return ShorthandStyleValue::create(property_id,
  299. { PropertyID::GridRowStart, PropertyID::GridColumnStart, PropertyID::GridRowEnd, PropertyID::GridColumnEnd },
  300. { grid_row_start.release_nonnull(), grid_column_start.release_nonnull(), grid_row_end.release_nonnull(), grid_column_end.release_nonnull() });
  301. }
  302. case PropertyID::GridColumn: {
  303. auto maybe_grid_column_end = property(PropertyID::GridColumnEnd);
  304. auto maybe_grid_column_start = property(PropertyID::GridColumnStart);
  305. RefPtr<GridTrackPlacementStyleValue const> grid_column_start, grid_column_end;
  306. if (maybe_grid_column_end.has_value()) {
  307. VERIFY(maybe_grid_column_end.value().value->is_grid_track_placement());
  308. grid_column_end = maybe_grid_column_end.value().value->as_grid_track_placement();
  309. }
  310. if (maybe_grid_column_start.has_value()) {
  311. VERIFY(maybe_grid_column_start.value().value->is_grid_track_placement());
  312. grid_column_start = maybe_grid_column_start.value().value->as_grid_track_placement();
  313. }
  314. return GridTrackPlacementShorthandStyleValue::create(grid_column_end.release_nonnull(), grid_column_start.release_nonnull());
  315. }
  316. case PropertyID::GridRow: {
  317. auto maybe_grid_row_end = property(PropertyID::GridRowEnd);
  318. auto maybe_grid_row_start = property(PropertyID::GridRowStart);
  319. RefPtr<GridTrackPlacementStyleValue const> grid_row_start, grid_row_end;
  320. if (maybe_grid_row_end.has_value()) {
  321. VERIFY(maybe_grid_row_end.value().value->is_grid_track_placement());
  322. grid_row_end = maybe_grid_row_end.value().value->as_grid_track_placement();
  323. }
  324. if (maybe_grid_row_start.has_value()) {
  325. VERIFY(maybe_grid_row_start.value().value->is_grid_track_placement());
  326. grid_row_start = maybe_grid_row_start.value().value->as_grid_track_placement();
  327. }
  328. return GridTrackPlacementShorthandStyleValue::create(grid_row_end.release_nonnull(), grid_row_start.release_nonnull());
  329. }
  330. case PropertyID::GridTemplate: {
  331. auto maybe_grid_template_areas = property(PropertyID::GridTemplateAreas);
  332. auto maybe_grid_template_rows = property(PropertyID::GridTemplateRows);
  333. auto maybe_grid_template_columns = property(PropertyID::GridTemplateColumns);
  334. RefPtr<GridTemplateAreaStyleValue const> grid_template_areas;
  335. RefPtr<GridTrackSizeListStyleValue const> grid_template_rows, grid_template_columns;
  336. if (maybe_grid_template_areas.has_value()) {
  337. VERIFY(maybe_grid_template_areas.value().value->is_grid_template_area());
  338. grid_template_areas = maybe_grid_template_areas.value().value->as_grid_template_area();
  339. }
  340. if (maybe_grid_template_rows.has_value()) {
  341. VERIFY(maybe_grid_template_rows.value().value->is_grid_track_size_list());
  342. grid_template_rows = maybe_grid_template_rows.value().value->as_grid_track_size_list();
  343. }
  344. if (maybe_grid_template_columns.has_value()) {
  345. VERIFY(maybe_grid_template_columns.value().value->is_grid_track_size_list());
  346. grid_template_columns = maybe_grid_template_columns.value().value->as_grid_track_size_list();
  347. }
  348. return GridTrackSizeListShorthandStyleValue::create(grid_template_areas.release_nonnull(), grid_template_rows.release_nonnull(), grid_template_columns.release_nonnull());
  349. }
  350. case PropertyID::Height:
  351. return style_value_for_size(layout_node.computed_values().height());
  352. case PropertyID::Left:
  353. return style_value_for_length_percentage(layout_node.computed_values().inset().left());
  354. case PropertyID::Margin: {
  355. auto margin = layout_node.computed_values().margin();
  356. auto top = style_value_for_length_percentage(margin.top());
  357. auto right = style_value_for_length_percentage(margin.right());
  358. auto bottom = style_value_for_length_percentage(margin.bottom());
  359. auto left = style_value_for_length_percentage(margin.left());
  360. return style_value_for_sided_shorthand(move(top), move(right), move(bottom), move(left));
  361. }
  362. case PropertyID::MarginBottom:
  363. return style_value_for_length_percentage(layout_node.computed_values().margin().bottom());
  364. case PropertyID::MarginLeft:
  365. return style_value_for_length_percentage(layout_node.computed_values().margin().left());
  366. case PropertyID::MarginRight:
  367. return style_value_for_length_percentage(layout_node.computed_values().margin().right());
  368. case PropertyID::MarginTop:
  369. return style_value_for_length_percentage(layout_node.computed_values().margin().top());
  370. case PropertyID::Outline: {
  371. return StyleValueList::create(
  372. { style_value_for_property(layout_node, PropertyID::OutlineColor).release_nonnull(),
  373. style_value_for_property(layout_node, PropertyID::OutlineStyle).release_nonnull(),
  374. style_value_for_property(layout_node, PropertyID::OutlineWidth).release_nonnull() },
  375. StyleValueList::Separator::Space);
  376. }
  377. case PropertyID::OutlineColor:
  378. return ColorStyleValue::create(layout_node.computed_values().outline_color());
  379. case PropertyID::Padding: {
  380. auto padding = layout_node.computed_values().padding();
  381. auto top = style_value_for_length_percentage(padding.top());
  382. auto right = style_value_for_length_percentage(padding.right());
  383. auto bottom = style_value_for_length_percentage(padding.bottom());
  384. auto left = style_value_for_length_percentage(padding.left());
  385. return style_value_for_sided_shorthand(move(top), move(right), move(bottom), move(left));
  386. }
  387. case PropertyID::PaddingBottom:
  388. return style_value_for_length_percentage(layout_node.computed_values().padding().bottom());
  389. case PropertyID::PaddingLeft:
  390. return style_value_for_length_percentage(layout_node.computed_values().padding().left());
  391. case PropertyID::PaddingRight:
  392. return style_value_for_length_percentage(layout_node.computed_values().padding().right());
  393. case PropertyID::PaddingTop:
  394. return style_value_for_length_percentage(layout_node.computed_values().padding().top());
  395. case PropertyID::Right:
  396. return style_value_for_length_percentage(layout_node.computed_values().inset().right());
  397. case PropertyID::TextDecoration: {
  398. auto line = style_value_for_property(layout_node, PropertyID::TextDecorationLine);
  399. auto thickness = style_value_for_property(layout_node, PropertyID::TextDecorationThickness);
  400. auto style = style_value_for_property(layout_node, PropertyID::TextDecorationStyle);
  401. auto color = style_value_for_property(layout_node, PropertyID::TextDecorationColor);
  402. return TextDecorationStyleValue::create(*line, *thickness, *style, *color);
  403. }
  404. case PropertyID::TextDecorationColor:
  405. return ColorStyleValue::create(layout_node.computed_values().text_decoration_color());
  406. case PropertyID::Top:
  407. return style_value_for_length_percentage(layout_node.computed_values().inset().top());
  408. case PropertyID::Transform: {
  409. // NOTE: The computed value for `transform` serializes as a single `matrix(...)` value, instead of
  410. // the original list of transform functions. So, we produce a StyleValue for that.
  411. // https://www.w3.org/TR/css-transforms-1/#serialization-of-the-computed-value
  412. // FIXME: Computing values should happen in the StyleComputer!
  413. auto transformations = layout_node.computed_values().transformations();
  414. if (transformations.is_empty())
  415. return IdentifierStyleValue::create(ValueID::None);
  416. // The transform matrix is held by the StackingContext, so we need to make sure we have one first.
  417. auto const* viewport = layout_node.document().paintable();
  418. VERIFY(viewport);
  419. const_cast<Painting::ViewportPaintable&>(*viewport).build_stacking_context_tree_if_needed();
  420. VERIFY(layout_node.paintable());
  421. auto const& paintable_box = verify_cast<Painting::PaintableBox const>(layout_node.paintable());
  422. VERIFY(paintable_box->stacking_context());
  423. // FIXME: This needs to serialize to matrix3d if the transformation matrix is a 3D matrix.
  424. // https://w3c.github.io/csswg-drafts/css-transforms-2/#serialization-of-the-computed-value
  425. auto affine_matrix = paintable_box->stacking_context()->affine_transform_matrix();
  426. StyleValueVector parameters;
  427. parameters.ensure_capacity(6);
  428. parameters.unchecked_append(NumberStyleValue::create(affine_matrix.a()));
  429. parameters.unchecked_append(NumberStyleValue::create(affine_matrix.b()));
  430. parameters.unchecked_append(NumberStyleValue::create(affine_matrix.c()));
  431. parameters.unchecked_append(NumberStyleValue::create(affine_matrix.d()));
  432. parameters.unchecked_append(NumberStyleValue::create(affine_matrix.e()));
  433. parameters.unchecked_append(NumberStyleValue::create(affine_matrix.f()));
  434. NonnullRefPtr<StyleValue> matrix_function = TransformationStyleValue::create(TransformFunction::Matrix, move(parameters));
  435. // Elsewhere we always store the transform property's value as a StyleValueList of TransformationStyleValues,
  436. // so this is just for consistency.
  437. StyleValueVector matrix_functions { matrix_function };
  438. return StyleValueList::create(move(matrix_functions), StyleValueList::Separator::Space);
  439. }
  440. case PropertyID::Width:
  441. return style_value_for_size(layout_node.computed_values().width());
  442. case PropertyID::Invalid:
  443. return IdentifierStyleValue::create(ValueID::Invalid);
  444. case PropertyID::Custom:
  445. dbgln_if(LIBWEB_CSS_DEBUG, "Computed style for custom properties was requested (?)");
  446. return nullptr;
  447. default:
  448. if (!property_is_shorthand(property_id))
  449. return static_cast<DOM::Element const&>(*layout_node.dom_node()).computed_css_values()->property(property_id);
  450. dbgln_if(LIBWEB_CSS_DEBUG, "FIXME: Computed style for the '{}' shorthand property was requested", string_from_property_id(property_id));
  451. return nullptr;
  452. }
  453. }
  454. Optional<StyleProperty> ResolvedCSSStyleDeclaration::property(PropertyID property_id) const
  455. {
  456. // https://www.w3.org/TR/cssom-1/#dom-window-getcomputedstyle
  457. // NOTE: This is a partial enforcement of step 5 ("If elt is connected, ...")
  458. if (!m_element->is_connected())
  459. return {};
  460. if (property_affects_layout(property_id)) {
  461. const_cast<DOM::Document&>(m_element->document()).update_layout();
  462. } else {
  463. // FIXME: If we had a way to update style for a single element, this would be a good place to use it.
  464. const_cast<DOM::Document&>(m_element->document()).update_style();
  465. }
  466. if (!m_element->layout_node()) {
  467. auto style_or_error = m_element->document().style_computer().compute_style(const_cast<DOM::Element&>(*m_element));
  468. if (style_or_error.is_error()) {
  469. dbgln("ResolvedCSSStyleDeclaration::property style computer failed");
  470. return {};
  471. }
  472. auto style = style_or_error.release_value();
  473. // FIXME: This is a stopgap until we implement shorthand -> longhand conversion.
  474. auto value = style->maybe_null_property(property_id);
  475. if (!value) {
  476. dbgln("FIXME: ResolvedCSSStyleDeclaration::property(property_id=0x{:x}) No value for property ID in newly computed style case.", to_underlying(property_id));
  477. return {};
  478. }
  479. return StyleProperty {
  480. .property_id = property_id,
  481. .value = value.release_nonnull(),
  482. };
  483. }
  484. auto& layout_node = *m_element->layout_node();
  485. auto value = style_value_for_property(layout_node, property_id);
  486. if (!value)
  487. return {};
  488. return StyleProperty {
  489. .property_id = property_id,
  490. .value = value.release_nonnull(),
  491. };
  492. }
  493. // https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-setproperty
  494. WebIDL::ExceptionOr<void> ResolvedCSSStyleDeclaration::set_property(PropertyID, StringView, StringView)
  495. {
  496. // 1. If the computed flag is set, then throw a NoModificationAllowedError exception.
  497. return WebIDL::NoModificationAllowedError::create(realm(), "Cannot modify properties in result of getComputedStyle()"_fly_string);
  498. }
  499. // https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-removeproperty
  500. WebIDL::ExceptionOr<String> ResolvedCSSStyleDeclaration::remove_property(PropertyID)
  501. {
  502. // 1. If the computed flag is set, then throw a NoModificationAllowedError exception.
  503. return WebIDL::NoModificationAllowedError::create(realm(), "Cannot remove properties from result of getComputedStyle()"_fly_string);
  504. }
  505. DeprecatedString ResolvedCSSStyleDeclaration::serialized() const
  506. {
  507. // https://www.w3.org/TR/cssom/#dom-cssstyledeclaration-csstext
  508. // If the computed flag is set, then return the empty string.
  509. // NOTE: ResolvedCSSStyleDeclaration is something you would only get from window.getComputedStyle(),
  510. // which returns what the spec calls "resolved style". The "computed flag" is always set here.
  511. return DeprecatedString::empty();
  512. }
  513. // https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-csstext
  514. WebIDL::ExceptionOr<void> ResolvedCSSStyleDeclaration::set_css_text(StringView)
  515. {
  516. // 1. If the computed flag is set, then throw a NoModificationAllowedError exception.
  517. return WebIDL::NoModificationAllowedError::create(realm(), "Cannot modify properties in result of getComputedStyle()"_fly_string);
  518. }
  519. }