ResolvedCSSStyleDeclaration.cpp 33 KB

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