ResolvedCSSStyleDeclaration.cpp 37 KB

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