ResolvedCSSStyleDeclaration.cpp 39 KB

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