ResolvedCSSStyleDeclaration.cpp 32 KB

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