ResolvedCSSStyleDeclaration.cpp 51 KB

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