StyleComputer.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  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/Page/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, bool is_internally_generated_pseudo_property = false)
  113. {
  114. if (is_pseudo_property(property_id) && !is_internally_generated_pseudo_property) {
  115. dbgln("Ignoring non-internally-generated pseudo property: {}", string_from_property_id(property_id));
  116. return;
  117. }
  118. auto assign_edge_values = [&style](PropertyID top_property, PropertyID right_property, PropertyID bottom_property, PropertyID left_property, auto const& values) {
  119. if (values.size() == 4) {
  120. style.set_property(top_property, values[0]);
  121. style.set_property(right_property, values[1]);
  122. style.set_property(bottom_property, values[2]);
  123. style.set_property(left_property, values[3]);
  124. } else if (values.size() == 3) {
  125. style.set_property(top_property, values[0]);
  126. style.set_property(right_property, values[1]);
  127. style.set_property(bottom_property, values[2]);
  128. style.set_property(left_property, values[1]);
  129. } else if (values.size() == 2) {
  130. style.set_property(top_property, values[0]);
  131. style.set_property(right_property, values[1]);
  132. style.set_property(bottom_property, values[0]);
  133. style.set_property(left_property, values[1]);
  134. } else if (values.size() == 1) {
  135. style.set_property(top_property, values[0]);
  136. style.set_property(right_property, values[0]);
  137. style.set_property(bottom_property, values[0]);
  138. style.set_property(left_property, values[0]);
  139. }
  140. };
  141. if (property_id == CSS::PropertyID::TextDecoration) {
  142. if (value.is_text_decoration()) {
  143. auto& text_decoration = value.as_text_decoration();
  144. style.set_property(CSS::PropertyID::TextDecorationLine, text_decoration.line());
  145. style.set_property(CSS::PropertyID::TextDecorationStyle, text_decoration.style());
  146. style.set_property(CSS::PropertyID::TextDecorationColor, text_decoration.color());
  147. return;
  148. }
  149. style.set_property(CSS::PropertyID::TextDecorationLine, value);
  150. style.set_property(CSS::PropertyID::TextDecorationStyle, value);
  151. style.set_property(CSS::PropertyID::TextDecorationColor, value);
  152. return;
  153. }
  154. if (property_id == CSS::PropertyID::Overflow) {
  155. if (value.is_overflow()) {
  156. auto& overflow = value.as_overflow();
  157. style.set_property(CSS::PropertyID::OverflowX, overflow.overflow_x());
  158. style.set_property(CSS::PropertyID::OverflowY, overflow.overflow_y());
  159. return;
  160. }
  161. style.set_property(CSS::PropertyID::OverflowX, value);
  162. style.set_property(CSS::PropertyID::OverflowY, value);
  163. return;
  164. }
  165. if (property_id == CSS::PropertyID::Border) {
  166. set_property_expanding_shorthands(style, CSS::PropertyID::BorderTop, value, document);
  167. set_property_expanding_shorthands(style, CSS::PropertyID::BorderRight, value, document);
  168. set_property_expanding_shorthands(style, CSS::PropertyID::BorderBottom, value, document);
  169. set_property_expanding_shorthands(style, CSS::PropertyID::BorderLeft, value, document);
  170. // FIXME: Also reset border-image, in line with the spec: https://www.w3.org/TR/css-backgrounds-3/#border-shorthands
  171. return;
  172. }
  173. if (property_id == CSS::PropertyID::BorderRadius) {
  174. if (value.is_value_list()) {
  175. auto& values_list = value.as_value_list();
  176. assign_edge_values(PropertyID::BorderTopLeftRadius, PropertyID::BorderTopRightRadius, PropertyID::BorderBottomRightRadius, PropertyID::BorderBottomLeftRadius, values_list.values());
  177. return;
  178. }
  179. style.set_property(CSS::PropertyID::BorderTopLeftRadius, value);
  180. style.set_property(CSS::PropertyID::BorderTopRightRadius, value);
  181. style.set_property(CSS::PropertyID::BorderBottomRightRadius, value);
  182. style.set_property(CSS::PropertyID::BorderBottomLeftRadius, value);
  183. return;
  184. }
  185. if (property_id == CSS::PropertyID::BorderTop
  186. || property_id == CSS::PropertyID::BorderRight
  187. || property_id == CSS::PropertyID::BorderBottom
  188. || property_id == CSS::PropertyID::BorderLeft) {
  189. Edge edge = Edge::All;
  190. switch (property_id) {
  191. case CSS::PropertyID::BorderTop:
  192. edge = Edge::Top;
  193. break;
  194. case CSS::PropertyID::BorderRight:
  195. edge = Edge::Right;
  196. break;
  197. case CSS::PropertyID::BorderBottom:
  198. edge = Edge::Bottom;
  199. break;
  200. case CSS::PropertyID::BorderLeft:
  201. edge = Edge::Left;
  202. break;
  203. default:
  204. break;
  205. }
  206. if (value.is_border()) {
  207. auto& border = value.as_border();
  208. if (contains(Edge::Top, edge)) {
  209. style.set_property(PropertyID::BorderTopWidth, border.border_width());
  210. style.set_property(PropertyID::BorderTopStyle, border.border_style());
  211. style.set_property(PropertyID::BorderTopColor, border.border_color());
  212. }
  213. if (contains(Edge::Right, edge)) {
  214. style.set_property(PropertyID::BorderRightWidth, border.border_width());
  215. style.set_property(PropertyID::BorderRightStyle, border.border_style());
  216. style.set_property(PropertyID::BorderRightColor, border.border_color());
  217. }
  218. if (contains(Edge::Bottom, edge)) {
  219. style.set_property(PropertyID::BorderBottomWidth, border.border_width());
  220. style.set_property(PropertyID::BorderBottomStyle, border.border_style());
  221. style.set_property(PropertyID::BorderBottomColor, border.border_color());
  222. }
  223. if (contains(Edge::Left, edge)) {
  224. style.set_property(PropertyID::BorderLeftWidth, border.border_width());
  225. style.set_property(PropertyID::BorderLeftStyle, border.border_style());
  226. style.set_property(PropertyID::BorderLeftColor, border.border_color());
  227. }
  228. return;
  229. }
  230. return;
  231. }
  232. if (property_id == CSS::PropertyID::BorderStyle) {
  233. if (value.is_value_list()) {
  234. auto& values_list = value.as_value_list();
  235. assign_edge_values(PropertyID::BorderTopStyle, PropertyID::BorderRightStyle, PropertyID::BorderBottomStyle, PropertyID::BorderLeftStyle, values_list.values());
  236. return;
  237. }
  238. style.set_property(CSS::PropertyID::BorderTopStyle, value);
  239. style.set_property(CSS::PropertyID::BorderRightStyle, value);
  240. style.set_property(CSS::PropertyID::BorderBottomStyle, value);
  241. style.set_property(CSS::PropertyID::BorderLeftStyle, value);
  242. return;
  243. }
  244. if (property_id == CSS::PropertyID::BorderWidth) {
  245. if (value.is_value_list()) {
  246. auto& values_list = value.as_value_list();
  247. assign_edge_values(PropertyID::BorderTopWidth, PropertyID::BorderRightWidth, PropertyID::BorderBottomWidth, PropertyID::BorderLeftWidth, values_list.values());
  248. return;
  249. }
  250. style.set_property(CSS::PropertyID::BorderTopWidth, value);
  251. style.set_property(CSS::PropertyID::BorderRightWidth, value);
  252. style.set_property(CSS::PropertyID::BorderBottomWidth, value);
  253. style.set_property(CSS::PropertyID::BorderLeftWidth, value);
  254. return;
  255. }
  256. if (property_id == CSS::PropertyID::BorderColor) {
  257. if (value.is_value_list()) {
  258. auto& values_list = value.as_value_list();
  259. assign_edge_values(PropertyID::BorderTopColor, PropertyID::BorderRightColor, PropertyID::BorderBottomColor, PropertyID::BorderLeftColor, values_list.values());
  260. return;
  261. }
  262. style.set_property(CSS::PropertyID::BorderTopColor, value);
  263. style.set_property(CSS::PropertyID::BorderRightColor, value);
  264. style.set_property(CSS::PropertyID::BorderBottomColor, value);
  265. style.set_property(CSS::PropertyID::BorderLeftColor, value);
  266. return;
  267. }
  268. if (property_id == CSS::PropertyID::Background) {
  269. auto set_single_background = [&](CSS::BackgroundStyleValue const& background) {
  270. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundColor, background.color(), document);
  271. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundImage, background.image(), document);
  272. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundPosition, background.position(), document);
  273. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundRepeatX, background.repeat_x(), document, true);
  274. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundRepeatY, background.repeat_y(), document, true);
  275. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundAttachment, background.attachment(), document);
  276. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundOrigin, background.origin(), document);
  277. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundClip, background.clip(), document);
  278. };
  279. if (value.is_background()) {
  280. auto& background = value.as_background();
  281. set_single_background(background);
  282. return;
  283. }
  284. if (value.is_value_list()) {
  285. auto& background_list = value.as_value_list().values();
  286. // FIXME: Handle multiple backgrounds.
  287. if (!background_list.is_empty()) {
  288. auto& background = background_list.first();
  289. if (background.is_background())
  290. set_single_background(background.as_background());
  291. }
  292. return;
  293. }
  294. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundColor, value, document);
  295. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundImage, value, document);
  296. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundPosition, value, document);
  297. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundRepeatX, value, document, true);
  298. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundRepeatY, value, document, true);
  299. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundAttachment, value, document);
  300. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundOrigin, value, document);
  301. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundClip, value, document);
  302. return;
  303. }
  304. if (property_id == CSS::PropertyID::BackgroundAttachment) {
  305. if (value.is_value_list()) {
  306. auto& background_attachment_list = value.as_value_list().values();
  307. // FIXME: Handle multiple backgrounds.
  308. if (!background_attachment_list.is_empty()) {
  309. auto& background_attachment = background_attachment_list.first();
  310. style.set_property(CSS::PropertyID::BackgroundAttachment, background_attachment);
  311. }
  312. return;
  313. }
  314. style.set_property(CSS::PropertyID::BackgroundAttachment, value);
  315. return;
  316. }
  317. if (property_id == CSS::PropertyID::BackgroundClip) {
  318. if (value.is_value_list()) {
  319. auto& background_clip_list = value.as_value_list().values();
  320. // FIXME: Handle multiple backgrounds.
  321. if (!background_clip_list.is_empty()) {
  322. auto& background_clip = background_clip_list.first();
  323. style.set_property(CSS::PropertyID::BackgroundClip, background_clip);
  324. }
  325. return;
  326. }
  327. style.set_property(CSS::PropertyID::BackgroundClip, value);
  328. return;
  329. }
  330. if (property_id == CSS::PropertyID::BackgroundImage) {
  331. if (value.is_value_list()) {
  332. auto& background_image_list = value.as_value_list().values();
  333. // FIXME: Handle multiple backgrounds.
  334. if (!background_image_list.is_empty()) {
  335. auto& background_image = background_image_list.first();
  336. style.set_property(CSS::PropertyID::BackgroundImage, background_image);
  337. }
  338. return;
  339. }
  340. style.set_property(CSS::PropertyID::BackgroundImage, value);
  341. return;
  342. }
  343. if (property_id == CSS::PropertyID::BackgroundOrigin) {
  344. if (value.is_value_list()) {
  345. auto& background_origin_list = value.as_value_list().values();
  346. // FIXME: Handle multiple backgrounds.
  347. if (!background_origin_list.is_empty()) {
  348. auto& background_origin = background_origin_list.first();
  349. style.set_property(CSS::PropertyID::BackgroundOrigin, background_origin);
  350. }
  351. return;
  352. }
  353. style.set_property(CSS::PropertyID::BackgroundOrigin, value);
  354. return;
  355. }
  356. if (property_id == CSS::PropertyID::BackgroundPosition) {
  357. if (value.is_value_list()) {
  358. auto& background_position_list = value.as_value_list().values();
  359. // FIXME: Handle multiple backgrounds.
  360. if (!background_position_list.is_empty()) {
  361. auto& background_position = background_position_list.first();
  362. style.set_property(CSS::PropertyID::BackgroundPosition, background_position);
  363. }
  364. return;
  365. }
  366. style.set_property(CSS::PropertyID::BackgroundPosition, value);
  367. return;
  368. }
  369. if (property_id == CSS::PropertyID::BackgroundRepeat) {
  370. if (value.is_value_list()) {
  371. auto& background_repeat_list = value.as_value_list().values();
  372. // FIXME: Handle multiple backgrounds.
  373. if (!background_repeat_list.is_empty()) {
  374. auto& maybe_background_repeat = background_repeat_list.first();
  375. if (maybe_background_repeat.is_background_repeat()) {
  376. auto& background_repeat = maybe_background_repeat.as_background_repeat();
  377. set_property_expanding_shorthands(style, PropertyID::BackgroundRepeatX, background_repeat.repeat_x(), document, true);
  378. set_property_expanding_shorthands(style, PropertyID::BackgroundRepeatY, background_repeat.repeat_y(), document, true);
  379. }
  380. }
  381. return;
  382. }
  383. if (value.is_background_repeat()) {
  384. auto& background_repeat = value.as_background_repeat();
  385. set_property_expanding_shorthands(style, PropertyID::BackgroundRepeatX, background_repeat.repeat_x(), document, true);
  386. set_property_expanding_shorthands(style, PropertyID::BackgroundRepeatY, background_repeat.repeat_y(), document, true);
  387. return;
  388. }
  389. set_property_expanding_shorthands(style, PropertyID::BackgroundRepeatX, value, document, true);
  390. set_property_expanding_shorthands(style, PropertyID::BackgroundRepeatY, value, document, true);
  391. return;
  392. }
  393. if (property_id == CSS::PropertyID::BackgroundRepeatX || property_id == CSS::PropertyID::BackgroundRepeatY) {
  394. auto value_id = value.to_identifier();
  395. if (value_id == CSS::ValueID::RepeatX || value_id == CSS::ValueID::RepeatY)
  396. return;
  397. style.set_property(property_id, value);
  398. return;
  399. }
  400. if (property_id == CSS::PropertyID::Margin) {
  401. if (value.is_value_list()) {
  402. auto& values_list = value.as_value_list();
  403. assign_edge_values(PropertyID::MarginTop, PropertyID::MarginRight, PropertyID::MarginBottom, PropertyID::MarginLeft, values_list.values());
  404. return;
  405. }
  406. style.set_property(CSS::PropertyID::MarginTop, value);
  407. style.set_property(CSS::PropertyID::MarginRight, value);
  408. style.set_property(CSS::PropertyID::MarginBottom, value);
  409. style.set_property(CSS::PropertyID::MarginLeft, value);
  410. return;
  411. }
  412. if (property_id == CSS::PropertyID::Padding) {
  413. if (value.is_value_list()) {
  414. auto& values_list = value.as_value_list();
  415. assign_edge_values(PropertyID::PaddingTop, PropertyID::PaddingRight, PropertyID::PaddingBottom, PropertyID::PaddingLeft, values_list.values());
  416. return;
  417. }
  418. style.set_property(CSS::PropertyID::PaddingTop, value);
  419. style.set_property(CSS::PropertyID::PaddingRight, value);
  420. style.set_property(CSS::PropertyID::PaddingBottom, value);
  421. style.set_property(CSS::PropertyID::PaddingLeft, value);
  422. return;
  423. }
  424. if (property_id == CSS::PropertyID::ListStyle) {
  425. if (value.is_list_style()) {
  426. auto& list_style = value.as_list_style();
  427. style.set_property(CSS::PropertyID::ListStylePosition, list_style.position());
  428. style.set_property(CSS::PropertyID::ListStyleImage, list_style.image());
  429. style.set_property(CSS::PropertyID::ListStyleType, list_style.style_type());
  430. return;
  431. }
  432. style.set_property(CSS::PropertyID::ListStylePosition, value);
  433. style.set_property(CSS::PropertyID::ListStyleImage, value);
  434. style.set_property(CSS::PropertyID::ListStyleType, value);
  435. return;
  436. }
  437. if (property_id == CSS::PropertyID::Font) {
  438. if (value.is_font()) {
  439. auto& font_shorthand = value.as_font();
  440. style.set_property(CSS::PropertyID::FontSize, font_shorthand.font_size());
  441. style.set_property(CSS::PropertyID::FontFamily, font_shorthand.font_families());
  442. style.set_property(CSS::PropertyID::FontStyle, font_shorthand.font_style());
  443. style.set_property(CSS::PropertyID::FontWeight, font_shorthand.font_weight());
  444. style.set_property(CSS::PropertyID::LineHeight, font_shorthand.line_height());
  445. // FIXME: Implement font-stretch and font-variant
  446. return;
  447. }
  448. style.set_property(CSS::PropertyID::FontSize, value);
  449. style.set_property(CSS::PropertyID::FontFamily, value);
  450. style.set_property(CSS::PropertyID::FontStyle, value);
  451. style.set_property(CSS::PropertyID::FontWeight, value);
  452. style.set_property(CSS::PropertyID::LineHeight, value);
  453. // FIXME: Implement font-stretch and font-variant
  454. return;
  455. }
  456. if (property_id == CSS::PropertyID::Flex) {
  457. if (value.is_flex()) {
  458. auto& flex = value.as_flex();
  459. style.set_property(CSS::PropertyID::FlexGrow, flex.grow());
  460. style.set_property(CSS::PropertyID::FlexShrink, flex.shrink());
  461. style.set_property(CSS::PropertyID::FlexBasis, flex.basis());
  462. return;
  463. }
  464. style.set_property(CSS::PropertyID::FlexGrow, value);
  465. style.set_property(CSS::PropertyID::FlexShrink, value);
  466. style.set_property(CSS::PropertyID::FlexBasis, value);
  467. return;
  468. }
  469. if (property_id == CSS::PropertyID::FlexFlow) {
  470. if (value.is_flex_flow()) {
  471. auto& flex_flow = value.as_flex_flow();
  472. style.set_property(CSS::PropertyID::FlexDirection, flex_flow.flex_direction());
  473. style.set_property(CSS::PropertyID::FlexWrap, flex_flow.flex_wrap());
  474. return;
  475. }
  476. style.set_property(CSS::PropertyID::FlexDirection, value);
  477. style.set_property(CSS::PropertyID::FlexWrap, value);
  478. return;
  479. }
  480. style.set_property(property_id, value);
  481. }
  482. StyleComputer::CustomPropertyResolutionTuple StyleComputer::resolve_custom_property_with_specificity(DOM::Element& element, String const& custom_property_name) const
  483. {
  484. if (auto maybe_property = element.resolve_custom_property(custom_property_name); maybe_property.has_value())
  485. return maybe_property.value();
  486. auto parent_element = element.parent_element();
  487. CustomPropertyResolutionTuple parent_resolved {};
  488. if (parent_element)
  489. parent_resolved = resolve_custom_property_with_specificity(*parent_element, custom_property_name);
  490. auto matching_rules = collect_matching_rules(element);
  491. sort_matching_rules(matching_rules);
  492. for (int i = matching_rules.size() - 1; i >= 0; --i) {
  493. auto& match = matching_rules[i];
  494. if (match.specificity < parent_resolved.specificity)
  495. continue;
  496. auto custom_property_style = verify_cast<PropertyOwningCSSStyleDeclaration>(match.rule->declaration()).custom_property(custom_property_name);
  497. if (custom_property_style.has_value()) {
  498. element.add_custom_property(custom_property_name, { custom_property_style.value(), match.specificity });
  499. return { custom_property_style.value(), match.specificity };
  500. }
  501. }
  502. return parent_resolved;
  503. }
  504. Optional<StyleProperty> StyleComputer::resolve_custom_property(DOM::Element& element, String const& custom_property_name) const
  505. {
  506. auto resolved_with_specificity = resolve_custom_property_with_specificity(element, custom_property_name);
  507. return resolved_with_specificity.style;
  508. }
  509. struct MatchingDeclarations {
  510. Vector<MatchingRule> user_agent_rules;
  511. Vector<MatchingRule> author_rules;
  512. };
  513. void StyleComputer::cascade_declarations(StyleProperties& style, DOM::Element& element, Vector<MatchingRule> const& matching_rules, CascadeOrigin cascade_origin, bool important) const
  514. {
  515. for (auto& match : matching_rules) {
  516. for (auto& property : verify_cast<PropertyOwningCSSStyleDeclaration>(match.rule->declaration()).properties()) {
  517. if (important != property.important)
  518. continue;
  519. auto property_value = property.value;
  520. if (property.value->is_custom_property()) {
  521. auto custom_property_name = property.value->as_custom_property().custom_property_name();
  522. auto resolved = resolve_custom_property(element, custom_property_name);
  523. if (resolved.has_value()) {
  524. property_value = resolved.value().value;
  525. }
  526. }
  527. set_property_expanding_shorthands(style, property.property_id, property_value, m_document);
  528. }
  529. }
  530. if (cascade_origin == CascadeOrigin::Author) {
  531. if (auto* inline_style = verify_cast<ElementInlineCSSStyleDeclaration>(element.inline_style())) {
  532. for (auto& property : inline_style->properties()) {
  533. if (important != property.important)
  534. continue;
  535. set_property_expanding_shorthands(style, property.property_id, property.value, m_document);
  536. }
  537. }
  538. }
  539. }
  540. // https://www.w3.org/TR/css-cascade/#cascading
  541. void StyleComputer::compute_cascaded_values(StyleProperties& style, DOM::Element& element) const
  542. {
  543. // First, we collect all the CSS rules whose selectors match `element`:
  544. MatchingRuleSet matching_rule_set;
  545. matching_rule_set.user_agent_rules = collect_matching_rules(element, CascadeOrigin::UserAgent);
  546. sort_matching_rules(matching_rule_set.user_agent_rules);
  547. matching_rule_set.author_rules = collect_matching_rules(element, CascadeOrigin::Author);
  548. sort_matching_rules(matching_rule_set.author_rules);
  549. // Then we apply the declarations from the matched rules in cascade order:
  550. // Normal user agent declarations
  551. cascade_declarations(style, element, matching_rule_set.user_agent_rules, CascadeOrigin::UserAgent, false);
  552. // FIXME: Normal user declarations
  553. // Normal author declarations
  554. cascade_declarations(style, element, matching_rule_set.author_rules, CascadeOrigin::Author, false);
  555. // Author presentational hints (NOTE: The spec doesn't say exactly how to prioritize these.)
  556. element.apply_presentational_hints(style);
  557. // FIXME: Animation declarations [css-animations-1]
  558. // Important author declarations
  559. cascade_declarations(style, element, matching_rule_set.author_rules, CascadeOrigin::Author, true);
  560. // FIXME: Important user declarations
  561. // Important user agent declarations
  562. cascade_declarations(style, element, matching_rule_set.user_agent_rules, CascadeOrigin::UserAgent, true);
  563. // FIXME: Transition declarations [css-transitions-1]
  564. }
  565. static NonnullRefPtr<StyleValue> get_initial_value(CSS::PropertyID property_id)
  566. {
  567. auto value = property_initial_value(property_id);
  568. if (!value)
  569. return InitialStyleValue::the();
  570. return value.release_nonnull();
  571. };
  572. static NonnullRefPtr<StyleValue> get_inherit_value(CSS::PropertyID property_id, DOM::Element const* element)
  573. {
  574. if (!element || !element->parent_element() || !element->parent_element()->specified_css_values())
  575. return get_initial_value(property_id);
  576. auto& map = element->parent_element()->specified_css_values()->properties();
  577. auto it = map.find(property_id);
  578. VERIFY(it != map.end());
  579. return *it->value;
  580. };
  581. void StyleComputer::compute_defaulted_property_value(StyleProperties& style, DOM::Element const* element, CSS::PropertyID property_id) const
  582. {
  583. // FIXME: If we don't know the correct initial value for a property, we fall back to InitialStyleValue.
  584. auto it = style.m_property_values.find(property_id);
  585. if (it == style.m_property_values.end()) {
  586. if (is_inherited_property(property_id))
  587. style.m_property_values.set(property_id, get_inherit_value(property_id, element));
  588. else
  589. style.m_property_values.set(property_id, get_initial_value(property_id));
  590. return;
  591. }
  592. if (it->value->is_initial()) {
  593. it->value = get_initial_value(property_id);
  594. return;
  595. }
  596. if (it->value->is_inherit()) {
  597. it->value = get_inherit_value(property_id, element);
  598. return;
  599. }
  600. }
  601. // https://www.w3.org/TR/css-cascade/#defaulting
  602. void StyleComputer::compute_defaulted_values(StyleProperties& style, DOM::Element const* element) const
  603. {
  604. // Walk the list of all known CSS properties and:
  605. // - Add them to `style` if they are missing.
  606. // - Resolve `inherit` and `initial` as needed.
  607. for (auto i = to_underlying(CSS::first_longhand_property_id); i <= to_underlying(CSS::last_longhand_property_id); ++i) {
  608. auto property_id = (CSS::PropertyID)i;
  609. compute_defaulted_property_value(style, element, property_id);
  610. }
  611. }
  612. void StyleComputer::compute_font(StyleProperties& style, DOM::Element const* element) const
  613. {
  614. // To compute the font, first ensure that we've defaulted the relevant CSS font properties.
  615. // FIXME: This should be more sophisticated.
  616. compute_defaulted_property_value(style, element, CSS::PropertyID::FontFamily);
  617. compute_defaulted_property_value(style, element, CSS::PropertyID::FontSize);
  618. compute_defaulted_property_value(style, element, CSS::PropertyID::FontWeight);
  619. auto viewport_rect = document().browsing_context()->viewport_rect();
  620. auto font_size = style.property(CSS::PropertyID::FontSize).value();
  621. auto font_weight = style.property(CSS::PropertyID::FontWeight).value();
  622. int weight = Gfx::FontWeight::Regular;
  623. if (font_weight->is_identifier()) {
  624. switch (static_cast<IdentifierStyleValue const&>(*font_weight).id()) {
  625. case CSS::ValueID::Normal:
  626. weight = Gfx::FontWeight::Regular;
  627. break;
  628. case CSS::ValueID::Bold:
  629. weight = Gfx::FontWeight::Bold;
  630. break;
  631. case CSS::ValueID::Lighter:
  632. // FIXME: This should be relative to the parent.
  633. weight = Gfx::FontWeight::Regular;
  634. break;
  635. case CSS::ValueID::Bolder:
  636. // FIXME: This should be relative to the parent.
  637. weight = Gfx::FontWeight::Bold;
  638. break;
  639. default:
  640. break;
  641. }
  642. } else if (font_weight->has_integer()) {
  643. int font_weight_integer = font_weight->to_integer();
  644. if (font_weight_integer <= Gfx::FontWeight::Regular)
  645. weight = Gfx::FontWeight::Regular;
  646. else if (font_weight_integer <= Gfx::FontWeight::Bold)
  647. weight = Gfx::FontWeight::Bold;
  648. else
  649. weight = Gfx::FontWeight::Black;
  650. }
  651. // FIXME: calc() for font-weight
  652. bool bold = weight > Gfx::FontWeight::Regular;
  653. int size = 10;
  654. if (font_size->is_identifier()) {
  655. switch (static_cast<const IdentifierStyleValue&>(*font_size).id()) {
  656. case CSS::ValueID::XxSmall:
  657. case CSS::ValueID::XSmall:
  658. case CSS::ValueID::Small:
  659. case CSS::ValueID::Medium:
  660. // FIXME: Should be based on "user's default font size"
  661. size = 10;
  662. break;
  663. case CSS::ValueID::Large:
  664. case CSS::ValueID::XLarge:
  665. case CSS::ValueID::XxLarge:
  666. case CSS::ValueID::XxxLarge:
  667. // FIXME: Should be based on "user's default font size"
  668. size = 12;
  669. break;
  670. case CSS::ValueID::Smaller:
  671. case CSS::ValueID::Larger:
  672. // FIXME: Should be based on parent element
  673. break;
  674. default:
  675. break;
  676. }
  677. } else {
  678. // FIXME: Get the root element font.
  679. float root_font_size = 10;
  680. Gfx::FontMetrics font_metrics;
  681. if (element && element->parent_element() && element->parent_element()->specified_css_values())
  682. font_metrics = element->parent_element()->specified_css_values()->computed_font().metrics('M');
  683. else
  684. font_metrics = Gfx::FontDatabase::default_font().metrics('M');
  685. Optional<Length> maybe_length;
  686. if (font_size->is_length()) {
  687. maybe_length = font_size->to_length();
  688. if (maybe_length->is_percentage()) {
  689. auto parent_font_size = size;
  690. if (element && element->parent_element() && element->parent_element()->layout_node() && element->parent_element()->specified_css_values()) {
  691. auto value = element->parent_element()->specified_css_values()->property(CSS::PropertyID::FontSize).value();
  692. if (value->is_length()) {
  693. auto length = static_cast<LengthStyleValue const&>(*value).to_length();
  694. if (length.is_absolute() || length.is_relative())
  695. parent_font_size = length.to_px(viewport_rect, font_metrics, root_font_size);
  696. }
  697. }
  698. maybe_length = Length::make_px(maybe_length->raw_value() / 100.0f * (parent_font_size));
  699. }
  700. } else if (font_size->is_calculated()) {
  701. Length length = Length(0, Length::Type::Calculated);
  702. length.set_calculated_style(verify_cast<CalculatedStyleValue>(font_size.ptr()));
  703. maybe_length = length;
  704. }
  705. if (maybe_length.has_value()) {
  706. // FIXME: Support font-size: calc(...)
  707. if (!maybe_length->is_calculated()) {
  708. auto px = maybe_length.value().to_px(viewport_rect, font_metrics, root_font_size);
  709. if (px != 0)
  710. size = px;
  711. }
  712. }
  713. }
  714. // FIXME: Implement the full font-matching algorithm: https://www.w3.org/TR/css-fonts-4/#font-matching-algorithm
  715. // Note: This is modified by the find_font() lambda
  716. FontSelector font_selector;
  717. bool monospace = false;
  718. auto find_font = [&](String const& family) -> RefPtr<Gfx::Font> {
  719. font_selector = { family, size, weight };
  720. if (auto found_font = FontCache::the().get(font_selector))
  721. return found_font;
  722. if (auto found_font = Gfx::FontDatabase::the().get(family, size, weight))
  723. return found_font;
  724. return {};
  725. };
  726. // FIXME: Replace hard-coded font names with a relevant call to FontDatabase.
  727. // Currently, we cannot request the default font's name, or request it at a specific size and weight.
  728. // So, hard-coded font names it is.
  729. auto find_generic_font = [&](ValueID font_id) -> RefPtr<Gfx::Font> {
  730. switch (font_id) {
  731. case ValueID::Monospace:
  732. case ValueID::UiMonospace:
  733. monospace = true;
  734. return find_font("Csilla");
  735. case ValueID::Serif:
  736. case ValueID::SansSerif:
  737. case ValueID::Cursive:
  738. case ValueID::Fantasy:
  739. case ValueID::UiSerif:
  740. case ValueID::UiSansSerif:
  741. case ValueID::UiRounded:
  742. return find_font("Katica");
  743. default:
  744. return {};
  745. }
  746. };
  747. RefPtr<Gfx::Font> found_font;
  748. auto family_value = style.property(PropertyID::FontFamily).value();
  749. if (family_value->is_value_list()) {
  750. auto& family_list = static_cast<StyleValueList const&>(*family_value).values();
  751. for (auto& family : family_list) {
  752. if (family.is_identifier()) {
  753. found_font = find_generic_font(family.to_identifier());
  754. } else if (family.is_string()) {
  755. found_font = find_font(family.to_string());
  756. }
  757. if (found_font)
  758. break;
  759. }
  760. } else if (family_value->is_identifier()) {
  761. found_font = find_generic_font(family_value->to_identifier());
  762. } else if (family_value->is_string()) {
  763. found_font = find_font(family_value->to_string());
  764. }
  765. if (!found_font) {
  766. found_font = StyleProperties::font_fallback(monospace, bold);
  767. }
  768. FontCache::the().set(font_selector, *found_font);
  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. for (auto& it : style.properties()) {
  778. it.value->visit_lengths([&](Length& length) {
  779. if (length.is_absolute() || length.is_relative()) {
  780. auto px = length.to_px(viewport_rect, font_metrics, root_font_size);
  781. length = Length::make_px(px);
  782. }
  783. });
  784. }
  785. }
  786. NonnullRefPtr<StyleProperties> StyleComputer::create_document_style() const
  787. {
  788. auto style = StyleProperties::create();
  789. compute_font(style, nullptr);
  790. compute_defaulted_values(style, nullptr);
  791. absolutize_values(style, nullptr);
  792. return style;
  793. }
  794. NonnullRefPtr<StyleProperties> StyleComputer::compute_style(DOM::Element& element) const
  795. {
  796. auto style = StyleProperties::create();
  797. // 1. Perform the cascade. This produces the "specified style"
  798. compute_cascaded_values(style, element);
  799. // 2. Compute the font, since that may be needed for font-relative CSS units
  800. compute_font(style, &element);
  801. // 3. Absolutize values, turning font/viewport relative lengths into absolute lengths
  802. absolutize_values(style, &element);
  803. // 4. Default the values, applying inheritance and 'initial' as needed
  804. compute_defaulted_values(style, &element);
  805. return style;
  806. }
  807. }