ResolvedCSSStyleDeclaration.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. * Copyright (c) 2021-2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Debug.h>
  8. #include <AK/NonnullRefPtr.h>
  9. #include <LibWeb/CSS/Enums.h>
  10. #include <LibWeb/CSS/ResolvedCSSStyleDeclaration.h>
  11. #include <LibWeb/CSS/StyleComputer.h>
  12. #include <LibWeb/DOM/Document.h>
  13. #include <LibWeb/DOM/Element.h>
  14. namespace Web::CSS {
  15. ResolvedCSSStyleDeclaration::ResolvedCSSStyleDeclaration(DOM::Element& element)
  16. : m_element(element)
  17. {
  18. }
  19. size_t ResolvedCSSStyleDeclaration::length() const
  20. {
  21. return 0;
  22. }
  23. String ResolvedCSSStyleDeclaration::item(size_t index) const
  24. {
  25. (void)index;
  26. return {};
  27. }
  28. static RefPtr<StyleValue> style_value_for_display(CSS::Display display)
  29. {
  30. if (display.is_none())
  31. return IdentifierStyleValue::create(CSS::ValueID::None);
  32. if (display.is_outside_and_inside()) {
  33. NonnullRefPtrVector<StyleValue> values;
  34. switch (display.outside()) {
  35. case CSS::Display::Outside::Inline:
  36. values.append(IdentifierStyleValue::create(CSS::ValueID::Inline));
  37. break;
  38. case CSS::Display::Outside::Block:
  39. values.append(IdentifierStyleValue::create(CSS::ValueID::Block));
  40. break;
  41. case CSS::Display::Outside::RunIn:
  42. values.append(IdentifierStyleValue::create(CSS::ValueID::RunIn));
  43. break;
  44. }
  45. switch (display.inside()) {
  46. case CSS::Display::Inside::Flow:
  47. values.append(IdentifierStyleValue::create(CSS::ValueID::Flow));
  48. break;
  49. case CSS::Display::Inside::FlowRoot:
  50. values.append(IdentifierStyleValue::create(CSS::ValueID::FlowRoot));
  51. break;
  52. case CSS::Display::Inside::Table:
  53. values.append(IdentifierStyleValue::create(CSS::ValueID::Table));
  54. break;
  55. case CSS::Display::Inside::Flex:
  56. values.append(IdentifierStyleValue::create(CSS::ValueID::Flex));
  57. break;
  58. case CSS::Display::Inside::Grid:
  59. values.append(IdentifierStyleValue::create(CSS::ValueID::Grid));
  60. break;
  61. case CSS::Display::Inside::Ruby:
  62. values.append(IdentifierStyleValue::create(CSS::ValueID::Ruby));
  63. break;
  64. }
  65. return StyleValueList::create(move(values), StyleValueList::Separator::Space);
  66. }
  67. if (display.is_internal()) {
  68. switch (display.internal()) {
  69. case CSS::Display::Internal::TableRowGroup:
  70. return IdentifierStyleValue::create(CSS::ValueID::TableRowGroup);
  71. case CSS::Display::Internal::TableHeaderGroup:
  72. return IdentifierStyleValue::create(CSS::ValueID::TableHeaderGroup);
  73. case CSS::Display::Internal::TableFooterGroup:
  74. return IdentifierStyleValue::create(CSS::ValueID::TableFooterGroup);
  75. case CSS::Display::Internal::TableRow:
  76. return IdentifierStyleValue::create(CSS::ValueID::TableRow);
  77. case CSS::Display::Internal::TableCell:
  78. return IdentifierStyleValue::create(CSS::ValueID::TableCell);
  79. case CSS::Display::Internal::TableColumnGroup:
  80. return IdentifierStyleValue::create(CSS::ValueID::TableColumnGroup);
  81. case CSS::Display::Internal::TableColumn:
  82. return IdentifierStyleValue::create(CSS::ValueID::TableColumn);
  83. case CSS::Display::Internal::TableCaption:
  84. return IdentifierStyleValue::create(CSS::ValueID::TableCaption);
  85. case CSS::Display::Internal::RubyBase:
  86. return IdentifierStyleValue::create(CSS::ValueID::RubyBase);
  87. case CSS::Display::Internal::RubyText:
  88. return IdentifierStyleValue::create(CSS::ValueID::RubyText);
  89. case CSS::Display::Internal::RubyBaseContainer:
  90. return IdentifierStyleValue::create(CSS::ValueID::RubyBaseContainer);
  91. case CSS::Display::Internal::RubyTextContainer:
  92. return IdentifierStyleValue::create(CSS::ValueID::RubyTextContainer);
  93. }
  94. }
  95. TODO();
  96. }
  97. static NonnullRefPtr<StyleValue> value_or_default(Optional<StyleProperty> property, NonnullRefPtr<StyleValue> default_style)
  98. {
  99. if (property.has_value())
  100. return property.value().value;
  101. return default_style;
  102. }
  103. static NonnullRefPtr<StyleValue> style_value_for_length_percentage(LengthPercentage const& length_percentage)
  104. {
  105. if (length_percentage.is_percentage())
  106. return PercentageStyleValue::create(length_percentage.percentage());
  107. if (length_percentage.is_length())
  108. return LengthStyleValue::create(length_percentage.length());
  109. return length_percentage.calculated();
  110. }
  111. RefPtr<StyleValue> ResolvedCSSStyleDeclaration::style_value_for_property(Layout::NodeWithStyle const& layout_node, PropertyID property_id) const
  112. {
  113. switch (property_id) {
  114. case CSS::PropertyID::Float:
  115. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().float_()));
  116. case CSS::PropertyID::Clear:
  117. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().clear()));
  118. case CSS::PropertyID::Cursor:
  119. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().cursor()));
  120. case CSS::PropertyID::Display:
  121. return style_value_for_display(layout_node.computed_values().display());
  122. case CSS::PropertyID::ZIndex: {
  123. auto maybe_z_index = layout_node.computed_values().z_index();
  124. if (!maybe_z_index.has_value())
  125. return {};
  126. return NumericStyleValue::create_integer(maybe_z_index.release_value());
  127. }
  128. case CSS::PropertyID::TextAlign:
  129. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().text_align()));
  130. case CSS::PropertyID::TextDecorationLine: {
  131. auto text_decoration_lines = layout_node.computed_values().text_decoration_line();
  132. if (text_decoration_lines.is_empty())
  133. return IdentifierStyleValue::create(ValueID::None);
  134. NonnullRefPtrVector<StyleValue> style_values;
  135. for (auto const& line : text_decoration_lines) {
  136. style_values.append(IdentifierStyleValue::create(to_value_id(line)));
  137. }
  138. return StyleValueList::create(move(style_values), StyleValueList::Separator::Space);
  139. }
  140. case CSS::PropertyID::TextDecorationStyle:
  141. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().text_decoration_style()));
  142. case CSS::PropertyID::TextTransform:
  143. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().text_transform()));
  144. case CSS::PropertyID::Position:
  145. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().position()));
  146. case CSS::PropertyID::WhiteSpace:
  147. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().white_space()));
  148. case CSS::PropertyID::FlexDirection:
  149. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().flex_direction()));
  150. case CSS::PropertyID::FlexWrap:
  151. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().flex_wrap()));
  152. case CSS::PropertyID::FlexBasis: {
  153. switch (layout_node.computed_values().flex_basis().type) {
  154. case FlexBasis::Content:
  155. return IdentifierStyleValue::create(CSS::ValueID::Content);
  156. case FlexBasis::LengthPercentage:
  157. return style_value_for_length_percentage(*layout_node.computed_values().flex_basis().length_percentage);
  158. case FlexBasis::Auto:
  159. return IdentifierStyleValue::create(CSS::ValueID::Auto);
  160. default:
  161. VERIFY_NOT_REACHED();
  162. }
  163. break;
  164. case CSS::PropertyID::FlexGrow:
  165. return NumericStyleValue::create_float(layout_node.computed_values().flex_grow());
  166. case CSS::PropertyID::FlexShrink:
  167. return NumericStyleValue::create_float(layout_node.computed_values().flex_shrink());
  168. case CSS::PropertyID::Order:
  169. return NumericStyleValue::create_integer(layout_node.computed_values().order());
  170. case CSS::PropertyID::Opacity:
  171. return NumericStyleValue::create_float(layout_node.computed_values().opacity());
  172. case CSS::PropertyID::ImageRendering:
  173. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().image_rendering()));
  174. case CSS::PropertyID::JustifyContent:
  175. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().justify_content()));
  176. case CSS::PropertyID::BoxShadow: {
  177. auto box_shadow_layers = layout_node.computed_values().box_shadow();
  178. if (box_shadow_layers.is_empty())
  179. return {};
  180. auto make_box_shadow_style_value = [](ShadowData const& data) {
  181. return ShadowStyleValue::create(data.color, data.offset_x, data.offset_y, data.blur_radius, data.spread_distance, data.placement);
  182. };
  183. if (box_shadow_layers.size() == 1)
  184. return make_box_shadow_style_value(box_shadow_layers.first());
  185. NonnullRefPtrVector<StyleValue> box_shadow;
  186. box_shadow.ensure_capacity(box_shadow_layers.size());
  187. for (auto const& layer : box_shadow_layers)
  188. box_shadow.append(make_box_shadow_style_value(layer));
  189. return StyleValueList::create(move(box_shadow), StyleValueList::Separator::Comma);
  190. }
  191. case CSS::PropertyID::Width:
  192. return style_value_for_length_percentage(layout_node.computed_values().width().value_or(Length::make_auto()));
  193. case CSS::PropertyID::MinWidth:
  194. if (!layout_node.computed_values().min_width().has_value())
  195. return IdentifierStyleValue::create(CSS::ValueID::Auto);
  196. return style_value_for_length_percentage(layout_node.computed_values().min_width().value());
  197. case CSS::PropertyID::MaxWidth:
  198. if (!layout_node.computed_values().max_width().has_value())
  199. return IdentifierStyleValue::create(CSS::ValueID::None);
  200. return style_value_for_length_percentage(layout_node.computed_values().max_width().value());
  201. case CSS::PropertyID::Height:
  202. return style_value_for_length_percentage(layout_node.computed_values().height().value_or(Length::make_auto()));
  203. case CSS::PropertyID::MinHeight:
  204. if (!layout_node.computed_values().min_height().has_value())
  205. return IdentifierStyleValue::create(CSS::ValueID::Auto);
  206. return style_value_for_length_percentage(layout_node.computed_values().min_height().value());
  207. case CSS::PropertyID::MaxHeight:
  208. if (!layout_node.computed_values().max_height().has_value())
  209. return IdentifierStyleValue::create(CSS::ValueID::None);
  210. return style_value_for_length_percentage(layout_node.computed_values().max_height().value());
  211. case CSS::PropertyID::Margin: {
  212. auto margin = layout_node.computed_values().margin();
  213. auto values = NonnullRefPtrVector<StyleValue> {};
  214. values.append(style_value_for_length_percentage(margin.top));
  215. values.append(style_value_for_length_percentage(margin.right));
  216. values.append(style_value_for_length_percentage(margin.bottom));
  217. values.append(style_value_for_length_percentage(margin.left));
  218. return StyleValueList::create(move(values), StyleValueList::Separator::Space);
  219. }
  220. case CSS::PropertyID::MarginTop:
  221. return style_value_for_length_percentage(layout_node.computed_values().margin().top);
  222. case CSS::PropertyID::MarginRight:
  223. return style_value_for_length_percentage(layout_node.computed_values().margin().right);
  224. case CSS::PropertyID::MarginBottom:
  225. return style_value_for_length_percentage(layout_node.computed_values().margin().bottom);
  226. case CSS::PropertyID::MarginLeft:
  227. return style_value_for_length_percentage(layout_node.computed_values().margin().left);
  228. case CSS::PropertyID::Padding: {
  229. auto padding = layout_node.computed_values().padding();
  230. auto values = NonnullRefPtrVector<StyleValue> {};
  231. values.append(style_value_for_length_percentage(padding.top));
  232. values.append(style_value_for_length_percentage(padding.right));
  233. values.append(style_value_for_length_percentage(padding.bottom));
  234. values.append(style_value_for_length_percentage(padding.left));
  235. return StyleValueList::create(move(values), StyleValueList::Separator::Space);
  236. }
  237. case CSS::PropertyID::PaddingTop:
  238. return style_value_for_length_percentage(layout_node.computed_values().padding().top);
  239. case CSS::PropertyID::PaddingRight:
  240. return style_value_for_length_percentage(layout_node.computed_values().padding().right);
  241. case CSS::PropertyID::PaddingBottom:
  242. return style_value_for_length_percentage(layout_node.computed_values().padding().bottom);
  243. case CSS::PropertyID::PaddingLeft:
  244. return style_value_for_length_percentage(layout_node.computed_values().padding().left);
  245. case CSS::PropertyID::BorderRadius: {
  246. auto maybe_top_left_radius = property(CSS::PropertyID::BorderTopLeftRadius);
  247. auto maybe_top_right_radius = property(CSS::PropertyID::BorderTopRightRadius);
  248. auto maybe_bottom_left_radius = property(CSS::PropertyID::BorderBottomLeftRadius);
  249. auto maybe_bottom_right_radius = property(CSS::PropertyID::BorderBottomRightRadius);
  250. RefPtr<BorderRadiusStyleValue> top_left_radius, top_right_radius, bottom_left_radius, bottom_right_radius;
  251. if (maybe_top_left_radius.has_value()) {
  252. VERIFY(maybe_top_left_radius.value().value->is_border_radius());
  253. top_left_radius = maybe_top_left_radius.value().value->as_border_radius();
  254. }
  255. if (maybe_top_right_radius.has_value()) {
  256. VERIFY(maybe_top_right_radius.value().value->is_border_radius());
  257. top_right_radius = maybe_top_right_radius.value().value->as_border_radius();
  258. }
  259. if (maybe_bottom_left_radius.has_value()) {
  260. VERIFY(maybe_bottom_left_radius.value().value->is_border_radius());
  261. bottom_left_radius = maybe_bottom_left_radius.value().value->as_border_radius();
  262. }
  263. if (maybe_bottom_right_radius.has_value()) {
  264. VERIFY(maybe_bottom_right_radius.value().value->is_border_radius());
  265. bottom_right_radius = maybe_bottom_right_radius.value().value->as_border_radius();
  266. }
  267. return BorderRadiusShorthandStyleValue::create(top_left_radius.release_nonnull(), top_right_radius.release_nonnull(), bottom_right_radius.release_nonnull(), bottom_left_radius.release_nonnull());
  268. }
  269. // FIXME: The two radius components are not yet stored, as we currently don't actually render them.
  270. case CSS::PropertyID::BorderBottomLeftRadius: {
  271. auto const& border_radius = layout_node.computed_values().border_bottom_left_radius();
  272. return BorderRadiusStyleValue::create(border_radius.horizontal_radius, border_radius.vertical_radius);
  273. }
  274. case CSS::PropertyID::BorderBottomRightRadius: {
  275. auto const& border_radius = layout_node.computed_values().border_bottom_right_radius();
  276. return BorderRadiusStyleValue::create(border_radius.horizontal_radius, border_radius.vertical_radius);
  277. }
  278. case CSS::PropertyID::BorderTopLeftRadius: {
  279. auto const& border_radius = layout_node.computed_values().border_top_left_radius();
  280. return BorderRadiusStyleValue::create(border_radius.horizontal_radius, border_radius.vertical_radius);
  281. }
  282. case CSS::PropertyID::BorderTopRightRadius: {
  283. auto const& border_radius = layout_node.computed_values().border_top_right_radius();
  284. return BorderRadiusStyleValue::create(border_radius.horizontal_radius, border_radius.vertical_radius);
  285. }
  286. case CSS::PropertyID::BorderTopWidth:
  287. return LengthStyleValue::create(Length::make_px(layout_node.computed_values().border_top().width));
  288. case CSS::PropertyID::BorderRightWidth:
  289. return LengthStyleValue::create(Length::make_px(layout_node.computed_values().border_right().width));
  290. case CSS::PropertyID::BorderBottomWidth:
  291. return LengthStyleValue::create(Length::make_px(layout_node.computed_values().border_bottom().width));
  292. case CSS::PropertyID::BorderLeftWidth:
  293. return LengthStyleValue::create(Length::make_px(layout_node.computed_values().border_left().width));
  294. case CSS::PropertyID::BorderTopColor:
  295. return ColorStyleValue::create(layout_node.computed_values().border_top().color);
  296. case CSS::PropertyID::BorderRightColor:
  297. return ColorStyleValue::create(layout_node.computed_values().border_right().color);
  298. case CSS::PropertyID::BorderBottomColor:
  299. return ColorStyleValue::create(layout_node.computed_values().border_bottom().color);
  300. case CSS::PropertyID::BorderLeftColor:
  301. return ColorStyleValue::create(layout_node.computed_values().border_left().color);
  302. case CSS::PropertyID::BorderTopStyle:
  303. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().border_top().line_style));
  304. case CSS::PropertyID::BorderRightStyle:
  305. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().border_right().line_style));
  306. case CSS::PropertyID::BorderBottomStyle:
  307. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().border_bottom().line_style));
  308. case CSS::PropertyID::BorderLeftStyle:
  309. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().border_left().line_style));
  310. case CSS::PropertyID::BorderTop: {
  311. auto border = layout_node.computed_values().border_top();
  312. auto width = LengthStyleValue::create(Length::make_px(border.width));
  313. auto style = IdentifierStyleValue::create(to_value_id(border.line_style));
  314. auto color = ColorStyleValue::create(border.color);
  315. return BorderStyleValue::create(width, style, color);
  316. }
  317. case CSS::PropertyID::BorderRight: {
  318. auto border = layout_node.computed_values().border_right();
  319. auto width = LengthStyleValue::create(Length::make_px(border.width));
  320. auto style = IdentifierStyleValue::create(to_value_id(border.line_style));
  321. auto color = ColorStyleValue::create(border.color);
  322. return BorderStyleValue::create(width, style, color);
  323. }
  324. case CSS::PropertyID::BorderBottom: {
  325. auto border = layout_node.computed_values().border_bottom();
  326. auto width = LengthStyleValue::create(Length::make_px(border.width));
  327. auto style = IdentifierStyleValue::create(to_value_id(border.line_style));
  328. auto color = ColorStyleValue::create(border.color);
  329. return BorderStyleValue::create(width, style, color);
  330. }
  331. case CSS::PropertyID::BorderLeft: {
  332. auto border = layout_node.computed_values().border_left();
  333. auto width = LengthStyleValue::create(Length::make_px(border.width));
  334. auto style = IdentifierStyleValue::create(to_value_id(border.line_style));
  335. auto color = ColorStyleValue::create(border.color);
  336. return BorderStyleValue::create(width, style, color);
  337. }
  338. case CSS::PropertyID::OverflowX:
  339. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().overflow_x()));
  340. case CSS::PropertyID::OverflowY:
  341. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().overflow_y()));
  342. case CSS::PropertyID::Color:
  343. return ColorStyleValue::create(layout_node.computed_values().color());
  344. case PropertyID::BackgroundColor:
  345. return ColorStyleValue::create(layout_node.computed_values().background_color());
  346. case CSS::PropertyID::Background: {
  347. auto maybe_background_color = property(CSS::PropertyID::BackgroundColor);
  348. auto maybe_background_image = property(CSS::PropertyID::BackgroundImage);
  349. auto maybe_background_position = property(CSS::PropertyID::BackgroundPosition);
  350. auto maybe_background_size = property(CSS::PropertyID::BackgroundSize);
  351. auto maybe_background_repeat = property(CSS::PropertyID::BackgroundRepeat);
  352. auto maybe_background_attachment = property(CSS::PropertyID::BackgroundAttachment);
  353. auto maybe_background_origin = property(CSS::PropertyID::BackgroundOrigin);
  354. auto maybe_background_clip = property(CSS::PropertyID::BackgroundClip);
  355. return BackgroundStyleValue::create(
  356. value_or_default(maybe_background_color, InitialStyleValue::the()),
  357. value_or_default(maybe_background_image, IdentifierStyleValue::create(CSS::ValueID::None)),
  358. value_or_default(maybe_background_position, PositionStyleValue::create(PositionEdge::Left, Length::make_px(0), PositionEdge::Top, Length::make_px(0))),
  359. value_or_default(maybe_background_size, IdentifierStyleValue::create(CSS::ValueID::Auto)),
  360. value_or_default(maybe_background_repeat, BackgroundRepeatStyleValue::create(CSS::Repeat::Repeat, CSS::Repeat::Repeat)),
  361. value_or_default(maybe_background_attachment, IdentifierStyleValue::create(CSS::ValueID::Scroll)),
  362. value_or_default(maybe_background_origin, IdentifierStyleValue::create(CSS::ValueID::PaddingBox)),
  363. value_or_default(maybe_background_clip, IdentifierStyleValue::create(CSS::ValueID::BorderBox)));
  364. }
  365. case CSS::PropertyID::VerticalAlign:
  366. if (auto const* length_percentage = layout_node.computed_values().vertical_align().get_pointer<CSS::LengthPercentage>()) {
  367. if (length_percentage->is_length())
  368. return LengthStyleValue::create(length_percentage->length());
  369. if (length_percentage->is_percentage())
  370. return PercentageStyleValue::create(length_percentage->percentage());
  371. VERIFY_NOT_REACHED();
  372. }
  373. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().vertical_align().get<CSS::VerticalAlign>()));
  374. case CSS::PropertyID::ListStyleType:
  375. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().list_style_type()));
  376. case CSS::PropertyID::BoxSizing:
  377. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().box_sizing()));
  378. case CSS::PropertyID::Invalid:
  379. return IdentifierStyleValue::create(CSS::ValueID::Invalid);
  380. case CSS::PropertyID::Custom:
  381. dbgln_if(LIBWEB_CSS_DEBUG, "Computed style for custom properties was requested (?)");
  382. return {};
  383. default:
  384. dbgln_if(LIBWEB_CSS_DEBUG, "FIXME: Computed style for the '{}' property was requested", string_from_property_id(property_id));
  385. return {};
  386. }
  387. }
  388. }
  389. Optional<StyleProperty> ResolvedCSSStyleDeclaration::property(PropertyID property_id) const
  390. {
  391. if (CSS::property_affects_layout(property_id)) {
  392. const_cast<DOM::Document&>(m_element->document()).update_layout();
  393. } else {
  394. // FIXME: If we had a way to update style for a single element, this would be a good place to use it.
  395. const_cast<DOM::Document&>(m_element->document()).update_style();
  396. }
  397. if (!m_element->layout_node()) {
  398. auto style = m_element->document().style_computer().compute_style(const_cast<DOM::Element&>(*m_element));
  399. return StyleProperty {
  400. .property_id = property_id,
  401. .value = style->property(property_id),
  402. };
  403. return {};
  404. }
  405. auto& layout_node = *m_element->layout_node();
  406. auto value = style_value_for_property(layout_node, property_id);
  407. if (!value)
  408. return {};
  409. return StyleProperty {
  410. .property_id = property_id,
  411. .value = value.release_nonnull(),
  412. };
  413. }
  414. // https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-setproperty
  415. DOM::ExceptionOr<void> ResolvedCSSStyleDeclaration::set_property(PropertyID, StringView, StringView)
  416. {
  417. // 1. If the computed flag is set, then throw a NoModificationAllowedError exception.
  418. return DOM::NoModificationAllowedError::create("Cannot modify properties in result of getComputedStyle()");
  419. }
  420. // https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-removeproperty
  421. DOM::ExceptionOr<String> ResolvedCSSStyleDeclaration::remove_property(PropertyID)
  422. {
  423. // 1. If the computed flag is set, then throw a NoModificationAllowedError exception.
  424. return DOM::NoModificationAllowedError::create("Cannot remove properties from result of getComputedStyle()");
  425. }
  426. String ResolvedCSSStyleDeclaration::serialized() const
  427. {
  428. // https://www.w3.org/TR/cssom/#dom-cssstyledeclaration-csstext
  429. // If the computed flag is set, then return the empty string.
  430. // NOTE: ResolvedCSSStyleDeclaration is something you would only get from window.getComputedStyle(),
  431. // which returns what the spec calls "resolved style". The "computed flag" is always set here.
  432. return String::empty();
  433. }
  434. }