ResolvedCSSStyleDeclaration.cpp 52 KB

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