StyleComputer.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  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 <LibGfx/Font.h>
  10. #include <LibGfx/FontDatabase.h>
  11. #include <LibWeb/CSS/CSSStyleRule.h>
  12. #include <LibWeb/CSS/Parser/Parser.h>
  13. #include <LibWeb/CSS/SelectorEngine.h>
  14. #include <LibWeb/CSS/StyleComputer.h>
  15. #include <LibWeb/CSS/StyleSheet.h>
  16. #include <LibWeb/DOM/Document.h>
  17. #include <LibWeb/DOM/Element.h>
  18. #include <LibWeb/Dump.h>
  19. #include <LibWeb/FontCache.h>
  20. #include <LibWeb/HTML/BrowsingContext.h>
  21. #include <ctype.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::Any || 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::Any || cascade_origin == CascadeOrigin::Author) {
  60. for (auto& sheet : document().style_sheets().sheets()) {
  61. callback(sheet);
  62. }
  63. }
  64. }
  65. Vector<MatchingRule> StyleComputer::collect_matching_rules(DOM::Element const& element, CascadeOrigin declaration_type) const
  66. {
  67. Vector<MatchingRule> matching_rules;
  68. size_t style_sheet_index = 0;
  69. for_each_stylesheet(declaration_type, [&](auto& sheet) {
  70. size_t rule_index = 0;
  71. static_cast<CSSStyleSheet const&>(sheet).for_each_effective_style_rule([&](auto const& rule) {
  72. size_t selector_index = 0;
  73. for (auto& selector : rule.selectors()) {
  74. if (SelectorEngine::matches(selector, element)) {
  75. matching_rules.append({ rule, style_sheet_index, rule_index, selector_index, selector.specificity() });
  76. break;
  77. }
  78. ++selector_index;
  79. }
  80. ++rule_index;
  81. });
  82. ++style_sheet_index;
  83. });
  84. return matching_rules;
  85. }
  86. void StyleComputer::sort_matching_rules(Vector<MatchingRule>& matching_rules) const
  87. {
  88. quick_sort(matching_rules, [&](MatchingRule& a, MatchingRule& b) {
  89. auto& a_selector = a.rule->selectors()[a.selector_index];
  90. auto& b_selector = b.rule->selectors()[b.selector_index];
  91. auto a_specificity = a_selector.specificity();
  92. auto b_specificity = b_selector.specificity();
  93. if (a_selector.specificity() == b_selector.specificity()) {
  94. if (a.style_sheet_index == b.style_sheet_index)
  95. return a.rule_index < b.rule_index;
  96. return a.style_sheet_index < b.style_sheet_index;
  97. }
  98. return a_specificity < b_specificity;
  99. });
  100. }
  101. enum class Edge {
  102. Top,
  103. Right,
  104. Bottom,
  105. Left,
  106. All,
  107. };
  108. static bool contains(Edge a, Edge b)
  109. {
  110. return a == b || b == Edge::All;
  111. }
  112. static void set_property_expanding_shorthands(StyleProperties& style, CSS::PropertyID property_id, StyleValue const& value, DOM::Document& document)
  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 = value.as_text_decoration();
  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. style.set_property(CSS::PropertyID::TextDecorationLine, value);
  146. style.set_property(CSS::PropertyID::TextDecorationStyle, value);
  147. style.set_property(CSS::PropertyID::TextDecorationColor, value);
  148. return;
  149. }
  150. if (property_id == CSS::PropertyID::Overflow) {
  151. if (value.is_overflow()) {
  152. auto& overflow = value.as_overflow();
  153. style.set_property(CSS::PropertyID::OverflowX, overflow.overflow_x());
  154. style.set_property(CSS::PropertyID::OverflowY, overflow.overflow_y());
  155. return;
  156. }
  157. style.set_property(CSS::PropertyID::OverflowX, value);
  158. style.set_property(CSS::PropertyID::OverflowY, value);
  159. return;
  160. }
  161. if (property_id == CSS::PropertyID::Border) {
  162. set_property_expanding_shorthands(style, CSS::PropertyID::BorderTop, value, document);
  163. set_property_expanding_shorthands(style, CSS::PropertyID::BorderRight, value, document);
  164. set_property_expanding_shorthands(style, CSS::PropertyID::BorderBottom, value, document);
  165. set_property_expanding_shorthands(style, CSS::PropertyID::BorderLeft, value, document);
  166. // FIXME: Also reset border-image, in line with the spec: https://www.w3.org/TR/css-backgrounds-3/#border-shorthands
  167. return;
  168. }
  169. if (property_id == CSS::PropertyID::BorderRadius) {
  170. if (value.is_value_list()) {
  171. auto& values_list = value.as_value_list();
  172. assign_edge_values(PropertyID::BorderTopLeftRadius, PropertyID::BorderTopRightRadius, PropertyID::BorderBottomRightRadius, PropertyID::BorderBottomLeftRadius, values_list.values());
  173. return;
  174. }
  175. style.set_property(CSS::PropertyID::BorderTopLeftRadius, value);
  176. style.set_property(CSS::PropertyID::BorderTopRightRadius, value);
  177. style.set_property(CSS::PropertyID::BorderBottomRightRadius, value);
  178. style.set_property(CSS::PropertyID::BorderBottomLeftRadius, value);
  179. return;
  180. }
  181. if (property_id == CSS::PropertyID::BorderTop
  182. || property_id == CSS::PropertyID::BorderRight
  183. || property_id == CSS::PropertyID::BorderBottom
  184. || property_id == CSS::PropertyID::BorderLeft) {
  185. Edge edge = Edge::All;
  186. switch (property_id) {
  187. case CSS::PropertyID::BorderTop:
  188. edge = Edge::Top;
  189. break;
  190. case CSS::PropertyID::BorderRight:
  191. edge = Edge::Right;
  192. break;
  193. case CSS::PropertyID::BorderBottom:
  194. edge = Edge::Bottom;
  195. break;
  196. case CSS::PropertyID::BorderLeft:
  197. edge = Edge::Left;
  198. break;
  199. default:
  200. break;
  201. }
  202. if (value.is_border()) {
  203. auto& border = value.as_border();
  204. if (contains(Edge::Top, edge)) {
  205. style.set_property(PropertyID::BorderTopWidth, border.border_width());
  206. style.set_property(PropertyID::BorderTopStyle, border.border_style());
  207. style.set_property(PropertyID::BorderTopColor, border.border_color());
  208. }
  209. if (contains(Edge::Right, edge)) {
  210. style.set_property(PropertyID::BorderRightWidth, border.border_width());
  211. style.set_property(PropertyID::BorderRightStyle, border.border_style());
  212. style.set_property(PropertyID::BorderRightColor, border.border_color());
  213. }
  214. if (contains(Edge::Bottom, edge)) {
  215. style.set_property(PropertyID::BorderBottomWidth, border.border_width());
  216. style.set_property(PropertyID::BorderBottomStyle, border.border_style());
  217. style.set_property(PropertyID::BorderBottomColor, border.border_color());
  218. }
  219. if (contains(Edge::Left, edge)) {
  220. style.set_property(PropertyID::BorderLeftWidth, border.border_width());
  221. style.set_property(PropertyID::BorderLeftStyle, border.border_style());
  222. style.set_property(PropertyID::BorderLeftColor, border.border_color());
  223. }
  224. return;
  225. }
  226. return;
  227. }
  228. if (property_id == CSS::PropertyID::BorderStyle) {
  229. if (value.is_value_list()) {
  230. auto& values_list = value.as_value_list();
  231. assign_edge_values(PropertyID::BorderTopStyle, PropertyID::BorderRightStyle, PropertyID::BorderBottomStyle, PropertyID::BorderLeftStyle, values_list.values());
  232. return;
  233. }
  234. style.set_property(CSS::PropertyID::BorderTopStyle, value);
  235. style.set_property(CSS::PropertyID::BorderRightStyle, value);
  236. style.set_property(CSS::PropertyID::BorderBottomStyle, value);
  237. style.set_property(CSS::PropertyID::BorderLeftStyle, value);
  238. return;
  239. }
  240. if (property_id == CSS::PropertyID::BorderWidth) {
  241. if (value.is_value_list()) {
  242. auto& values_list = value.as_value_list();
  243. assign_edge_values(PropertyID::BorderTopWidth, PropertyID::BorderRightWidth, PropertyID::BorderBottomWidth, PropertyID::BorderLeftWidth, values_list.values());
  244. return;
  245. }
  246. style.set_property(CSS::PropertyID::BorderTopWidth, value);
  247. style.set_property(CSS::PropertyID::BorderRightWidth, value);
  248. style.set_property(CSS::PropertyID::BorderBottomWidth, value);
  249. style.set_property(CSS::PropertyID::BorderLeftWidth, value);
  250. return;
  251. }
  252. if (property_id == CSS::PropertyID::BorderColor) {
  253. if (value.is_value_list()) {
  254. auto& values_list = value.as_value_list();
  255. assign_edge_values(PropertyID::BorderTopColor, PropertyID::BorderRightColor, PropertyID::BorderBottomColor, PropertyID::BorderLeftColor, values_list.values());
  256. return;
  257. }
  258. style.set_property(CSS::PropertyID::BorderTopColor, value);
  259. style.set_property(CSS::PropertyID::BorderRightColor, value);
  260. style.set_property(CSS::PropertyID::BorderBottomColor, value);
  261. style.set_property(CSS::PropertyID::BorderLeftColor, value);
  262. return;
  263. }
  264. if (property_id == CSS::PropertyID::Background) {
  265. if (value.is_background()) {
  266. auto& background = value.as_background();
  267. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundColor, background.color(), document);
  268. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundImage, background.image(), document);
  269. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundPosition, background.position(), document);
  270. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundSize, background.size(), document);
  271. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundRepeat, background.repeat(), document);
  272. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundAttachment, background.attachment(), document);
  273. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundOrigin, background.origin(), document);
  274. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundClip, background.clip(), document);
  275. return;
  276. }
  277. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundColor, value, document);
  278. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundImage, value, document);
  279. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundPosition, value, document);
  280. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundSize, value, document);
  281. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundRepeat, value, document);
  282. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundAttachment, value, document);
  283. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundOrigin, value, document);
  284. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundClip, value, document);
  285. return;
  286. }
  287. if (property_id == CSS::PropertyID::Margin) {
  288. if (value.is_value_list()) {
  289. auto& values_list = value.as_value_list();
  290. assign_edge_values(PropertyID::MarginTop, PropertyID::MarginRight, PropertyID::MarginBottom, PropertyID::MarginLeft, values_list.values());
  291. return;
  292. }
  293. style.set_property(CSS::PropertyID::MarginTop, value);
  294. style.set_property(CSS::PropertyID::MarginRight, value);
  295. style.set_property(CSS::PropertyID::MarginBottom, value);
  296. style.set_property(CSS::PropertyID::MarginLeft, value);
  297. return;
  298. }
  299. if (property_id == CSS::PropertyID::Padding) {
  300. if (value.is_value_list()) {
  301. auto& values_list = value.as_value_list();
  302. assign_edge_values(PropertyID::PaddingTop, PropertyID::PaddingRight, PropertyID::PaddingBottom, PropertyID::PaddingLeft, values_list.values());
  303. return;
  304. }
  305. style.set_property(CSS::PropertyID::PaddingTop, value);
  306. style.set_property(CSS::PropertyID::PaddingRight, value);
  307. style.set_property(CSS::PropertyID::PaddingBottom, value);
  308. style.set_property(CSS::PropertyID::PaddingLeft, value);
  309. return;
  310. }
  311. if (property_id == CSS::PropertyID::ListStyle) {
  312. if (value.is_list_style()) {
  313. auto& list_style = value.as_list_style();
  314. style.set_property(CSS::PropertyID::ListStylePosition, list_style.position());
  315. style.set_property(CSS::PropertyID::ListStyleImage, list_style.image());
  316. style.set_property(CSS::PropertyID::ListStyleType, list_style.style_type());
  317. return;
  318. }
  319. style.set_property(CSS::PropertyID::ListStylePosition, value);
  320. style.set_property(CSS::PropertyID::ListStyleImage, value);
  321. style.set_property(CSS::PropertyID::ListStyleType, value);
  322. return;
  323. }
  324. if (property_id == CSS::PropertyID::Font) {
  325. if (value.is_font()) {
  326. auto& font_shorthand = value.as_font();
  327. style.set_property(CSS::PropertyID::FontSize, font_shorthand.font_size());
  328. style.set_property(CSS::PropertyID::FontFamily, font_shorthand.font_families());
  329. style.set_property(CSS::PropertyID::FontStyle, font_shorthand.font_style());
  330. style.set_property(CSS::PropertyID::FontWeight, font_shorthand.font_weight());
  331. style.set_property(CSS::PropertyID::LineHeight, font_shorthand.line_height());
  332. // FIXME: Implement font-stretch and font-variant
  333. return;
  334. }
  335. style.set_property(CSS::PropertyID::FontSize, value);
  336. style.set_property(CSS::PropertyID::FontFamily, value);
  337. style.set_property(CSS::PropertyID::FontStyle, value);
  338. style.set_property(CSS::PropertyID::FontWeight, value);
  339. style.set_property(CSS::PropertyID::LineHeight, value);
  340. // FIXME: Implement font-stretch and font-variant
  341. return;
  342. }
  343. if (property_id == CSS::PropertyID::Flex) {
  344. if (value.is_flex()) {
  345. auto& flex = value.as_flex();
  346. style.set_property(CSS::PropertyID::FlexGrow, flex.grow());
  347. style.set_property(CSS::PropertyID::FlexShrink, flex.shrink());
  348. style.set_property(CSS::PropertyID::FlexBasis, flex.basis());
  349. return;
  350. }
  351. style.set_property(CSS::PropertyID::FlexGrow, value);
  352. style.set_property(CSS::PropertyID::FlexShrink, value);
  353. style.set_property(CSS::PropertyID::FlexBasis, value);
  354. return;
  355. }
  356. if (property_id == CSS::PropertyID::FlexFlow) {
  357. if (value.is_flex_flow()) {
  358. auto& flex_flow = value.as_flex_flow();
  359. style.set_property(CSS::PropertyID::FlexDirection, flex_flow.flex_direction());
  360. style.set_property(CSS::PropertyID::FlexWrap, flex_flow.flex_wrap());
  361. return;
  362. }
  363. style.set_property(CSS::PropertyID::FlexDirection, value);
  364. style.set_property(CSS::PropertyID::FlexWrap, value);
  365. return;
  366. }
  367. style.set_property(property_id, value);
  368. }
  369. StyleComputer::CustomPropertyResolutionTuple StyleComputer::resolve_custom_property_with_specificity(DOM::Element& element, String const& custom_property_name) const
  370. {
  371. if (auto maybe_property = element.resolve_custom_property(custom_property_name); maybe_property.has_value())
  372. return maybe_property.value();
  373. auto parent_element = element.parent_element();
  374. CustomPropertyResolutionTuple parent_resolved {};
  375. if (parent_element)
  376. parent_resolved = resolve_custom_property_with_specificity(*parent_element, custom_property_name);
  377. auto matching_rules = collect_matching_rules(element);
  378. sort_matching_rules(matching_rules);
  379. for (int i = matching_rules.size() - 1; i >= 0; --i) {
  380. auto& match = matching_rules[i];
  381. if (match.specificity < parent_resolved.specificity)
  382. continue;
  383. auto custom_property_style = verify_cast<PropertyOwningCSSStyleDeclaration>(match.rule->declaration()).custom_property(custom_property_name);
  384. if (custom_property_style.has_value()) {
  385. element.add_custom_property(custom_property_name, { custom_property_style.value(), match.specificity });
  386. return { custom_property_style.value(), match.specificity };
  387. }
  388. }
  389. return parent_resolved;
  390. }
  391. Optional<StyleProperty> StyleComputer::resolve_custom_property(DOM::Element& element, String const& custom_property_name) const
  392. {
  393. auto resolved_with_specificity = resolve_custom_property_with_specificity(element, custom_property_name);
  394. return resolved_with_specificity.style;
  395. }
  396. struct MatchingDeclarations {
  397. Vector<MatchingRule> user_agent_rules;
  398. Vector<MatchingRule> author_rules;
  399. };
  400. void StyleComputer::cascade_declarations(StyleProperties& style, DOM::Element& element, Vector<MatchingRule> const& matching_rules, CascadeOrigin cascade_origin, bool important) const
  401. {
  402. for (auto& match : matching_rules) {
  403. for (auto& property : verify_cast<PropertyOwningCSSStyleDeclaration>(match.rule->declaration()).properties()) {
  404. if (important != property.important)
  405. continue;
  406. auto property_value = property.value;
  407. if (property.value->is_custom_property()) {
  408. auto custom_property_name = property.value->as_custom_property().custom_property_name();
  409. auto resolved = resolve_custom_property(element, custom_property_name);
  410. if (resolved.has_value()) {
  411. property_value = resolved.value().value;
  412. }
  413. }
  414. set_property_expanding_shorthands(style, property.property_id, property_value, m_document);
  415. }
  416. }
  417. if (cascade_origin == CascadeOrigin::Author) {
  418. if (auto* inline_style = verify_cast<ElementInlineCSSStyleDeclaration>(element.inline_style())) {
  419. for (auto& property : inline_style->properties()) {
  420. if (important != property.important)
  421. continue;
  422. set_property_expanding_shorthands(style, property.property_id, property.value, m_document);
  423. }
  424. }
  425. }
  426. }
  427. // https://www.w3.org/TR/css-cascade/#cascading
  428. void StyleComputer::compute_cascaded_values(StyleProperties& style, DOM::Element& element) const
  429. {
  430. // First, we collect all the CSS rules whose selectors match `element`:
  431. MatchingRuleSet matching_rule_set;
  432. matching_rule_set.user_agent_rules = collect_matching_rules(element, CascadeOrigin::UserAgent);
  433. sort_matching_rules(matching_rule_set.user_agent_rules);
  434. matching_rule_set.author_rules = collect_matching_rules(element, CascadeOrigin::Author);
  435. sort_matching_rules(matching_rule_set.author_rules);
  436. // Then we apply the declarations from the matched rules in cascade order:
  437. // Normal user agent declarations
  438. cascade_declarations(style, element, matching_rule_set.user_agent_rules, CascadeOrigin::UserAgent, false);
  439. // FIXME: Normal user declarations
  440. // Normal author declarations
  441. cascade_declarations(style, element, matching_rule_set.author_rules, CascadeOrigin::Author, false);
  442. // Author presentational hints (NOTE: The spec doesn't say exactly how to prioritize these.)
  443. element.apply_presentational_hints(style);
  444. // FIXME: Animation declarations [css-animations-1]
  445. // Important author declarations
  446. cascade_declarations(style, element, matching_rule_set.author_rules, CascadeOrigin::Author, true);
  447. // FIXME: Important user declarations
  448. // Important user agent declarations
  449. cascade_declarations(style, element, matching_rule_set.user_agent_rules, CascadeOrigin::UserAgent, true);
  450. // FIXME: Transition declarations [css-transitions-1]
  451. }
  452. static NonnullRefPtr<StyleValue> get_inherit_value(CSS::PropertyID property_id, DOM::Element const* element)
  453. {
  454. if (!element || !element->parent_element() || !element->parent_element()->specified_css_values())
  455. return property_initial_value(property_id);
  456. auto& map = element->parent_element()->specified_css_values()->properties();
  457. auto it = map.find(property_id);
  458. VERIFY(it != map.end());
  459. return *it->value;
  460. };
  461. void StyleComputer::compute_defaulted_property_value(StyleProperties& style, DOM::Element const* element, CSS::PropertyID property_id) const
  462. {
  463. // FIXME: If we don't know the correct initial value for a property, we fall back to InitialStyleValue.
  464. auto it = style.m_property_values.find(property_id);
  465. if (it == style.m_property_values.end()) {
  466. if (is_inherited_property(property_id))
  467. style.m_property_values.set(property_id, get_inherit_value(property_id, element));
  468. else
  469. style.m_property_values.set(property_id, property_initial_value(property_id));
  470. return;
  471. }
  472. if (it->value->is_initial()) {
  473. it->value = property_initial_value(property_id);
  474. return;
  475. }
  476. if (it->value->is_inherit()) {
  477. it->value = get_inherit_value(property_id, element);
  478. return;
  479. }
  480. }
  481. // https://www.w3.org/TR/css-cascade/#defaulting
  482. void StyleComputer::compute_defaulted_values(StyleProperties& style, DOM::Element const* element) const
  483. {
  484. // Walk the list of all known CSS properties and:
  485. // - Add them to `style` if they are missing.
  486. // - Resolve `inherit` and `initial` as needed.
  487. for (auto i = to_underlying(CSS::first_longhand_property_id); i <= to_underlying(CSS::last_longhand_property_id); ++i) {
  488. auto property_id = (CSS::PropertyID)i;
  489. compute_defaulted_property_value(style, element, property_id);
  490. }
  491. }
  492. void StyleComputer::compute_font(StyleProperties& style, DOM::Element const* element) const
  493. {
  494. // To compute the font, first ensure that we've defaulted the relevant CSS font properties.
  495. // FIXME: This should be more sophisticated.
  496. compute_defaulted_property_value(style, element, CSS::PropertyID::FontFamily);
  497. compute_defaulted_property_value(style, element, CSS::PropertyID::FontSize);
  498. compute_defaulted_property_value(style, element, CSS::PropertyID::FontWeight);
  499. auto viewport_rect = document().browsing_context()->viewport_rect();
  500. auto font_size = style.property(CSS::PropertyID::FontSize).value();
  501. auto font_weight = style.property(CSS::PropertyID::FontWeight).value();
  502. int weight = Gfx::FontWeight::Regular;
  503. if (font_weight->is_identifier()) {
  504. switch (static_cast<IdentifierStyleValue const&>(*font_weight).id()) {
  505. case CSS::ValueID::Normal:
  506. weight = Gfx::FontWeight::Regular;
  507. break;
  508. case CSS::ValueID::Bold:
  509. weight = Gfx::FontWeight::Bold;
  510. break;
  511. case CSS::ValueID::Lighter:
  512. // FIXME: This should be relative to the parent.
  513. weight = Gfx::FontWeight::Regular;
  514. break;
  515. case CSS::ValueID::Bolder:
  516. // FIXME: This should be relative to the parent.
  517. weight = Gfx::FontWeight::Bold;
  518. break;
  519. default:
  520. break;
  521. }
  522. } else if (font_weight->has_integer()) {
  523. int font_weight_integer = font_weight->to_integer();
  524. if (font_weight_integer <= Gfx::FontWeight::Regular)
  525. weight = Gfx::FontWeight::Regular;
  526. else if (font_weight_integer <= Gfx::FontWeight::Bold)
  527. weight = Gfx::FontWeight::Bold;
  528. else
  529. weight = Gfx::FontWeight::Black;
  530. }
  531. // FIXME: calc() for font-weight
  532. bool bold = weight > Gfx::FontWeight::Regular;
  533. int size = 10;
  534. if (font_size->is_identifier()) {
  535. switch (static_cast<const IdentifierStyleValue&>(*font_size).id()) {
  536. case CSS::ValueID::XxSmall:
  537. case CSS::ValueID::XSmall:
  538. case CSS::ValueID::Small:
  539. case CSS::ValueID::Medium:
  540. // FIXME: Should be based on "user's default font size"
  541. size = 10;
  542. break;
  543. case CSS::ValueID::Large:
  544. case CSS::ValueID::XLarge:
  545. case CSS::ValueID::XxLarge:
  546. case CSS::ValueID::XxxLarge:
  547. // FIXME: Should be based on "user's default font size"
  548. size = 12;
  549. break;
  550. case CSS::ValueID::Smaller:
  551. case CSS::ValueID::Larger:
  552. // FIXME: Should be based on parent element
  553. break;
  554. default:
  555. break;
  556. }
  557. } else {
  558. // FIXME: Get the root element font.
  559. float root_font_size = 10;
  560. Gfx::FontMetrics font_metrics;
  561. if (element && element->parent_element() && element->parent_element()->specified_css_values())
  562. font_metrics = element->parent_element()->specified_css_values()->computed_font().metrics('M');
  563. else
  564. font_metrics = Gfx::FontDatabase::default_font().metrics('M');
  565. Optional<Length> maybe_length;
  566. if (font_size->is_length()) {
  567. maybe_length = font_size->to_length();
  568. if (maybe_length->is_percentage()) {
  569. auto parent_font_size = size;
  570. if (element && element->parent_element() && element->parent_element()->layout_node() && element->parent_element()->specified_css_values()) {
  571. auto value = element->parent_element()->specified_css_values()->property(CSS::PropertyID::FontSize).value();
  572. if (value->is_length()) {
  573. auto length = static_cast<LengthStyleValue const&>(*value).to_length();
  574. if (length.is_absolute() || length.is_relative())
  575. parent_font_size = length.to_px(viewport_rect, font_metrics, root_font_size);
  576. }
  577. }
  578. maybe_length = Length::make_px(maybe_length->raw_value() / 100.0f * (parent_font_size));
  579. }
  580. } else if (font_size->is_calculated()) {
  581. Length length = Length(0, Length::Type::Calculated);
  582. length.set_calculated_style(verify_cast<CalculatedStyleValue>(font_size.ptr()));
  583. maybe_length = length;
  584. }
  585. if (maybe_length.has_value()) {
  586. // FIXME: Support font-size: calc(...)
  587. if (!maybe_length->is_calculated()) {
  588. auto px = maybe_length.value().to_px(viewport_rect, font_metrics, root_font_size);
  589. if (px != 0)
  590. size = px;
  591. }
  592. }
  593. }
  594. // FIXME: Implement the full font-matching algorithm: https://www.w3.org/TR/css-fonts-4/#font-matching-algorithm
  595. // Note: This is modified by the find_font() lambda
  596. FontSelector font_selector;
  597. bool monospace = false;
  598. auto find_font = [&](String const& family) -> RefPtr<Gfx::Font> {
  599. font_selector = { family, size, weight };
  600. if (auto found_font = FontCache::the().get(font_selector))
  601. return found_font;
  602. if (auto found_font = Gfx::FontDatabase::the().get(family, size, weight))
  603. return found_font;
  604. return {};
  605. };
  606. // FIXME: Replace hard-coded font names with a relevant call to FontDatabase.
  607. // Currently, we cannot request the default font's name, or request it at a specific size and weight.
  608. // So, hard-coded font names it is.
  609. auto find_generic_font = [&](ValueID font_id) -> RefPtr<Gfx::Font> {
  610. switch (font_id) {
  611. case ValueID::Monospace:
  612. case ValueID::UiMonospace:
  613. monospace = true;
  614. return find_font("Csilla");
  615. case ValueID::Serif:
  616. case ValueID::SansSerif:
  617. case ValueID::Cursive:
  618. case ValueID::Fantasy:
  619. case ValueID::UiSerif:
  620. case ValueID::UiSansSerif:
  621. case ValueID::UiRounded:
  622. return find_font("Katica");
  623. default:
  624. return {};
  625. }
  626. };
  627. RefPtr<Gfx::Font> found_font;
  628. auto family_value = style.property(PropertyID::FontFamily).value();
  629. if (family_value->is_value_list()) {
  630. auto& family_list = static_cast<StyleValueList const&>(*family_value).values();
  631. for (auto& family : family_list) {
  632. if (family.is_identifier()) {
  633. found_font = find_generic_font(family.to_identifier());
  634. } else if (family.is_string()) {
  635. found_font = find_font(family.to_string());
  636. }
  637. if (found_font)
  638. break;
  639. }
  640. } else if (family_value->is_identifier()) {
  641. found_font = find_generic_font(family_value->to_identifier());
  642. } else if (family_value->is_string()) {
  643. found_font = find_font(family_value->to_string());
  644. }
  645. if (!found_font) {
  646. found_font = StyleProperties::font_fallback(monospace, bold);
  647. }
  648. FontCache::the().set(font_selector, *found_font);
  649. style.set_computed_font(found_font.release_nonnull());
  650. }
  651. void StyleComputer::absolutize_values(StyleProperties& style, DOM::Element const*) const
  652. {
  653. auto viewport_rect = document().browsing_context()->viewport_rect();
  654. auto font_metrics = style.computed_font().metrics('M');
  655. // FIXME: Get the root element font.
  656. float root_font_size = 10;
  657. for (auto& it : style.properties()) {
  658. it.value->visit_lengths([&](Length& length) {
  659. if (length.is_absolute() || length.is_relative()) {
  660. auto px = length.to_px(viewport_rect, font_metrics, root_font_size);
  661. length = Length::make_px(px);
  662. }
  663. });
  664. }
  665. }
  666. NonnullRefPtr<StyleProperties> StyleComputer::create_document_style() const
  667. {
  668. auto style = StyleProperties::create();
  669. compute_font(style, nullptr);
  670. compute_defaulted_values(style, nullptr);
  671. absolutize_values(style, nullptr);
  672. return style;
  673. }
  674. NonnullRefPtr<StyleProperties> StyleComputer::compute_style(DOM::Element& element) const
  675. {
  676. auto style = StyleProperties::create();
  677. // 1. Perform the cascade. This produces the "specified style"
  678. compute_cascaded_values(style, element);
  679. // 2. Compute the font, since that may be needed for font-relative CSS units
  680. compute_font(style, &element);
  681. // 3. Absolutize values, turning font/viewport relative lengths into absolute lengths
  682. absolutize_values(style, &element);
  683. // 4. Default the values, applying inheritance and 'initial' as needed
  684. compute_defaulted_values(style, &element);
  685. return style;
  686. }
  687. }