ResolvedCSSStyleDeclaration.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. /*
  2. * Copyright (c) 2021-2023, Andreas Kling <andreas@ladybird.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/BorderRadiusStyleValue.h>
  17. #include <LibWeb/CSS/StyleValues/CSSColorValue.h>
  18. #include <LibWeb/CSS/StyleValues/CSSKeywordValue.h>
  19. #include <LibWeb/CSS/StyleValues/CSSMathValue.h>
  20. #include <LibWeb/CSS/StyleValues/EdgeStyleValue.h>
  21. #include <LibWeb/CSS/StyleValues/GridTrackPlacementStyleValue.h>
  22. #include <LibWeb/CSS/StyleValues/GridTrackSizeListStyleValue.h>
  23. #include <LibWeb/CSS/StyleValues/IntegerStyleValue.h>
  24. #include <LibWeb/CSS/StyleValues/LengthStyleValue.h>
  25. #include <LibWeb/CSS/StyleValues/NumberStyleValue.h>
  26. #include <LibWeb/CSS/StyleValues/PercentageStyleValue.h>
  27. #include <LibWeb/CSS/StyleValues/PositionStyleValue.h>
  28. #include <LibWeb/CSS/StyleValues/RatioStyleValue.h>
  29. #include <LibWeb/CSS/StyleValues/RectStyleValue.h>
  30. #include <LibWeb/CSS/StyleValues/ShadowStyleValue.h>
  31. #include <LibWeb/CSS/StyleValues/ShorthandStyleValue.h>
  32. #include <LibWeb/CSS/StyleValues/StyleValueList.h>
  33. #include <LibWeb/CSS/StyleValues/TimeStyleValue.h>
  34. #include <LibWeb/CSS/StyleValues/TransformationStyleValue.h>
  35. #include <LibWeb/CSS/StyleValues/URLStyleValue.h>
  36. #include <LibWeb/DOM/Document.h>
  37. #include <LibWeb/DOM/Element.h>
  38. #include <LibWeb/Layout/Viewport.h>
  39. #include <LibWeb/Painting/PaintableBox.h>
  40. #include <LibWeb/Painting/StackingContext.h>
  41. #include <LibWeb/Painting/ViewportPaintable.h>
  42. namespace Web::CSS {
  43. JS_DEFINE_ALLOCATOR(ResolvedCSSStyleDeclaration);
  44. JS::NonnullGCPtr<ResolvedCSSStyleDeclaration> ResolvedCSSStyleDeclaration::create(DOM::Element& element, Optional<Selector::PseudoElement::Type> pseudo_element)
  45. {
  46. return element.realm().heap().allocate<ResolvedCSSStyleDeclaration>(element.realm(), element, move(pseudo_element));
  47. }
  48. ResolvedCSSStyleDeclaration::ResolvedCSSStyleDeclaration(DOM::Element& element, Optional<CSS::Selector::PseudoElement::Type> pseudo_element)
  49. : CSSStyleDeclaration(element.realm())
  50. , m_element(element)
  51. , m_pseudo_element(move(pseudo_element))
  52. {
  53. }
  54. void ResolvedCSSStyleDeclaration::visit_edges(Cell::Visitor& visitor)
  55. {
  56. Base::visit_edges(visitor);
  57. visitor.visit(m_element);
  58. }
  59. // https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-length
  60. size_t ResolvedCSSStyleDeclaration::length() const
  61. {
  62. // The length attribute must return the number of CSS declarations in the declarations.
  63. // FIXME: Include the number of custom properties.
  64. return to_underlying(last_longhand_property_id) - to_underlying(first_longhand_property_id) + 1;
  65. }
  66. // https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-item
  67. String ResolvedCSSStyleDeclaration::item(size_t index) const
  68. {
  69. // The item(index) method must return the property name of the CSS declaration at position index.
  70. // FIXME: Return custom properties if index > last_longhand_property_id.
  71. if (index > length())
  72. return {};
  73. auto property_id = static_cast<PropertyID>(index + to_underlying(first_longhand_property_id));
  74. return string_from_property_id(property_id).to_string();
  75. }
  76. static NonnullRefPtr<CSSStyleValue const> style_value_for_background_property(Layout::NodeWithStyle const& layout_node, Function<NonnullRefPtr<CSSStyleValue const>(BackgroundLayerData const&)> callback, Function<NonnullRefPtr<CSSStyleValue const>()> default_value)
  77. {
  78. auto const& background_layers = layout_node.background_layers();
  79. if (background_layers.is_empty())
  80. return default_value();
  81. if (background_layers.size() == 1)
  82. return callback(background_layers.first());
  83. StyleValueVector values;
  84. values.ensure_capacity(background_layers.size());
  85. for (auto const& layer : background_layers)
  86. values.unchecked_append(callback(layer));
  87. return StyleValueList::create(move(values), StyleValueList::Separator::Comma);
  88. }
  89. static NonnullRefPtr<CSSStyleValue const> style_value_for_length_percentage(LengthPercentage const& length_percentage)
  90. {
  91. if (length_percentage.is_auto())
  92. return CSSKeywordValue::create(Keyword::Auto);
  93. if (length_percentage.is_percentage())
  94. return PercentageStyleValue::create(length_percentage.percentage());
  95. if (length_percentage.is_length())
  96. return LengthStyleValue::create(length_percentage.length());
  97. return length_percentage.calculated();
  98. }
  99. static NonnullRefPtr<CSSStyleValue const> style_value_for_size(Size const& size)
  100. {
  101. if (size.is_none())
  102. return CSSKeywordValue::create(Keyword::None);
  103. if (size.is_percentage())
  104. return PercentageStyleValue::create(size.percentage());
  105. if (size.is_length())
  106. return LengthStyleValue::create(size.length());
  107. if (size.is_auto())
  108. return CSSKeywordValue::create(Keyword::Auto);
  109. if (size.is_calculated())
  110. return size.calculated();
  111. if (size.is_min_content())
  112. return CSSKeywordValue::create(Keyword::MinContent);
  113. if (size.is_max_content())
  114. return CSSKeywordValue::create(Keyword::MaxContent);
  115. // FIXME: Support fit-content(<length>)
  116. if (size.is_fit_content())
  117. return CSSKeywordValue::create(Keyword::FitContent);
  118. TODO();
  119. }
  120. static NonnullRefPtr<CSSStyleValue const> style_value_for_sided_shorthand(ValueComparingNonnullRefPtr<CSSStyleValue const> top, ValueComparingNonnullRefPtr<CSSStyleValue const> right, ValueComparingNonnullRefPtr<CSSStyleValue const> bottom, ValueComparingNonnullRefPtr<CSSStyleValue const> left)
  121. {
  122. bool top_and_bottom_same = top == bottom;
  123. bool left_and_right_same = left == right;
  124. if (top_and_bottom_same && left_and_right_same && top == left)
  125. return top;
  126. if (top_and_bottom_same && left_and_right_same)
  127. return StyleValueList::create(StyleValueVector { move(top), move(right) }, StyleValueList::Separator::Space);
  128. if (left_and_right_same)
  129. return StyleValueList::create(StyleValueVector { move(top), move(right), move(bottom) }, StyleValueList::Separator::Space);
  130. return StyleValueList::create(StyleValueVector { move(top), move(right), move(bottom), move(left) }, StyleValueList::Separator::Space);
  131. }
  132. enum class LogicalSide {
  133. BlockStart,
  134. BlockEnd,
  135. InlineStart,
  136. InlineEnd,
  137. };
  138. static RefPtr<CSSStyleValue const> style_value_for_length_box_logical_side(Layout::NodeWithStyle const&, LengthBox const& box, LogicalSide logical_side)
  139. {
  140. // FIXME: Actually determine the logical sides based on layout_node's writing-mode and direction.
  141. switch (logical_side) {
  142. case LogicalSide::BlockStart:
  143. return style_value_for_length_percentage(box.top());
  144. case LogicalSide::BlockEnd:
  145. return style_value_for_length_percentage(box.bottom());
  146. case LogicalSide::InlineStart:
  147. return style_value_for_length_percentage(box.left());
  148. case LogicalSide::InlineEnd:
  149. return style_value_for_length_percentage(box.right());
  150. }
  151. VERIFY_NOT_REACHED();
  152. }
  153. static RefPtr<CSSStyleValue const> style_value_for_shadow(Vector<ShadowData> const& shadow_data)
  154. {
  155. if (shadow_data.is_empty())
  156. return CSSKeywordValue::create(Keyword::None);
  157. auto make_shadow_style_value = [](ShadowData const& shadow) {
  158. return ShadowStyleValue::create(
  159. CSSColorValue::create_from_color(shadow.color),
  160. style_value_for_length_percentage(shadow.offset_x),
  161. style_value_for_length_percentage(shadow.offset_y),
  162. style_value_for_length_percentage(shadow.blur_radius),
  163. style_value_for_length_percentage(shadow.spread_distance),
  164. shadow.placement);
  165. };
  166. if (shadow_data.size() == 1)
  167. return make_shadow_style_value(shadow_data.first());
  168. StyleValueVector style_values;
  169. style_values.ensure_capacity(shadow_data.size());
  170. for (auto& shadow : shadow_data)
  171. style_values.unchecked_append(make_shadow_style_value(shadow));
  172. return StyleValueList::create(move(style_values), StyleValueList::Separator::Comma);
  173. }
  174. RefPtr<CSSStyleValue const> ResolvedCSSStyleDeclaration::style_value_for_property(Layout::NodeWithStyle const& layout_node, PropertyID property_id) const
  175. {
  176. auto used_value_for_property = [&layout_node, property_id](Function<CSSPixels(Painting::PaintableBox const&)>&& used_value_getter) -> Optional<CSSPixels> {
  177. auto const& display = layout_node.computed_values().display();
  178. if (!display.is_none() && !display.is_contents() && layout_node.paintable()) {
  179. if (layout_node.paintable()->is_paintable_box()) {
  180. auto const& paintable_box = static_cast<Painting::PaintableBox const&>(*layout_node.paintable());
  181. return used_value_getter(paintable_box);
  182. }
  183. dbgln("FIXME: Support getting used value for property `{}` on {}", string_from_property_id(property_id), layout_node.debug_description());
  184. }
  185. return {};
  186. };
  187. auto get_computed_value = [this](PropertyID property_id) {
  188. if (m_pseudo_element.has_value())
  189. return m_element->pseudo_element_computed_css_values(m_pseudo_element.value())->property(property_id);
  190. return m_element->computed_css_values()->property(property_id);
  191. };
  192. // A limited number of properties have special rules for producing their "resolved value".
  193. // We also have to manually construct shorthands from their longhands here.
  194. // Everything else uses the computed value.
  195. // https://www.w3.org/TR/cssom-1/#resolved-values
  196. // The resolved value for a given longhand property can be determined as follows:
  197. switch (property_id) {
  198. // -> background-color
  199. // FIXME: -> border-block-end-color
  200. // FIXME: -> border-block-start-color
  201. // -> border-bottom-color
  202. // FIXME: -> border-inline-end-color
  203. // FIXME: -> border-inline-start-color
  204. // -> border-left-color
  205. // -> border-right-color
  206. // -> border-top-color
  207. // -> box-shadow
  208. // FIXME: -> caret-color
  209. // -> color
  210. // -> outline-color
  211. // -> A resolved value special case property like color defined in another specification
  212. // The resolved value is the used value.
  213. case PropertyID::BackgroundColor:
  214. return CSSColorValue::create_from_color(layout_node.computed_values().background_color());
  215. case PropertyID::BorderBottomColor:
  216. return CSSColorValue::create_from_color(layout_node.computed_values().border_bottom().color);
  217. case PropertyID::BorderLeftColor:
  218. return CSSColorValue::create_from_color(layout_node.computed_values().border_left().color);
  219. case PropertyID::BorderRightColor:
  220. return CSSColorValue::create_from_color(layout_node.computed_values().border_right().color);
  221. case PropertyID::BorderTopColor:
  222. return CSSColorValue::create_from_color(layout_node.computed_values().border_top().color);
  223. case PropertyID::BoxShadow:
  224. return style_value_for_shadow(layout_node.computed_values().box_shadow());
  225. case PropertyID::Color:
  226. return CSSColorValue::create_from_color(layout_node.computed_values().color());
  227. case PropertyID::OutlineColor:
  228. return CSSColorValue::create_from_color(layout_node.computed_values().outline_color());
  229. case PropertyID::TextDecorationColor:
  230. return CSSColorValue::create_from_color(layout_node.computed_values().text_decoration_color());
  231. // NOTE: text-shadow isn't listed, but is computed the same as box-shadow.
  232. case PropertyID::TextShadow:
  233. return style_value_for_shadow(layout_node.computed_values().text_shadow());
  234. // -> line-height
  235. // The resolved value is normal if the computed value is normal, or the used value otherwise.
  236. case PropertyID::LineHeight: {
  237. auto line_height = get_computed_value(property_id);
  238. if (line_height->is_keyword() && line_height->to_keyword() == Keyword::Normal)
  239. return line_height;
  240. return LengthStyleValue::create(Length::make_px(layout_node.computed_values().line_height()));
  241. }
  242. // FIXME: -> block-size
  243. // -> height
  244. // FIXME: -> inline-size
  245. // -> margin-block-end
  246. // -> margin-block-start
  247. // -> margin-bottom
  248. // -> margin-inline-end
  249. // -> margin-inline-start
  250. // -> margin-left
  251. // -> margin-right
  252. // -> margin-top
  253. // -> padding-block-end
  254. // -> padding-block-start
  255. // -> padding-bottom
  256. // -> padding-inline-end
  257. // -> padding-inline-start
  258. // -> padding-left
  259. // -> padding-right
  260. // -> padding-top
  261. // -> width
  262. // If the property applies to the element or pseudo-element and the resolved value of the
  263. // display property is not none or contents, then the resolved value is the used value.
  264. // Otherwise the resolved value is the computed value.
  265. case PropertyID::Height: {
  266. auto maybe_used_height = used_value_for_property([](auto const& paintable_box) { return paintable_box.content_height(); });
  267. if (maybe_used_height.has_value())
  268. return style_value_for_size(Size::make_px(maybe_used_height.release_value()));
  269. return style_value_for_size(layout_node.computed_values().height());
  270. }
  271. case PropertyID::MarginBlockEnd:
  272. return style_value_for_length_box_logical_side(layout_node, layout_node.computed_values().margin(), LogicalSide::BlockEnd);
  273. case PropertyID::MarginBlockStart:
  274. return style_value_for_length_box_logical_side(layout_node, layout_node.computed_values().margin(), LogicalSide::BlockStart);
  275. case PropertyID::MarginBottom:
  276. return style_value_for_length_percentage(layout_node.computed_values().margin().bottom());
  277. case PropertyID::MarginInlineEnd:
  278. return style_value_for_length_box_logical_side(layout_node, layout_node.computed_values().margin(), LogicalSide::InlineEnd);
  279. case PropertyID::MarginInlineStart:
  280. return style_value_for_length_box_logical_side(layout_node, layout_node.computed_values().margin(), LogicalSide::InlineStart);
  281. case PropertyID::MarginLeft:
  282. return style_value_for_length_percentage(layout_node.computed_values().margin().left());
  283. case PropertyID::MarginRight:
  284. return style_value_for_length_percentage(layout_node.computed_values().margin().right());
  285. case PropertyID::MarginTop:
  286. return style_value_for_length_percentage(layout_node.computed_values().margin().top());
  287. case PropertyID::PaddingBlockEnd:
  288. return style_value_for_length_box_logical_side(layout_node, layout_node.computed_values().padding(), LogicalSide::BlockEnd);
  289. case PropertyID::PaddingBlockStart:
  290. return style_value_for_length_box_logical_side(layout_node, layout_node.computed_values().padding(), LogicalSide::BlockStart);
  291. case PropertyID::PaddingBottom:
  292. return style_value_for_length_percentage(layout_node.computed_values().padding().bottom());
  293. case PropertyID::PaddingInlineEnd:
  294. return style_value_for_length_box_logical_side(layout_node, layout_node.computed_values().padding(), LogicalSide::InlineEnd);
  295. case PropertyID::PaddingInlineStart:
  296. return style_value_for_length_box_logical_side(layout_node, layout_node.computed_values().padding(), LogicalSide::InlineStart);
  297. case PropertyID::PaddingLeft:
  298. return style_value_for_length_percentage(layout_node.computed_values().padding().left());
  299. case PropertyID::PaddingRight:
  300. return style_value_for_length_percentage(layout_node.computed_values().padding().right());
  301. case PropertyID::PaddingTop:
  302. return style_value_for_length_percentage(layout_node.computed_values().padding().top());
  303. case PropertyID::Width: {
  304. auto maybe_used_width = used_value_for_property([](auto const& paintable_box) { return paintable_box.content_width(); });
  305. if (maybe_used_width.has_value())
  306. return style_value_for_size(Size::make_px(maybe_used_width.release_value()));
  307. return style_value_for_size(layout_node.computed_values().width());
  308. }
  309. // -> bottom
  310. // -> left
  311. // -> inset-block-end
  312. // -> inset-block-start
  313. // -> inset-inline-end
  314. // -> inset-inline-start
  315. // -> right
  316. // -> top
  317. // -> A resolved value special case property like top defined in another specification
  318. // FIXME: If the property applies to a positioned element and the resolved value of the display property is not
  319. // none or contents, and the property is not over-constrained, then the resolved value is the used value.
  320. // Otherwise the resolved value is the computed value.
  321. case PropertyID::Bottom:
  322. return style_value_for_length_percentage(layout_node.computed_values().inset().bottom());
  323. case PropertyID::InsetBlockEnd:
  324. return style_value_for_length_box_logical_side(layout_node, layout_node.computed_values().inset(), LogicalSide::BlockEnd);
  325. case PropertyID::InsetBlockStart:
  326. return style_value_for_length_box_logical_side(layout_node, layout_node.computed_values().inset(), LogicalSide::BlockStart);
  327. case PropertyID::InsetInlineEnd:
  328. return style_value_for_length_box_logical_side(layout_node, layout_node.computed_values().inset(), LogicalSide::InlineEnd);
  329. case PropertyID::InsetInlineStart:
  330. return style_value_for_length_box_logical_side(layout_node, layout_node.computed_values().inset(), LogicalSide::InlineStart);
  331. case PropertyID::Left:
  332. return style_value_for_length_percentage(layout_node.computed_values().inset().left());
  333. case PropertyID::Right:
  334. return style_value_for_length_percentage(layout_node.computed_values().inset().right());
  335. case PropertyID::Top:
  336. return style_value_for_length_percentage(layout_node.computed_values().inset().top());
  337. // -> A resolved value special case property defined in another specification
  338. // As defined in the relevant specification.
  339. case PropertyID::Transform: {
  340. auto transformations = layout_node.computed_values().transformations();
  341. if (transformations.is_empty())
  342. return CSSKeywordValue::create(Keyword::None);
  343. // https://drafts.csswg.org/css-transforms-2/#serialization-of-the-computed-value
  344. // The transform property is a resolved value special case property. [CSSOM]
  345. // When the computed value is a <transform-list>, the resolved value is one <matrix()> function or one <matrix3d()> function computed by the following algorithm:
  346. // 1. Let transform be a 4x4 matrix initialized to the identity matrix.
  347. // The elements m11, m22, m33 and m44 of transform must be set to 1; all other elements of transform must be set to 0.
  348. auto transform = FloatMatrix4x4::identity();
  349. // 2. Post-multiply all <transform-function>s in <transform-list> to transform.
  350. VERIFY(layout_node.paintable());
  351. auto const& paintable_box = verify_cast<Painting::PaintableBox const>(*layout_node.paintable());
  352. for (auto transformation : transformations) {
  353. transform = transform * transformation.to_matrix(paintable_box).release_value();
  354. }
  355. // https://drafts.csswg.org/css-transforms-1/#2d-matrix
  356. auto is_2d_matrix = [](Gfx::FloatMatrix4x4 const& matrix) -> bool {
  357. // A 3x2 transformation matrix,
  358. // or a 4x4 matrix where the items m31, m32, m13, m23, m43, m14, m24, m34 are equal to 0
  359. // and m33, m44 are equal to 1.
  360. // NOTE: We only care about 4x4 matrices here.
  361. // NOTE: Our elements are 0-indexed not 1-indexed, and in the opposite order.
  362. if (matrix.elements()[0][2] != 0 // m31
  363. || matrix.elements()[1][2] != 0 // m32
  364. || matrix.elements()[2][0] != 0 // m13
  365. || matrix.elements()[2][1] != 0 // m23
  366. || matrix.elements()[2][3] != 0 // m43
  367. || matrix.elements()[3][0] != 0 // m14
  368. || matrix.elements()[3][1] != 0 // m24
  369. || matrix.elements()[3][2] != 0) // m34
  370. return false;
  371. if (matrix.elements()[2][2] != 1 // m33
  372. || matrix.elements()[3][3] != 1) // m44
  373. return false;
  374. return true;
  375. };
  376. // 3. Chose between <matrix()> or <matrix3d()> serialization:
  377. // -> If transform is a 2D matrix
  378. // Serialize transform to a <matrix()> function.
  379. if (is_2d_matrix(transform)) {
  380. StyleValueVector parameters {
  381. NumberStyleValue::create(transform.elements()[0][0]),
  382. NumberStyleValue::create(transform.elements()[1][0]),
  383. NumberStyleValue::create(transform.elements()[0][1]),
  384. NumberStyleValue::create(transform.elements()[1][1]),
  385. NumberStyleValue::create(transform.elements()[0][3]),
  386. NumberStyleValue::create(transform.elements()[1][3]),
  387. };
  388. return TransformationStyleValue::create(TransformFunction::Matrix, move(parameters));
  389. }
  390. // -> Otherwise
  391. // Serialize transform to a <matrix3d()> function.
  392. else {
  393. StyleValueVector parameters {
  394. NumberStyleValue::create(transform.elements()[0][0]),
  395. NumberStyleValue::create(transform.elements()[1][0]),
  396. NumberStyleValue::create(transform.elements()[2][0]),
  397. NumberStyleValue::create(transform.elements()[3][0]),
  398. NumberStyleValue::create(transform.elements()[0][1]),
  399. NumberStyleValue::create(transform.elements()[1][1]),
  400. NumberStyleValue::create(transform.elements()[2][1]),
  401. NumberStyleValue::create(transform.elements()[3][1]),
  402. NumberStyleValue::create(transform.elements()[0][2]),
  403. NumberStyleValue::create(transform.elements()[1][2]),
  404. NumberStyleValue::create(transform.elements()[2][2]),
  405. NumberStyleValue::create(transform.elements()[3][2]),
  406. NumberStyleValue::create(transform.elements()[0][3]),
  407. NumberStyleValue::create(transform.elements()[1][3]),
  408. NumberStyleValue::create(transform.elements()[2][3]),
  409. NumberStyleValue::create(transform.elements()[3][3]),
  410. };
  411. return TransformationStyleValue::create(TransformFunction::Matrix3d, move(parameters));
  412. }
  413. }
  414. // -> Any other property
  415. // The resolved value is the computed value.
  416. // NOTE: This is handled inside the `default` case.
  417. // NOTE: Everything below is a shorthand that requires some manual construction.
  418. case PropertyID::BackgroundPosition:
  419. return style_value_for_background_property(
  420. layout_node,
  421. [](auto& layer) -> NonnullRefPtr<CSSStyleValue> {
  422. return PositionStyleValue::create(
  423. EdgeStyleValue::create(layer.position_edge_x, layer.position_offset_x),
  424. EdgeStyleValue::create(layer.position_edge_y, layer.position_offset_y));
  425. },
  426. []() -> NonnullRefPtr<CSSStyleValue> {
  427. return PositionStyleValue::create(
  428. EdgeStyleValue::create(PositionEdge::Left, Percentage(0)),
  429. EdgeStyleValue::create(PositionEdge::Top, Percentage(0)));
  430. });
  431. case PropertyID::Border: {
  432. auto width = style_value_for_property(layout_node, PropertyID::BorderWidth);
  433. auto style = style_value_for_property(layout_node, PropertyID::BorderStyle);
  434. auto color = style_value_for_property(layout_node, PropertyID::BorderColor);
  435. // `border` only has a reasonable value if all four sides are the same.
  436. if (width->is_value_list() || style->is_value_list() || color->is_value_list())
  437. return nullptr;
  438. return ShorthandStyleValue::create(property_id,
  439. { PropertyID::BorderWidth, PropertyID::BorderStyle, PropertyID::BorderColor },
  440. { width.release_nonnull(), style.release_nonnull(), color.release_nonnull() });
  441. }
  442. case PropertyID::BorderColor: {
  443. auto top = style_value_for_property(layout_node, PropertyID::BorderTopColor);
  444. auto right = style_value_for_property(layout_node, PropertyID::BorderRightColor);
  445. auto bottom = style_value_for_property(layout_node, PropertyID::BorderBottomColor);
  446. auto left = style_value_for_property(layout_node, PropertyID::BorderLeftColor);
  447. return style_value_for_sided_shorthand(top.release_nonnull(), right.release_nonnull(), bottom.release_nonnull(), left.release_nonnull());
  448. }
  449. case PropertyID::BorderStyle: {
  450. auto top = style_value_for_property(layout_node, PropertyID::BorderTopStyle);
  451. auto right = style_value_for_property(layout_node, PropertyID::BorderRightStyle);
  452. auto bottom = style_value_for_property(layout_node, PropertyID::BorderBottomStyle);
  453. auto left = style_value_for_property(layout_node, PropertyID::BorderLeftStyle);
  454. return style_value_for_sided_shorthand(top.release_nonnull(), right.release_nonnull(), bottom.release_nonnull(), left.release_nonnull());
  455. }
  456. case PropertyID::BorderWidth: {
  457. auto top = style_value_for_property(layout_node, PropertyID::BorderTopWidth);
  458. auto right = style_value_for_property(layout_node, PropertyID::BorderRightWidth);
  459. auto bottom = style_value_for_property(layout_node, PropertyID::BorderBottomWidth);
  460. auto left = style_value_for_property(layout_node, PropertyID::BorderLeftWidth);
  461. return style_value_for_sided_shorthand(top.release_nonnull(), right.release_nonnull(), bottom.release_nonnull(), left.release_nonnull());
  462. }
  463. case PropertyID::Margin: {
  464. auto top = style_value_for_property(layout_node, PropertyID::MarginTop);
  465. auto right = style_value_for_property(layout_node, PropertyID::MarginRight);
  466. auto bottom = style_value_for_property(layout_node, PropertyID::MarginBottom);
  467. auto left = style_value_for_property(layout_node, PropertyID::MarginLeft);
  468. return style_value_for_sided_shorthand(top.release_nonnull(), right.release_nonnull(), bottom.release_nonnull(), left.release_nonnull());
  469. }
  470. case PropertyID::Padding: {
  471. auto top = style_value_for_property(layout_node, PropertyID::PaddingTop);
  472. auto right = style_value_for_property(layout_node, PropertyID::PaddingRight);
  473. auto bottom = style_value_for_property(layout_node, PropertyID::PaddingBottom);
  474. auto left = style_value_for_property(layout_node, PropertyID::PaddingLeft);
  475. return style_value_for_sided_shorthand(top.release_nonnull(), right.release_nonnull(), bottom.release_nonnull(), left.release_nonnull());
  476. }
  477. case PropertyID::WebkitTextFillColor:
  478. return CSSColorValue::create_from_color(layout_node.computed_values().webkit_text_fill_color());
  479. case PropertyID::Invalid:
  480. return CSSKeywordValue::create(Keyword::Invalid);
  481. case PropertyID::Custom:
  482. dbgln_if(LIBWEB_CSS_DEBUG, "Computed style for custom properties was requested (?)");
  483. return nullptr;
  484. default:
  485. // For grid-template-columns and grid-template-rows the resolved value is the used value.
  486. // https://www.w3.org/TR/css-grid-2/#resolved-track-list-standalone
  487. if (property_id == PropertyID::GridTemplateColumns) {
  488. if (layout_node.paintable() && layout_node.paintable()->is_paintable_box()) {
  489. auto const& paintable_box = verify_cast<Painting::PaintableBox const>(*layout_node.paintable());
  490. if (auto used_values_for_grid_template_columns = paintable_box.used_values_for_grid_template_columns()) {
  491. return used_values_for_grid_template_columns;
  492. }
  493. }
  494. } else if (property_id == PropertyID::GridTemplateRows) {
  495. if (layout_node.paintable() && layout_node.paintable()->is_paintable_box()) {
  496. auto const& paintable_box = verify_cast<Painting::PaintableBox const>(*layout_node.paintable());
  497. if (auto used_values_for_grid_template_rows = paintable_box.used_values_for_grid_template_rows()) {
  498. return used_values_for_grid_template_rows;
  499. }
  500. }
  501. }
  502. if (!property_is_shorthand(property_id))
  503. return get_computed_value(property_id);
  504. // Handle shorthands in a generic way
  505. auto longhand_ids = longhands_for_shorthand(property_id);
  506. StyleValueVector longhand_values;
  507. longhand_values.ensure_capacity(longhand_ids.size());
  508. for (auto longhand_id : longhand_ids)
  509. longhand_values.append(style_value_for_property(layout_node, longhand_id).release_nonnull());
  510. return ShorthandStyleValue::create(property_id, move(longhand_ids), move(longhand_values));
  511. }
  512. }
  513. Optional<StyleProperty> ResolvedCSSStyleDeclaration::property(PropertyID property_id) const
  514. {
  515. // https://www.w3.org/TR/cssom-1/#dom-window-getcomputedstyle
  516. // NOTE: This is a partial enforcement of step 5 ("If elt is connected, ...")
  517. if (!m_element->is_connected())
  518. return {};
  519. auto get_layout_node = [&]() {
  520. if (m_pseudo_element.has_value())
  521. return m_element->get_pseudo_element_node(m_pseudo_element.value());
  522. return m_element->layout_node();
  523. };
  524. Layout::NodeWithStyle* layout_node = get_layout_node();
  525. // FIXME: Be smarter about updating layout if there's no layout node.
  526. // We may legitimately have no layout node if we're not visible, but this protects against situations
  527. // where we're requesting the computed style before layout has happened.
  528. if (!layout_node || property_affects_layout(property_id)) {
  529. const_cast<DOM::Document&>(m_element->document()).update_layout();
  530. layout_node = get_layout_node();
  531. } else {
  532. // FIXME: If we had a way to update style for a single element, this would be a good place to use it.
  533. const_cast<DOM::Document&>(m_element->document()).update_style();
  534. }
  535. if (!layout_node) {
  536. auto style = m_element->document().style_computer().compute_style(const_cast<DOM::Element&>(*m_element), m_pseudo_element);
  537. // FIXME: This is a stopgap until we implement shorthand -> longhand conversion.
  538. auto value = style->maybe_null_property(property_id);
  539. if (!value) {
  540. dbgln("FIXME: ResolvedCSSStyleDeclaration::property(property_id={:#x}) No value for property ID in newly computed style case.", to_underlying(property_id));
  541. return {};
  542. }
  543. return StyleProperty {
  544. .property_id = property_id,
  545. .value = value.release_nonnull(),
  546. };
  547. }
  548. auto value = style_value_for_property(*layout_node, property_id);
  549. if (!value)
  550. return {};
  551. return StyleProperty {
  552. .property_id = property_id,
  553. .value = value.release_nonnull(),
  554. };
  555. }
  556. static WebIDL::ExceptionOr<void> cannot_modify_computed_property_error(JS::Realm& realm)
  557. {
  558. return WebIDL::NoModificationAllowedError::create(realm, "Cannot modify properties in result of getComputedStyle()"_string);
  559. }
  560. // https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-setproperty
  561. WebIDL::ExceptionOr<void> ResolvedCSSStyleDeclaration::set_property(PropertyID, StringView, StringView)
  562. {
  563. // 1. If the computed flag is set, then throw a NoModificationAllowedError exception.
  564. return cannot_modify_computed_property_error(realm());
  565. }
  566. // https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-setproperty
  567. WebIDL::ExceptionOr<void> ResolvedCSSStyleDeclaration::set_property(StringView, StringView, StringView)
  568. {
  569. // 1. If the computed flag is set, then throw a NoModificationAllowedError exception.
  570. return cannot_modify_computed_property_error(realm());
  571. }
  572. static WebIDL::ExceptionOr<String> cannot_remove_computed_property_error(JS::Realm& realm)
  573. {
  574. return WebIDL::NoModificationAllowedError::create(realm, "Cannot remove properties from result of getComputedStyle()"_string);
  575. }
  576. // https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-removeproperty
  577. WebIDL::ExceptionOr<String> ResolvedCSSStyleDeclaration::remove_property(PropertyID)
  578. {
  579. // 1. If the computed flag is set, then throw a NoModificationAllowedError exception.
  580. return cannot_remove_computed_property_error(realm());
  581. }
  582. // https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-removeproperty
  583. WebIDL::ExceptionOr<String> ResolvedCSSStyleDeclaration::remove_property(StringView)
  584. {
  585. // 1. If the computed flag is set, then throw a NoModificationAllowedError exception.
  586. return cannot_remove_computed_property_error(realm());
  587. }
  588. String ResolvedCSSStyleDeclaration::serialized() const
  589. {
  590. // https://www.w3.org/TR/cssom/#dom-cssstyledeclaration-csstext
  591. // If the computed flag is set, then return the empty string.
  592. // NOTE: ResolvedCSSStyleDeclaration is something you would only get from window.getComputedStyle(),
  593. // which returns what the spec calls "resolved style". The "computed flag" is always set here.
  594. return String {};
  595. }
  596. // https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-csstext
  597. WebIDL::ExceptionOr<void> ResolvedCSSStyleDeclaration::set_css_text(StringView)
  598. {
  599. // 1. If the computed flag is set, then throw a NoModificationAllowedError exception.
  600. return WebIDL::NoModificationAllowedError::create(realm(), "Cannot modify properties in result of getComputedStyle()"_string);
  601. }
  602. }