ResolvedCSSStyleDeclaration.cpp 34 KB

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