StyleResolver.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/QuickSort.h>
  8. #include <LibWeb/CSS/CSSStyleRule.h>
  9. #include <LibWeb/CSS/Parser/DeprecatedCSSParser.h>
  10. #include <LibWeb/CSS/SelectorEngine.h>
  11. #include <LibWeb/CSS/StyleResolver.h>
  12. #include <LibWeb/CSS/StyleSheet.h>
  13. #include <LibWeb/DOM/Document.h>
  14. #include <LibWeb/DOM/Element.h>
  15. #include <LibWeb/Dump.h>
  16. #include <ctype.h>
  17. #include <stdio.h>
  18. namespace Web::CSS {
  19. StyleResolver::StyleResolver(DOM::Document& document)
  20. : m_document(document)
  21. {
  22. }
  23. StyleResolver::~StyleResolver()
  24. {
  25. }
  26. static StyleSheet& default_stylesheet()
  27. {
  28. static StyleSheet* sheet;
  29. if (!sheet) {
  30. extern const char default_stylesheet_source[];
  31. String css = default_stylesheet_source;
  32. sheet = parse_css(CSS::ParsingContext(), css).leak_ref();
  33. }
  34. return *sheet;
  35. }
  36. static StyleSheet& quirks_mode_stylesheet()
  37. {
  38. static StyleSheet* sheet;
  39. if (!sheet) {
  40. extern const char quirks_mode_stylesheet_source[];
  41. String css = quirks_mode_stylesheet_source;
  42. sheet = parse_css(CSS::ParsingContext(), css).leak_ref();
  43. }
  44. return *sheet;
  45. }
  46. template<typename Callback>
  47. void StyleResolver::for_each_stylesheet(Callback callback) const
  48. {
  49. callback(default_stylesheet());
  50. if (document().in_quirks_mode())
  51. callback(quirks_mode_stylesheet());
  52. for (auto& sheet : document().style_sheets().sheets()) {
  53. callback(sheet);
  54. }
  55. }
  56. Vector<MatchingRule> StyleResolver::collect_matching_rules(const DOM::Element& element) const
  57. {
  58. Vector<MatchingRule> matching_rules;
  59. size_t style_sheet_index = 0;
  60. for_each_stylesheet([&](auto& sheet) {
  61. if (!is<CSSStyleSheet>(sheet))
  62. return;
  63. size_t rule_index = 0;
  64. static_cast<const CSSStyleSheet&>(sheet).for_each_effective_style_rule([&](auto& rule) {
  65. size_t selector_index = 0;
  66. for (auto& selector : rule.selectors()) {
  67. if (SelectorEngine::matches(selector, element)) {
  68. matching_rules.append({ rule, style_sheet_index, rule_index, selector_index });
  69. break;
  70. }
  71. ++selector_index;
  72. }
  73. ++rule_index;
  74. });
  75. ++style_sheet_index;
  76. });
  77. return matching_rules;
  78. }
  79. void StyleResolver::sort_matching_rules(Vector<MatchingRule>& matching_rules) const
  80. {
  81. quick_sort(matching_rules, [&](MatchingRule& a, MatchingRule& b) {
  82. auto& a_selector = a.rule->selectors()[a.selector_index];
  83. auto& b_selector = b.rule->selectors()[b.selector_index];
  84. auto a_specificity = a_selector.specificity();
  85. auto b_specificity = b_selector.specificity();
  86. if (a_selector.specificity() == b_selector.specificity()) {
  87. if (a.style_sheet_index == b.style_sheet_index)
  88. return a.rule_index < b.rule_index;
  89. return a.style_sheet_index < b.style_sheet_index;
  90. }
  91. return a_specificity < b_specificity;
  92. });
  93. }
  94. bool StyleResolver::is_inherited_property(CSS::PropertyID property_id)
  95. {
  96. static HashTable<CSS::PropertyID> inherited_properties;
  97. if (inherited_properties.is_empty()) {
  98. inherited_properties.set(CSS::PropertyID::BorderCollapse);
  99. inherited_properties.set(CSS::PropertyID::BorderSpacing);
  100. inherited_properties.set(CSS::PropertyID::Color);
  101. inherited_properties.set(CSS::PropertyID::FontFamily);
  102. inherited_properties.set(CSS::PropertyID::FontSize);
  103. inherited_properties.set(CSS::PropertyID::FontStyle);
  104. inherited_properties.set(CSS::PropertyID::FontVariant);
  105. inherited_properties.set(CSS::PropertyID::FontWeight);
  106. inherited_properties.set(CSS::PropertyID::LetterSpacing);
  107. inherited_properties.set(CSS::PropertyID::LineHeight);
  108. inherited_properties.set(CSS::PropertyID::ListStyle);
  109. inherited_properties.set(CSS::PropertyID::ListStyleImage);
  110. inherited_properties.set(CSS::PropertyID::ListStylePosition);
  111. inherited_properties.set(CSS::PropertyID::ListStyleType);
  112. inherited_properties.set(CSS::PropertyID::TextAlign);
  113. inherited_properties.set(CSS::PropertyID::TextIndent);
  114. inherited_properties.set(CSS::PropertyID::TextTransform);
  115. inherited_properties.set(CSS::PropertyID::Visibility);
  116. inherited_properties.set(CSS::PropertyID::WhiteSpace);
  117. inherited_properties.set(CSS::PropertyID::WordSpacing);
  118. // FIXME: This property is not supposed to be inherited, but we currently
  119. // rely on inheritance to propagate decorations into line boxes.
  120. inherited_properties.set(CSS::PropertyID::TextDecorationLine);
  121. }
  122. return inherited_properties.contains(property_id);
  123. }
  124. static Vector<String> split_on_whitespace(const StringView& string)
  125. {
  126. if (string.is_empty())
  127. return {};
  128. Vector<String> v;
  129. size_t substart = 0;
  130. for (size_t i = 0; i < string.length(); ++i) {
  131. char ch = string.characters_without_null_termination()[i];
  132. if (isspace(ch)) {
  133. size_t sublen = i - substart;
  134. if (sublen != 0)
  135. v.append(string.substring_view(substart, sublen));
  136. substart = i + 1;
  137. }
  138. }
  139. size_t taillen = string.length() - substart;
  140. if (taillen != 0)
  141. v.append(string.substring_view(substart, taillen));
  142. return v;
  143. }
  144. enum class Edge {
  145. Top,
  146. Right,
  147. Bottom,
  148. Left,
  149. All,
  150. };
  151. static bool contains(Edge a, Edge b)
  152. {
  153. return a == b || b == Edge::All;
  154. }
  155. static inline void set_property_border_width(StyleProperties& style, const StyleValue& value, Edge edge)
  156. {
  157. VERIFY(value.is_length());
  158. if (contains(Edge::Top, edge))
  159. style.set_property(CSS::PropertyID::BorderTopWidth, value);
  160. if (contains(Edge::Right, edge))
  161. style.set_property(CSS::PropertyID::BorderRightWidth, value);
  162. if (contains(Edge::Bottom, edge))
  163. style.set_property(CSS::PropertyID::BorderBottomWidth, value);
  164. if (contains(Edge::Left, edge))
  165. style.set_property(CSS::PropertyID::BorderLeftWidth, value);
  166. }
  167. static inline void set_property_border_color(StyleProperties& style, const StyleValue& value, Edge edge)
  168. {
  169. VERIFY(value.is_color());
  170. if (contains(Edge::Top, edge))
  171. style.set_property(CSS::PropertyID::BorderTopColor, value);
  172. if (contains(Edge::Right, edge))
  173. style.set_property(CSS::PropertyID::BorderRightColor, value);
  174. if (contains(Edge::Bottom, edge))
  175. style.set_property(CSS::PropertyID::BorderBottomColor, value);
  176. if (contains(Edge::Left, edge))
  177. style.set_property(CSS::PropertyID::BorderLeftColor, value);
  178. }
  179. static inline void set_property_border_style(StyleProperties& style, const StyleValue& value, Edge edge)
  180. {
  181. VERIFY(value.type() == CSS::StyleValue::Type::Identifier);
  182. if (contains(Edge::Top, edge))
  183. style.set_property(CSS::PropertyID::BorderTopStyle, value);
  184. if (contains(Edge::Right, edge))
  185. style.set_property(CSS::PropertyID::BorderRightStyle, value);
  186. if (contains(Edge::Bottom, edge))
  187. style.set_property(CSS::PropertyID::BorderBottomStyle, value);
  188. if (contains(Edge::Left, edge))
  189. style.set_property(CSS::PropertyID::BorderLeftStyle, value);
  190. }
  191. static inline bool is_background_repeat_property(const StyleValue& value)
  192. {
  193. if (!value.is_identifier())
  194. return false;
  195. switch (value.to_identifier()) {
  196. case CSS::ValueID::NoRepeat:
  197. case CSS::ValueID::Repeat:
  198. case CSS::ValueID::RepeatX:
  199. case CSS::ValueID::RepeatY:
  200. case CSS::ValueID::Round:
  201. case CSS::ValueID::Space:
  202. return true;
  203. default:
  204. return false;
  205. }
  206. }
  207. static void set_property_expanding_shorthands(StyleProperties& style, CSS::PropertyID property_id, const StyleValue& value, DOM::Document& document, bool is_internally_generated_pseudo_property = false)
  208. {
  209. CSS::ParsingContext context(document);
  210. if (is_pseudo_property(property_id) && !is_internally_generated_pseudo_property) {
  211. dbgln("Ignoring non-internally-generated pseudo property: {}", string_from_property_id(property_id));
  212. return;
  213. }
  214. if (property_id == CSS::PropertyID::TextDecoration) {
  215. switch (value.to_identifier()) {
  216. case CSS::ValueID::None:
  217. case CSS::ValueID::Underline:
  218. case CSS::ValueID::Overline:
  219. case CSS::ValueID::LineThrough:
  220. case CSS::ValueID::Blink:
  221. set_property_expanding_shorthands(style, CSS::PropertyID::TextDecorationLine, value, document);
  222. default:
  223. break;
  224. }
  225. return;
  226. }
  227. if (property_id == CSS::PropertyID::Overflow) {
  228. style.set_property(CSS::PropertyID::OverflowX, value);
  229. style.set_property(CSS::PropertyID::OverflowY, value);
  230. return;
  231. }
  232. if (property_id == CSS::PropertyID::Border) {
  233. set_property_expanding_shorthands(style, CSS::PropertyID::BorderTop, value, document);
  234. set_property_expanding_shorthands(style, CSS::PropertyID::BorderRight, value, document);
  235. set_property_expanding_shorthands(style, CSS::PropertyID::BorderBottom, value, document);
  236. set_property_expanding_shorthands(style, CSS::PropertyID::BorderLeft, value, document);
  237. return;
  238. }
  239. if (property_id == CSS::PropertyID::BorderTop
  240. || property_id == CSS::PropertyID::BorderRight
  241. || property_id == CSS::PropertyID::BorderBottom
  242. || property_id == CSS::PropertyID::BorderLeft) {
  243. Edge edge = Edge::All;
  244. switch (property_id) {
  245. case CSS::PropertyID::BorderTop:
  246. edge = Edge::Top;
  247. break;
  248. case CSS::PropertyID::BorderRight:
  249. edge = Edge::Right;
  250. break;
  251. case CSS::PropertyID::BorderBottom:
  252. edge = Edge::Bottom;
  253. break;
  254. case CSS::PropertyID::BorderLeft:
  255. edge = Edge::Left;
  256. break;
  257. default:
  258. break;
  259. }
  260. auto parts = split_on_whitespace(value.to_string());
  261. if (value.is_length()) {
  262. set_property_border_width(style, value, edge);
  263. return;
  264. }
  265. if (value.is_color()) {
  266. set_property_border_color(style, value, edge);
  267. return;
  268. }
  269. if (value.is_string()) {
  270. auto parts = split_on_whitespace(value.to_string());
  271. if (parts.size() == 1) {
  272. if (auto value = parse_line_style(context, parts[0])) {
  273. set_property_border_style(style, value.release_nonnull(), edge);
  274. set_property_border_color(style, ColorStyleValue::create(Gfx::Color::Black), edge);
  275. set_property_border_width(style, LengthStyleValue::create(Length(3, Length::Type::Px)), edge);
  276. return;
  277. }
  278. }
  279. RefPtr<LengthStyleValue> line_width_value;
  280. RefPtr<ColorStyleValue> color_value;
  281. RefPtr<IdentifierStyleValue> line_style_value;
  282. for (auto& part : parts) {
  283. if (auto value = parse_line_width(context, part)) {
  284. if (line_width_value)
  285. return;
  286. line_width_value = move(value);
  287. continue;
  288. }
  289. if (auto value = parse_color(context, part)) {
  290. if (color_value)
  291. return;
  292. color_value = move(value);
  293. continue;
  294. }
  295. if (auto value = parse_line_style(context, part)) {
  296. if (line_style_value)
  297. return;
  298. line_style_value = move(value);
  299. continue;
  300. }
  301. }
  302. if (line_width_value)
  303. set_property_border_width(style, line_width_value.release_nonnull(), edge);
  304. if (color_value)
  305. set_property_border_color(style, color_value.release_nonnull(), edge);
  306. if (line_style_value)
  307. set_property_border_style(style, line_style_value.release_nonnull(), edge);
  308. return;
  309. }
  310. return;
  311. }
  312. if (property_id == CSS::PropertyID::BorderStyle) {
  313. auto parts = split_on_whitespace(value.to_string());
  314. if (value.is_string() && parts.size() == 4) {
  315. auto top = parse_css_value(context, parts[0]);
  316. auto right = parse_css_value(context, parts[1]);
  317. auto bottom = parse_css_value(context, parts[2]);
  318. auto left = parse_css_value(context, parts[3]);
  319. if (top && right && bottom && left) {
  320. style.set_property(CSS::PropertyID::BorderTopStyle, *top);
  321. style.set_property(CSS::PropertyID::BorderRightStyle, *right);
  322. style.set_property(CSS::PropertyID::BorderBottomStyle, *bottom);
  323. style.set_property(CSS::PropertyID::BorderLeftStyle, *left);
  324. }
  325. } else if (value.is_string() && parts.size() == 3) {
  326. auto top = parse_css_value(context, parts[0]);
  327. auto right = parse_css_value(context, parts[1]);
  328. auto bottom = parse_css_value(context, parts[2]);
  329. auto left = parse_css_value(context, parts[1]);
  330. if (top && right && bottom && left) {
  331. style.set_property(CSS::PropertyID::BorderTopStyle, *top);
  332. style.set_property(CSS::PropertyID::BorderRightStyle, *right);
  333. style.set_property(CSS::PropertyID::BorderBottomStyle, *bottom);
  334. style.set_property(CSS::PropertyID::BorderLeftStyle, *left);
  335. }
  336. } else if (value.is_string() && parts.size() == 2) {
  337. auto vertical = parse_css_value(context, parts[0]);
  338. auto horizontal = parse_css_value(context, parts[1]);
  339. if (vertical && horizontal) {
  340. style.set_property(CSS::PropertyID::BorderTopStyle, *vertical);
  341. style.set_property(CSS::PropertyID::BorderRightStyle, *horizontal);
  342. style.set_property(CSS::PropertyID::BorderBottomStyle, *vertical);
  343. style.set_property(CSS::PropertyID::BorderLeftStyle, *horizontal);
  344. }
  345. } else {
  346. style.set_property(CSS::PropertyID::BorderTopStyle, value);
  347. style.set_property(CSS::PropertyID::BorderRightStyle, value);
  348. style.set_property(CSS::PropertyID::BorderBottomStyle, value);
  349. style.set_property(CSS::PropertyID::BorderLeftStyle, value);
  350. }
  351. return;
  352. }
  353. if (property_id == CSS::PropertyID::BorderWidth) {
  354. auto parts = split_on_whitespace(value.to_string());
  355. if (value.is_string() && parts.size() == 2) {
  356. auto vertical_border_width = parse_css_value(context, parts[0]);
  357. auto horizontal_border_width = parse_css_value(context, parts[1]);
  358. if (vertical_border_width && horizontal_border_width) {
  359. style.set_property(CSS::PropertyID::BorderTopWidth, *vertical_border_width);
  360. style.set_property(CSS::PropertyID::BorderRightWidth, *horizontal_border_width);
  361. style.set_property(CSS::PropertyID::BorderBottomWidth, *vertical_border_width);
  362. style.set_property(CSS::PropertyID::BorderLeftWidth, *horizontal_border_width);
  363. }
  364. } else {
  365. style.set_property(CSS::PropertyID::BorderTopWidth, value);
  366. style.set_property(CSS::PropertyID::BorderRightWidth, value);
  367. style.set_property(CSS::PropertyID::BorderBottomWidth, value);
  368. style.set_property(CSS::PropertyID::BorderLeftWidth, value);
  369. }
  370. return;
  371. }
  372. if (property_id == CSS::PropertyID::BorderColor) {
  373. auto parts = split_on_whitespace(value.to_string());
  374. if (value.is_string() && parts.size() == 4) {
  375. auto top = parse_css_value(context, parts[0]);
  376. auto right = parse_css_value(context, parts[1]);
  377. auto bottom = parse_css_value(context, parts[2]);
  378. auto left = parse_css_value(context, parts[3]);
  379. if (top && right && bottom && left) {
  380. style.set_property(CSS::PropertyID::BorderTopColor, *top);
  381. style.set_property(CSS::PropertyID::BorderRightColor, *right);
  382. style.set_property(CSS::PropertyID::BorderBottomColor, *bottom);
  383. style.set_property(CSS::PropertyID::BorderLeftColor, *left);
  384. }
  385. } else {
  386. style.set_property(CSS::PropertyID::BorderTopColor, value);
  387. style.set_property(CSS::PropertyID::BorderRightColor, value);
  388. style.set_property(CSS::PropertyID::BorderBottomColor, value);
  389. style.set_property(CSS::PropertyID::BorderLeftColor, value);
  390. }
  391. return;
  392. }
  393. if (property_id == CSS::PropertyID::Background) {
  394. if (value.is_identifier() && static_cast<const IdentifierStyleValue&>(value).id() == CSS::ValueID::None) {
  395. style.set_property(CSS::PropertyID::BackgroundColor, ColorStyleValue::create(Color::Transparent));
  396. return;
  397. }
  398. auto parts = split_on_whitespace(value.to_string());
  399. NonnullRefPtrVector<StyleValue> values;
  400. for (auto& part : parts) {
  401. auto value = parse_css_value(context, part);
  402. if (!value)
  403. return;
  404. values.append(value.release_nonnull());
  405. }
  406. // HACK: Disallow more than one color value in a 'background' shorthand
  407. size_t color_value_count = 0;
  408. for (auto& value : values)
  409. color_value_count += value.is_color();
  410. if (values[0].is_color() && color_value_count == 1)
  411. style.set_property(CSS::PropertyID::BackgroundColor, values[0]);
  412. for (auto it = values.begin(); it != values.end(); ++it) {
  413. auto& value = *it;
  414. if (is_background_repeat_property(value)) {
  415. if ((it + 1 != values.end()) && is_background_repeat_property(*(it + 1))) {
  416. ++it;
  417. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundRepeatX, value, document, true);
  418. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundRepeatY, *it, document, true);
  419. } else {
  420. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundRepeat, value, document);
  421. }
  422. }
  423. if (!value.is_string())
  424. continue;
  425. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundImage, value, document);
  426. }
  427. return;
  428. }
  429. if (property_id == CSS::PropertyID::BackgroundImage) {
  430. if (!value.is_string())
  431. return;
  432. auto string = value.to_string();
  433. if (!string.starts_with("url("))
  434. return;
  435. if (!string.ends_with(')'))
  436. return;
  437. auto url = string.substring_view(4, string.length() - 5);
  438. if (url.length() >= 2 && url.starts_with('"') && url.ends_with('"'))
  439. url = url.substring_view(1, url.length() - 2);
  440. else if (url.length() >= 2 && url.starts_with('\'') && url.ends_with('\''))
  441. url = url.substring_view(1, url.length() - 2);
  442. auto background_image_value = ImageStyleValue::create(document.complete_url(url), document);
  443. style.set_property(CSS::PropertyID::BackgroundImage, move(background_image_value));
  444. return;
  445. }
  446. if (property_id == CSS::PropertyID::BackgroundRepeat) {
  447. auto parts = split_on_whitespace(value.to_string());
  448. NonnullRefPtrVector<StyleValue> values;
  449. for (auto& part : parts) {
  450. auto value = parse_css_value(context, part);
  451. if (!value || !is_background_repeat_property(*value))
  452. return;
  453. values.append(value.release_nonnull());
  454. }
  455. if (values.size() == 1) {
  456. auto value_id = values[0].to_identifier();
  457. if (value_id == CSS::ValueID::RepeatX || value_id == CSS::ValueID::RepeatY) {
  458. auto repeat_x = IdentifierStyleValue::create(value_id == CSS::ValueID::RepeatX ? CSS::ValueID::Repeat : CSS::ValueID::NoRepeat);
  459. auto repeat_y = IdentifierStyleValue::create(value_id == CSS::ValueID::RepeatX ? CSS::ValueID::NoRepeat : CSS::ValueID::Repeat);
  460. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundRepeatX, repeat_x, document, true);
  461. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundRepeatY, repeat_y, document, true);
  462. } else {
  463. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundRepeatX, values[0], document, true);
  464. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundRepeatY, values[0], document, true);
  465. }
  466. } else if (values.size() == 2) {
  467. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundRepeatX, values[0], document, true);
  468. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundRepeatY, values[1], document, true);
  469. }
  470. return;
  471. }
  472. if (property_id == CSS::PropertyID::BackgroundRepeatX || property_id == CSS::PropertyID::BackgroundRepeatY) {
  473. auto value_id = value.to_identifier();
  474. if (value_id == CSS::ValueID::RepeatX || value_id == CSS::ValueID::RepeatY)
  475. return;
  476. style.set_property(property_id, value);
  477. return;
  478. }
  479. if (property_id == CSS::PropertyID::Margin) {
  480. if (value.is_length()) {
  481. style.set_property(CSS::PropertyID::MarginTop, value);
  482. style.set_property(CSS::PropertyID::MarginRight, value);
  483. style.set_property(CSS::PropertyID::MarginBottom, value);
  484. style.set_property(CSS::PropertyID::MarginLeft, value);
  485. return;
  486. }
  487. if (value.is_string()) {
  488. auto parts = split_on_whitespace(value.to_string());
  489. if (value.is_string() && parts.size() == 2) {
  490. auto vertical = parse_css_value(context, parts[0]);
  491. auto horizontal = parse_css_value(context, parts[1]);
  492. if (vertical && horizontal) {
  493. style.set_property(CSS::PropertyID::MarginTop, *vertical);
  494. style.set_property(CSS::PropertyID::MarginBottom, *vertical);
  495. style.set_property(CSS::PropertyID::MarginLeft, *horizontal);
  496. style.set_property(CSS::PropertyID::MarginRight, *horizontal);
  497. }
  498. return;
  499. }
  500. if (value.is_string() && parts.size() == 3) {
  501. auto top = parse_css_value(context, parts[0]);
  502. auto horizontal = parse_css_value(context, parts[1]);
  503. auto bottom = parse_css_value(context, parts[2]);
  504. if (top && horizontal && bottom) {
  505. style.set_property(CSS::PropertyID::MarginTop, *top);
  506. style.set_property(CSS::PropertyID::MarginBottom, *bottom);
  507. style.set_property(CSS::PropertyID::MarginLeft, *horizontal);
  508. style.set_property(CSS::PropertyID::MarginRight, *horizontal);
  509. }
  510. return;
  511. }
  512. if (value.is_string() && parts.size() == 4) {
  513. auto top = parse_css_value(context, parts[0]);
  514. auto right = parse_css_value(context, parts[1]);
  515. auto bottom = parse_css_value(context, parts[2]);
  516. auto left = parse_css_value(context, parts[3]);
  517. if (top && right && bottom && left) {
  518. style.set_property(CSS::PropertyID::MarginTop, *top);
  519. style.set_property(CSS::PropertyID::MarginBottom, *bottom);
  520. style.set_property(CSS::PropertyID::MarginLeft, *left);
  521. style.set_property(CSS::PropertyID::MarginRight, *right);
  522. }
  523. return;
  524. }
  525. dbgln("Unsure what to do with CSS margin value '{}'", value.to_string());
  526. return;
  527. }
  528. return;
  529. }
  530. if (property_id == CSS::PropertyID::Padding) {
  531. if (value.is_length()) {
  532. style.set_property(CSS::PropertyID::PaddingTop, value);
  533. style.set_property(CSS::PropertyID::PaddingRight, value);
  534. style.set_property(CSS::PropertyID::PaddingBottom, value);
  535. style.set_property(CSS::PropertyID::PaddingLeft, value);
  536. return;
  537. }
  538. if (value.is_string()) {
  539. auto parts = split_on_whitespace(value.to_string());
  540. if (value.is_string() && parts.size() == 2) {
  541. auto vertical = parse_css_value(context, parts[0]);
  542. auto horizontal = parse_css_value(context, parts[1]);
  543. if (vertical && horizontal) {
  544. style.set_property(CSS::PropertyID::PaddingTop, *vertical);
  545. style.set_property(CSS::PropertyID::PaddingBottom, *vertical);
  546. style.set_property(CSS::PropertyID::PaddingLeft, *horizontal);
  547. style.set_property(CSS::PropertyID::PaddingRight, *horizontal);
  548. }
  549. return;
  550. }
  551. if (value.is_string() && parts.size() == 3) {
  552. auto top = parse_css_value(context, parts[0]);
  553. auto horizontal = parse_css_value(context, parts[1]);
  554. auto bottom = parse_css_value(context, parts[2]);
  555. if (top && bottom && horizontal) {
  556. style.set_property(CSS::PropertyID::PaddingTop, *top);
  557. style.set_property(CSS::PropertyID::PaddingBottom, *bottom);
  558. style.set_property(CSS::PropertyID::PaddingLeft, *horizontal);
  559. style.set_property(CSS::PropertyID::PaddingRight, *horizontal);
  560. }
  561. return;
  562. }
  563. if (value.is_string() && parts.size() == 4) {
  564. auto top = parse_css_value(context, parts[0]);
  565. auto right = parse_css_value(context, parts[1]);
  566. auto bottom = parse_css_value(context, parts[2]);
  567. auto left = parse_css_value(context, parts[3]);
  568. if (top && bottom && left && right) {
  569. style.set_property(CSS::PropertyID::PaddingTop, *top);
  570. style.set_property(CSS::PropertyID::PaddingBottom, *bottom);
  571. style.set_property(CSS::PropertyID::PaddingLeft, *left);
  572. style.set_property(CSS::PropertyID::PaddingRight, *right);
  573. }
  574. return;
  575. }
  576. dbgln("Unsure what to do with CSS padding value '{}'", value.to_string());
  577. return;
  578. }
  579. return;
  580. }
  581. if (property_id == CSS::PropertyID::ListStyle) {
  582. auto parts = split_on_whitespace(value.to_string());
  583. if (!parts.is_empty()) {
  584. auto value = parse_css_value(context, parts[0]);
  585. if (!value)
  586. return;
  587. style.set_property(CSS::PropertyID::ListStyleType, value.release_nonnull());
  588. }
  589. return;
  590. }
  591. // FIXME: parse other values as well
  592. if (property_id == CSS::PropertyID::Font) {
  593. auto parts = split_on_whitespace(value.to_string());
  594. if (parts.size() < 2)
  595. return;
  596. auto size_parts = parts[0].split_view('/');
  597. if (size_parts.size() == 2) {
  598. auto size = parse_css_value(context, size_parts[0]);
  599. auto line_height = parse_css_value(context, size_parts[1]);
  600. if (!size || !line_height)
  601. return;
  602. style.set_property(CSS::PropertyID::FontSize, size.release_nonnull());
  603. style.set_property(CSS::PropertyID::LineHeight, line_height.release_nonnull());
  604. } else if (size_parts.size() == 1) {
  605. auto size = parse_css_value(context, parts[0]);
  606. if (!size)
  607. return;
  608. style.set_property(CSS::PropertyID::FontSize, size.release_nonnull());
  609. }
  610. auto family = parse_css_value(context, parts[1]);
  611. style.set_property(CSS::PropertyID::FontFamily, family.release_nonnull());
  612. return;
  613. }
  614. style.set_property(property_id, value);
  615. }
  616. NonnullRefPtr<StyleProperties> StyleResolver::resolve_style(const DOM::Element& element) const
  617. {
  618. auto style = StyleProperties::create();
  619. if (auto* parent_style = element.parent_element() ? element.parent_element()->specified_css_values() : nullptr) {
  620. parent_style->for_each_property([&](auto property_id, auto& value) {
  621. if (is_inherited_property(property_id))
  622. set_property_expanding_shorthands(style, property_id, value, m_document);
  623. });
  624. }
  625. element.apply_presentational_hints(*style);
  626. auto matching_rules = collect_matching_rules(element);
  627. sort_matching_rules(matching_rules);
  628. for (auto& match : matching_rules) {
  629. for (auto& property : match.rule->declaration().properties()) {
  630. set_property_expanding_shorthands(style, property.property_id, property.value, m_document);
  631. }
  632. }
  633. if (auto* inline_style = element.inline_style()) {
  634. for (auto& property : inline_style->properties()) {
  635. set_property_expanding_shorthands(style, property.property_id, property.value, m_document);
  636. }
  637. }
  638. return style;
  639. }
  640. }