ResolvedCSSStyleDeclaration.cpp 31 KB

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