StyleComputer.cpp 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  1. /*
  2. * Copyright (c) 2018-2022, 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/Debug.h>
  9. #include <AK/QuickSort.h>
  10. #include <AK/TemporaryChange.h>
  11. #include <LibGfx/Font.h>
  12. #include <LibGfx/FontDatabase.h>
  13. #include <LibGfx/FontStyleMapping.h>
  14. #include <LibWeb/CSS/CSSStyleRule.h>
  15. #include <LibWeb/CSS/Parser/Parser.h>
  16. #include <LibWeb/CSS/SelectorEngine.h>
  17. #include <LibWeb/CSS/StyleComputer.h>
  18. #include <LibWeb/CSS/StyleSheet.h>
  19. #include <LibWeb/DOM/Document.h>
  20. #include <LibWeb/DOM/Element.h>
  21. #include <LibWeb/FontCache.h>
  22. #include <stdio.h>
  23. namespace Web::CSS {
  24. StyleComputer::StyleComputer(DOM::Document& document)
  25. : m_document(document)
  26. {
  27. }
  28. StyleComputer::~StyleComputer()
  29. {
  30. }
  31. static StyleSheet& default_stylesheet()
  32. {
  33. static StyleSheet* sheet;
  34. if (!sheet) {
  35. extern char const default_stylesheet_source[];
  36. String css = default_stylesheet_source;
  37. sheet = parse_css(CSS::ParsingContext(), css).leak_ref();
  38. }
  39. return *sheet;
  40. }
  41. static StyleSheet& quirks_mode_stylesheet()
  42. {
  43. static StyleSheet* sheet;
  44. if (!sheet) {
  45. extern char const quirks_mode_stylesheet_source[];
  46. String css = quirks_mode_stylesheet_source;
  47. sheet = parse_css(CSS::ParsingContext(), css).leak_ref();
  48. }
  49. return *sheet;
  50. }
  51. template<typename Callback>
  52. void StyleComputer::for_each_stylesheet(CascadeOrigin cascade_origin, Callback callback) const
  53. {
  54. if (cascade_origin == CascadeOrigin::UserAgent) {
  55. callback(default_stylesheet());
  56. if (document().in_quirks_mode())
  57. callback(quirks_mode_stylesheet());
  58. }
  59. if (cascade_origin == CascadeOrigin::Author) {
  60. for (auto const& sheet : document().style_sheets().sheets()) {
  61. callback(sheet);
  62. }
  63. }
  64. }
  65. Vector<MatchingRule> StyleComputer::collect_matching_rules(DOM::Element const& element, CascadeOrigin cascade_origin) const
  66. {
  67. if (cascade_origin == CascadeOrigin::Author) {
  68. Vector<MatchingRule> rules_to_run;
  69. for (auto const& class_name : element.class_names()) {
  70. if (auto it = m_rule_cache->rules_by_class.find(class_name); it != m_rule_cache->rules_by_class.end())
  71. rules_to_run.extend(it->value);
  72. }
  73. if (auto id = element.get_attribute(HTML::AttributeNames::id); !id.is_null()) {
  74. if (auto it = m_rule_cache->rules_by_id.find(id); it != m_rule_cache->rules_by_id.end())
  75. rules_to_run.extend(it->value);
  76. }
  77. if (auto it = m_rule_cache->rules_by_tag_name.find(element.local_name()); it != m_rule_cache->rules_by_tag_name.end())
  78. rules_to_run.extend(it->value);
  79. rules_to_run.extend(m_rule_cache->other_rules);
  80. Vector<MatchingRule> matching_rules;
  81. for (auto const& rule_to_run : rules_to_run) {
  82. auto const& selector = rule_to_run.rule->selectors()[rule_to_run.selector_index];
  83. if (SelectorEngine::matches(selector, element))
  84. matching_rules.append(rule_to_run);
  85. }
  86. return matching_rules;
  87. }
  88. Vector<MatchingRule> matching_rules;
  89. size_t style_sheet_index = 0;
  90. for_each_stylesheet(cascade_origin, [&](auto& sheet) {
  91. size_t rule_index = 0;
  92. static_cast<CSSStyleSheet const&>(sheet).for_each_effective_style_rule([&](auto const& rule) {
  93. size_t selector_index = 0;
  94. for (auto& selector : rule.selectors()) {
  95. if (SelectorEngine::matches(selector, element)) {
  96. matching_rules.append({ rule, style_sheet_index, rule_index, selector_index, selector.specificity() });
  97. break;
  98. }
  99. ++selector_index;
  100. }
  101. ++rule_index;
  102. });
  103. ++style_sheet_index;
  104. });
  105. return matching_rules;
  106. }
  107. static void sort_matching_rules(Vector<MatchingRule>& matching_rules)
  108. {
  109. quick_sort(matching_rules, [&](MatchingRule& a, MatchingRule& b) {
  110. auto const& a_selector = a.rule->selectors()[a.selector_index];
  111. auto const& b_selector = b.rule->selectors()[b.selector_index];
  112. auto a_specificity = a_selector.specificity();
  113. auto b_specificity = b_selector.specificity();
  114. if (a_selector.specificity() == b_selector.specificity()) {
  115. if (a.style_sheet_index == b.style_sheet_index)
  116. return a.rule_index < b.rule_index;
  117. return a.style_sheet_index < b.style_sheet_index;
  118. }
  119. return a_specificity < b_specificity;
  120. });
  121. }
  122. enum class Edge {
  123. Top,
  124. Right,
  125. Bottom,
  126. Left,
  127. All,
  128. };
  129. static bool contains(Edge a, Edge b)
  130. {
  131. return a == b || b == Edge::All;
  132. }
  133. static void set_property_expanding_shorthands(StyleProperties& style, CSS::PropertyID property_id, StyleValue const& value, DOM::Document& document)
  134. {
  135. auto assign_edge_values = [&style](PropertyID top_property, PropertyID right_property, PropertyID bottom_property, PropertyID left_property, auto const& values) {
  136. if (values.size() == 4) {
  137. style.set_property(top_property, values[0]);
  138. style.set_property(right_property, values[1]);
  139. style.set_property(bottom_property, values[2]);
  140. style.set_property(left_property, values[3]);
  141. } else if (values.size() == 3) {
  142. style.set_property(top_property, values[0]);
  143. style.set_property(right_property, values[1]);
  144. style.set_property(bottom_property, values[2]);
  145. style.set_property(left_property, values[1]);
  146. } else if (values.size() == 2) {
  147. style.set_property(top_property, values[0]);
  148. style.set_property(right_property, values[1]);
  149. style.set_property(bottom_property, values[0]);
  150. style.set_property(left_property, values[1]);
  151. } else if (values.size() == 1) {
  152. style.set_property(top_property, values[0]);
  153. style.set_property(right_property, values[0]);
  154. style.set_property(bottom_property, values[0]);
  155. style.set_property(left_property, values[0]);
  156. }
  157. };
  158. if (property_id == CSS::PropertyID::TextDecoration) {
  159. if (value.is_text_decoration()) {
  160. auto const& text_decoration = value.as_text_decoration();
  161. style.set_property(CSS::PropertyID::TextDecorationLine, text_decoration.line());
  162. style.set_property(CSS::PropertyID::TextDecorationStyle, text_decoration.style());
  163. style.set_property(CSS::PropertyID::TextDecorationColor, text_decoration.color());
  164. return;
  165. }
  166. style.set_property(CSS::PropertyID::TextDecorationLine, value);
  167. style.set_property(CSS::PropertyID::TextDecorationStyle, value);
  168. style.set_property(CSS::PropertyID::TextDecorationColor, value);
  169. return;
  170. }
  171. if (property_id == CSS::PropertyID::Overflow) {
  172. if (value.is_overflow()) {
  173. auto const& overflow = value.as_overflow();
  174. style.set_property(CSS::PropertyID::OverflowX, overflow.overflow_x());
  175. style.set_property(CSS::PropertyID::OverflowY, overflow.overflow_y());
  176. return;
  177. }
  178. style.set_property(CSS::PropertyID::OverflowX, value);
  179. style.set_property(CSS::PropertyID::OverflowY, value);
  180. return;
  181. }
  182. if (property_id == CSS::PropertyID::Border) {
  183. set_property_expanding_shorthands(style, CSS::PropertyID::BorderTop, value, document);
  184. set_property_expanding_shorthands(style, CSS::PropertyID::BorderRight, value, document);
  185. set_property_expanding_shorthands(style, CSS::PropertyID::BorderBottom, value, document);
  186. set_property_expanding_shorthands(style, CSS::PropertyID::BorderLeft, value, document);
  187. // FIXME: Also reset border-image, in line with the spec: https://www.w3.org/TR/css-backgrounds-3/#border-shorthands
  188. return;
  189. }
  190. if (property_id == CSS::PropertyID::BorderRadius) {
  191. if (value.is_value_list()) {
  192. auto const& values_list = value.as_value_list();
  193. assign_edge_values(PropertyID::BorderTopLeftRadius, PropertyID::BorderTopRightRadius, PropertyID::BorderBottomRightRadius, PropertyID::BorderBottomLeftRadius, values_list.values());
  194. return;
  195. }
  196. style.set_property(CSS::PropertyID::BorderTopLeftRadius, value);
  197. style.set_property(CSS::PropertyID::BorderTopRightRadius, value);
  198. style.set_property(CSS::PropertyID::BorderBottomRightRadius, value);
  199. style.set_property(CSS::PropertyID::BorderBottomLeftRadius, value);
  200. return;
  201. }
  202. if (property_id == CSS::PropertyID::BorderTop
  203. || property_id == CSS::PropertyID::BorderRight
  204. || property_id == CSS::PropertyID::BorderBottom
  205. || property_id == CSS::PropertyID::BorderLeft) {
  206. Edge edge = Edge::All;
  207. switch (property_id) {
  208. case CSS::PropertyID::BorderTop:
  209. edge = Edge::Top;
  210. break;
  211. case CSS::PropertyID::BorderRight:
  212. edge = Edge::Right;
  213. break;
  214. case CSS::PropertyID::BorderBottom:
  215. edge = Edge::Bottom;
  216. break;
  217. case CSS::PropertyID::BorderLeft:
  218. edge = Edge::Left;
  219. break;
  220. default:
  221. break;
  222. }
  223. if (value.is_border()) {
  224. auto const& border = value.as_border();
  225. if (contains(Edge::Top, edge)) {
  226. style.set_property(PropertyID::BorderTopWidth, border.border_width());
  227. style.set_property(PropertyID::BorderTopStyle, border.border_style());
  228. style.set_property(PropertyID::BorderTopColor, border.border_color());
  229. }
  230. if (contains(Edge::Right, edge)) {
  231. style.set_property(PropertyID::BorderRightWidth, border.border_width());
  232. style.set_property(PropertyID::BorderRightStyle, border.border_style());
  233. style.set_property(PropertyID::BorderRightColor, border.border_color());
  234. }
  235. if (contains(Edge::Bottom, edge)) {
  236. style.set_property(PropertyID::BorderBottomWidth, border.border_width());
  237. style.set_property(PropertyID::BorderBottomStyle, border.border_style());
  238. style.set_property(PropertyID::BorderBottomColor, border.border_color());
  239. }
  240. if (contains(Edge::Left, edge)) {
  241. style.set_property(PropertyID::BorderLeftWidth, border.border_width());
  242. style.set_property(PropertyID::BorderLeftStyle, border.border_style());
  243. style.set_property(PropertyID::BorderLeftColor, border.border_color());
  244. }
  245. return;
  246. }
  247. return;
  248. }
  249. if (property_id == CSS::PropertyID::BorderStyle) {
  250. if (value.is_value_list()) {
  251. auto const& values_list = value.as_value_list();
  252. assign_edge_values(PropertyID::BorderTopStyle, PropertyID::BorderRightStyle, PropertyID::BorderBottomStyle, PropertyID::BorderLeftStyle, values_list.values());
  253. return;
  254. }
  255. style.set_property(CSS::PropertyID::BorderTopStyle, value);
  256. style.set_property(CSS::PropertyID::BorderRightStyle, value);
  257. style.set_property(CSS::PropertyID::BorderBottomStyle, value);
  258. style.set_property(CSS::PropertyID::BorderLeftStyle, value);
  259. return;
  260. }
  261. if (property_id == CSS::PropertyID::BorderWidth) {
  262. if (value.is_value_list()) {
  263. auto const& values_list = value.as_value_list();
  264. assign_edge_values(PropertyID::BorderTopWidth, PropertyID::BorderRightWidth, PropertyID::BorderBottomWidth, PropertyID::BorderLeftWidth, values_list.values());
  265. return;
  266. }
  267. style.set_property(CSS::PropertyID::BorderTopWidth, value);
  268. style.set_property(CSS::PropertyID::BorderRightWidth, value);
  269. style.set_property(CSS::PropertyID::BorderBottomWidth, value);
  270. style.set_property(CSS::PropertyID::BorderLeftWidth, value);
  271. return;
  272. }
  273. if (property_id == CSS::PropertyID::BorderColor) {
  274. if (value.is_value_list()) {
  275. auto const& values_list = value.as_value_list();
  276. assign_edge_values(PropertyID::BorderTopColor, PropertyID::BorderRightColor, PropertyID::BorderBottomColor, PropertyID::BorderLeftColor, values_list.values());
  277. return;
  278. }
  279. style.set_property(CSS::PropertyID::BorderTopColor, value);
  280. style.set_property(CSS::PropertyID::BorderRightColor, value);
  281. style.set_property(CSS::PropertyID::BorderBottomColor, value);
  282. style.set_property(CSS::PropertyID::BorderLeftColor, value);
  283. return;
  284. }
  285. if (property_id == CSS::PropertyID::Background) {
  286. if (value.is_background()) {
  287. auto const& background = value.as_background();
  288. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundColor, background.color(), document);
  289. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundImage, background.image(), document);
  290. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundPosition, background.position(), document);
  291. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundSize, background.size(), document);
  292. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundRepeat, background.repeat(), document);
  293. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundAttachment, background.attachment(), document);
  294. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundOrigin, background.origin(), document);
  295. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundClip, background.clip(), document);
  296. return;
  297. }
  298. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundColor, value, document);
  299. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundImage, value, document);
  300. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundPosition, value, document);
  301. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundSize, value, document);
  302. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundRepeat, value, document);
  303. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundAttachment, value, document);
  304. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundOrigin, value, document);
  305. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundClip, value, document);
  306. return;
  307. }
  308. if (property_id == CSS::PropertyID::Margin) {
  309. if (value.is_value_list()) {
  310. auto const& values_list = value.as_value_list();
  311. assign_edge_values(PropertyID::MarginTop, PropertyID::MarginRight, PropertyID::MarginBottom, PropertyID::MarginLeft, values_list.values());
  312. return;
  313. }
  314. style.set_property(CSS::PropertyID::MarginTop, value);
  315. style.set_property(CSS::PropertyID::MarginRight, value);
  316. style.set_property(CSS::PropertyID::MarginBottom, value);
  317. style.set_property(CSS::PropertyID::MarginLeft, value);
  318. return;
  319. }
  320. if (property_id == CSS::PropertyID::Padding) {
  321. if (value.is_value_list()) {
  322. auto const& values_list = value.as_value_list();
  323. assign_edge_values(PropertyID::PaddingTop, PropertyID::PaddingRight, PropertyID::PaddingBottom, PropertyID::PaddingLeft, values_list.values());
  324. return;
  325. }
  326. style.set_property(CSS::PropertyID::PaddingTop, value);
  327. style.set_property(CSS::PropertyID::PaddingRight, value);
  328. style.set_property(CSS::PropertyID::PaddingBottom, value);
  329. style.set_property(CSS::PropertyID::PaddingLeft, value);
  330. return;
  331. }
  332. if (property_id == CSS::PropertyID::ListStyle) {
  333. if (value.is_list_style()) {
  334. auto const& list_style = value.as_list_style();
  335. style.set_property(CSS::PropertyID::ListStylePosition, list_style.position());
  336. style.set_property(CSS::PropertyID::ListStyleImage, list_style.image());
  337. style.set_property(CSS::PropertyID::ListStyleType, list_style.style_type());
  338. return;
  339. }
  340. style.set_property(CSS::PropertyID::ListStylePosition, value);
  341. style.set_property(CSS::PropertyID::ListStyleImage, value);
  342. style.set_property(CSS::PropertyID::ListStyleType, value);
  343. return;
  344. }
  345. if (property_id == CSS::PropertyID::Font) {
  346. if (value.is_font()) {
  347. auto const& font_shorthand = value.as_font();
  348. style.set_property(CSS::PropertyID::FontSize, font_shorthand.font_size());
  349. style.set_property(CSS::PropertyID::FontFamily, font_shorthand.font_families());
  350. style.set_property(CSS::PropertyID::FontStyle, font_shorthand.font_style());
  351. style.set_property(CSS::PropertyID::FontWeight, font_shorthand.font_weight());
  352. style.set_property(CSS::PropertyID::LineHeight, font_shorthand.line_height());
  353. // FIXME: Implement font-stretch and font-variant
  354. return;
  355. }
  356. style.set_property(CSS::PropertyID::FontSize, value);
  357. style.set_property(CSS::PropertyID::FontFamily, value);
  358. style.set_property(CSS::PropertyID::FontStyle, value);
  359. style.set_property(CSS::PropertyID::FontWeight, value);
  360. style.set_property(CSS::PropertyID::LineHeight, value);
  361. // FIXME: Implement font-stretch and font-variant
  362. return;
  363. }
  364. if (property_id == CSS::PropertyID::Flex) {
  365. if (value.is_flex()) {
  366. auto const& flex = value.as_flex();
  367. style.set_property(CSS::PropertyID::FlexGrow, flex.grow());
  368. style.set_property(CSS::PropertyID::FlexShrink, flex.shrink());
  369. style.set_property(CSS::PropertyID::FlexBasis, flex.basis());
  370. return;
  371. }
  372. style.set_property(CSS::PropertyID::FlexGrow, value);
  373. style.set_property(CSS::PropertyID::FlexShrink, value);
  374. style.set_property(CSS::PropertyID::FlexBasis, value);
  375. return;
  376. }
  377. if (property_id == CSS::PropertyID::FlexFlow) {
  378. if (value.is_flex_flow()) {
  379. auto const& flex_flow = value.as_flex_flow();
  380. style.set_property(CSS::PropertyID::FlexDirection, flex_flow.flex_direction());
  381. style.set_property(CSS::PropertyID::FlexWrap, flex_flow.flex_wrap());
  382. return;
  383. }
  384. style.set_property(CSS::PropertyID::FlexDirection, value);
  385. style.set_property(CSS::PropertyID::FlexWrap, value);
  386. return;
  387. }
  388. style.set_property(property_id, value);
  389. }
  390. bool StyleComputer::expand_unresolved_values(DOM::Element& element, StringView property_name, HashMap<String, NonnullRefPtr<PropertyDependencyNode>>& dependencies, Vector<StyleComponentValueRule> const& source, Vector<StyleComponentValueRule>& dest, size_t source_start_index, HashMap<String, StyleProperty const*> const& custom_properties) const
  391. {
  392. // FIXME: Do this better!
  393. // We build a copy of the tree of StyleComponentValueRules, with all var()s replaced with their contents.
  394. // This is a very naive solution, and we could do better if the CSS Parser could accept tokens one at a time.
  395. // Arbitrary large value chosen to avoid the billion-laughs attack.
  396. // https://www.w3.org/TR/css-variables-1/#long-variables
  397. const size_t MAX_VALUE_COUNT = 16384;
  398. if (source.size() + dest.size() > MAX_VALUE_COUNT) {
  399. dbgln("Stopped expanding CSS variables: maximum length reached.");
  400. return false;
  401. }
  402. auto get_custom_property = [&custom_properties](auto& name) -> RefPtr<StyleValue> {
  403. auto it = custom_properties.find(name);
  404. if (it != custom_properties.end())
  405. return it->value->value;
  406. return nullptr;
  407. };
  408. auto get_dependency_node = [&](auto name) -> NonnullRefPtr<PropertyDependencyNode> {
  409. if (auto existing = dependencies.get(name); existing.has_value())
  410. return *existing.value();
  411. auto new_node = PropertyDependencyNode::create(name);
  412. dependencies.set(name, new_node);
  413. return new_node;
  414. };
  415. for (size_t source_index = source_start_index; source_index < source.size(); source_index++) {
  416. auto const& value = source[source_index];
  417. if (value.is_function()) {
  418. if (value.function().name().equals_ignoring_case("var"sv)) {
  419. auto const& var_contents = value.function().values();
  420. if (var_contents.is_empty())
  421. return false;
  422. auto const& custom_property_name_token = var_contents.first();
  423. if (!custom_property_name_token.is(Token::Type::Ident))
  424. return false;
  425. auto custom_property_name = custom_property_name_token.token().ident();
  426. if (!custom_property_name.starts_with("--"))
  427. return false;
  428. // Detect dependency cycles. https://www.w3.org/TR/css-variables-1/#cycles
  429. // We do not do this by the spec, since we are not keeping a graph of var dependencies around,
  430. // but rebuilding it every time.
  431. if (custom_property_name == property_name)
  432. return false;
  433. auto parent = get_dependency_node(property_name);
  434. auto child = get_dependency_node(custom_property_name);
  435. parent->add_child(child);
  436. if (parent->has_cycles())
  437. return false;
  438. if (auto custom_property_value = get_custom_property(custom_property_name)) {
  439. VERIFY(custom_property_value->is_unresolved());
  440. if (!expand_unresolved_values(element, custom_property_name, dependencies, custom_property_value->as_unresolved().values(), dest, 0, custom_properties))
  441. return false;
  442. continue;
  443. }
  444. // Use the provided fallback value, if any.
  445. if (var_contents.size() > 2 && var_contents[1].is(Token::Type::Comma)) {
  446. if (!expand_unresolved_values(element, property_name, dependencies, var_contents, dest, 2, custom_properties))
  447. return false;
  448. continue;
  449. }
  450. }
  451. auto const& source_function = value.function();
  452. Vector<StyleComponentValueRule> function_values;
  453. if (!expand_unresolved_values(element, property_name, dependencies, source_function.values(), function_values, 0, custom_properties))
  454. return false;
  455. NonnullRefPtr<StyleFunctionRule> function = adopt_ref(*new StyleFunctionRule(source_function.name(), move(function_values)));
  456. dest.empend(function);
  457. continue;
  458. }
  459. if (value.is_block()) {
  460. auto const& source_block = value.block();
  461. Vector<StyleComponentValueRule> block_values;
  462. if (!expand_unresolved_values(element, property_name, dependencies, source_block.values(), block_values, 0, custom_properties))
  463. return false;
  464. NonnullRefPtr<StyleBlockRule> block = adopt_ref(*new StyleBlockRule(source_block.token(), move(block_values)));
  465. dest.empend(block);
  466. continue;
  467. }
  468. dest.empend(value.token());
  469. }
  470. return true;
  471. }
  472. RefPtr<StyleValue> StyleComputer::resolve_unresolved_style_value(DOM::Element& element, PropertyID property_id, UnresolvedStyleValue const& unresolved, HashMap<String, StyleProperty const*> const& custom_properties) const
  473. {
  474. // Unresolved always contains a var(), unless it is a custom property's value, in which case we shouldn't be trying
  475. // to produce a different StyleValue from it.
  476. VERIFY(unresolved.contains_var());
  477. Vector<StyleComponentValueRule> expanded_values;
  478. HashMap<String, NonnullRefPtr<PropertyDependencyNode>> dependencies;
  479. if (!expand_unresolved_values(element, string_from_property_id(property_id), dependencies, unresolved.values(), expanded_values, 0, custom_properties))
  480. return {};
  481. if (auto parsed_value = Parser::parse_css_value({}, ParsingContext { document() }, property_id, expanded_values))
  482. return parsed_value.release_nonnull();
  483. return {};
  484. }
  485. void StyleComputer::cascade_declarations(StyleProperties& style, DOM::Element& element, Vector<MatchingRule> const& matching_rules, CascadeOrigin cascade_origin, Important important, HashMap<String, StyleProperty const*> const& custom_properties) const
  486. {
  487. for (auto const& match : matching_rules) {
  488. for (auto const& property : verify_cast<PropertyOwningCSSStyleDeclaration>(match.rule->declaration()).properties()) {
  489. if (important != property.important)
  490. continue;
  491. auto property_value = property.value;
  492. if (property.value->is_unresolved()) {
  493. if (auto resolved = resolve_unresolved_style_value(element, property.property_id, property.value->as_unresolved(), custom_properties))
  494. property_value = resolved.release_nonnull();
  495. }
  496. set_property_expanding_shorthands(style, property.property_id, property_value, m_document);
  497. }
  498. }
  499. if (cascade_origin == CascadeOrigin::Author) {
  500. if (auto const* inline_style = verify_cast<ElementInlineCSSStyleDeclaration>(element.inline_style())) {
  501. for (auto const& property : inline_style->properties()) {
  502. if (important != property.important)
  503. continue;
  504. set_property_expanding_shorthands(style, property.property_id, property.value, m_document);
  505. }
  506. }
  507. }
  508. }
  509. static HashMap<String, StyleProperty const*> cascade_custom_properties(DOM::Element& element, Vector<MatchingRule> const& matching_rules)
  510. {
  511. HashMap<String, StyleProperty const*> custom_properties;
  512. if (auto* parent_element = element.parent_element()) {
  513. for (auto const& it : parent_element->custom_properties())
  514. custom_properties.set(it.key, &it.value);
  515. }
  516. for (auto const& matching_rule : matching_rules) {
  517. for (auto const& it : verify_cast<PropertyOwningCSSStyleDeclaration>(matching_rule.rule->declaration()).custom_properties()) {
  518. custom_properties.set(it.key, &it.value);
  519. }
  520. }
  521. element.custom_properties().clear();
  522. for (auto& it : custom_properties)
  523. element.add_custom_property(it.key, *it.value);
  524. return custom_properties;
  525. }
  526. // https://www.w3.org/TR/css-cascade/#cascading
  527. void StyleComputer::compute_cascaded_values(StyleProperties& style, DOM::Element& element) const
  528. {
  529. // First, we collect all the CSS rules whose selectors match `element`:
  530. MatchingRuleSet matching_rule_set;
  531. matching_rule_set.user_agent_rules = collect_matching_rules(element, CascadeOrigin::UserAgent);
  532. sort_matching_rules(matching_rule_set.user_agent_rules);
  533. matching_rule_set.author_rules = collect_matching_rules(element, CascadeOrigin::Author);
  534. sort_matching_rules(matching_rule_set.author_rules);
  535. // Then we resolve all the CSS custom properties ("variables") for this element:
  536. auto custom_properties = cascade_custom_properties(element, matching_rule_set.author_rules);
  537. // Then we apply the declarations from the matched rules in cascade order:
  538. // Normal user agent declarations
  539. cascade_declarations(style, element, matching_rule_set.user_agent_rules, CascadeOrigin::UserAgent, Important::No, custom_properties);
  540. // FIXME: Normal user declarations
  541. // Normal author declarations
  542. cascade_declarations(style, element, matching_rule_set.author_rules, CascadeOrigin::Author, Important::No, custom_properties);
  543. // Author presentational hints (NOTE: The spec doesn't say exactly how to prioritize these.)
  544. element.apply_presentational_hints(style);
  545. // FIXME: Animation declarations [css-animations-1]
  546. // Important author declarations
  547. cascade_declarations(style, element, matching_rule_set.author_rules, CascadeOrigin::Author, Important::Yes, custom_properties);
  548. // FIXME: Important user declarations
  549. // Important user agent declarations
  550. cascade_declarations(style, element, matching_rule_set.user_agent_rules, CascadeOrigin::UserAgent, Important::Yes, custom_properties);
  551. // FIXME: Transition declarations [css-transitions-1]
  552. }
  553. static NonnullRefPtr<StyleValue> get_inherit_value(CSS::PropertyID property_id, DOM::Element const* element)
  554. {
  555. if (!element || !element->parent_element() || !element->parent_element()->specified_css_values())
  556. return property_initial_value(property_id);
  557. return element->parent_element()->specified_css_values()->property(property_id).release_value();
  558. };
  559. void StyleComputer::compute_defaulted_property_value(StyleProperties& style, DOM::Element const* element, CSS::PropertyID property_id) const
  560. {
  561. // FIXME: If we don't know the correct initial value for a property, we fall back to InitialStyleValue.
  562. auto& value_slot = style.m_property_values[to_underlying(property_id)];
  563. if (!value_slot) {
  564. if (is_inherited_property(property_id))
  565. style.m_property_values[to_underlying(property_id)] = get_inherit_value(property_id, element);
  566. else
  567. style.m_property_values[to_underlying(property_id)] = property_initial_value(property_id);
  568. return;
  569. }
  570. if (value_slot->is_initial()) {
  571. value_slot = property_initial_value(property_id);
  572. return;
  573. }
  574. if (value_slot->is_inherit()) {
  575. value_slot = get_inherit_value(property_id, element);
  576. return;
  577. }
  578. }
  579. // https://www.w3.org/TR/css-cascade/#defaulting
  580. void StyleComputer::compute_defaulted_values(StyleProperties& style, DOM::Element const* element) const
  581. {
  582. // Walk the list of all known CSS properties and:
  583. // - Add them to `style` if they are missing.
  584. // - Resolve `inherit` and `initial` as needed.
  585. for (auto i = to_underlying(CSS::first_longhand_property_id); i <= to_underlying(CSS::last_longhand_property_id); ++i) {
  586. auto property_id = (CSS::PropertyID)i;
  587. compute_defaulted_property_value(style, element, property_id);
  588. }
  589. }
  590. void StyleComputer::compute_font(StyleProperties& style, DOM::Element const* element) const
  591. {
  592. // To compute the font, first ensure that we've defaulted the relevant CSS font properties.
  593. // FIXME: This should be more sophisticated.
  594. compute_defaulted_property_value(style, element, CSS::PropertyID::FontFamily);
  595. compute_defaulted_property_value(style, element, CSS::PropertyID::FontSize);
  596. compute_defaulted_property_value(style, element, CSS::PropertyID::FontStyle);
  597. compute_defaulted_property_value(style, element, CSS::PropertyID::FontWeight);
  598. auto viewport_rect = document().browsing_context()->viewport_rect();
  599. auto font_size = style.property(CSS::PropertyID::FontSize).value();
  600. auto font_style = style.property(CSS::PropertyID::FontStyle).value();
  601. auto font_weight = style.property(CSS::PropertyID::FontWeight).value();
  602. int weight = Gfx::FontWeight::Regular;
  603. if (font_weight->is_identifier()) {
  604. switch (static_cast<IdentifierStyleValue const&>(*font_weight).id()) {
  605. case CSS::ValueID::Normal:
  606. weight = Gfx::FontWeight::Regular;
  607. break;
  608. case CSS::ValueID::Bold:
  609. weight = Gfx::FontWeight::Bold;
  610. break;
  611. case CSS::ValueID::Lighter:
  612. // FIXME: This should be relative to the parent.
  613. weight = Gfx::FontWeight::Regular;
  614. break;
  615. case CSS::ValueID::Bolder:
  616. // FIXME: This should be relative to the parent.
  617. weight = Gfx::FontWeight::Bold;
  618. break;
  619. default:
  620. break;
  621. }
  622. } else if (font_weight->has_integer()) {
  623. int font_weight_integer = font_weight->to_integer();
  624. if (font_weight_integer <= Gfx::FontWeight::Regular)
  625. weight = Gfx::FontWeight::Regular;
  626. else if (font_weight_integer <= Gfx::FontWeight::Bold)
  627. weight = Gfx::FontWeight::Bold;
  628. else
  629. weight = Gfx::FontWeight::Black;
  630. } else if (font_weight->is_calculated()) {
  631. auto maybe_weight = font_weight->as_calculated().resolve_integer();
  632. if (maybe_weight.has_value())
  633. weight = maybe_weight.value();
  634. }
  635. bool bold = weight > Gfx::FontWeight::Regular;
  636. int size = 10;
  637. if (font_size->is_identifier()) {
  638. switch (static_cast<const IdentifierStyleValue&>(*font_size).id()) {
  639. case CSS::ValueID::XxSmall:
  640. case CSS::ValueID::XSmall:
  641. case CSS::ValueID::Small:
  642. case CSS::ValueID::Medium:
  643. // FIXME: Should be based on "user's default font size"
  644. size = 10;
  645. break;
  646. case CSS::ValueID::Large:
  647. case CSS::ValueID::XLarge:
  648. case CSS::ValueID::XxLarge:
  649. case CSS::ValueID::XxxLarge:
  650. // FIXME: Should be based on "user's default font size"
  651. size = 12;
  652. break;
  653. case CSS::ValueID::Smaller:
  654. case CSS::ValueID::Larger:
  655. // FIXME: Should be based on parent element
  656. break;
  657. default:
  658. break;
  659. }
  660. } else {
  661. // FIXME: Get the root element font.
  662. float root_font_size = 10;
  663. Gfx::FontMetrics font_metrics;
  664. if (element && element->parent_element() && element->parent_element()->specified_css_values())
  665. font_metrics = element->parent_element()->specified_css_values()->computed_font().metrics('M');
  666. else
  667. font_metrics = Gfx::FontDatabase::default_font().metrics('M');
  668. Optional<Length> maybe_length;
  669. if (font_size->is_percentage()) {
  670. // Percentages refer to parent element's font size
  671. auto percentage = font_size->as_percentage().percentage();
  672. auto parent_font_size = size;
  673. if (element && element->parent_element() && element->parent_element()->layout_node() && element->parent_element()->specified_css_values()) {
  674. auto value = element->parent_element()->specified_css_values()->property(CSS::PropertyID::FontSize).value();
  675. if (value->is_length()) {
  676. auto length = static_cast<LengthStyleValue const&>(*value).to_length();
  677. if (length.is_absolute() || length.is_relative())
  678. parent_font_size = length.to_px(viewport_rect, font_metrics, size, root_font_size);
  679. }
  680. }
  681. maybe_length = Length::make_px(percentage.as_fraction() * parent_font_size);
  682. } else if (font_size->is_length()) {
  683. maybe_length = font_size->to_length();
  684. } else if (font_size->is_calculated()) {
  685. maybe_length = Length::make_calculated(font_size->as_calculated());
  686. }
  687. if (maybe_length.has_value()) {
  688. // FIXME: Support font-size: calc(...)
  689. // Theoretically we can do this now, but to resolve it we need a layout_node which we might not have. :^(
  690. if (!maybe_length->is_calculated()) {
  691. auto px = maybe_length.value().to_px(viewport_rect, font_metrics, size, root_font_size);
  692. if (px != 0)
  693. size = px;
  694. }
  695. }
  696. }
  697. int slope = Gfx::name_to_slope("Normal");
  698. // FIXME: Implement oblique <angle>
  699. if (font_style->is_identifier()) {
  700. switch (static_cast<IdentifierStyleValue const&>(*font_style).id()) {
  701. case CSS::ValueID::Italic:
  702. slope = Gfx::name_to_slope("Italic");
  703. break;
  704. case CSS::ValueID::Oblique:
  705. slope = Gfx::name_to_slope("Oblique");
  706. break;
  707. case CSS::ValueID::Normal:
  708. default:
  709. break;
  710. }
  711. }
  712. // FIXME: Implement the full font-matching algorithm: https://www.w3.org/TR/css-fonts-4/#font-matching-algorithm
  713. // Note: This is modified by the find_font() lambda
  714. FontSelector font_selector;
  715. bool monospace = false;
  716. auto find_font = [&](String const& family) -> RefPtr<Gfx::Font> {
  717. font_selector = { family, size, weight, slope };
  718. if (auto found_font = FontCache::the().get(font_selector))
  719. return found_font;
  720. if (auto found_font = Gfx::FontDatabase::the().get(family, size, weight, slope))
  721. return found_font;
  722. return {};
  723. };
  724. // FIXME: Replace hard-coded font names with a relevant call to FontDatabase.
  725. // Currently, we cannot request the default font's name, or request it at a specific size and weight.
  726. // So, hard-coded font names it is.
  727. auto find_generic_font = [&](ValueID font_id) -> RefPtr<Gfx::Font> {
  728. switch (font_id) {
  729. case ValueID::Monospace:
  730. case ValueID::UiMonospace:
  731. monospace = true;
  732. return find_font("Csilla");
  733. case ValueID::Serif:
  734. case ValueID::SansSerif:
  735. case ValueID::Cursive:
  736. case ValueID::Fantasy:
  737. case ValueID::UiSerif:
  738. case ValueID::UiSansSerif:
  739. case ValueID::UiRounded:
  740. return find_font("Katica");
  741. default:
  742. return {};
  743. }
  744. };
  745. RefPtr<Gfx::Font> found_font;
  746. auto family_value = style.property(PropertyID::FontFamily).value();
  747. if (family_value->is_value_list()) {
  748. auto const& family_list = static_cast<StyleValueList const&>(*family_value).values();
  749. for (auto const& family : family_list) {
  750. if (family.is_identifier()) {
  751. found_font = find_generic_font(family.to_identifier());
  752. } else if (family.is_string()) {
  753. found_font = find_font(family.to_string());
  754. }
  755. if (found_font)
  756. break;
  757. }
  758. } else if (family_value->is_identifier()) {
  759. found_font = find_generic_font(family_value->to_identifier());
  760. } else if (family_value->is_string()) {
  761. found_font = find_font(family_value->to_string());
  762. }
  763. if (!found_font) {
  764. found_font = StyleProperties::font_fallback(monospace, bold);
  765. }
  766. FontCache::the().set(font_selector, *found_font);
  767. style.set_property(CSS::PropertyID::FontSize, LengthStyleValue::create(CSS::Length::make_px(size)));
  768. style.set_property(CSS::PropertyID::FontWeight, NumericStyleValue::create_integer(weight));
  769. style.set_computed_font(found_font.release_nonnull());
  770. }
  771. void StyleComputer::absolutize_values(StyleProperties& style, DOM::Element const*) const
  772. {
  773. auto viewport_rect = document().browsing_context()->viewport_rect();
  774. auto font_metrics = style.computed_font().metrics('M');
  775. // FIXME: Get the root element font.
  776. float root_font_size = 10;
  777. float font_size = style.property(CSS::PropertyID::FontSize).value()->to_length().to_px(viewport_rect, font_metrics, root_font_size, root_font_size);
  778. for (auto& value_slot : style.m_property_values) {
  779. if (!value_slot)
  780. continue;
  781. value_slot->visit_lengths([&](Length& length) {
  782. if (length.is_px())
  783. return;
  784. if (length.is_absolute() || length.is_relative()) {
  785. auto px = length.to_px(viewport_rect, font_metrics, font_size, root_font_size);
  786. length = Length::make_px(px);
  787. }
  788. });
  789. }
  790. }
  791. // https://drafts.csswg.org/css-display/#transformations
  792. void StyleComputer::transform_box_type_if_needed(StyleProperties& style, DOM::Element const&) const
  793. {
  794. // 2.7. Automatic Box Type Transformations
  795. // Some layout effects require blockification or inlinification of the box type,
  796. // which sets the box’s computed outer display type to block or inline (respectively).
  797. // (This has no effect on display types that generate no box at all, such as none or contents.)
  798. // Additionally:
  799. // FIXME: If a block box (block flow) is inlinified, its inner display type is set to flow-root so that it remains a block container.
  800. //
  801. // FIXME: If an inline box (inline flow) is inlinified, it recursively inlinifies all of its in-flow children,
  802. // so that no block-level descendants break up the inline formatting context in which it participates.
  803. //
  804. // FIXME: For legacy reasons, if an inline block box (inline flow-root) is blockified, it becomes a block box (losing its flow-root nature).
  805. // For consistency, a run-in flow-root box also blockifies to a block box.
  806. //
  807. // FIXME: If a layout-internal box is blockified, its inner display type converts to flow so that it becomes a block container.
  808. // Inlinification has no effect on layout-internal boxes. (However, placement in such an inline context will typically cause them
  809. // to be wrapped in an appropriately-typed anonymous inline-level box.)
  810. // Absolute positioning or floating an element blockifies the box’s display type. [CSS2]
  811. auto display = style.display();
  812. if (!display.is_none() && !display.is_contents() && !display.is_block_outside()) {
  813. if (style.position() == CSS::Position::Absolute || style.position() == CSS::Position::Fixed || style.float_() != CSS::Float::None)
  814. style.set_property(CSS::PropertyID::Display, IdentifierStyleValue::create(CSS::ValueID::Block));
  815. }
  816. // FIXME: Containment in a ruby container inlinifies the box’s display type, as described in [CSS-RUBY-1].
  817. // FIXME: A parent with a grid or flex display value blockifies the box’s display type. [CSS-GRID-1] [CSS-FLEXBOX-1]
  818. }
  819. NonnullRefPtr<StyleProperties> StyleComputer::create_document_style() const
  820. {
  821. auto style = StyleProperties::create();
  822. compute_font(style, nullptr);
  823. compute_defaulted_values(style, nullptr);
  824. absolutize_values(style, nullptr);
  825. if (auto* browsing_context = m_document.browsing_context()) {
  826. auto viewport_rect = browsing_context->viewport_rect();
  827. style->set_property(CSS::PropertyID::Width, CSS::LengthStyleValue::create(CSS::Length::make_px(viewport_rect.width())));
  828. style->set_property(CSS::PropertyID::Height, CSS::LengthStyleValue::create(CSS::Length::make_px(viewport_rect.height())));
  829. }
  830. return style;
  831. }
  832. NonnullRefPtr<StyleProperties> StyleComputer::compute_style(DOM::Element& element) const
  833. {
  834. build_rule_cache_if_needed();
  835. auto style = StyleProperties::create();
  836. // 1. Perform the cascade. This produces the "specified style"
  837. compute_cascaded_values(style, element);
  838. // 2. Compute the font, since that may be needed for font-relative CSS units
  839. compute_font(style, &element);
  840. // 3. Absolutize values, turning font/viewport relative lengths into absolute lengths
  841. absolutize_values(style, &element);
  842. // 4. Default the values, applying inheritance and 'initial' as needed
  843. compute_defaulted_values(style, &element);
  844. // 5. Run automatic box type transformations
  845. transform_box_type_if_needed(style, element);
  846. return style;
  847. }
  848. PropertyDependencyNode::PropertyDependencyNode(String name)
  849. : m_name(move(name))
  850. {
  851. }
  852. void PropertyDependencyNode::add_child(NonnullRefPtr<PropertyDependencyNode> new_child)
  853. {
  854. for (auto const& child : m_children) {
  855. if (child.m_name == new_child->m_name)
  856. return;
  857. }
  858. // We detect self-reference already.
  859. VERIFY(new_child->m_name != m_name);
  860. m_children.append(move(new_child));
  861. }
  862. bool PropertyDependencyNode::has_cycles()
  863. {
  864. if (m_marked)
  865. return true;
  866. TemporaryChange change { m_marked, true };
  867. for (auto& child : m_children) {
  868. if (child.has_cycles())
  869. return true;
  870. }
  871. return false;
  872. }
  873. void StyleComputer::build_rule_cache_if_needed() const
  874. {
  875. if (m_rule_cache && m_rule_cache->generation == m_document.style_sheets().generation())
  876. return;
  877. const_cast<StyleComputer&>(*this).build_rule_cache();
  878. }
  879. void StyleComputer::build_rule_cache()
  880. {
  881. // FIXME: Make a rule cache for UA style as well.
  882. m_rule_cache = make<RuleCache>();
  883. size_t num_class_rules = 0;
  884. size_t num_id_rules = 0;
  885. size_t num_tag_name_rules = 0;
  886. Vector<MatchingRule> matching_rules;
  887. size_t style_sheet_index = 0;
  888. for_each_stylesheet(CascadeOrigin::Author, [&](auto& sheet) {
  889. size_t rule_index = 0;
  890. static_cast<CSSStyleSheet const&>(sheet).for_each_effective_style_rule([&](auto const& rule) {
  891. size_t selector_index = 0;
  892. for (CSS::Selector const& selector : rule.selectors()) {
  893. MatchingRule matching_rule { rule, style_sheet_index, rule_index, selector_index, selector.specificity() };
  894. bool added_to_bucket = false;
  895. for (auto const& simple_selector : selector.compound_selectors().last().simple_selectors) {
  896. if (simple_selector.type == CSS::Selector::SimpleSelector::Type::Id) {
  897. m_rule_cache->rules_by_id.ensure(simple_selector.value).append(move(matching_rule));
  898. ++num_id_rules;
  899. added_to_bucket = true;
  900. break;
  901. }
  902. if (simple_selector.type == CSS::Selector::SimpleSelector::Type::Class) {
  903. m_rule_cache->rules_by_class.ensure(simple_selector.value).append(move(matching_rule));
  904. ++num_class_rules;
  905. added_to_bucket = true;
  906. break;
  907. }
  908. if (simple_selector.type == CSS::Selector::SimpleSelector::Type::TagName) {
  909. m_rule_cache->rules_by_tag_name.ensure(simple_selector.value).append(move(matching_rule));
  910. ++num_tag_name_rules;
  911. added_to_bucket = true;
  912. break;
  913. }
  914. }
  915. if (!added_to_bucket)
  916. m_rule_cache->other_rules.append(move(matching_rule));
  917. ++selector_index;
  918. }
  919. ++rule_index;
  920. });
  921. ++style_sheet_index;
  922. });
  923. if constexpr (LIBWEB_CSS_DEBUG) {
  924. dbgln("Built rule cache!");
  925. dbgln(" ID: {}", num_id_rules);
  926. dbgln(" Class: {}", num_class_rules);
  927. dbgln("TagName: {}", num_tag_name_rules);
  928. dbgln(" Other: {}", m_rule_cache->other_rules.size());
  929. dbgln(" Total: {}", num_class_rules + num_id_rules + num_tag_name_rules + m_rule_cache->other_rules.size());
  930. }
  931. m_rule_cache->generation = m_document.style_sheets().generation();
  932. }
  933. void StyleComputer::invalidate_rule_cache()
  934. {
  935. m_rule_cache = nullptr;
  936. }
  937. }