ResolvedCSSStyleDeclaration.cpp 40 KB

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