ResolvedCSSStyleDeclaration.cpp 47 KB

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