ResolvedCSSStyleDeclaration.cpp 31 KB

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