StyleResolver.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  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@serenityos.org>
  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(CascadeOrigin cascade_origin, Callback callback) const
  49. {
  50. if (cascade_origin == CascadeOrigin::Any || cascade_origin == CascadeOrigin::UserAgent) {
  51. callback(default_stylesheet());
  52. if (document().in_quirks_mode())
  53. callback(quirks_mode_stylesheet());
  54. }
  55. if (cascade_origin == CascadeOrigin::Any || cascade_origin == CascadeOrigin::Author) {
  56. for (auto& sheet : document().style_sheets().sheets()) {
  57. callback(sheet);
  58. }
  59. }
  60. }
  61. Vector<MatchingRule> StyleResolver::collect_matching_rules(DOM::Element const& element, CascadeOrigin declaration_type) const
  62. {
  63. Vector<MatchingRule> matching_rules;
  64. size_t style_sheet_index = 0;
  65. for_each_stylesheet(declaration_type, [&](auto& sheet) {
  66. size_t rule_index = 0;
  67. static_cast<CSSStyleSheet const&>(sheet).for_each_effective_style_rule([&](auto& rule) {
  68. size_t selector_index = 0;
  69. for (auto& selector : rule.selectors()) {
  70. if (SelectorEngine::matches(selector, element)) {
  71. matching_rules.append({ rule, style_sheet_index, rule_index, selector_index, selector.specificity() });
  72. break;
  73. }
  74. ++selector_index;
  75. }
  76. ++rule_index;
  77. });
  78. ++style_sheet_index;
  79. });
  80. return matching_rules;
  81. }
  82. void StyleResolver::sort_matching_rules(Vector<MatchingRule>& matching_rules) const
  83. {
  84. quick_sort(matching_rules, [&](MatchingRule& a, MatchingRule& b) {
  85. auto& a_selector = a.rule->selectors()[a.selector_index];
  86. auto& b_selector = b.rule->selectors()[b.selector_index];
  87. auto a_specificity = a_selector.specificity();
  88. auto b_specificity = b_selector.specificity();
  89. if (a_selector.specificity() == b_selector.specificity()) {
  90. if (a.style_sheet_index == b.style_sheet_index)
  91. return a.rule_index < b.rule_index;
  92. return a.style_sheet_index < b.style_sheet_index;
  93. }
  94. return a_specificity < b_specificity;
  95. });
  96. }
  97. enum class Edge {
  98. Top,
  99. Right,
  100. Bottom,
  101. Left,
  102. All,
  103. };
  104. static bool contains(Edge a, Edge b)
  105. {
  106. return a == b || b == Edge::All;
  107. }
  108. 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)
  109. {
  110. if (is_pseudo_property(property_id) && !is_internally_generated_pseudo_property) {
  111. dbgln("Ignoring non-internally-generated pseudo property: {}", string_from_property_id(property_id));
  112. return;
  113. }
  114. auto assign_edge_values = [&style](PropertyID top_property, PropertyID right_property, PropertyID bottom_property, PropertyID left_property, auto const& values) {
  115. if (values.size() == 4) {
  116. style.set_property(top_property, values[0]);
  117. style.set_property(right_property, values[1]);
  118. style.set_property(bottom_property, values[2]);
  119. style.set_property(left_property, values[3]);
  120. } else if (values.size() == 3) {
  121. style.set_property(top_property, values[0]);
  122. style.set_property(right_property, values[1]);
  123. style.set_property(bottom_property, values[2]);
  124. style.set_property(left_property, values[1]);
  125. } else if (values.size() == 2) {
  126. style.set_property(top_property, values[0]);
  127. style.set_property(right_property, values[1]);
  128. style.set_property(bottom_property, values[0]);
  129. style.set_property(left_property, values[1]);
  130. } else if (values.size() == 1) {
  131. style.set_property(top_property, values[0]);
  132. style.set_property(right_property, values[0]);
  133. style.set_property(bottom_property, values[0]);
  134. style.set_property(left_property, values[0]);
  135. }
  136. };
  137. if (property_id == CSS::PropertyID::TextDecoration) {
  138. if (value.is_text_decoration()) {
  139. auto& text_decoration = static_cast<TextDecorationStyleValue const&>(value);
  140. style.set_property(CSS::PropertyID::TextDecorationLine, text_decoration.line());
  141. style.set_property(CSS::PropertyID::TextDecorationStyle, text_decoration.style());
  142. style.set_property(CSS::PropertyID::TextDecorationColor, text_decoration.color());
  143. return;
  144. }
  145. if (value.is_builtin()) {
  146. style.set_property(CSS::PropertyID::TextDecorationLine, value);
  147. style.set_property(CSS::PropertyID::TextDecorationStyle, value);
  148. style.set_property(CSS::PropertyID::TextDecorationColor, value);
  149. return;
  150. }
  151. return;
  152. }
  153. if (property_id == CSS::PropertyID::Overflow) {
  154. if (value.is_overflow()) {
  155. auto& overflow = static_cast<OverflowStyleValue const&>(value);
  156. style.set_property(CSS::PropertyID::OverflowX, overflow.overflow_x());
  157. style.set_property(CSS::PropertyID::OverflowY, overflow.overflow_y());
  158. return;
  159. }
  160. if (value.is_builtin()) {
  161. style.set_property(CSS::PropertyID::OverflowX, value);
  162. style.set_property(CSS::PropertyID::OverflowY, value);
  163. return;
  164. }
  165. return;
  166. }
  167. if (property_id == CSS::PropertyID::Border) {
  168. set_property_expanding_shorthands(style, CSS::PropertyID::BorderTop, value, document);
  169. set_property_expanding_shorthands(style, CSS::PropertyID::BorderRight, value, document);
  170. set_property_expanding_shorthands(style, CSS::PropertyID::BorderBottom, value, document);
  171. set_property_expanding_shorthands(style, CSS::PropertyID::BorderLeft, value, document);
  172. // FIXME: Also reset border-image, in line with the spec: https://www.w3.org/TR/css-backgrounds-3/#border-shorthands
  173. return;
  174. }
  175. if (property_id == CSS::PropertyID::BorderRadius) {
  176. if (value.is_value_list()) {
  177. auto& values_list = static_cast<StyleValueList const&>(value);
  178. assign_edge_values(PropertyID::BorderTopLeftRadius, PropertyID::BorderTopRightRadius, PropertyID::BorderBottomRightRadius, PropertyID::BorderBottomLeftRadius, values_list.values());
  179. return;
  180. }
  181. if (value.is_builtin()) {
  182. style.set_property(CSS::PropertyID::BorderTopLeftRadius, value);
  183. style.set_property(CSS::PropertyID::BorderTopRightRadius, value);
  184. style.set_property(CSS::PropertyID::BorderBottomRightRadius, value);
  185. style.set_property(CSS::PropertyID::BorderBottomLeftRadius, value);
  186. return;
  187. }
  188. return;
  189. }
  190. if (property_id == CSS::PropertyID::BorderTop
  191. || property_id == CSS::PropertyID::BorderRight
  192. || property_id == CSS::PropertyID::BorderBottom
  193. || property_id == CSS::PropertyID::BorderLeft) {
  194. Edge edge = Edge::All;
  195. switch (property_id) {
  196. case CSS::PropertyID::BorderTop:
  197. edge = Edge::Top;
  198. break;
  199. case CSS::PropertyID::BorderRight:
  200. edge = Edge::Right;
  201. break;
  202. case CSS::PropertyID::BorderBottom:
  203. edge = Edge::Bottom;
  204. break;
  205. case CSS::PropertyID::BorderLeft:
  206. edge = Edge::Left;
  207. break;
  208. default:
  209. break;
  210. }
  211. if (value.is_border()) {
  212. auto& border = static_cast<BorderStyleValue const&>(value);
  213. if (contains(Edge::Top, edge)) {
  214. style.set_property(PropertyID::BorderTopWidth, border.border_width());
  215. style.set_property(PropertyID::BorderTopStyle, border.border_style());
  216. style.set_property(PropertyID::BorderTopColor, border.border_color());
  217. }
  218. if (contains(Edge::Right, edge)) {
  219. style.set_property(PropertyID::BorderRightWidth, border.border_width());
  220. style.set_property(PropertyID::BorderRightStyle, border.border_style());
  221. style.set_property(PropertyID::BorderRightColor, border.border_color());
  222. }
  223. if (contains(Edge::Bottom, edge)) {
  224. style.set_property(PropertyID::BorderBottomWidth, border.border_width());
  225. style.set_property(PropertyID::BorderBottomStyle, border.border_style());
  226. style.set_property(PropertyID::BorderBottomColor, border.border_color());
  227. }
  228. if (contains(Edge::Left, edge)) {
  229. style.set_property(PropertyID::BorderLeftWidth, border.border_width());
  230. style.set_property(PropertyID::BorderLeftStyle, border.border_style());
  231. style.set_property(PropertyID::BorderLeftColor, border.border_color());
  232. }
  233. return;
  234. }
  235. return;
  236. }
  237. if (property_id == CSS::PropertyID::BorderStyle) {
  238. if (value.is_value_list()) {
  239. auto& values_list = static_cast<StyleValueList const&>(value);
  240. assign_edge_values(PropertyID::BorderTopStyle, PropertyID::BorderRightStyle, PropertyID::BorderBottomStyle, PropertyID::BorderLeftStyle, values_list.values());
  241. return;
  242. }
  243. if (value.is_builtin()) {
  244. style.set_property(CSS::PropertyID::BorderTopStyle, value);
  245. style.set_property(CSS::PropertyID::BorderRightStyle, value);
  246. style.set_property(CSS::PropertyID::BorderBottomStyle, value);
  247. style.set_property(CSS::PropertyID::BorderLeftStyle, value);
  248. return;
  249. }
  250. return;
  251. }
  252. if (property_id == CSS::PropertyID::BorderWidth) {
  253. if (value.is_value_list()) {
  254. auto& values_list = static_cast<StyleValueList const&>(value);
  255. assign_edge_values(PropertyID::BorderTopWidth, PropertyID::BorderRightWidth, PropertyID::BorderBottomWidth, PropertyID::BorderLeftWidth, values_list.values());
  256. return;
  257. }
  258. if (value.is_builtin()) {
  259. style.set_property(CSS::PropertyID::BorderTopWidth, value);
  260. style.set_property(CSS::PropertyID::BorderRightWidth, value);
  261. style.set_property(CSS::PropertyID::BorderBottomWidth, value);
  262. style.set_property(CSS::PropertyID::BorderLeftWidth, value);
  263. return;
  264. }
  265. return;
  266. }
  267. if (property_id == CSS::PropertyID::BorderColor) {
  268. if (value.is_value_list()) {
  269. auto& values_list = static_cast<StyleValueList const&>(value);
  270. assign_edge_values(PropertyID::BorderTopColor, PropertyID::BorderRightColor, PropertyID::BorderBottomColor, PropertyID::BorderLeftColor, values_list.values());
  271. return;
  272. }
  273. if (value.is_builtin()) {
  274. style.set_property(CSS::PropertyID::BorderTopColor, value);
  275. style.set_property(CSS::PropertyID::BorderRightColor, value);
  276. style.set_property(CSS::PropertyID::BorderBottomColor, value);
  277. style.set_property(CSS::PropertyID::BorderLeftColor, value);
  278. return;
  279. }
  280. return;
  281. }
  282. if (property_id == CSS::PropertyID::Background) {
  283. auto set_single_background = [&](CSS::BackgroundStyleValue const& background) {
  284. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundColor, background.color(), document);
  285. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundImage, background.image(), document);
  286. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundRepeatX, background.repeat_x(), document, true);
  287. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundRepeatY, background.repeat_y(), document, true);
  288. };
  289. if (value.is_background()) {
  290. auto& background = static_cast<CSS::BackgroundStyleValue const&>(value);
  291. set_single_background(background);
  292. return;
  293. }
  294. if (value.is_value_list()) {
  295. auto& background_list = static_cast<CSS::StyleValueList const&>(value).values();
  296. // FIXME: Handle multiple backgrounds.
  297. if (!background_list.is_empty()) {
  298. auto& background = background_list.first();
  299. if (background.is_background())
  300. set_single_background(static_cast<CSS::BackgroundStyleValue const&>(background));
  301. }
  302. return;
  303. }
  304. if (value.is_builtin()) {
  305. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundColor, value, document);
  306. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundImage, value, document);
  307. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundRepeatX, value, document, true);
  308. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundRepeatY, value, document, true);
  309. return;
  310. }
  311. return;
  312. }
  313. if (property_id == CSS::PropertyID::BackgroundImage) {
  314. if (value.is_value_list()) {
  315. auto& background_image_list = static_cast<CSS::StyleValueList const&>(value).values();
  316. // FIXME: Handle multiple backgrounds.
  317. if (!background_image_list.is_empty()) {
  318. auto& background_image = background_image_list.first();
  319. style.set_property(CSS::PropertyID::BackgroundImage, background_image);
  320. }
  321. return;
  322. }
  323. if (value.is_builtin() || value.is_image() || value.to_identifier() == ValueID::None) {
  324. style.set_property(CSS::PropertyID::BackgroundImage, value);
  325. return;
  326. }
  327. return;
  328. }
  329. if (property_id == CSS::PropertyID::BackgroundRepeat) {
  330. if (value.is_value_list()) {
  331. auto& background_repeat_list = static_cast<CSS::StyleValueList const&>(value).values();
  332. // FIXME: Handle multiple backgrounds.
  333. if (!background_repeat_list.is_empty()) {
  334. auto& maybe_background_repeat = background_repeat_list.first();
  335. if (maybe_background_repeat.is_background_repeat()) {
  336. auto& background_repeat = static_cast<BackgroundRepeatStyleValue const&>(maybe_background_repeat);
  337. set_property_expanding_shorthands(style, PropertyID::BackgroundRepeatX, background_repeat.repeat_x(), document, true);
  338. set_property_expanding_shorthands(style, PropertyID::BackgroundRepeatY, background_repeat.repeat_y(), document, true);
  339. }
  340. }
  341. return;
  342. }
  343. if (value.is_background_repeat()) {
  344. auto& background_repeat = static_cast<BackgroundRepeatStyleValue const&>(value);
  345. set_property_expanding_shorthands(style, PropertyID::BackgroundRepeatX, background_repeat.repeat_x(), document, true);
  346. set_property_expanding_shorthands(style, PropertyID::BackgroundRepeatY, background_repeat.repeat_y(), document, true);
  347. return;
  348. }
  349. if (value.is_builtin()) {
  350. set_property_expanding_shorthands(style, PropertyID::BackgroundRepeatX, value, document, true);
  351. set_property_expanding_shorthands(style, PropertyID::BackgroundRepeatY, value, document, true);
  352. return;
  353. }
  354. return;
  355. }
  356. if (property_id == CSS::PropertyID::BackgroundRepeatX || property_id == CSS::PropertyID::BackgroundRepeatY) {
  357. auto value_id = value.to_identifier();
  358. if (value_id == CSS::ValueID::RepeatX || value_id == CSS::ValueID::RepeatY)
  359. return;
  360. style.set_property(property_id, value);
  361. return;
  362. }
  363. if (property_id == CSS::PropertyID::Margin) {
  364. if (value.is_value_list()) {
  365. auto& values_list = static_cast<StyleValueList const&>(value);
  366. assign_edge_values(PropertyID::MarginTop, PropertyID::MarginRight, PropertyID::MarginBottom, PropertyID::MarginLeft, values_list.values());
  367. return;
  368. }
  369. if (value.is_length() || value.is_builtin()) {
  370. style.set_property(CSS::PropertyID::MarginTop, value);
  371. style.set_property(CSS::PropertyID::MarginRight, value);
  372. style.set_property(CSS::PropertyID::MarginBottom, value);
  373. style.set_property(CSS::PropertyID::MarginLeft, value);
  374. return;
  375. }
  376. return;
  377. }
  378. if (property_id == CSS::PropertyID::Padding) {
  379. if (value.is_value_list()) {
  380. auto& values_list = static_cast<StyleValueList const&>(value);
  381. assign_edge_values(PropertyID::PaddingTop, PropertyID::PaddingRight, PropertyID::PaddingBottom, PropertyID::PaddingLeft, values_list.values());
  382. return;
  383. }
  384. if (value.is_length() || value.is_builtin()) {
  385. style.set_property(CSS::PropertyID::PaddingTop, value);
  386. style.set_property(CSS::PropertyID::PaddingRight, value);
  387. style.set_property(CSS::PropertyID::PaddingBottom, value);
  388. style.set_property(CSS::PropertyID::PaddingLeft, value);
  389. return;
  390. }
  391. return;
  392. }
  393. if (property_id == CSS::PropertyID::ListStyle) {
  394. if (value.is_list_style()) {
  395. auto& list_style = static_cast<CSS::ListStyleStyleValue const&>(value);
  396. style.set_property(CSS::PropertyID::ListStylePosition, list_style.position());
  397. style.set_property(CSS::PropertyID::ListStyleImage, list_style.image());
  398. style.set_property(CSS::PropertyID::ListStyleType, list_style.style_type());
  399. return;
  400. }
  401. if (value.is_builtin()) {
  402. style.set_property(CSS::PropertyID::ListStylePosition, value);
  403. style.set_property(CSS::PropertyID::ListStyleImage, value);
  404. style.set_property(CSS::PropertyID::ListStyleType, value);
  405. return;
  406. }
  407. return;
  408. }
  409. if (property_id == CSS::PropertyID::Font) {
  410. if (value.is_font()) {
  411. auto& font_shorthand = static_cast<CSS::FontStyleValue const&>(value);
  412. style.set_property(CSS::PropertyID::FontSize, font_shorthand.font_size());
  413. style.set_property(CSS::PropertyID::FontFamily, font_shorthand.font_families());
  414. style.set_property(CSS::PropertyID::FontStyle, font_shorthand.font_style());
  415. style.set_property(CSS::PropertyID::FontWeight, font_shorthand.font_weight());
  416. style.set_property(CSS::PropertyID::LineHeight, font_shorthand.line_height());
  417. // FIXME: Implement font-stretch and font-variant
  418. return;
  419. }
  420. if (value.is_builtin()) {
  421. style.set_property(CSS::PropertyID::FontSize, value);
  422. style.set_property(CSS::PropertyID::FontFamily, value);
  423. style.set_property(CSS::PropertyID::FontStyle, value);
  424. style.set_property(CSS::PropertyID::FontWeight, value);
  425. style.set_property(CSS::PropertyID::LineHeight, value);
  426. // FIXME: Implement font-stretch and font-variant
  427. return;
  428. }
  429. return;
  430. }
  431. if (property_id == CSS::PropertyID::Flex) {
  432. if (value.is_flex()) {
  433. auto& flex = static_cast<CSS::FlexStyleValue const&>(value);
  434. style.set_property(CSS::PropertyID::FlexGrow, flex.grow());
  435. style.set_property(CSS::PropertyID::FlexShrink, flex.shrink());
  436. style.set_property(CSS::PropertyID::FlexBasis, flex.basis());
  437. return;
  438. }
  439. if (value.is_builtin()) {
  440. style.set_property(CSS::PropertyID::FlexGrow, value);
  441. style.set_property(CSS::PropertyID::FlexShrink, value);
  442. style.set_property(CSS::PropertyID::FlexBasis, value);
  443. return;
  444. }
  445. return;
  446. }
  447. if (property_id == CSS::PropertyID::FlexFlow) {
  448. if (value.is_flex_flow()) {
  449. auto& flex_flow = static_cast<FlexFlowStyleValue const&>(value);
  450. style.set_property(CSS::PropertyID::FlexDirection, flex_flow.flex_direction());
  451. style.set_property(CSS::PropertyID::FlexWrap, flex_flow.flex_wrap());
  452. return;
  453. }
  454. if (value.is_builtin()) {
  455. style.set_property(CSS::PropertyID::FlexDirection, value);
  456. style.set_property(CSS::PropertyID::FlexWrap, value);
  457. return;
  458. }
  459. return;
  460. }
  461. style.set_property(property_id, value);
  462. }
  463. StyleResolver::CustomPropertyResolutionTuple StyleResolver::resolve_custom_property_with_specificity(DOM::Element& element, String const& custom_property_name) const
  464. {
  465. if (auto maybe_property = element.resolve_custom_property(custom_property_name); maybe_property.has_value())
  466. return maybe_property.value();
  467. auto parent_element = element.parent_element();
  468. CustomPropertyResolutionTuple parent_resolved {};
  469. if (parent_element)
  470. parent_resolved = resolve_custom_property_with_specificity(*parent_element, custom_property_name);
  471. auto matching_rules = collect_matching_rules(element);
  472. sort_matching_rules(matching_rules);
  473. for (int i = matching_rules.size() - 1; i >= 0; --i) {
  474. auto& match = matching_rules[i];
  475. if (match.specificity < parent_resolved.specificity)
  476. continue;
  477. auto custom_property_style = verify_cast<PropertyOwningCSSStyleDeclaration>(match.rule->declaration()).custom_property(custom_property_name);
  478. if (custom_property_style.has_value()) {
  479. element.add_custom_property(custom_property_name, { custom_property_style.value(), match.specificity });
  480. return { custom_property_style.value(), match.specificity };
  481. }
  482. }
  483. return parent_resolved;
  484. }
  485. Optional<StyleProperty> StyleResolver::resolve_custom_property(DOM::Element& element, String const& custom_property_name) const
  486. {
  487. auto resolved_with_specificity = resolve_custom_property_with_specificity(element, custom_property_name);
  488. return resolved_with_specificity.style;
  489. }
  490. struct MatchingDeclarations {
  491. Vector<MatchingRule> user_agent_rules;
  492. Vector<MatchingRule> author_rules;
  493. };
  494. void StyleResolver::cascade_declarations(StyleProperties& style, DOM::Element& element, Vector<MatchingRule> const& matching_rules, CascadeOrigin cascade_origin, bool important) const
  495. {
  496. for (auto& match : matching_rules) {
  497. for (auto& property : verify_cast<PropertyOwningCSSStyleDeclaration>(match.rule->declaration()).properties()) {
  498. if (important != property.important)
  499. continue;
  500. auto property_value = property.value;
  501. if (property.value->is_custom_property()) {
  502. auto custom_property_name = static_cast<CSS::CustomStyleValue const&>(*property.value).custom_property_name();
  503. auto resolved = resolve_custom_property(element, custom_property_name);
  504. if (resolved.has_value()) {
  505. property_value = resolved.value().value;
  506. }
  507. }
  508. set_property_expanding_shorthands(style, property.property_id, property_value, m_document);
  509. }
  510. }
  511. if (cascade_origin == CascadeOrigin::Author) {
  512. if (auto* inline_style = verify_cast<ElementInlineCSSStyleDeclaration>(element.inline_style())) {
  513. for (auto& property : inline_style->properties()) {
  514. if (important != property.important)
  515. continue;
  516. set_property_expanding_shorthands(style, property.property_id, property.value, m_document);
  517. }
  518. }
  519. }
  520. }
  521. // https://drafts.csswg.org/css-cascade/#cascading
  522. void StyleResolver::compute_cascaded_values(StyleProperties& style, DOM::Element& element) const
  523. {
  524. // First, we collect all the CSS rules whose selectors match `element`:
  525. MatchingRuleSet matching_rule_set;
  526. matching_rule_set.user_agent_rules = collect_matching_rules(element, CascadeOrigin::UserAgent);
  527. sort_matching_rules(matching_rule_set.user_agent_rules);
  528. matching_rule_set.author_rules = collect_matching_rules(element, CascadeOrigin::Author);
  529. sort_matching_rules(matching_rule_set.author_rules);
  530. // Then we apply the declarations from the matched rules in cascade order:
  531. // Normal user agent declarations
  532. cascade_declarations(style, element, matching_rule_set.user_agent_rules, CascadeOrigin::UserAgent, false);
  533. // FIXME: Normal user declarations
  534. // Normal author declarations
  535. cascade_declarations(style, element, matching_rule_set.author_rules, CascadeOrigin::Author, false);
  536. // Author presentational hints (NOTE: The spec doesn't say exactly how to prioritize these.)
  537. element.apply_presentational_hints(style);
  538. // FIXME: Animation declarations [css-animations-1]
  539. // Important author declarations
  540. cascade_declarations(style, element, matching_rule_set.author_rules, CascadeOrigin::Author, true);
  541. // FIXME: Important user declarations
  542. // Important user agent declarations
  543. cascade_declarations(style, element, matching_rule_set.user_agent_rules, CascadeOrigin::UserAgent, true);
  544. // FIXME: Transition declarations [css-transitions-1]
  545. }
  546. // https://drafts.csswg.org/css-cascade/#defaulting
  547. void StyleResolver::compute_defaulted_values(StyleProperties& style, DOM::Element const& element) const
  548. {
  549. // FIXME: If we don't know the correct initial value for a property, we fall back to InitialStyleValue.
  550. auto get_initial_value = [&](PropertyID property_id) -> NonnullRefPtr<StyleValue> {
  551. auto value = property_initial_value(property_id);
  552. if (!value)
  553. return InitialStyleValue::the();
  554. return value.release_nonnull();
  555. };
  556. auto get_inherit_value = [&](PropertyID property_id) -> NonnullRefPtr<StyleValue> {
  557. if (!element.parent_element() || !element.parent_element()->specified_css_values())
  558. return get_initial_value(property_id);
  559. auto& map = element.parent_element()->specified_css_values()->m_property_values;
  560. auto it = map.find(property_id);
  561. VERIFY(it != map.end());
  562. return const_cast<StyleValue&>(*it->value);
  563. };
  564. // Walk the list of all known CSS properties and:
  565. // - Add them to `style` if they are missing.
  566. // - Resolve `inherit` and `initial` as needed.
  567. for (auto i = to_underlying(CSS::first_longhand_property_id); i <= to_underlying(CSS::last_longhand_property_id); ++i) {
  568. auto property_id = (CSS::PropertyID)i;
  569. auto it = style.m_property_values.find(property_id);
  570. if (it == style.m_property_values.end()) {
  571. if (is_inherited_property(property_id))
  572. style.m_property_values.set(property_id, get_inherit_value(property_id));
  573. else
  574. style.m_property_values.set(property_id, get_initial_value(property_id));
  575. continue;
  576. }
  577. if (it->value->is_initial()) {
  578. it->value = get_initial_value(property_id);
  579. continue;
  580. }
  581. if (it->value->is_inherit()) {
  582. it->value = get_inherit_value(property_id);
  583. continue;
  584. }
  585. }
  586. }
  587. NonnullRefPtr<StyleProperties> StyleResolver::resolve_style(DOM::Element& element) const
  588. {
  589. auto style = StyleProperties::create();
  590. compute_cascaded_values(style, element);
  591. compute_defaulted_values(style, element);
  592. return style;
  593. }
  594. }