StyleResolver.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, the SerenityOS developers.
  4. * Copyright (c) 2021, Sam Atkins <atkinssj@gmail.com>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/QuickSort.h>
  9. #include <LibWeb/CSS/CSSStyleRule.h>
  10. #include <LibWeb/CSS/Parser/Parser.h>
  11. #include <LibWeb/CSS/SelectorEngine.h>
  12. #include <LibWeb/CSS/StyleResolver.h>
  13. #include <LibWeb/CSS/StyleSheet.h>
  14. #include <LibWeb/DOM/Document.h>
  15. #include <LibWeb/DOM/Element.h>
  16. #include <LibWeb/Dump.h>
  17. #include <ctype.h>
  18. #include <stdio.h>
  19. namespace Web::CSS {
  20. StyleResolver::StyleResolver(DOM::Document& document)
  21. : m_document(document)
  22. {
  23. }
  24. StyleResolver::~StyleResolver()
  25. {
  26. }
  27. static StyleSheet& default_stylesheet()
  28. {
  29. static StyleSheet* sheet;
  30. if (!sheet) {
  31. extern char const default_stylesheet_source[];
  32. String css = default_stylesheet_source;
  33. sheet = parse_css(CSS::ParsingContext(), css).leak_ref();
  34. }
  35. return *sheet;
  36. }
  37. static StyleSheet& quirks_mode_stylesheet()
  38. {
  39. static StyleSheet* sheet;
  40. if (!sheet) {
  41. extern char const quirks_mode_stylesheet_source[];
  42. String css = quirks_mode_stylesheet_source;
  43. sheet = parse_css(CSS::ParsingContext(), css).leak_ref();
  44. }
  45. return *sheet;
  46. }
  47. template<typename Callback>
  48. void StyleResolver::for_each_stylesheet(Callback callback) const
  49. {
  50. callback(default_stylesheet());
  51. if (document().in_quirks_mode())
  52. callback(quirks_mode_stylesheet());
  53. for (auto& sheet : document().style_sheets().sheets()) {
  54. callback(sheet);
  55. }
  56. }
  57. Vector<MatchingRule> StyleResolver::collect_matching_rules(DOM::Element const& element) const
  58. {
  59. Vector<MatchingRule> matching_rules;
  60. size_t style_sheet_index = 0;
  61. for_each_stylesheet([&](auto& sheet) {
  62. size_t rule_index = 0;
  63. static_cast<CSSStyleSheet const&>(sheet).for_each_effective_style_rule([&](auto& rule) {
  64. size_t selector_index = 0;
  65. for (auto& selector : rule.selectors()) {
  66. if (SelectorEngine::matches(selector, element)) {
  67. matching_rules.append({ rule, style_sheet_index, rule_index, selector_index, selector.specificity() });
  68. break;
  69. }
  70. ++selector_index;
  71. }
  72. ++rule_index;
  73. });
  74. ++style_sheet_index;
  75. });
  76. return matching_rules;
  77. }
  78. void StyleResolver::sort_matching_rules(Vector<MatchingRule>& matching_rules) const
  79. {
  80. quick_sort(matching_rules, [&](MatchingRule& a, MatchingRule& b) {
  81. auto& a_selector = a.rule->selectors()[a.selector_index];
  82. auto& b_selector = b.rule->selectors()[b.selector_index];
  83. auto a_specificity = a_selector.specificity();
  84. auto b_specificity = b_selector.specificity();
  85. if (a_selector.specificity() == b_selector.specificity()) {
  86. if (a.style_sheet_index == b.style_sheet_index)
  87. return a.rule_index < b.rule_index;
  88. return a.style_sheet_index < b.style_sheet_index;
  89. }
  90. return a_specificity < b_specificity;
  91. });
  92. }
  93. bool StyleResolver::is_inherited_property(CSS::PropertyID property_id)
  94. {
  95. static HashTable<CSS::PropertyID> inherited_properties;
  96. if (inherited_properties.is_empty()) {
  97. inherited_properties.set(CSS::PropertyID::BorderCollapse);
  98. inherited_properties.set(CSS::PropertyID::BorderSpacing);
  99. inherited_properties.set(CSS::PropertyID::Color);
  100. inherited_properties.set(CSS::PropertyID::FontFamily);
  101. inherited_properties.set(CSS::PropertyID::FontSize);
  102. inherited_properties.set(CSS::PropertyID::FontStyle);
  103. inherited_properties.set(CSS::PropertyID::FontVariant);
  104. inherited_properties.set(CSS::PropertyID::FontWeight);
  105. inherited_properties.set(CSS::PropertyID::LetterSpacing);
  106. inherited_properties.set(CSS::PropertyID::LineHeight);
  107. inherited_properties.set(CSS::PropertyID::ListStyle);
  108. inherited_properties.set(CSS::PropertyID::ListStyleImage);
  109. inherited_properties.set(CSS::PropertyID::ListStylePosition);
  110. inherited_properties.set(CSS::PropertyID::ListStyleType);
  111. inherited_properties.set(CSS::PropertyID::TextAlign);
  112. inherited_properties.set(CSS::PropertyID::TextIndent);
  113. inherited_properties.set(CSS::PropertyID::TextTransform);
  114. inherited_properties.set(CSS::PropertyID::Visibility);
  115. inherited_properties.set(CSS::PropertyID::WhiteSpace);
  116. inherited_properties.set(CSS::PropertyID::WordSpacing);
  117. // FIXME: This property is not supposed to be inherited, but we currently
  118. // rely on inheritance to propagate decorations into line boxes.
  119. inherited_properties.set(CSS::PropertyID::TextDecorationLine);
  120. }
  121. return inherited_properties.contains(property_id);
  122. }
  123. static Vector<String> split_on_whitespace(StringView const& string)
  124. {
  125. if (string.is_empty())
  126. return {};
  127. Vector<String> v;
  128. size_t substart = 0;
  129. for (size_t i = 0; i < string.length(); ++i) {
  130. char ch = string.characters_without_null_termination()[i];
  131. if (isspace(ch)) {
  132. size_t sublen = i - substart;
  133. if (sublen != 0)
  134. v.append(string.substring_view(substart, sublen));
  135. substart = i + 1;
  136. }
  137. }
  138. size_t taillen = string.length() - substart;
  139. if (taillen != 0)
  140. v.append(string.substring_view(substart, taillen));
  141. return v;
  142. }
  143. enum class Edge {
  144. Top,
  145. Right,
  146. Bottom,
  147. Left,
  148. All,
  149. };
  150. static bool contains(Edge a, Edge b)
  151. {
  152. return a == b || b == Edge::All;
  153. }
  154. static inline void set_property_border_width(StyleProperties& style, StyleValue const& value, Edge edge)
  155. {
  156. VERIFY(value.is_length());
  157. if (contains(Edge::Top, edge))
  158. style.set_property(CSS::PropertyID::BorderTopWidth, value);
  159. if (contains(Edge::Right, edge))
  160. style.set_property(CSS::PropertyID::BorderRightWidth, value);
  161. if (contains(Edge::Bottom, edge))
  162. style.set_property(CSS::PropertyID::BorderBottomWidth, value);
  163. if (contains(Edge::Left, edge))
  164. style.set_property(CSS::PropertyID::BorderLeftWidth, value);
  165. }
  166. static inline void set_property_border_color(StyleProperties& style, StyleValue const& value, Edge edge)
  167. {
  168. VERIFY(value.is_color());
  169. if (contains(Edge::Top, edge))
  170. style.set_property(CSS::PropertyID::BorderTopColor, value);
  171. if (contains(Edge::Right, edge))
  172. style.set_property(CSS::PropertyID::BorderRightColor, value);
  173. if (contains(Edge::Bottom, edge))
  174. style.set_property(CSS::PropertyID::BorderBottomColor, value);
  175. if (contains(Edge::Left, edge))
  176. style.set_property(CSS::PropertyID::BorderLeftColor, value);
  177. }
  178. static inline void set_property_border_style(StyleProperties& style, StyleValue const& value, Edge edge)
  179. {
  180. VERIFY(value.type() == CSS::StyleValue::Type::Identifier);
  181. if (contains(Edge::Top, edge))
  182. style.set_property(CSS::PropertyID::BorderTopStyle, value);
  183. if (contains(Edge::Right, edge))
  184. style.set_property(CSS::PropertyID::BorderRightStyle, value);
  185. if (contains(Edge::Bottom, edge))
  186. style.set_property(CSS::PropertyID::BorderBottomStyle, value);
  187. if (contains(Edge::Left, edge))
  188. style.set_property(CSS::PropertyID::BorderLeftStyle, value);
  189. }
  190. static inline bool is_color(StyleValue const& value)
  191. {
  192. if (value.is_builtin_or_dynamic())
  193. return true;
  194. if (value.is_color())
  195. return true;
  196. return false;
  197. }
  198. static inline bool is_font_family(StyleValue const& value)
  199. {
  200. if (value.is_builtin_or_dynamic())
  201. return true;
  202. if (value.is_string())
  203. return true;
  204. switch (value.to_identifier()) {
  205. case ValueID::Cursive:
  206. case ValueID::Fantasy:
  207. case ValueID::Monospace:
  208. case ValueID::Serif:
  209. case ValueID::SansSerif:
  210. case ValueID::UiMonospace:
  211. case ValueID::UiRounded:
  212. case ValueID::UiSerif:
  213. case ValueID::UiSansSerif:
  214. return true;
  215. default:
  216. return false;
  217. }
  218. }
  219. static inline bool is_line_style(StyleValue const& value)
  220. {
  221. if (value.is_builtin_or_dynamic())
  222. return true;
  223. switch (value.to_identifier()) {
  224. case ValueID::Dotted:
  225. case ValueID::Dashed:
  226. case ValueID::Solid:
  227. case ValueID::Double:
  228. case ValueID::Groove:
  229. case ValueID::Ridge:
  230. case ValueID::None:
  231. case ValueID::Hidden:
  232. case ValueID::Inset:
  233. case ValueID::Outset:
  234. return true;
  235. default:
  236. return false;
  237. }
  238. }
  239. static inline bool is_line_width(StyleValue const& value)
  240. {
  241. if (value.is_builtin_or_dynamic())
  242. return true;
  243. if (value.is_length())
  244. return true;
  245. // FIXME: Implement thin/medium/thick
  246. switch (value.to_identifier()) {
  247. case ValueID::None:
  248. return true;
  249. default:
  250. return false;
  251. }
  252. }
  253. 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)
  254. {
  255. CSS::ParsingContext context(document);
  256. if (is_pseudo_property(property_id) && !is_internally_generated_pseudo_property) {
  257. dbgln("Ignoring non-internally-generated pseudo property: {}", string_from_property_id(property_id));
  258. return;
  259. }
  260. auto assign_edge_values = [&style](PropertyID top_property, PropertyID right_property, PropertyID bottom_property, PropertyID left_property, auto const& values) {
  261. if (values.size() == 4) {
  262. style.set_property(top_property, values[0]);
  263. style.set_property(right_property, values[1]);
  264. style.set_property(bottom_property, values[2]);
  265. style.set_property(left_property, values[3]);
  266. } else if (values.size() == 3) {
  267. style.set_property(top_property, values[0]);
  268. style.set_property(right_property, values[1]);
  269. style.set_property(bottom_property, values[2]);
  270. style.set_property(left_property, values[1]);
  271. } else if (values.size() == 2) {
  272. style.set_property(top_property, values[0]);
  273. style.set_property(right_property, values[1]);
  274. style.set_property(bottom_property, values[0]);
  275. style.set_property(left_property, values[1]);
  276. } else if (values.size() == 1) {
  277. style.set_property(top_property, values[0]);
  278. style.set_property(right_property, values[0]);
  279. style.set_property(bottom_property, values[0]);
  280. style.set_property(left_property, values[0]);
  281. }
  282. };
  283. if (property_id == CSS::PropertyID::TextDecoration) {
  284. if (value.is_text_decoration()) {
  285. auto& text_decoration = static_cast<TextDecorationStyleValue const&>(value);
  286. style.set_property(CSS::PropertyID::TextDecorationLine, text_decoration.line());
  287. style.set_property(CSS::PropertyID::TextDecorationStyle, text_decoration.style());
  288. style.set_property(CSS::PropertyID::TextDecorationColor, text_decoration.color());
  289. return;
  290. }
  291. if (value.is_builtin()) {
  292. style.set_property(CSS::PropertyID::TextDecorationLine, value);
  293. style.set_property(CSS::PropertyID::TextDecorationStyle, value);
  294. style.set_property(CSS::PropertyID::TextDecorationColor, value);
  295. return;
  296. }
  297. return;
  298. }
  299. if (property_id == CSS::PropertyID::Overflow) {
  300. style.set_property(CSS::PropertyID::OverflowX, value);
  301. style.set_property(CSS::PropertyID::OverflowY, value);
  302. return;
  303. }
  304. if (property_id == CSS::PropertyID::Border) {
  305. set_property_expanding_shorthands(style, CSS::PropertyID::BorderTop, value, document);
  306. set_property_expanding_shorthands(style, CSS::PropertyID::BorderRight, value, document);
  307. set_property_expanding_shorthands(style, CSS::PropertyID::BorderBottom, value, document);
  308. set_property_expanding_shorthands(style, CSS::PropertyID::BorderLeft, value, document);
  309. return;
  310. }
  311. if (property_id == CSS::PropertyID::BorderRadius) {
  312. // FIXME: Allow for two values per corner to support elliptical radii.
  313. // FIXME: Add support the '/' to specify elliptical radii.
  314. if (value.is_length()) {
  315. style.set_property(CSS::PropertyID::BorderTopLeftRadius, value);
  316. style.set_property(CSS::PropertyID::BorderTopRightRadius, value);
  317. style.set_property(CSS::PropertyID::BorderBottomRightRadius, value);
  318. style.set_property(CSS::PropertyID::BorderBottomLeftRadius, value);
  319. return;
  320. }
  321. if (value.is_component_value_list()) {
  322. auto& parts = static_cast<CSS::ValueListStyleValue const&>(value).values();
  323. if (parts.size() == 2) {
  324. auto diagonal1 = Parser::parse_css_value(context, property_id, parts[0]);
  325. auto diagonal2 = Parser::parse_css_value(context, property_id, parts[1]);
  326. if (diagonal1 && diagonal2) {
  327. style.set_property(CSS::PropertyID::BorderTopLeftRadius, *diagonal1);
  328. style.set_property(CSS::PropertyID::BorderBottomRightRadius, *diagonal1);
  329. style.set_property(CSS::PropertyID::BorderTopRightRadius, *diagonal2);
  330. style.set_property(CSS::PropertyID::BorderBottomLeftRadius, *diagonal2);
  331. }
  332. return;
  333. }
  334. if (parts.size() == 3) {
  335. auto top_left = Parser::parse_css_value(context, property_id, parts[0]);
  336. auto diagonal = Parser::parse_css_value(context, property_id, parts[1]);
  337. auto bottom_right = Parser::parse_css_value(context, property_id, parts[2]);
  338. if (top_left && diagonal && bottom_right) {
  339. style.set_property(CSS::PropertyID::BorderTopLeftRadius, *top_left);
  340. style.set_property(CSS::PropertyID::BorderBottomRightRadius, *bottom_right);
  341. style.set_property(CSS::PropertyID::BorderTopRightRadius, *diagonal);
  342. style.set_property(CSS::PropertyID::BorderBottomLeftRadius, *diagonal);
  343. }
  344. return;
  345. }
  346. if (parts.size() == 4) {
  347. auto top_left = Parser::parse_css_value(context, property_id, parts[0]);
  348. auto top_right = Parser::parse_css_value(context, property_id, parts[1]);
  349. auto bottom_right = Parser::parse_css_value(context, property_id, parts[2]);
  350. auto bottom_left = Parser::parse_css_value(context, property_id, parts[3]);
  351. if (top_left && top_right && bottom_right && bottom_left) {
  352. style.set_property(CSS::PropertyID::BorderTopLeftRadius, *top_left);
  353. style.set_property(CSS::PropertyID::BorderBottomRightRadius, *bottom_right);
  354. style.set_property(CSS::PropertyID::BorderTopRightRadius, *top_right);
  355. style.set_property(CSS::PropertyID::BorderBottomLeftRadius, *bottom_left);
  356. }
  357. return;
  358. }
  359. dbgln("Unsure what to do with CSS border-radius value '{}'", value.to_string());
  360. return;
  361. }
  362. return;
  363. }
  364. if (property_id == CSS::PropertyID::BorderTop
  365. || property_id == CSS::PropertyID::BorderRight
  366. || property_id == CSS::PropertyID::BorderBottom
  367. || property_id == CSS::PropertyID::BorderLeft) {
  368. Edge edge = Edge::All;
  369. switch (property_id) {
  370. case CSS::PropertyID::BorderTop:
  371. edge = Edge::Top;
  372. break;
  373. case CSS::PropertyID::BorderRight:
  374. edge = Edge::Right;
  375. break;
  376. case CSS::PropertyID::BorderBottom:
  377. edge = Edge::Bottom;
  378. break;
  379. case CSS::PropertyID::BorderLeft:
  380. edge = Edge::Left;
  381. break;
  382. default:
  383. break;
  384. }
  385. auto parts = split_on_whitespace(value.to_string());
  386. if (value.is_length()) {
  387. set_property_border_width(style, value, edge);
  388. return;
  389. }
  390. if (value.is_color()) {
  391. set_property_border_color(style, value, edge);
  392. return;
  393. }
  394. if (value.is_component_value_list()) {
  395. auto& parts = static_cast<CSS::ValueListStyleValue const&>(value).values();
  396. if (parts.size() == 1) {
  397. auto value = Parser::parse_css_value(context, property_id, parts[0]);
  398. if (value && is_line_style(*value)) {
  399. set_property_border_style(style, value.release_nonnull(), edge);
  400. set_property_border_color(style, ColorStyleValue::create(Gfx::Color::Black), edge);
  401. set_property_border_width(style, LengthStyleValue::create(Length(3, Length::Type::Px)), edge);
  402. return;
  403. }
  404. }
  405. RefPtr<StyleValue> line_width_value;
  406. RefPtr<StyleValue> color_value;
  407. RefPtr<StyleValue> line_style_value;
  408. for (auto& part : parts) {
  409. auto value = Parser::parse_css_value(context, property_id, part);
  410. if (!value)
  411. return;
  412. if (is_line_width(*value)) {
  413. if (line_width_value)
  414. return;
  415. line_width_value = move(value);
  416. continue;
  417. }
  418. if (is_color(*value)) {
  419. if (color_value)
  420. return;
  421. color_value = move(value);
  422. continue;
  423. }
  424. if (is_line_style(*value)) {
  425. if (line_style_value)
  426. return;
  427. line_style_value = move(value);
  428. continue;
  429. }
  430. }
  431. if (line_width_value)
  432. set_property_border_width(style, line_width_value.release_nonnull(), edge);
  433. if (color_value)
  434. set_property_border_color(style, color_value.release_nonnull(), edge);
  435. if (line_style_value)
  436. set_property_border_style(style, line_style_value.release_nonnull(), edge);
  437. return;
  438. }
  439. return;
  440. }
  441. if (property_id == CSS::PropertyID::BorderStyle) {
  442. if (value.is_value_list()) {
  443. auto& values_list = static_cast<StyleValueList const&>(value);
  444. assign_edge_values(PropertyID::BorderTopStyle, PropertyID::BorderRightStyle, PropertyID::BorderBottomStyle, PropertyID::BorderLeftStyle, values_list.values());
  445. return;
  446. }
  447. if (value.is_builtin()) {
  448. style.set_property(CSS::PropertyID::BorderTopStyle, value);
  449. style.set_property(CSS::PropertyID::BorderRightStyle, value);
  450. style.set_property(CSS::PropertyID::BorderBottomStyle, value);
  451. style.set_property(CSS::PropertyID::BorderLeftStyle, value);
  452. return;
  453. }
  454. return;
  455. }
  456. if (property_id == CSS::PropertyID::BorderWidth) {
  457. if (value.is_value_list()) {
  458. auto& values_list = static_cast<StyleValueList const&>(value);
  459. assign_edge_values(PropertyID::BorderTopWidth, PropertyID::BorderRightWidth, PropertyID::BorderBottomWidth, PropertyID::BorderLeftWidth, values_list.values());
  460. return;
  461. }
  462. if (value.is_builtin()) {
  463. style.set_property(CSS::PropertyID::BorderTopWidth, value);
  464. style.set_property(CSS::PropertyID::BorderRightWidth, value);
  465. style.set_property(CSS::PropertyID::BorderBottomWidth, value);
  466. style.set_property(CSS::PropertyID::BorderLeftWidth, value);
  467. return;
  468. }
  469. return;
  470. }
  471. if (property_id == CSS::PropertyID::BorderColor) {
  472. if (value.is_value_list()) {
  473. auto& values_list = static_cast<StyleValueList const&>(value);
  474. assign_edge_values(PropertyID::BorderTopColor, PropertyID::BorderRightColor, PropertyID::BorderBottomColor, PropertyID::BorderLeftColor, values_list.values());
  475. return;
  476. }
  477. if (value.is_builtin()) {
  478. style.set_property(CSS::PropertyID::BorderTopColor, value);
  479. style.set_property(CSS::PropertyID::BorderRightColor, value);
  480. style.set_property(CSS::PropertyID::BorderBottomColor, value);
  481. style.set_property(CSS::PropertyID::BorderLeftColor, value);
  482. return;
  483. }
  484. return;
  485. }
  486. if (property_id == CSS::PropertyID::Background) {
  487. auto set_single_background = [&](CSS::BackgroundStyleValue const& background) {
  488. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundColor, background.color(), document);
  489. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundImage, background.image(), document);
  490. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundRepeatX, background.repeat_x(), document, true);
  491. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundRepeatY, background.repeat_y(), document, true);
  492. };
  493. if (value.is_background()) {
  494. auto& background = static_cast<CSS::BackgroundStyleValue const&>(value);
  495. set_single_background(background);
  496. return;
  497. }
  498. if (value.is_value_list()) {
  499. auto& background_list = static_cast<CSS::StyleValueList const&>(value).values();
  500. // FIXME: Handle multiple backgrounds.
  501. if (!background_list.is_empty()) {
  502. auto& background = background_list.first();
  503. if (background.is_background())
  504. set_single_background(static_cast<CSS::BackgroundStyleValue const&>(background));
  505. }
  506. return;
  507. }
  508. if (value.is_builtin()) {
  509. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundColor, value, document);
  510. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundImage, value, document);
  511. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundRepeatX, value, document, true);
  512. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundRepeatY, value, document, true);
  513. return;
  514. }
  515. return;
  516. }
  517. if (property_id == CSS::PropertyID::BackgroundImage) {
  518. // FIXME: Remove string parsing once DeprecatedCSSParser is gone.
  519. if (value.is_string()) {
  520. return;
  521. auto string = value.to_string();
  522. if (!string.starts_with("url("))
  523. return;
  524. if (!string.ends_with(')'))
  525. return;
  526. auto url = string.substring_view(4, string.length() - 5);
  527. if (url.length() >= 2 && url.starts_with('"') && url.ends_with('"'))
  528. url = url.substring_view(1, url.length() - 2);
  529. else if (url.length() >= 2 && url.starts_with('\'') && url.ends_with('\''))
  530. url = url.substring_view(1, url.length() - 2);
  531. auto background_image_value = ImageStyleValue::create(document.complete_url(url), document);
  532. style.set_property(CSS::PropertyID::BackgroundImage, move(background_image_value));
  533. return;
  534. }
  535. if (value.is_component_value_list()) {
  536. auto parts = static_cast<CSS::ValueListStyleValue const&>(value).values();
  537. // FIXME: Handle multiple backgrounds.
  538. if (!parts.is_empty()) {
  539. auto first_value = Parser::parse_css_value(context, property_id, parts[0]);
  540. if (first_value)
  541. style.set_property(CSS::PropertyID::BackgroundImage, *first_value);
  542. }
  543. return;
  544. }
  545. style.set_property(CSS::PropertyID::BackgroundImage, value);
  546. return;
  547. }
  548. if (property_id == CSS::PropertyID::BackgroundRepeat) {
  549. auto assign_background_repeat_from_single_value = [&](StyleValue const& value) {
  550. auto value_id = value.to_identifier();
  551. if (value_id == ValueID::RepeatX || value_id == ValueID::RepeatY) {
  552. auto repeat_x = IdentifierStyleValue::create(value_id == ValueID::RepeatX ? ValueID::Repeat : ValueID::NoRepeat);
  553. auto repeat_y = IdentifierStyleValue::create(value_id == ValueID::RepeatX ? ValueID::NoRepeat : ValueID::Repeat);
  554. set_property_expanding_shorthands(style, PropertyID::BackgroundRepeatX, repeat_x, document, true);
  555. set_property_expanding_shorthands(style, PropertyID::BackgroundRepeatY, repeat_y, document, true);
  556. } else {
  557. set_property_expanding_shorthands(style, PropertyID::BackgroundRepeatX, value, document, true);
  558. set_property_expanding_shorthands(style, PropertyID::BackgroundRepeatY, value, document, true);
  559. }
  560. };
  561. if (value.is_component_value_list()) {
  562. auto parts = static_cast<CSS::ValueListStyleValue const&>(value).values();
  563. NonnullRefPtrVector<StyleValue> repeat_values;
  564. for (auto& part : parts) {
  565. if (part.is(Token::Type::Comma)) {
  566. // FIXME: Handle multiple backgrounds.
  567. break;
  568. }
  569. auto parsed_value = Parser::parse_css_value(context, property_id, part);
  570. if (parsed_value)
  571. repeat_values.append(parsed_value.release_nonnull());
  572. }
  573. if (repeat_values.size() == 1) {
  574. assign_background_repeat_from_single_value(repeat_values[0]);
  575. } else if (repeat_values.size() == 2) {
  576. set_property_expanding_shorthands(style, PropertyID::BackgroundRepeatX, repeat_values[0], document, true);
  577. set_property_expanding_shorthands(style, PropertyID::BackgroundRepeatY, repeat_values[1], document, true);
  578. }
  579. return;
  580. }
  581. assign_background_repeat_from_single_value(value);
  582. return;
  583. }
  584. if (property_id == CSS::PropertyID::BackgroundRepeatX || property_id == CSS::PropertyID::BackgroundRepeatY) {
  585. auto value_id = value.to_identifier();
  586. if (value_id == CSS::ValueID::RepeatX || value_id == CSS::ValueID::RepeatY)
  587. return;
  588. style.set_property(property_id, value);
  589. return;
  590. }
  591. if (property_id == CSS::PropertyID::Margin) {
  592. if (value.is_value_list()) {
  593. auto& values_list = static_cast<StyleValueList const&>(value);
  594. assign_edge_values(PropertyID::MarginTop, PropertyID::MarginRight, PropertyID::MarginBottom, PropertyID::MarginLeft, values_list.values());
  595. return;
  596. }
  597. if (value.is_length() || value.is_builtin()) {
  598. style.set_property(CSS::PropertyID::MarginTop, value);
  599. style.set_property(CSS::PropertyID::MarginRight, value);
  600. style.set_property(CSS::PropertyID::MarginBottom, value);
  601. style.set_property(CSS::PropertyID::MarginLeft, value);
  602. return;
  603. }
  604. return;
  605. }
  606. if (property_id == CSS::PropertyID::Padding) {
  607. if (value.is_value_list()) {
  608. auto& values_list = static_cast<StyleValueList const&>(value);
  609. assign_edge_values(PropertyID::PaddingTop, PropertyID::PaddingRight, PropertyID::PaddingBottom, PropertyID::PaddingLeft, values_list.values());
  610. return;
  611. }
  612. if (value.is_length() || value.is_builtin()) {
  613. style.set_property(CSS::PropertyID::PaddingTop, value);
  614. style.set_property(CSS::PropertyID::PaddingRight, value);
  615. style.set_property(CSS::PropertyID::PaddingBottom, value);
  616. style.set_property(CSS::PropertyID::PaddingLeft, value);
  617. return;
  618. }
  619. return;
  620. }
  621. if (property_id == CSS::PropertyID::ListStyle) {
  622. if (value.is_list_style()) {
  623. auto& list_style = static_cast<CSS::ListStyleStyleValue const&>(value);
  624. style.set_property(CSS::PropertyID::ListStylePosition, list_style.position());
  625. style.set_property(CSS::PropertyID::ListStyleImage, list_style.image());
  626. style.set_property(CSS::PropertyID::ListStyleType, list_style.style_type());
  627. return;
  628. }
  629. if (value.is_builtin()) {
  630. style.set_property(CSS::PropertyID::ListStylePosition, value);
  631. style.set_property(CSS::PropertyID::ListStyleImage, value);
  632. style.set_property(CSS::PropertyID::ListStyleType, value);
  633. return;
  634. }
  635. return;
  636. }
  637. if (property_id == CSS::PropertyID::Font) {
  638. if (value.is_font()) {
  639. auto& font_shorthand = static_cast<CSS::FontStyleValue const&>(value);
  640. style.set_property(CSS::PropertyID::FontSize, font_shorthand.font_size());
  641. // FIXME: Support multiple font-families
  642. style.set_property(CSS::PropertyID::FontFamily, font_shorthand.font_families().first());
  643. style.set_property(CSS::PropertyID::FontStyle, font_shorthand.font_style());
  644. style.set_property(CSS::PropertyID::FontWeight, font_shorthand.font_weight());
  645. style.set_property(CSS::PropertyID::LineHeight, font_shorthand.line_height());
  646. // FIXME: Implement font-stretch and font-variant
  647. return;
  648. }
  649. if (value.is_builtin()) {
  650. style.set_property(CSS::PropertyID::FontSize, value);
  651. // FIXME: Support multiple font-families
  652. style.set_property(CSS::PropertyID::FontFamily, value);
  653. style.set_property(CSS::PropertyID::FontStyle, value);
  654. style.set_property(CSS::PropertyID::FontWeight, value);
  655. style.set_property(CSS::PropertyID::LineHeight, value);
  656. // FIXME: Implement font-stretch and font-variant
  657. return;
  658. }
  659. return;
  660. }
  661. if (property_id == CSS::PropertyID::FontFamily) {
  662. if (value.is_component_value_list()) {
  663. auto parts = static_cast<CSS::ValueListStyleValue const&>(value).values();
  664. // FIXME: Handle multiple font-families separated by commas, for fallback purposes.
  665. for (auto& part : parts) {
  666. auto value = Parser::parse_css_value(context, property_id, part);
  667. if (!value)
  668. return;
  669. if (is_font_family(*value))
  670. style.set_property(CSS::PropertyID::FontFamily, *value);
  671. break;
  672. }
  673. return;
  674. }
  675. style.set_property(CSS::PropertyID::FontFamily, value);
  676. return;
  677. }
  678. if (property_id == CSS::PropertyID::Flex) {
  679. if (value.is_flex()) {
  680. auto& flex = static_cast<CSS::FlexStyleValue const&>(value);
  681. style.set_property(CSS::PropertyID::FlexGrow, flex.grow());
  682. style.set_property(CSS::PropertyID::FlexShrink, flex.shrink());
  683. style.set_property(CSS::PropertyID::FlexBasis, flex.basis());
  684. return;
  685. }
  686. if (value.is_builtin()) {
  687. style.set_property(CSS::PropertyID::FlexGrow, value);
  688. style.set_property(CSS::PropertyID::FlexShrink, value);
  689. style.set_property(CSS::PropertyID::FlexBasis, value);
  690. return;
  691. }
  692. return;
  693. }
  694. if (property_id == CSS::PropertyID::FlexFlow) {
  695. if (value.is_flex_flow()) {
  696. auto& flex_flow = static_cast<FlexFlowStyleValue const&>(value);
  697. style.set_property(CSS::PropertyID::FlexDirection, flex_flow.flex_direction());
  698. style.set_property(CSS::PropertyID::FlexWrap, flex_flow.flex_wrap());
  699. return;
  700. }
  701. if (value.is_builtin()) {
  702. style.set_property(CSS::PropertyID::FlexDirection, value);
  703. style.set_property(CSS::PropertyID::FlexWrap, value);
  704. return;
  705. }
  706. return;
  707. }
  708. if (value.is_component_value_list()) {
  709. dbgln("Values list for CSS property '{}' went unhandled. List: '{}'", string_from_property_id(property_id), value.to_string());
  710. return;
  711. }
  712. style.set_property(property_id, value);
  713. }
  714. StyleResolver::CustomPropertyResolutionTuple StyleResolver::resolve_custom_property_with_specificity(DOM::Element& element, String const& custom_property_name) const
  715. {
  716. if (auto maybe_property = element.resolve_custom_property(custom_property_name); maybe_property.has_value())
  717. return maybe_property.value();
  718. auto parent_element = element.parent_element();
  719. CustomPropertyResolutionTuple parent_resolved {};
  720. if (parent_element)
  721. parent_resolved = resolve_custom_property_with_specificity(*parent_element, custom_property_name);
  722. auto matching_rules = collect_matching_rules(element);
  723. sort_matching_rules(matching_rules);
  724. for (int i = matching_rules.size() - 1; i >= 0; --i) {
  725. auto& match = matching_rules[i];
  726. if (match.specificity < parent_resolved.specificity)
  727. continue;
  728. auto custom_property_style = match.rule->declaration().custom_property(custom_property_name);
  729. if (custom_property_style.has_value()) {
  730. element.add_custom_property(custom_property_name, { custom_property_style.value(), match.specificity });
  731. return { custom_property_style.value(), match.specificity };
  732. }
  733. }
  734. return parent_resolved;
  735. }
  736. Optional<StyleProperty> StyleResolver::resolve_custom_property(DOM::Element& element, String const& custom_property_name) const
  737. {
  738. auto resolved_with_specificity = resolve_custom_property_with_specificity(element, custom_property_name);
  739. return resolved_with_specificity.style;
  740. }
  741. NonnullRefPtr<StyleProperties> StyleResolver::resolve_style(DOM::Element& element) const
  742. {
  743. auto style = StyleProperties::create();
  744. if (auto* parent_style = element.parent_element() ? element.parent_element()->specified_css_values() : nullptr) {
  745. parent_style->for_each_property([&](auto property_id, auto& value) {
  746. if (is_inherited_property(property_id))
  747. set_property_expanding_shorthands(style, property_id, value, m_document);
  748. });
  749. }
  750. element.apply_presentational_hints(*style);
  751. auto matching_rules = collect_matching_rules(element);
  752. sort_matching_rules(matching_rules);
  753. for (auto& match : matching_rules) {
  754. for (auto& property : match.rule->declaration().properties()) {
  755. auto property_value = property.value;
  756. if (property.value->is_custom_property()) {
  757. auto prop = reinterpret_cast<CSS::CustomStyleValue const*>(property.value.ptr());
  758. auto custom_prop_name = prop->custom_property_name();
  759. auto resolved = resolve_custom_property(element, custom_prop_name);
  760. if (resolved.has_value()) {
  761. property_value = resolved.value().value;
  762. }
  763. }
  764. set_property_expanding_shorthands(style, property.property_id, property_value, m_document);
  765. }
  766. }
  767. if (auto* inline_style = element.inline_style()) {
  768. for (auto& property : inline_style->properties()) {
  769. set_property_expanding_shorthands(style, property.property_id, property.value, m_document);
  770. }
  771. }
  772. return style;
  773. }
  774. }