ResolvedCSSStyleDeclaration.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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::Background: {
  115. auto maybe_background_color = property(CSS::PropertyID::BackgroundColor);
  116. auto maybe_background_image = property(CSS::PropertyID::BackgroundImage);
  117. auto maybe_background_position = property(CSS::PropertyID::BackgroundPosition);
  118. auto maybe_background_size = property(CSS::PropertyID::BackgroundSize);
  119. auto maybe_background_repeat = property(CSS::PropertyID::BackgroundRepeat);
  120. auto maybe_background_attachment = property(CSS::PropertyID::BackgroundAttachment);
  121. auto maybe_background_origin = property(CSS::PropertyID::BackgroundOrigin);
  122. auto maybe_background_clip = property(CSS::PropertyID::BackgroundClip);
  123. return BackgroundStyleValue::create(
  124. value_or_default(maybe_background_color, InitialStyleValue::the()),
  125. value_or_default(maybe_background_image, IdentifierStyleValue::create(CSS::ValueID::None)),
  126. value_or_default(maybe_background_position, PositionStyleValue::create(PositionEdge::Left, Length::make_px(0), PositionEdge::Top, Length::make_px(0))),
  127. value_or_default(maybe_background_size, IdentifierStyleValue::create(CSS::ValueID::Auto)),
  128. value_or_default(maybe_background_repeat, BackgroundRepeatStyleValue::create(CSS::Repeat::Repeat, CSS::Repeat::Repeat)),
  129. value_or_default(maybe_background_attachment, IdentifierStyleValue::create(CSS::ValueID::Scroll)),
  130. value_or_default(maybe_background_origin, IdentifierStyleValue::create(CSS::ValueID::PaddingBox)),
  131. value_or_default(maybe_background_clip, IdentifierStyleValue::create(CSS::ValueID::BorderBox)));
  132. }
  133. case PropertyID::BackgroundColor:
  134. return ColorStyleValue::create(layout_node.computed_values().background_color());
  135. case CSS::PropertyID::BorderBottom: {
  136. auto border = layout_node.computed_values().border_bottom();
  137. auto width = LengthStyleValue::create(Length::make_px(border.width));
  138. auto style = IdentifierStyleValue::create(to_value_id(border.line_style));
  139. auto color = ColorStyleValue::create(border.color);
  140. return BorderStyleValue::create(width, style, color);
  141. }
  142. case CSS::PropertyID::BorderBottomColor:
  143. return ColorStyleValue::create(layout_node.computed_values().border_bottom().color);
  144. case CSS::PropertyID::BorderBottomLeftRadius: {
  145. auto const& border_radius = layout_node.computed_values().border_bottom_left_radius();
  146. return BorderRadiusStyleValue::create(border_radius.horizontal_radius, border_radius.vertical_radius);
  147. }
  148. case CSS::PropertyID::BorderBottomRightRadius: {
  149. auto const& border_radius = layout_node.computed_values().border_bottom_right_radius();
  150. return BorderRadiusStyleValue::create(border_radius.horizontal_radius, border_radius.vertical_radius);
  151. }
  152. case CSS::PropertyID::BorderBottomStyle:
  153. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().border_bottom().line_style));
  154. case CSS::PropertyID::BorderBottomWidth:
  155. return LengthStyleValue::create(Length::make_px(layout_node.computed_values().border_bottom().width));
  156. case CSS::PropertyID::BorderLeft: {
  157. auto border = layout_node.computed_values().border_left();
  158. auto width = LengthStyleValue::create(Length::make_px(border.width));
  159. auto style = IdentifierStyleValue::create(to_value_id(border.line_style));
  160. auto color = ColorStyleValue::create(border.color);
  161. return BorderStyleValue::create(width, style, color);
  162. }
  163. case CSS::PropertyID::BorderLeftColor:
  164. return ColorStyleValue::create(layout_node.computed_values().border_left().color);
  165. case CSS::PropertyID::BorderLeftStyle:
  166. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().border_left().line_style));
  167. case CSS::PropertyID::BorderLeftWidth:
  168. return LengthStyleValue::create(Length::make_px(layout_node.computed_values().border_left().width));
  169. case CSS::PropertyID::BorderRadius: {
  170. auto maybe_top_left_radius = property(CSS::PropertyID::BorderTopLeftRadius);
  171. auto maybe_top_right_radius = property(CSS::PropertyID::BorderTopRightRadius);
  172. auto maybe_bottom_left_radius = property(CSS::PropertyID::BorderBottomLeftRadius);
  173. auto maybe_bottom_right_radius = property(CSS::PropertyID::BorderBottomRightRadius);
  174. RefPtr<BorderRadiusStyleValue> top_left_radius, top_right_radius, bottom_left_radius, bottom_right_radius;
  175. if (maybe_top_left_radius.has_value()) {
  176. VERIFY(maybe_top_left_radius.value().value->is_border_radius());
  177. top_left_radius = maybe_top_left_radius.value().value->as_border_radius();
  178. }
  179. if (maybe_top_right_radius.has_value()) {
  180. VERIFY(maybe_top_right_radius.value().value->is_border_radius());
  181. top_right_radius = maybe_top_right_radius.value().value->as_border_radius();
  182. }
  183. if (maybe_bottom_left_radius.has_value()) {
  184. VERIFY(maybe_bottom_left_radius.value().value->is_border_radius());
  185. bottom_left_radius = maybe_bottom_left_radius.value().value->as_border_radius();
  186. }
  187. if (maybe_bottom_right_radius.has_value()) {
  188. VERIFY(maybe_bottom_right_radius.value().value->is_border_radius());
  189. bottom_right_radius = maybe_bottom_right_radius.value().value->as_border_radius();
  190. }
  191. return BorderRadiusShorthandStyleValue::create(top_left_radius.release_nonnull(), top_right_radius.release_nonnull(), bottom_right_radius.release_nonnull(), bottom_left_radius.release_nonnull());
  192. }
  193. case CSS::PropertyID::BorderRight: {
  194. auto border = layout_node.computed_values().border_right();
  195. auto width = LengthStyleValue::create(Length::make_px(border.width));
  196. auto style = IdentifierStyleValue::create(to_value_id(border.line_style));
  197. auto color = ColorStyleValue::create(border.color);
  198. return BorderStyleValue::create(width, style, color);
  199. }
  200. case CSS::PropertyID::BorderRightColor:
  201. return ColorStyleValue::create(layout_node.computed_values().border_right().color);
  202. case CSS::PropertyID::BorderRightStyle:
  203. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().border_right().line_style));
  204. case CSS::PropertyID::BorderRightWidth:
  205. return LengthStyleValue::create(Length::make_px(layout_node.computed_values().border_right().width));
  206. case CSS::PropertyID::BorderTop: {
  207. auto border = layout_node.computed_values().border_top();
  208. auto width = LengthStyleValue::create(Length::make_px(border.width));
  209. auto style = IdentifierStyleValue::create(to_value_id(border.line_style));
  210. auto color = ColorStyleValue::create(border.color);
  211. return BorderStyleValue::create(width, style, color);
  212. }
  213. case CSS::PropertyID::BorderTopColor:
  214. return ColorStyleValue::create(layout_node.computed_values().border_top().color);
  215. case CSS::PropertyID::BorderTopLeftRadius: {
  216. auto const& border_radius = layout_node.computed_values().border_top_left_radius();
  217. return BorderRadiusStyleValue::create(border_radius.horizontal_radius, border_radius.vertical_radius);
  218. }
  219. case CSS::PropertyID::BorderTopRightRadius: {
  220. auto const& border_radius = layout_node.computed_values().border_top_right_radius();
  221. return BorderRadiusStyleValue::create(border_radius.horizontal_radius, border_radius.vertical_radius);
  222. }
  223. case CSS::PropertyID::BorderTopStyle:
  224. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().border_top().line_style));
  225. case CSS::PropertyID::BorderTopWidth:
  226. return LengthStyleValue::create(Length::make_px(layout_node.computed_values().border_top().width));
  227. case CSS::PropertyID::BoxShadow: {
  228. auto box_shadow_layers = layout_node.computed_values().box_shadow();
  229. if (box_shadow_layers.is_empty())
  230. return {};
  231. auto make_box_shadow_style_value = [](ShadowData const& data) {
  232. return ShadowStyleValue::create(data.color, data.offset_x, data.offset_y, data.blur_radius, data.spread_distance, data.placement);
  233. };
  234. if (box_shadow_layers.size() == 1)
  235. return make_box_shadow_style_value(box_shadow_layers.first());
  236. NonnullRefPtrVector<StyleValue> box_shadow;
  237. box_shadow.ensure_capacity(box_shadow_layers.size());
  238. for (auto const& layer : box_shadow_layers)
  239. box_shadow.append(make_box_shadow_style_value(layer));
  240. return StyleValueList::create(move(box_shadow), StyleValueList::Separator::Comma);
  241. }
  242. case CSS::PropertyID::BoxSizing:
  243. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().box_sizing()));
  244. case CSS::PropertyID::Clear:
  245. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().clear()));
  246. case CSS::PropertyID::Color:
  247. return ColorStyleValue::create(layout_node.computed_values().color());
  248. case CSS::PropertyID::Cursor:
  249. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().cursor()));
  250. case CSS::PropertyID::Display:
  251. return style_value_for_display(layout_node.computed_values().display());
  252. case CSS::PropertyID::FlexBasis: {
  253. switch (layout_node.computed_values().flex_basis().type) {
  254. case FlexBasis::Content:
  255. return IdentifierStyleValue::create(CSS::ValueID::Content);
  256. case FlexBasis::LengthPercentage:
  257. return style_value_for_length_percentage(*layout_node.computed_values().flex_basis().length_percentage);
  258. case FlexBasis::Auto:
  259. return IdentifierStyleValue::create(CSS::ValueID::Auto);
  260. default:
  261. VERIFY_NOT_REACHED();
  262. }
  263. break;
  264. case CSS::PropertyID::FlexDirection:
  265. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().flex_direction()));
  266. case CSS::PropertyID::FlexGrow:
  267. return NumericStyleValue::create_float(layout_node.computed_values().flex_grow());
  268. case CSS::PropertyID::FlexShrink:
  269. return NumericStyleValue::create_float(layout_node.computed_values().flex_shrink());
  270. case CSS::PropertyID::FlexWrap:
  271. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().flex_wrap()));
  272. case CSS::PropertyID::Float:
  273. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().float_()));
  274. case CSS::PropertyID::Height:
  275. return style_value_for_length_percentage(layout_node.computed_values().height());
  276. case CSS::PropertyID::ImageRendering:
  277. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().image_rendering()));
  278. case CSS::PropertyID::JustifyContent:
  279. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().justify_content()));
  280. case CSS::PropertyID::ListStyleType:
  281. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().list_style_type()));
  282. case CSS::PropertyID::Margin: {
  283. auto margin = layout_node.computed_values().margin();
  284. auto values = NonnullRefPtrVector<StyleValue> {};
  285. values.append(style_value_for_length_percentage(margin.top));
  286. values.append(style_value_for_length_percentage(margin.right));
  287. values.append(style_value_for_length_percentage(margin.bottom));
  288. values.append(style_value_for_length_percentage(margin.left));
  289. return StyleValueList::create(move(values), StyleValueList::Separator::Space);
  290. }
  291. case CSS::PropertyID::MarginBottom:
  292. return style_value_for_length_percentage(layout_node.computed_values().margin().bottom);
  293. case CSS::PropertyID::MarginLeft:
  294. return style_value_for_length_percentage(layout_node.computed_values().margin().left);
  295. case CSS::PropertyID::MarginRight:
  296. return style_value_for_length_percentage(layout_node.computed_values().margin().right);
  297. case CSS::PropertyID::MarginTop:
  298. return style_value_for_length_percentage(layout_node.computed_values().margin().top);
  299. case CSS::PropertyID::MaxHeight:
  300. return style_value_for_length_percentage(layout_node.computed_values().max_height());
  301. case CSS::PropertyID::MaxWidth:
  302. return style_value_for_length_percentage(layout_node.computed_values().max_width());
  303. case CSS::PropertyID::MinHeight:
  304. return style_value_for_length_percentage(layout_node.computed_values().min_height());
  305. case CSS::PropertyID::MinWidth:
  306. return style_value_for_length_percentage(layout_node.computed_values().min_width());
  307. case CSS::PropertyID::Opacity:
  308. return NumericStyleValue::create_float(layout_node.computed_values().opacity());
  309. case CSS::PropertyID::Order:
  310. return NumericStyleValue::create_integer(layout_node.computed_values().order());
  311. case CSS::PropertyID::OverflowX:
  312. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().overflow_x()));
  313. case CSS::PropertyID::OverflowY:
  314. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().overflow_y()));
  315. case CSS::PropertyID::Padding: {
  316. auto padding = layout_node.computed_values().padding();
  317. auto values = NonnullRefPtrVector<StyleValue> {};
  318. values.append(style_value_for_length_percentage(padding.top));
  319. values.append(style_value_for_length_percentage(padding.right));
  320. values.append(style_value_for_length_percentage(padding.bottom));
  321. values.append(style_value_for_length_percentage(padding.left));
  322. return StyleValueList::create(move(values), StyleValueList::Separator::Space);
  323. }
  324. case CSS::PropertyID::PaddingBottom:
  325. case CSS::PropertyID::PaddingLeft:
  326. return style_value_for_length_percentage(layout_node.computed_values().padding().left);
  327. return style_value_for_length_percentage(layout_node.computed_values().padding().bottom);
  328. case CSS::PropertyID::PaddingRight:
  329. return style_value_for_length_percentage(layout_node.computed_values().padding().right);
  330. case CSS::PropertyID::PaddingTop:
  331. return style_value_for_length_percentage(layout_node.computed_values().padding().top);
  332. case CSS::PropertyID::Position:
  333. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().position()));
  334. case CSS::PropertyID::TextAlign:
  335. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().text_align()));
  336. case CSS::PropertyID::TextDecorationLine: {
  337. auto text_decoration_lines = layout_node.computed_values().text_decoration_line();
  338. if (text_decoration_lines.is_empty())
  339. return IdentifierStyleValue::create(ValueID::None);
  340. NonnullRefPtrVector<StyleValue> style_values;
  341. for (auto const& line : text_decoration_lines) {
  342. style_values.append(IdentifierStyleValue::create(to_value_id(line)));
  343. }
  344. return StyleValueList::create(move(style_values), StyleValueList::Separator::Space);
  345. }
  346. case CSS::PropertyID::TextDecorationStyle:
  347. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().text_decoration_style()));
  348. case CSS::PropertyID::TextTransform:
  349. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().text_transform()));
  350. case CSS::PropertyID::VerticalAlign:
  351. if (auto const* length_percentage = layout_node.computed_values().vertical_align().get_pointer<CSS::LengthPercentage>()) {
  352. if (length_percentage->is_length())
  353. return LengthStyleValue::create(length_percentage->length());
  354. if (length_percentage->is_percentage())
  355. return PercentageStyleValue::create(length_percentage->percentage());
  356. VERIFY_NOT_REACHED();
  357. }
  358. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().vertical_align().get<CSS::VerticalAlign>()));
  359. case CSS::PropertyID::WhiteSpace:
  360. return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().white_space()));
  361. case CSS::PropertyID::Width:
  362. return style_value_for_length_percentage(layout_node.computed_values().width());
  363. case CSS::PropertyID::ZIndex: {
  364. auto maybe_z_index = layout_node.computed_values().z_index();
  365. if (!maybe_z_index.has_value())
  366. return {};
  367. return NumericStyleValue::create_integer(maybe_z_index.release_value());
  368. }
  369. case CSS::PropertyID::Invalid:
  370. return IdentifierStyleValue::create(CSS::ValueID::Invalid);
  371. case CSS::PropertyID::Custom:
  372. dbgln_if(LIBWEB_CSS_DEBUG, "Computed style for custom properties was requested (?)");
  373. return {};
  374. default:
  375. dbgln_if(LIBWEB_CSS_DEBUG, "FIXME: Computed style for the '{}' property was requested", string_from_property_id(property_id));
  376. return {};
  377. }
  378. }
  379. }
  380. Optional<StyleProperty> ResolvedCSSStyleDeclaration::property(PropertyID property_id) const
  381. {
  382. if (CSS::property_affects_layout(property_id)) {
  383. const_cast<DOM::Document&>(m_element->document()).update_layout();
  384. } else {
  385. // FIXME: If we had a way to update style for a single element, this would be a good place to use it.
  386. const_cast<DOM::Document&>(m_element->document()).update_style();
  387. }
  388. if (!m_element->layout_node()) {
  389. auto style = m_element->document().style_computer().compute_style(const_cast<DOM::Element&>(*m_element));
  390. return StyleProperty {
  391. .property_id = property_id,
  392. .value = style->property(property_id),
  393. };
  394. }
  395. auto& layout_node = *m_element->layout_node();
  396. auto value = style_value_for_property(layout_node, property_id);
  397. if (!value)
  398. return {};
  399. return StyleProperty {
  400. .property_id = property_id,
  401. .value = value.release_nonnull(),
  402. };
  403. }
  404. // https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-setproperty
  405. DOM::ExceptionOr<void> ResolvedCSSStyleDeclaration::set_property(PropertyID, StringView, StringView)
  406. {
  407. // 1. If the computed flag is set, then throw a NoModificationAllowedError exception.
  408. return DOM::NoModificationAllowedError::create("Cannot modify properties in result of getComputedStyle()");
  409. }
  410. // https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-removeproperty
  411. DOM::ExceptionOr<String> ResolvedCSSStyleDeclaration::remove_property(PropertyID)
  412. {
  413. // 1. If the computed flag is set, then throw a NoModificationAllowedError exception.
  414. return DOM::NoModificationAllowedError::create("Cannot remove properties from result of getComputedStyle()");
  415. }
  416. String ResolvedCSSStyleDeclaration::serialized() const
  417. {
  418. // https://www.w3.org/TR/cssom/#dom-cssstyledeclaration-csstext
  419. // If the computed flag is set, then return the empty string.
  420. // NOTE: ResolvedCSSStyleDeclaration is something you would only get from window.getComputedStyle(),
  421. // which returns what the spec calls "resolved style". The "computed flag" is always set here.
  422. return String::empty();
  423. }
  424. }