ResolvedCSSStyleDeclaration.cpp 49 KB

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