ResolvedCSSStyleDeclaration.cpp 48 KB

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