ResolvedCSSStyleDeclaration.cpp 30 KB

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