ResolvedCSSStyleDeclaration.cpp 52 KB

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