ResolvedCSSStyleDeclaration.cpp 32 KB

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