StyleResolver.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, the SerenityOS developers.
  4. * Copyright (c) 2021, Sam Atkins <atkinssj@gmail.com>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/QuickSort.h>
  9. #include <LibWeb/CSS/CSSStyleRule.h>
  10. #include <LibWeb/CSS/Parser/Parser.h>
  11. #include <LibWeb/CSS/SelectorEngine.h>
  12. #include <LibWeb/CSS/StyleResolver.h>
  13. #include <LibWeb/CSS/StyleSheet.h>
  14. #include <LibWeb/DOM/Document.h>
  15. #include <LibWeb/DOM/Element.h>
  16. #include <LibWeb/Dump.h>
  17. #include <ctype.h>
  18. #include <stdio.h>
  19. namespace Web::CSS {
  20. StyleResolver::StyleResolver(DOM::Document& document)
  21. : m_document(document)
  22. {
  23. }
  24. StyleResolver::~StyleResolver()
  25. {
  26. }
  27. static StyleSheet& default_stylesheet()
  28. {
  29. static StyleSheet* sheet;
  30. if (!sheet) {
  31. extern char const default_stylesheet_source[];
  32. String css = default_stylesheet_source;
  33. sheet = parse_css(CSS::ParsingContext(), css).leak_ref();
  34. }
  35. return *sheet;
  36. }
  37. static StyleSheet& quirks_mode_stylesheet()
  38. {
  39. static StyleSheet* sheet;
  40. if (!sheet) {
  41. extern char const quirks_mode_stylesheet_source[];
  42. String css = quirks_mode_stylesheet_source;
  43. sheet = parse_css(CSS::ParsingContext(), css).leak_ref();
  44. }
  45. return *sheet;
  46. }
  47. template<typename Callback>
  48. void StyleResolver::for_each_stylesheet(Callback callback) const
  49. {
  50. callback(default_stylesheet());
  51. if (document().in_quirks_mode())
  52. callback(quirks_mode_stylesheet());
  53. for (auto& sheet : document().style_sheets().sheets()) {
  54. callback(sheet);
  55. }
  56. }
  57. Vector<MatchingRule> StyleResolver::collect_matching_rules(DOM::Element const& element) const
  58. {
  59. Vector<MatchingRule> matching_rules;
  60. size_t style_sheet_index = 0;
  61. for_each_stylesheet([&](auto& sheet) {
  62. size_t rule_index = 0;
  63. static_cast<CSSStyleSheet const&>(sheet).for_each_effective_style_rule([&](auto& rule) {
  64. size_t selector_index = 0;
  65. for (auto& selector : rule.selectors()) {
  66. if (SelectorEngine::matches(selector, element)) {
  67. matching_rules.append({ rule, style_sheet_index, rule_index, selector_index, selector.specificity() });
  68. break;
  69. }
  70. ++selector_index;
  71. }
  72. ++rule_index;
  73. });
  74. ++style_sheet_index;
  75. });
  76. return matching_rules;
  77. }
  78. void StyleResolver::sort_matching_rules(Vector<MatchingRule>& matching_rules) const
  79. {
  80. quick_sort(matching_rules, [&](MatchingRule& a, MatchingRule& b) {
  81. auto& a_selector = a.rule->selectors()[a.selector_index];
  82. auto& b_selector = b.rule->selectors()[b.selector_index];
  83. auto a_specificity = a_selector.specificity();
  84. auto b_specificity = b_selector.specificity();
  85. if (a_selector.specificity() == b_selector.specificity()) {
  86. if (a.style_sheet_index == b.style_sheet_index)
  87. return a.rule_index < b.rule_index;
  88. return a.style_sheet_index < b.style_sheet_index;
  89. }
  90. return a_specificity < b_specificity;
  91. });
  92. }
  93. enum class Edge {
  94. Top,
  95. Right,
  96. Bottom,
  97. Left,
  98. All,
  99. };
  100. static bool contains(Edge a, Edge b)
  101. {
  102. return a == b || b == Edge::All;
  103. }
  104. static void set_property_expanding_shorthands(StyleProperties& style, CSS::PropertyID property_id, StyleValue const& value, DOM::Document& document, bool is_internally_generated_pseudo_property = false)
  105. {
  106. if (is_pseudo_property(property_id) && !is_internally_generated_pseudo_property) {
  107. dbgln("Ignoring non-internally-generated pseudo property: {}", string_from_property_id(property_id));
  108. return;
  109. }
  110. auto assign_edge_values = [&style](PropertyID top_property, PropertyID right_property, PropertyID bottom_property, PropertyID left_property, auto const& values) {
  111. if (values.size() == 4) {
  112. style.set_property(top_property, values[0]);
  113. style.set_property(right_property, values[1]);
  114. style.set_property(bottom_property, values[2]);
  115. style.set_property(left_property, values[3]);
  116. } else if (values.size() == 3) {
  117. style.set_property(top_property, values[0]);
  118. style.set_property(right_property, values[1]);
  119. style.set_property(bottom_property, values[2]);
  120. style.set_property(left_property, values[1]);
  121. } else if (values.size() == 2) {
  122. style.set_property(top_property, values[0]);
  123. style.set_property(right_property, values[1]);
  124. style.set_property(bottom_property, values[0]);
  125. style.set_property(left_property, values[1]);
  126. } else if (values.size() == 1) {
  127. style.set_property(top_property, values[0]);
  128. style.set_property(right_property, values[0]);
  129. style.set_property(bottom_property, values[0]);
  130. style.set_property(left_property, values[0]);
  131. }
  132. };
  133. if (property_id == CSS::PropertyID::TextDecoration) {
  134. if (value.is_text_decoration()) {
  135. auto& text_decoration = static_cast<TextDecorationStyleValue const&>(value);
  136. style.set_property(CSS::PropertyID::TextDecorationLine, text_decoration.line());
  137. style.set_property(CSS::PropertyID::TextDecorationStyle, text_decoration.style());
  138. style.set_property(CSS::PropertyID::TextDecorationColor, text_decoration.color());
  139. return;
  140. }
  141. if (value.is_builtin()) {
  142. style.set_property(CSS::PropertyID::TextDecorationLine, value);
  143. style.set_property(CSS::PropertyID::TextDecorationStyle, value);
  144. style.set_property(CSS::PropertyID::TextDecorationColor, value);
  145. return;
  146. }
  147. return;
  148. }
  149. if (property_id == CSS::PropertyID::Overflow) {
  150. if (value.is_overflow()) {
  151. auto& overflow = static_cast<OverflowStyleValue const&>(value);
  152. style.set_property(CSS::PropertyID::OverflowX, overflow.overflow_x());
  153. style.set_property(CSS::PropertyID::OverflowY, overflow.overflow_y());
  154. return;
  155. }
  156. if (value.is_builtin()) {
  157. style.set_property(CSS::PropertyID::OverflowX, value);
  158. style.set_property(CSS::PropertyID::OverflowY, value);
  159. return;
  160. }
  161. return;
  162. }
  163. if (property_id == CSS::PropertyID::Border) {
  164. set_property_expanding_shorthands(style, CSS::PropertyID::BorderTop, value, document);
  165. set_property_expanding_shorthands(style, CSS::PropertyID::BorderRight, value, document);
  166. set_property_expanding_shorthands(style, CSS::PropertyID::BorderBottom, value, document);
  167. set_property_expanding_shorthands(style, CSS::PropertyID::BorderLeft, value, document);
  168. // FIXME: Also reset border-image, in line with the spec: https://www.w3.org/TR/css-backgrounds-3/#border-shorthands
  169. return;
  170. }
  171. if (property_id == CSS::PropertyID::BorderRadius) {
  172. if (value.is_value_list()) {
  173. auto& values_list = static_cast<StyleValueList const&>(value);
  174. assign_edge_values(PropertyID::BorderTopLeftRadius, PropertyID::BorderTopRightRadius, PropertyID::BorderBottomRightRadius, PropertyID::BorderBottomLeftRadius, values_list.values());
  175. return;
  176. }
  177. if (value.is_builtin()) {
  178. style.set_property(CSS::PropertyID::BorderTopLeftRadius, value);
  179. style.set_property(CSS::PropertyID::BorderTopRightRadius, value);
  180. style.set_property(CSS::PropertyID::BorderBottomRightRadius, value);
  181. style.set_property(CSS::PropertyID::BorderBottomLeftRadius, value);
  182. return;
  183. }
  184. return;
  185. }
  186. if (property_id == CSS::PropertyID::BorderTop
  187. || property_id == CSS::PropertyID::BorderRight
  188. || property_id == CSS::PropertyID::BorderBottom
  189. || property_id == CSS::PropertyID::BorderLeft) {
  190. Edge edge = Edge::All;
  191. switch (property_id) {
  192. case CSS::PropertyID::BorderTop:
  193. edge = Edge::Top;
  194. break;
  195. case CSS::PropertyID::BorderRight:
  196. edge = Edge::Right;
  197. break;
  198. case CSS::PropertyID::BorderBottom:
  199. edge = Edge::Bottom;
  200. break;
  201. case CSS::PropertyID::BorderLeft:
  202. edge = Edge::Left;
  203. break;
  204. default:
  205. break;
  206. }
  207. if (value.is_border()) {
  208. auto& border = static_cast<BorderStyleValue const&>(value);
  209. if (contains(Edge::Top, edge)) {
  210. style.set_property(PropertyID::BorderTopWidth, border.border_width());
  211. style.set_property(PropertyID::BorderTopStyle, border.border_style());
  212. style.set_property(PropertyID::BorderTopColor, border.border_color());
  213. }
  214. if (contains(Edge::Right, edge)) {
  215. style.set_property(PropertyID::BorderRightWidth, border.border_width());
  216. style.set_property(PropertyID::BorderRightStyle, border.border_style());
  217. style.set_property(PropertyID::BorderRightColor, border.border_color());
  218. }
  219. if (contains(Edge::Bottom, edge)) {
  220. style.set_property(PropertyID::BorderBottomWidth, border.border_width());
  221. style.set_property(PropertyID::BorderBottomStyle, border.border_style());
  222. style.set_property(PropertyID::BorderBottomColor, border.border_color());
  223. }
  224. if (contains(Edge::Left, edge)) {
  225. style.set_property(PropertyID::BorderLeftWidth, border.border_width());
  226. style.set_property(PropertyID::BorderLeftStyle, border.border_style());
  227. style.set_property(PropertyID::BorderLeftColor, border.border_color());
  228. }
  229. return;
  230. }
  231. return;
  232. }
  233. if (property_id == CSS::PropertyID::BorderStyle) {
  234. if (value.is_value_list()) {
  235. auto& values_list = static_cast<StyleValueList const&>(value);
  236. assign_edge_values(PropertyID::BorderTopStyle, PropertyID::BorderRightStyle, PropertyID::BorderBottomStyle, PropertyID::BorderLeftStyle, values_list.values());
  237. return;
  238. }
  239. if (value.is_builtin()) {
  240. style.set_property(CSS::PropertyID::BorderTopStyle, value);
  241. style.set_property(CSS::PropertyID::BorderRightStyle, value);
  242. style.set_property(CSS::PropertyID::BorderBottomStyle, value);
  243. style.set_property(CSS::PropertyID::BorderLeftStyle, value);
  244. return;
  245. }
  246. return;
  247. }
  248. if (property_id == CSS::PropertyID::BorderWidth) {
  249. if (value.is_value_list()) {
  250. auto& values_list = static_cast<StyleValueList const&>(value);
  251. assign_edge_values(PropertyID::BorderTopWidth, PropertyID::BorderRightWidth, PropertyID::BorderBottomWidth, PropertyID::BorderLeftWidth, values_list.values());
  252. return;
  253. }
  254. if (value.is_builtin()) {
  255. style.set_property(CSS::PropertyID::BorderTopWidth, value);
  256. style.set_property(CSS::PropertyID::BorderRightWidth, value);
  257. style.set_property(CSS::PropertyID::BorderBottomWidth, value);
  258. style.set_property(CSS::PropertyID::BorderLeftWidth, value);
  259. return;
  260. }
  261. return;
  262. }
  263. if (property_id == CSS::PropertyID::BorderColor) {
  264. if (value.is_value_list()) {
  265. auto& values_list = static_cast<StyleValueList const&>(value);
  266. assign_edge_values(PropertyID::BorderTopColor, PropertyID::BorderRightColor, PropertyID::BorderBottomColor, PropertyID::BorderLeftColor, values_list.values());
  267. return;
  268. }
  269. if (value.is_builtin()) {
  270. style.set_property(CSS::PropertyID::BorderTopColor, value);
  271. style.set_property(CSS::PropertyID::BorderRightColor, value);
  272. style.set_property(CSS::PropertyID::BorderBottomColor, value);
  273. style.set_property(CSS::PropertyID::BorderLeftColor, value);
  274. return;
  275. }
  276. return;
  277. }
  278. if (property_id == CSS::PropertyID::Background) {
  279. auto set_single_background = [&](CSS::BackgroundStyleValue const& background) {
  280. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundColor, background.color(), document);
  281. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundImage, background.image(), document);
  282. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundRepeatX, background.repeat_x(), document, true);
  283. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundRepeatY, background.repeat_y(), document, true);
  284. };
  285. if (value.is_background()) {
  286. auto& background = static_cast<CSS::BackgroundStyleValue const&>(value);
  287. set_single_background(background);
  288. return;
  289. }
  290. if (value.is_value_list()) {
  291. auto& background_list = static_cast<CSS::StyleValueList const&>(value).values();
  292. // FIXME: Handle multiple backgrounds.
  293. if (!background_list.is_empty()) {
  294. auto& background = background_list.first();
  295. if (background.is_background())
  296. set_single_background(static_cast<CSS::BackgroundStyleValue const&>(background));
  297. }
  298. return;
  299. }
  300. if (value.is_builtin()) {
  301. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundColor, value, document);
  302. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundImage, value, document);
  303. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundRepeatX, value, document, true);
  304. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundRepeatY, value, document, true);
  305. return;
  306. }
  307. return;
  308. }
  309. if (property_id == CSS::PropertyID::BackgroundImage) {
  310. if (value.is_value_list()) {
  311. auto& background_image_list = static_cast<CSS::StyleValueList const&>(value).values();
  312. // FIXME: Handle multiple backgrounds.
  313. if (!background_image_list.is_empty()) {
  314. auto& background_image = background_image_list.first();
  315. style.set_property(CSS::PropertyID::BackgroundImage, background_image);
  316. }
  317. return;
  318. }
  319. if (value.is_builtin() || value.is_image() || value.to_identifier() == ValueID::None) {
  320. style.set_property(CSS::PropertyID::BackgroundImage, value);
  321. return;
  322. }
  323. return;
  324. }
  325. if (property_id == CSS::PropertyID::BackgroundRepeat) {
  326. if (value.is_value_list()) {
  327. auto& background_repeat_list = static_cast<CSS::StyleValueList const&>(value).values();
  328. // FIXME: Handle multiple backgrounds.
  329. if (!background_repeat_list.is_empty()) {
  330. auto& maybe_background_repeat = background_repeat_list.first();
  331. if (maybe_background_repeat.is_background_repeat()) {
  332. auto& background_repeat = static_cast<BackgroundRepeatStyleValue const&>(maybe_background_repeat);
  333. set_property_expanding_shorthands(style, PropertyID::BackgroundRepeatX, background_repeat.repeat_x(), document, true);
  334. set_property_expanding_shorthands(style, PropertyID::BackgroundRepeatY, background_repeat.repeat_y(), document, true);
  335. }
  336. }
  337. return;
  338. }
  339. if (value.is_background_repeat()) {
  340. auto& background_repeat = static_cast<BackgroundRepeatStyleValue const&>(value);
  341. set_property_expanding_shorthands(style, PropertyID::BackgroundRepeatX, background_repeat.repeat_x(), document, true);
  342. set_property_expanding_shorthands(style, PropertyID::BackgroundRepeatY, background_repeat.repeat_y(), document, true);
  343. return;
  344. }
  345. if (value.is_builtin()) {
  346. set_property_expanding_shorthands(style, PropertyID::BackgroundRepeatX, value, document, true);
  347. set_property_expanding_shorthands(style, PropertyID::BackgroundRepeatY, value, document, true);
  348. return;
  349. }
  350. return;
  351. }
  352. if (property_id == CSS::PropertyID::BackgroundRepeatX || property_id == CSS::PropertyID::BackgroundRepeatY) {
  353. auto value_id = value.to_identifier();
  354. if (value_id == CSS::ValueID::RepeatX || value_id == CSS::ValueID::RepeatY)
  355. return;
  356. style.set_property(property_id, value);
  357. return;
  358. }
  359. if (property_id == CSS::PropertyID::Margin) {
  360. if (value.is_value_list()) {
  361. auto& values_list = static_cast<StyleValueList const&>(value);
  362. assign_edge_values(PropertyID::MarginTop, PropertyID::MarginRight, PropertyID::MarginBottom, PropertyID::MarginLeft, values_list.values());
  363. return;
  364. }
  365. if (value.is_length() || value.is_builtin()) {
  366. style.set_property(CSS::PropertyID::MarginTop, value);
  367. style.set_property(CSS::PropertyID::MarginRight, value);
  368. style.set_property(CSS::PropertyID::MarginBottom, value);
  369. style.set_property(CSS::PropertyID::MarginLeft, value);
  370. return;
  371. }
  372. return;
  373. }
  374. if (property_id == CSS::PropertyID::Padding) {
  375. if (value.is_value_list()) {
  376. auto& values_list = static_cast<StyleValueList const&>(value);
  377. assign_edge_values(PropertyID::PaddingTop, PropertyID::PaddingRight, PropertyID::PaddingBottom, PropertyID::PaddingLeft, values_list.values());
  378. return;
  379. }
  380. if (value.is_length() || value.is_builtin()) {
  381. style.set_property(CSS::PropertyID::PaddingTop, value);
  382. style.set_property(CSS::PropertyID::PaddingRight, value);
  383. style.set_property(CSS::PropertyID::PaddingBottom, value);
  384. style.set_property(CSS::PropertyID::PaddingLeft, value);
  385. return;
  386. }
  387. return;
  388. }
  389. if (property_id == CSS::PropertyID::ListStyle) {
  390. if (value.is_list_style()) {
  391. auto& list_style = static_cast<CSS::ListStyleStyleValue const&>(value);
  392. style.set_property(CSS::PropertyID::ListStylePosition, list_style.position());
  393. style.set_property(CSS::PropertyID::ListStyleImage, list_style.image());
  394. style.set_property(CSS::PropertyID::ListStyleType, list_style.style_type());
  395. return;
  396. }
  397. if (value.is_builtin()) {
  398. style.set_property(CSS::PropertyID::ListStylePosition, value);
  399. style.set_property(CSS::PropertyID::ListStyleImage, value);
  400. style.set_property(CSS::PropertyID::ListStyleType, value);
  401. return;
  402. }
  403. return;
  404. }
  405. if (property_id == CSS::PropertyID::Font) {
  406. if (value.is_font()) {
  407. auto& font_shorthand = static_cast<CSS::FontStyleValue const&>(value);
  408. style.set_property(CSS::PropertyID::FontSize, font_shorthand.font_size());
  409. style.set_property(CSS::PropertyID::FontFamily, font_shorthand.font_families());
  410. style.set_property(CSS::PropertyID::FontStyle, font_shorthand.font_style());
  411. style.set_property(CSS::PropertyID::FontWeight, font_shorthand.font_weight());
  412. style.set_property(CSS::PropertyID::LineHeight, font_shorthand.line_height());
  413. // FIXME: Implement font-stretch and font-variant
  414. return;
  415. }
  416. if (value.is_builtin()) {
  417. style.set_property(CSS::PropertyID::FontSize, value);
  418. style.set_property(CSS::PropertyID::FontFamily, value);
  419. style.set_property(CSS::PropertyID::FontStyle, value);
  420. style.set_property(CSS::PropertyID::FontWeight, value);
  421. style.set_property(CSS::PropertyID::LineHeight, value);
  422. // FIXME: Implement font-stretch and font-variant
  423. return;
  424. }
  425. return;
  426. }
  427. if (property_id == CSS::PropertyID::Flex) {
  428. if (value.is_flex()) {
  429. auto& flex = static_cast<CSS::FlexStyleValue const&>(value);
  430. style.set_property(CSS::PropertyID::FlexGrow, flex.grow());
  431. style.set_property(CSS::PropertyID::FlexShrink, flex.shrink());
  432. style.set_property(CSS::PropertyID::FlexBasis, flex.basis());
  433. return;
  434. }
  435. if (value.is_builtin()) {
  436. style.set_property(CSS::PropertyID::FlexGrow, value);
  437. style.set_property(CSS::PropertyID::FlexShrink, value);
  438. style.set_property(CSS::PropertyID::FlexBasis, value);
  439. return;
  440. }
  441. return;
  442. }
  443. if (property_id == CSS::PropertyID::FlexFlow) {
  444. if (value.is_flex_flow()) {
  445. auto& flex_flow = static_cast<FlexFlowStyleValue const&>(value);
  446. style.set_property(CSS::PropertyID::FlexDirection, flex_flow.flex_direction());
  447. style.set_property(CSS::PropertyID::FlexWrap, flex_flow.flex_wrap());
  448. return;
  449. }
  450. if (value.is_builtin()) {
  451. style.set_property(CSS::PropertyID::FlexDirection, value);
  452. style.set_property(CSS::PropertyID::FlexWrap, value);
  453. return;
  454. }
  455. return;
  456. }
  457. style.set_property(property_id, value);
  458. }
  459. StyleResolver::CustomPropertyResolutionTuple StyleResolver::resolve_custom_property_with_specificity(DOM::Element& element, String const& custom_property_name) const
  460. {
  461. if (auto maybe_property = element.resolve_custom_property(custom_property_name); maybe_property.has_value())
  462. return maybe_property.value();
  463. auto parent_element = element.parent_element();
  464. CustomPropertyResolutionTuple parent_resolved {};
  465. if (parent_element)
  466. parent_resolved = resolve_custom_property_with_specificity(*parent_element, custom_property_name);
  467. auto matching_rules = collect_matching_rules(element);
  468. sort_matching_rules(matching_rules);
  469. for (int i = matching_rules.size() - 1; i >= 0; --i) {
  470. auto& match = matching_rules[i];
  471. if (match.specificity < parent_resolved.specificity)
  472. continue;
  473. auto custom_property_style = match.rule->declaration().custom_property(custom_property_name);
  474. if (custom_property_style.has_value()) {
  475. element.add_custom_property(custom_property_name, { custom_property_style.value(), match.specificity });
  476. return { custom_property_style.value(), match.specificity };
  477. }
  478. }
  479. return parent_resolved;
  480. }
  481. Optional<StyleProperty> StyleResolver::resolve_custom_property(DOM::Element& element, String const& custom_property_name) const
  482. {
  483. auto resolved_with_specificity = resolve_custom_property_with_specificity(element, custom_property_name);
  484. return resolved_with_specificity.style;
  485. }
  486. NonnullRefPtr<StyleProperties> StyleResolver::resolve_style(DOM::Element& element) const
  487. {
  488. auto style = StyleProperties::create();
  489. if (auto* parent_style = element.parent_element() ? element.parent_element()->specified_css_values() : nullptr) {
  490. parent_style->for_each_property([&](auto property_id, auto& value) {
  491. if (is_inherited_property(property_id))
  492. set_property_expanding_shorthands(style, property_id, value, m_document);
  493. });
  494. }
  495. element.apply_presentational_hints(*style);
  496. auto matching_rules = collect_matching_rules(element);
  497. sort_matching_rules(matching_rules);
  498. for (auto& match : matching_rules) {
  499. for (auto& property : match.rule->declaration().properties()) {
  500. auto property_value = property.value;
  501. if (property.value->is_custom_property()) {
  502. auto prop = reinterpret_cast<CSS::CustomStyleValue const*>(property.value.ptr());
  503. auto custom_prop_name = prop->custom_property_name();
  504. auto resolved = resolve_custom_property(element, custom_prop_name);
  505. if (resolved.has_value()) {
  506. property_value = resolved.value().value;
  507. }
  508. }
  509. set_property_expanding_shorthands(style, property.property_id, property_value, m_document);
  510. }
  511. }
  512. if (auto* inline_style = element.inline_style()) {
  513. for (auto& property : inline_style->properties()) {
  514. set_property_expanding_shorthands(style, property.property_id, property.value, m_document);
  515. }
  516. }
  517. return style;
  518. }
  519. }