ResolvedCSSStyleDeclaration.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  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/BackgroundStyleValue.h>
  16. #include <LibWeb/CSS/StyleValues/BorderRadiusShorthandStyleValue.h>
  17. #include <LibWeb/CSS/StyleValues/BorderRadiusStyleValue.h>
  18. #include <LibWeb/CSS/StyleValues/BorderStyleValue.h>
  19. #include <LibWeb/CSS/StyleValues/CalculatedStyleValue.h>
  20. #include <LibWeb/CSS/StyleValues/ColorStyleValue.h>
  21. #include <LibWeb/CSS/StyleValues/EdgeStyleValue.h>
  22. #include <LibWeb/CSS/StyleValues/GridAreaShorthandStyleValue.h>
  23. #include <LibWeb/CSS/StyleValues/GridTrackPlacementShorthandStyleValue.h>
  24. #include <LibWeb/CSS/StyleValues/GridTrackPlacementStyleValue.h>
  25. #include <LibWeb/CSS/StyleValues/GridTrackSizeStyleValue.h>
  26. #include <LibWeb/CSS/StyleValues/IdentifierStyleValue.h>
  27. #include <LibWeb/CSS/StyleValues/InitialStyleValue.h>
  28. #include <LibWeb/CSS/StyleValues/LengthStyleValue.h>
  29. #include <LibWeb/CSS/StyleValues/NumericStyleValue.h>
  30. #include <LibWeb/CSS/StyleValues/PercentageStyleValue.h>
  31. #include <LibWeb/CSS/StyleValues/PositionStyleValue.h>
  32. #include <LibWeb/CSS/StyleValues/RectStyleValue.h>
  33. #include <LibWeb/CSS/StyleValues/ShadowStyleValue.h>
  34. #include <LibWeb/CSS/StyleValues/StyleValueList.h>
  35. #include <LibWeb/CSS/StyleValues/TransformationStyleValue.h>
  36. #include <LibWeb/DOM/Document.h>
  37. #include <LibWeb/DOM/Element.h>
  38. #include <LibWeb/Layout/Viewport.h>
  39. #include <LibWeb/Painting/PaintableBox.h>
  40. #include <LibWeb/Painting/StackingContext.h>
  41. namespace Web::CSS {
  42. WebIDL::ExceptionOr<JS::NonnullGCPtr<ResolvedCSSStyleDeclaration>> ResolvedCSSStyleDeclaration::create(DOM::Element& element)
  43. {
  44. return MUST_OR_THROW_OOM(element.realm().heap().allocate<ResolvedCSSStyleDeclaration>(element.realm(), element));
  45. }
  46. ResolvedCSSStyleDeclaration::ResolvedCSSStyleDeclaration(DOM::Element& element)
  47. : CSSStyleDeclaration(element.realm())
  48. , m_element(element)
  49. {
  50. }
  51. void ResolvedCSSStyleDeclaration::visit_edges(Cell::Visitor& visitor)
  52. {
  53. Base::visit_edges(visitor);
  54. visitor.visit(m_element.ptr());
  55. }
  56. size_t ResolvedCSSStyleDeclaration::length() const
  57. {
  58. return 0;
  59. }
  60. DeprecatedString ResolvedCSSStyleDeclaration::item(size_t index) const
  61. {
  62. (void)index;
  63. return {};
  64. }
  65. 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)
  66. {
  67. auto const& background_layers = layout_node.background_layers();
  68. if (background_layers.is_empty())
  69. return default_value();
  70. if (background_layers.size() == 1)
  71. return callback(background_layers.first());
  72. StyleValueVector values;
  73. for (auto const& layer : background_layers)
  74. values.append(callback(layer));
  75. return StyleValueList::create(move(values), StyleValueList::Separator::Comma);
  76. }
  77. static RefPtr<StyleValue> style_value_for_display(CSS::Display display)
  78. {
  79. if (display.is_none())
  80. return IdentifierStyleValue::create(CSS::ValueID::None);
  81. if (display.is_outside_and_inside()) {
  82. StyleValueVector values;
  83. switch (display.outside()) {
  84. case CSS::Display::Outside::Inline:
  85. values.append(IdentifierStyleValue::create(CSS::ValueID::Inline));
  86. break;
  87. case CSS::Display::Outside::Block:
  88. values.append(IdentifierStyleValue::create(CSS::ValueID::Block));
  89. break;
  90. case CSS::Display::Outside::RunIn:
  91. values.append(IdentifierStyleValue::create(CSS::ValueID::RunIn));
  92. break;
  93. }
  94. switch (display.inside()) {
  95. case CSS::Display::Inside::Flow:
  96. values.append(IdentifierStyleValue::create(CSS::ValueID::Flow));
  97. break;
  98. case CSS::Display::Inside::FlowRoot:
  99. values.append(IdentifierStyleValue::create(CSS::ValueID::FlowRoot));
  100. break;
  101. case CSS::Display::Inside::Table:
  102. values.append(IdentifierStyleValue::create(CSS::ValueID::Table));
  103. break;
  104. case CSS::Display::Inside::Flex:
  105. values.append(IdentifierStyleValue::create(CSS::ValueID::Flex));
  106. break;
  107. case CSS::Display::Inside::Grid:
  108. values.append(IdentifierStyleValue::create(CSS::ValueID::Grid));
  109. break;
  110. case CSS::Display::Inside::Ruby:
  111. values.append(IdentifierStyleValue::create(CSS::ValueID::Ruby));
  112. break;
  113. }
  114. return StyleValueList::create(move(values), StyleValueList::Separator::Space);
  115. }
  116. if (display.is_internal()) {
  117. switch (display.internal()) {
  118. case CSS::Display::Internal::TableRowGroup:
  119. return IdentifierStyleValue::create(CSS::ValueID::TableRowGroup);
  120. case CSS::Display::Internal::TableHeaderGroup:
  121. return IdentifierStyleValue::create(CSS::ValueID::TableHeaderGroup);
  122. case CSS::Display::Internal::TableFooterGroup:
  123. return IdentifierStyleValue::create(CSS::ValueID::TableFooterGroup);
  124. case CSS::Display::Internal::TableRow:
  125. return IdentifierStyleValue::create(CSS::ValueID::TableRow);
  126. case CSS::Display::Internal::TableCell:
  127. return IdentifierStyleValue::create(CSS::ValueID::TableCell);
  128. case CSS::Display::Internal::TableColumnGroup:
  129. return IdentifierStyleValue::create(CSS::ValueID::TableColumnGroup);
  130. case CSS::Display::Internal::TableColumn:
  131. return IdentifierStyleValue::create(CSS::ValueID::TableColumn);
  132. case CSS::Display::Internal::TableCaption:
  133. return IdentifierStyleValue::create(CSS::ValueID::TableCaption);
  134. case CSS::Display::Internal::RubyBase:
  135. return IdentifierStyleValue::create(CSS::ValueID::RubyBase);
  136. case CSS::Display::Internal::RubyText:
  137. return IdentifierStyleValue::create(CSS::ValueID::RubyText);
  138. case CSS::Display::Internal::RubyBaseContainer:
  139. return IdentifierStyleValue::create(CSS::ValueID::RubyBaseContainer);
  140. case CSS::Display::Internal::RubyTextContainer:
  141. return IdentifierStyleValue::create(CSS::ValueID::RubyTextContainer);
  142. }
  143. }
  144. TODO();
  145. }
  146. static NonnullRefPtr<StyleValue const> value_or_default(Optional<StyleProperty> property, NonnullRefPtr<StyleValue> default_style)
  147. {
  148. if (property.has_value())
  149. return property.value().value;
  150. return default_style;
  151. }
  152. static NonnullRefPtr<StyleValue const> style_value_for_length_percentage(LengthPercentage const& length_percentage)
  153. {
  154. if (length_percentage.is_percentage())
  155. return PercentageStyleValue::create(length_percentage.percentage());
  156. if (length_percentage.is_length())
  157. return LengthStyleValue::create(length_percentage.length());
  158. return length_percentage.calculated();
  159. }
  160. static NonnullRefPtr<StyleValue const> style_value_for_size(CSS::Size const& size)
  161. {
  162. if (size.is_none())
  163. return IdentifierStyleValue::create(ValueID::None);
  164. if (size.is_percentage())
  165. return PercentageStyleValue::create(size.percentage());
  166. if (size.is_length())
  167. return LengthStyleValue::create(size.length());
  168. if (size.is_auto())
  169. return IdentifierStyleValue::create(ValueID::Auto);
  170. if (size.is_calculated())
  171. return size.calculated();
  172. if (size.is_min_content())
  173. return IdentifierStyleValue::create(ValueID::MinContent);
  174. if (size.is_max_content())
  175. return IdentifierStyleValue::create(ValueID::MaxContent);
  176. // FIXME: Support fit-content(<length>)
  177. TODO();
  178. }
  179. RefPtr<StyleValue const> ResolvedCSSStyleDeclaration::style_value_for_property(Layout::NodeWithStyle const& layout_node, PropertyID property_id) const
  180. {
  181. switch (property_id) {
  182. case CSS::PropertyID::Background: {
  183. auto maybe_background_color = property(CSS::PropertyID::BackgroundColor);
  184. auto maybe_background_image = property(CSS::PropertyID::BackgroundImage);
  185. auto maybe_background_position = property(CSS::PropertyID::BackgroundPosition);
  186. auto maybe_background_size = property(CSS::PropertyID::BackgroundSize);
  187. auto maybe_background_repeat = property(CSS::PropertyID::BackgroundRepeat);
  188. auto maybe_background_attachment = property(CSS::PropertyID::BackgroundAttachment);
  189. auto maybe_background_origin = property(CSS::PropertyID::BackgroundOrigin);
  190. auto maybe_background_clip = property(CSS::PropertyID::BackgroundClip);
  191. return BackgroundStyleValue::create(
  192. value_or_default(maybe_background_color, InitialStyleValue::the()),
  193. value_or_default(maybe_background_image, IdentifierStyleValue::create(CSS::ValueID::None)),
  194. value_or_default(maybe_background_position, PositionStyleValue::create(EdgeStyleValue::create(PositionEdge::Left, Length::make_px(0)), EdgeStyleValue::create(PositionEdge::Top, Length::make_px(0)))),
  195. value_or_default(maybe_background_size, IdentifierStyleValue::create(CSS::ValueID::Auto)),
  196. value_or_default(maybe_background_repeat, BackgroundRepeatStyleValue::create(CSS::Repeat::Repeat, CSS::Repeat::Repeat)),
  197. value_or_default(maybe_background_attachment, IdentifierStyleValue::create(CSS::ValueID::Scroll)),
  198. value_or_default(maybe_background_origin, IdentifierStyleValue::create(CSS::ValueID::PaddingBox)),
  199. value_or_default(maybe_background_clip, IdentifierStyleValue::create(CSS::ValueID::BorderBox)));
  200. }
  201. case CSS::PropertyID::BackgroundAttachment:
  202. return style_value_for_background_property(
  203. layout_node,
  204. [](auto& layer) { return IdentifierStyleValue::create(to_value_id(layer.attachment)); },
  205. [] { return IdentifierStyleValue::create(CSS::ValueID::Scroll); });
  206. case CSS::PropertyID::BackgroundClip:
  207. return style_value_for_background_property(
  208. layout_node,
  209. [](auto& layer) { return IdentifierStyleValue::create(to_value_id(layer.clip)); },
  210. [] { return IdentifierStyleValue::create(CSS::ValueID::BorderBox); });
  211. case PropertyID::BackgroundColor:
  212. return ColorStyleValue::create(layout_node.computed_values().background_color());
  213. case CSS::PropertyID::BackgroundImage:
  214. return style_value_for_background_property(
  215. layout_node,
  216. [](auto& layer) -> NonnullRefPtr<StyleValue const> {
  217. if (layer.background_image)
  218. return *layer.background_image;
  219. return IdentifierStyleValue::create(CSS::ValueID::None);
  220. },
  221. [] { return IdentifierStyleValue::create(CSS::ValueID::None); });
  222. case CSS::PropertyID::BackgroundOrigin:
  223. return style_value_for_background_property(
  224. layout_node,
  225. [](auto& layer) { return IdentifierStyleValue::create(to_value_id(layer.origin)); },
  226. [] { return IdentifierStyleValue::create(CSS::ValueID::PaddingBox); });
  227. case CSS::PropertyID::BackgroundRepeat:
  228. return style_value_for_background_property(
  229. layout_node,
  230. [](auto& layer) {
  231. StyleValueVector repeat {
  232. IdentifierStyleValue::create(to_value_id(layer.repeat_x)),
  233. IdentifierStyleValue::create(to_value_id(layer.repeat_y)),
  234. };
  235. return StyleValueList::create(move(repeat), StyleValueList::Separator::Space);
  236. },
  237. [] { return BackgroundRepeatStyleValue::create(CSS::Repeat::Repeat, CSS::Repeat::Repeat); });
  238. case CSS::PropertyID::BorderBottom: {
  239. auto border = layout_node.computed_values().border_bottom();
  240. auto width = LengthStyleValue::create(Length::make_px(border.width));
  241. auto style = IdentifierStyleValue::create(to_value_id(border.line_style));
  242. auto color = ColorStyleValue::create(border.color);
  243. return BorderStyleValue::create(width, style, color);
  244. }
  245. case CSS::PropertyID::BorderBottomColor:
  246. return ColorStyleValue::create(layout_node.computed_values().border_bottom().color);
  247. case CSS::PropertyID::BorderBottomLeftRadius: {
  248. auto const& border_radius = layout_node.computed_values().border_bottom_left_radius();
  249. return BorderRadiusStyleValue::create(border_radius.horizontal_radius, border_radius.vertical_radius);
  250. }
  251. case CSS::PropertyID::BorderBottomRightRadius: {
  252. auto const& border_radius = layout_node.computed_values().border_bottom_right_radius();
  253. return BorderRadiusStyleValue::create(border_radius.horizontal_radius, border_radius.vertical_radius);
  254. }
  255. case CSS::PropertyID::BorderBottomStyle:
  256. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().border_bottom().line_style));
  257. case CSS::PropertyID::BorderBottomWidth:
  258. return LengthStyleValue::create(Length::make_px(layout_node.computed_values().border_bottom().width));
  259. case CSS::PropertyID::BorderCollapse:
  260. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().border_collapse()));
  261. case CSS::PropertyID::BorderLeft: {
  262. auto border = layout_node.computed_values().border_left();
  263. auto width = LengthStyleValue::create(Length::make_px(border.width));
  264. auto style = IdentifierStyleValue::create(to_value_id(border.line_style));
  265. auto color = ColorStyleValue::create(border.color);
  266. return BorderStyleValue::create(width, style, color);
  267. }
  268. case CSS::PropertyID::BorderLeftColor:
  269. return ColorStyleValue::create(layout_node.computed_values().border_left().color);
  270. case CSS::PropertyID::BorderLeftStyle:
  271. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().border_left().line_style));
  272. case CSS::PropertyID::BorderLeftWidth:
  273. return LengthStyleValue::create(Length::make_px(layout_node.computed_values().border_left().width));
  274. case CSS::PropertyID::BorderRadius: {
  275. auto maybe_top_left_radius = property(CSS::PropertyID::BorderTopLeftRadius);
  276. auto maybe_top_right_radius = property(CSS::PropertyID::BorderTopRightRadius);
  277. auto maybe_bottom_left_radius = property(CSS::PropertyID::BorderBottomLeftRadius);
  278. auto maybe_bottom_right_radius = property(CSS::PropertyID::BorderBottomRightRadius);
  279. RefPtr<BorderRadiusStyleValue const> top_left_radius, top_right_radius, bottom_left_radius, bottom_right_radius;
  280. if (maybe_top_left_radius.has_value()) {
  281. VERIFY(maybe_top_left_radius.value().value->is_border_radius());
  282. top_left_radius = maybe_top_left_radius.value().value->as_border_radius();
  283. }
  284. if (maybe_top_right_radius.has_value()) {
  285. VERIFY(maybe_top_right_radius.value().value->is_border_radius());
  286. top_right_radius = maybe_top_right_radius.value().value->as_border_radius();
  287. }
  288. if (maybe_bottom_left_radius.has_value()) {
  289. VERIFY(maybe_bottom_left_radius.value().value->is_border_radius());
  290. bottom_left_radius = maybe_bottom_left_radius.value().value->as_border_radius();
  291. }
  292. if (maybe_bottom_right_radius.has_value()) {
  293. VERIFY(maybe_bottom_right_radius.value().value->is_border_radius());
  294. bottom_right_radius = maybe_bottom_right_radius.value().value->as_border_radius();
  295. }
  296. return BorderRadiusShorthandStyleValue::create(top_left_radius.release_nonnull(), top_right_radius.release_nonnull(), bottom_right_radius.release_nonnull(), bottom_left_radius.release_nonnull());
  297. }
  298. case CSS::PropertyID::BorderRight: {
  299. auto border = layout_node.computed_values().border_right();
  300. auto width = LengthStyleValue::create(Length::make_px(border.width));
  301. auto style = IdentifierStyleValue::create(to_value_id(border.line_style));
  302. auto color = ColorStyleValue::create(border.color);
  303. return BorderStyleValue::create(width, style, color);
  304. }
  305. case CSS::PropertyID::BorderRightColor:
  306. return ColorStyleValue::create(layout_node.computed_values().border_right().color);
  307. case CSS::PropertyID::BorderRightStyle:
  308. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().border_right().line_style));
  309. case CSS::PropertyID::BorderRightWidth:
  310. return LengthStyleValue::create(Length::make_px(layout_node.computed_values().border_right().width));
  311. case CSS::PropertyID::BorderTop: {
  312. auto border = layout_node.computed_values().border_top();
  313. auto width = LengthStyleValue::create(Length::make_px(border.width));
  314. auto style = IdentifierStyleValue::create(to_value_id(border.line_style));
  315. auto color = ColorStyleValue::create(border.color);
  316. return BorderStyleValue::create(width, style, color);
  317. }
  318. case CSS::PropertyID::BorderTopColor:
  319. return ColorStyleValue::create(layout_node.computed_values().border_top().color);
  320. case CSS::PropertyID::BorderTopLeftRadius: {
  321. auto const& border_radius = layout_node.computed_values().border_top_left_radius();
  322. return BorderRadiusStyleValue::create(border_radius.horizontal_radius, border_radius.vertical_radius);
  323. }
  324. case CSS::PropertyID::BorderTopRightRadius: {
  325. auto const& border_radius = layout_node.computed_values().border_top_right_radius();
  326. return BorderRadiusStyleValue::create(border_radius.horizontal_radius, border_radius.vertical_radius);
  327. }
  328. case CSS::PropertyID::BorderTopStyle:
  329. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().border_top().line_style));
  330. case CSS::PropertyID::BorderTopWidth:
  331. return LengthStyleValue::create(Length::make_px(layout_node.computed_values().border_top().width));
  332. case CSS::PropertyID::BoxShadow: {
  333. auto box_shadow_layers = layout_node.computed_values().box_shadow();
  334. if (box_shadow_layers.is_empty())
  335. return {};
  336. auto make_box_shadow_style_value = [](ShadowData const& data) {
  337. return ShadowStyleValue::create(data.color, data.offset_x, data.offset_y, data.blur_radius, data.spread_distance, data.placement);
  338. };
  339. if (box_shadow_layers.size() == 1)
  340. return make_box_shadow_style_value(box_shadow_layers.first());
  341. StyleValueVector box_shadow;
  342. box_shadow.ensure_capacity(box_shadow_layers.size());
  343. for (auto const& layer : box_shadow_layers)
  344. box_shadow.append(make_box_shadow_style_value(layer));
  345. return StyleValueList::create(move(box_shadow), StyleValueList::Separator::Comma);
  346. }
  347. case CSS::PropertyID::BoxSizing:
  348. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().box_sizing()));
  349. case CSS::PropertyID::Bottom:
  350. return style_value_for_length_percentage(layout_node.computed_values().inset().bottom());
  351. case CSS::PropertyID::Clear:
  352. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().clear()));
  353. case CSS::PropertyID::Clip:
  354. return RectStyleValue::create(layout_node.computed_values().clip().to_rect());
  355. case CSS::PropertyID::Color:
  356. return ColorStyleValue::create(layout_node.computed_values().color());
  357. case CSS::PropertyID::ColumnGap:
  358. return style_value_for_size(layout_node.computed_values().column_gap());
  359. case CSS::PropertyID::Cursor:
  360. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().cursor()));
  361. case CSS::PropertyID::Display:
  362. return style_value_for_display(layout_node.display());
  363. case CSS::PropertyID::FlexBasis: {
  364. switch (layout_node.computed_values().flex_basis().type) {
  365. case FlexBasis::Content:
  366. return IdentifierStyleValue::create(CSS::ValueID::Content);
  367. case FlexBasis::LengthPercentage:
  368. return style_value_for_length_percentage(*layout_node.computed_values().flex_basis().length_percentage);
  369. case FlexBasis::Auto:
  370. return IdentifierStyleValue::create(CSS::ValueID::Auto);
  371. default:
  372. VERIFY_NOT_REACHED();
  373. }
  374. break;
  375. case CSS::PropertyID::FlexDirection:
  376. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().flex_direction()));
  377. case CSS::PropertyID::FlexGrow:
  378. return NumericStyleValue::create_float(layout_node.computed_values().flex_grow());
  379. case CSS::PropertyID::FlexShrink:
  380. return NumericStyleValue::create_float(layout_node.computed_values().flex_shrink());
  381. case CSS::PropertyID::FlexWrap:
  382. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().flex_wrap()));
  383. case CSS::PropertyID::Float:
  384. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().float_()));
  385. case CSS::PropertyID::GridArea: {
  386. auto maybe_grid_row_start = property(CSS::PropertyID::GridRowStart);
  387. auto maybe_grid_column_start = property(CSS::PropertyID::GridColumnStart);
  388. auto maybe_grid_row_end = property(CSS::PropertyID::GridRowEnd);
  389. auto maybe_grid_column_end = property(CSS::PropertyID::GridColumnEnd);
  390. RefPtr<GridTrackPlacementStyleValue const> grid_row_start, grid_column_start, grid_row_end, grid_column_end;
  391. if (maybe_grid_row_start.has_value()) {
  392. VERIFY(maybe_grid_row_start.value().value->is_grid_track_placement());
  393. grid_row_start = maybe_grid_row_start.value().value->as_grid_track_placement();
  394. }
  395. if (maybe_grid_column_start.has_value()) {
  396. VERIFY(maybe_grid_column_start.value().value->is_grid_track_placement());
  397. grid_column_start = maybe_grid_column_start.value().value->as_grid_track_placement();
  398. }
  399. if (maybe_grid_row_end.has_value()) {
  400. VERIFY(maybe_grid_row_end.value().value->is_grid_track_placement());
  401. grid_row_end = maybe_grid_row_end.value().value->as_grid_track_placement();
  402. }
  403. if (maybe_grid_column_end.has_value()) {
  404. VERIFY(maybe_grid_column_end.value().value->is_grid_track_placement());
  405. grid_column_end = maybe_grid_column_end.value().value->as_grid_track_placement();
  406. }
  407. return GridAreaShorthandStyleValue::create(
  408. grid_row_start.release_nonnull(),
  409. grid_column_start.release_nonnull(),
  410. grid_row_end.release_nonnull(),
  411. grid_column_end.release_nonnull());
  412. }
  413. case CSS::PropertyID::GridColumn: {
  414. auto maybe_grid_column_end = property(CSS::PropertyID::GridColumnEnd);
  415. auto maybe_grid_column_start = property(CSS::PropertyID::GridColumnStart);
  416. RefPtr<GridTrackPlacementStyleValue const> grid_column_start, grid_column_end;
  417. if (maybe_grid_column_end.has_value()) {
  418. VERIFY(maybe_grid_column_end.value().value->is_grid_track_placement());
  419. grid_column_end = maybe_grid_column_end.value().value->as_grid_track_placement();
  420. }
  421. if (maybe_grid_column_start.has_value()) {
  422. VERIFY(maybe_grid_column_start.value().value->is_grid_track_placement());
  423. grid_column_start = maybe_grid_column_start.value().value->as_grid_track_placement();
  424. }
  425. return GridTrackPlacementShorthandStyleValue::create(grid_column_end.release_nonnull(), grid_column_start.release_nonnull());
  426. }
  427. case CSS::PropertyID::GridColumnEnd:
  428. return GridTrackPlacementStyleValue::create(layout_node.computed_values().grid_column_end());
  429. case CSS::PropertyID::GridColumnStart:
  430. return GridTrackPlacementStyleValue::create(layout_node.computed_values().grid_column_start());
  431. case CSS::PropertyID::GridRow: {
  432. auto maybe_grid_row_end = property(CSS::PropertyID::GridRowEnd);
  433. auto maybe_grid_row_start = property(CSS::PropertyID::GridRowStart);
  434. RefPtr<GridTrackPlacementStyleValue const> grid_row_start, grid_row_end;
  435. if (maybe_grid_row_end.has_value()) {
  436. VERIFY(maybe_grid_row_end.value().value->is_grid_track_placement());
  437. grid_row_end = maybe_grid_row_end.value().value->as_grid_track_placement();
  438. }
  439. if (maybe_grid_row_start.has_value()) {
  440. VERIFY(maybe_grid_row_start.value().value->is_grid_track_placement());
  441. grid_row_start = maybe_grid_row_start.value().value->as_grid_track_placement();
  442. }
  443. return GridTrackPlacementShorthandStyleValue::create(grid_row_end.release_nonnull(), grid_row_start.release_nonnull());
  444. }
  445. case CSS::PropertyID::GridRowEnd:
  446. return GridTrackPlacementStyleValue::create(layout_node.computed_values().grid_row_end());
  447. case CSS::PropertyID::GridRowStart:
  448. return GridTrackPlacementStyleValue::create(layout_node.computed_values().grid_row_start());
  449. case CSS::PropertyID::GridTemplateColumns:
  450. return GridTrackSizeStyleValue::create(layout_node.computed_values().grid_template_columns());
  451. case CSS::PropertyID::GridTemplateRows:
  452. return GridTrackSizeStyleValue::create(layout_node.computed_values().grid_template_rows());
  453. case CSS::PropertyID::Height:
  454. return style_value_for_size(layout_node.computed_values().height());
  455. case CSS::PropertyID::ImageRendering:
  456. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().image_rendering()));
  457. case CSS::PropertyID::JustifyContent:
  458. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().justify_content()));
  459. case CSS::PropertyID::Left:
  460. return style_value_for_length_percentage(layout_node.computed_values().inset().left());
  461. case CSS::PropertyID::ListStyleType:
  462. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().list_style_type()));
  463. case CSS::PropertyID::Margin: {
  464. auto margin = layout_node.computed_values().margin();
  465. auto values = StyleValueVector {};
  466. values.append(style_value_for_length_percentage(margin.top()));
  467. values.append(style_value_for_length_percentage(margin.right()));
  468. values.append(style_value_for_length_percentage(margin.bottom()));
  469. values.append(style_value_for_length_percentage(margin.left()));
  470. return StyleValueList::create(move(values), StyleValueList::Separator::Space);
  471. }
  472. case CSS::PropertyID::MarginBottom:
  473. return style_value_for_length_percentage(layout_node.computed_values().margin().bottom());
  474. case CSS::PropertyID::MarginLeft:
  475. return style_value_for_length_percentage(layout_node.computed_values().margin().left());
  476. case CSS::PropertyID::MarginRight:
  477. return style_value_for_length_percentage(layout_node.computed_values().margin().right());
  478. case CSS::PropertyID::MarginTop:
  479. return style_value_for_length_percentage(layout_node.computed_values().margin().top());
  480. case CSS::PropertyID::MaxHeight:
  481. return style_value_for_size(layout_node.computed_values().max_height());
  482. case CSS::PropertyID::MaxWidth:
  483. return style_value_for_size(layout_node.computed_values().max_width());
  484. case CSS::PropertyID::MinHeight:
  485. return style_value_for_size(layout_node.computed_values().min_height());
  486. case CSS::PropertyID::MinWidth:
  487. return style_value_for_size(layout_node.computed_values().min_width());
  488. case CSS::PropertyID::Opacity:
  489. return NumericStyleValue::create_float(layout_node.computed_values().opacity());
  490. case CSS::PropertyID::Order:
  491. return NumericStyleValue::create_integer(layout_node.computed_values().order());
  492. case CSS::PropertyID::OverflowX:
  493. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().overflow_x()));
  494. case CSS::PropertyID::OverflowY:
  495. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().overflow_y()));
  496. case CSS::PropertyID::Padding: {
  497. auto padding = layout_node.computed_values().padding();
  498. auto values = StyleValueVector {};
  499. values.append(style_value_for_length_percentage(padding.top()));
  500. values.append(style_value_for_length_percentage(padding.right()));
  501. values.append(style_value_for_length_percentage(padding.bottom()));
  502. values.append(style_value_for_length_percentage(padding.left()));
  503. return StyleValueList::create(move(values), StyleValueList::Separator::Space);
  504. }
  505. case CSS::PropertyID::PaddingBottom:
  506. return style_value_for_length_percentage(layout_node.computed_values().padding().bottom());
  507. case CSS::PropertyID::PaddingLeft:
  508. return style_value_for_length_percentage(layout_node.computed_values().padding().left());
  509. case CSS::PropertyID::PaddingRight:
  510. return style_value_for_length_percentage(layout_node.computed_values().padding().right());
  511. case CSS::PropertyID::PaddingTop:
  512. return style_value_for_length_percentage(layout_node.computed_values().padding().top());
  513. case CSS::PropertyID::Position:
  514. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().position()));
  515. case CSS::PropertyID::Right:
  516. return style_value_for_length_percentage(layout_node.computed_values().inset().right());
  517. case CSS::PropertyID::RowGap:
  518. return style_value_for_size(layout_node.computed_values().row_gap());
  519. case CSS::PropertyID::TextAlign:
  520. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().text_align()));
  521. case CSS::PropertyID::TextDecorationLine: {
  522. auto text_decoration_lines = layout_node.computed_values().text_decoration_line();
  523. if (text_decoration_lines.is_empty())
  524. return IdentifierStyleValue::create(ValueID::None);
  525. StyleValueVector style_values;
  526. for (auto const& line : text_decoration_lines) {
  527. style_values.append(IdentifierStyleValue::create(to_value_id(line)));
  528. }
  529. return StyleValueList::create(move(style_values), StyleValueList::Separator::Space);
  530. }
  531. case CSS::PropertyID::TextDecorationStyle:
  532. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().text_decoration_style()));
  533. case CSS::PropertyID::TextTransform:
  534. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().text_transform()));
  535. case CSS::PropertyID::Top:
  536. return style_value_for_length_percentage(layout_node.computed_values().inset().top());
  537. case CSS::PropertyID::Transform: {
  538. // NOTE: The computed value for `transform` serializes as a single `matrix(...)` value, instead of
  539. // the original list of transform functions. So, we produce a StyleValue for that.
  540. // https://www.w3.org/TR/css-transforms-1/#serialization-of-the-computed-value
  541. auto transformations = layout_node.computed_values().transformations();
  542. if (transformations.is_empty())
  543. return IdentifierStyleValue::create(ValueID::None);
  544. // The transform matrix is held by the StackingContext, so we need to make sure we have one first.
  545. auto const* viewport = layout_node.document().layout_node();
  546. VERIFY(viewport);
  547. const_cast<Layout::Viewport&>(*viewport).build_stacking_context_tree_if_needed();
  548. VERIFY(layout_node.paintable());
  549. auto const& paintable_box = verify_cast<Painting::PaintableBox const>(layout_node.paintable());
  550. VERIFY(paintable_box->stacking_context());
  551. // FIXME: This needs to serialize to matrix3d if the transformation matrix is a 3D matrix.
  552. // https://w3c.github.io/csswg-drafts/css-transforms-2/#serialization-of-the-computed-value
  553. auto affine_matrix = paintable_box->stacking_context()->affine_transform_matrix();
  554. StyleValueVector parameters;
  555. parameters.ensure_capacity(6);
  556. parameters.append(NumericStyleValue::create_float(affine_matrix.a()));
  557. parameters.append(NumericStyleValue::create_float(affine_matrix.b()));
  558. parameters.append(NumericStyleValue::create_float(affine_matrix.c()));
  559. parameters.append(NumericStyleValue::create_float(affine_matrix.d()));
  560. parameters.append(NumericStyleValue::create_float(affine_matrix.e()));
  561. parameters.append(NumericStyleValue::create_float(affine_matrix.f()));
  562. NonnullRefPtr<StyleValue> matrix_function = TransformationStyleValue::create(TransformFunction::Matrix, move(parameters));
  563. // Elsewhere we always store the transform property's value as a StyleValueList of TransformationStyleValues,
  564. // so this is just for consistency.
  565. StyleValueVector matrix_functions;
  566. matrix_functions.append(matrix_function);
  567. return StyleValueList::create(move(matrix_functions), StyleValueList::Separator::Space);
  568. }
  569. case CSS::PropertyID::VerticalAlign:
  570. if (auto const* length_percentage = layout_node.computed_values().vertical_align().get_pointer<CSS::LengthPercentage>()) {
  571. if (length_percentage->is_length())
  572. return LengthStyleValue::create(length_percentage->length());
  573. if (length_percentage->is_percentage())
  574. return PercentageStyleValue::create(length_percentage->percentage());
  575. VERIFY_NOT_REACHED();
  576. }
  577. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().vertical_align().get<CSS::VerticalAlign>()));
  578. case CSS::PropertyID::WhiteSpace:
  579. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().white_space()));
  580. case CSS::PropertyID::Width:
  581. return style_value_for_size(layout_node.computed_values().width());
  582. case CSS::PropertyID::ZIndex: {
  583. auto maybe_z_index = layout_node.computed_values().z_index();
  584. if (!maybe_z_index.has_value())
  585. return {};
  586. return NumericStyleValue::create_integer(maybe_z_index.release_value());
  587. }
  588. case CSS::PropertyID::Invalid:
  589. return IdentifierStyleValue::create(CSS::ValueID::Invalid);
  590. case CSS::PropertyID::Custom:
  591. dbgln_if(LIBWEB_CSS_DEBUG, "Computed style for custom properties was requested (?)");
  592. return {};
  593. default:
  594. dbgln_if(LIBWEB_CSS_DEBUG, "FIXME: Computed style for the '{}' property was requested", string_from_property_id(property_id));
  595. return {};
  596. }
  597. }
  598. }
  599. Optional<StyleProperty> ResolvedCSSStyleDeclaration::property(PropertyID property_id) const
  600. {
  601. if (CSS::property_affects_layout(property_id)) {
  602. const_cast<DOM::Document&>(m_element->document()).update_layout();
  603. } else {
  604. // FIXME: If we had a way to update style for a single element, this would be a good place to use it.
  605. const_cast<DOM::Document&>(m_element->document()).update_style();
  606. }
  607. if (!m_element->layout_node()) {
  608. auto style_or_error = m_element->document().style_computer().compute_style(const_cast<DOM::Element&>(*m_element));
  609. if (style_or_error.is_error()) {
  610. dbgln("ResolvedCSSStyleDeclaration::property style computer failed");
  611. return {};
  612. }
  613. auto style = style_or_error.release_value();
  614. // FIXME: This is a stopgap until we implement shorthand -> longhand conversion.
  615. auto value = style->maybe_null_property(property_id);
  616. if (!value) {
  617. dbgln("FIXME: ResolvedCSSStyleDeclaration::property(property_id=0x{:x}) No value for property ID in newly computed style case.", to_underlying(property_id));
  618. return {};
  619. }
  620. return StyleProperty {
  621. .property_id = property_id,
  622. .value = value.release_nonnull(),
  623. };
  624. }
  625. auto& layout_node = *m_element->layout_node();
  626. auto value = style_value_for_property(layout_node, property_id);
  627. if (!value)
  628. return {};
  629. return StyleProperty {
  630. .property_id = property_id,
  631. .value = value.release_nonnull(),
  632. };
  633. }
  634. // https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-setproperty
  635. WebIDL::ExceptionOr<void> ResolvedCSSStyleDeclaration::set_property(PropertyID, StringView, StringView)
  636. {
  637. // 1. If the computed flag is set, then throw a NoModificationAllowedError exception.
  638. return WebIDL::NoModificationAllowedError::create(realm(), "Cannot modify properties in result of getComputedStyle()");
  639. }
  640. // https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-removeproperty
  641. WebIDL::ExceptionOr<DeprecatedString> ResolvedCSSStyleDeclaration::remove_property(PropertyID)
  642. {
  643. // 1. If the computed flag is set, then throw a NoModificationAllowedError exception.
  644. return WebIDL::NoModificationAllowedError::create(realm(), "Cannot remove properties from result of getComputedStyle()");
  645. }
  646. DeprecatedString ResolvedCSSStyleDeclaration::serialized() const
  647. {
  648. // https://www.w3.org/TR/cssom/#dom-cssstyledeclaration-csstext
  649. // If the computed flag is set, then return the empty string.
  650. // NOTE: ResolvedCSSStyleDeclaration is something you would only get from window.getComputedStyle(),
  651. // which returns what the spec calls "resolved style". The "computed flag" is always set here.
  652. return DeprecatedString::empty();
  653. }
  654. // https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-csstext
  655. WebIDL::ExceptionOr<void> ResolvedCSSStyleDeclaration::set_css_text(StringView)
  656. {
  657. // 1. If the computed flag is set, then throw a NoModificationAllowedError exception.
  658. return WebIDL::NoModificationAllowedError::create(realm(), "Cannot modify properties in result of getComputedStyle()");
  659. }
  660. }