ResolvedCSSStyleDeclaration.cpp 43 KB

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