StyleResolver.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  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, selector.specificity() });
  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::BorderRadius) {
  240. // FIXME: Allow for two values per corner to support elliptical radii.
  241. // FIXME: Add support the '/' to specify elliptical radii.
  242. if (value.is_length()) {
  243. style.set_property(CSS::PropertyID::BorderTopLeftRadius, value);
  244. style.set_property(CSS::PropertyID::BorderTopRightRadius, value);
  245. style.set_property(CSS::PropertyID::BorderBottomRightRadius, value);
  246. style.set_property(CSS::PropertyID::BorderBottomLeftRadius, value);
  247. return;
  248. }
  249. if (value.is_string()) {
  250. auto parts = split_on_whitespace(value.to_string());
  251. if (value.is_string() && parts.size() == 2) {
  252. auto diagonal1 = parse_css_value(context, parts[0]);
  253. auto diagonal2 = parse_css_value(context, parts[1]);
  254. if (diagonal1 && diagonal2) {
  255. style.set_property(CSS::PropertyID::BorderTopLeftRadius, *diagonal1);
  256. style.set_property(CSS::PropertyID::BorderBottomRightRadius, *diagonal1);
  257. style.set_property(CSS::PropertyID::BorderTopRightRadius, *diagonal2);
  258. style.set_property(CSS::PropertyID::BorderBottomLeftRadius, *diagonal2);
  259. }
  260. return;
  261. }
  262. if (value.is_string() && parts.size() == 3) {
  263. auto top_left = parse_css_value(context, parts[0]);
  264. auto diagonal = parse_css_value(context, parts[1]);
  265. auto bottom_right = parse_css_value(context, parts[2]);
  266. if (top_left && diagonal && bottom_right) {
  267. style.set_property(CSS::PropertyID::BorderTopLeftRadius, *top_left);
  268. style.set_property(CSS::PropertyID::BorderBottomRightRadius, *bottom_right);
  269. style.set_property(CSS::PropertyID::BorderTopRightRadius, *diagonal);
  270. style.set_property(CSS::PropertyID::BorderBottomLeftRadius, *diagonal);
  271. }
  272. return;
  273. }
  274. if (value.is_string() && parts.size() == 4) {
  275. auto top_left = parse_css_value(context, parts[0]);
  276. auto top_right = parse_css_value(context, parts[1]);
  277. auto bottom_right = parse_css_value(context, parts[2]);
  278. auto bottom_left = parse_css_value(context, parts[3]);
  279. if (top_left && top_right && bottom_right && bottom_left) {
  280. style.set_property(CSS::PropertyID::BorderTopLeftRadius, *top_left);
  281. style.set_property(CSS::PropertyID::BorderBottomRightRadius, *bottom_right);
  282. style.set_property(CSS::PropertyID::BorderTopRightRadius, *top_right);
  283. style.set_property(CSS::PropertyID::BorderBottomLeftRadius, *bottom_left);
  284. }
  285. return;
  286. }
  287. dbgln("Unsure what to do with CSS border-radius value '{}'", value.to_string());
  288. return;
  289. }
  290. return;
  291. }
  292. if (property_id == CSS::PropertyID::Flex) {
  293. if (value.is_length() || (value.is_identifier() && value.to_identifier() == CSS::ValueID::Content)) {
  294. style.set_property(CSS::PropertyID::FlexBasis, value);
  295. return;
  296. }
  297. if (!value.is_string())
  298. return;
  299. auto parts = split_on_whitespace(value.to_string());
  300. if (parts.size() == 1) {
  301. // FIXME: Parse float value here.
  302. // FIXME: Maybe add NumericStyleValue or sth for that.
  303. // Also extend parse_css_value.
  304. style.set_property(CSS::PropertyID::FlexGrow, ""sv);
  305. return;
  306. }
  307. if (parts.size() == 2) {
  308. // FIXME: Parse float value from parts[0] here.
  309. style.set_property(CSS::PropertyID::FlexGrow, ""sv);
  310. auto second_value = parse_css_value(context, parts[1]);
  311. if (second_value->is_length() || (second_value->is_identifier() && second_value->to_identifier() == CSS::ValueID::Content)) {
  312. style.set_property(CSS::PropertyID::FlexBasis, *second_value);
  313. } else {
  314. // FIXME: Parse float value from parts[1] here.
  315. style.set_property(CSS::PropertyID::FlexShrink, ""sv);
  316. }
  317. return;
  318. }
  319. if (parts.size() == 3) {
  320. // FIXME: Parse float value from parts[0] here.
  321. style.set_property(CSS::PropertyID::FlexGrow, ""sv);
  322. // FIXME: Parse float value from parts[1] here.
  323. style.set_property(CSS::PropertyID::FlexShrink, ""sv);
  324. auto third_value = parse_css_value(context, parts[2]);
  325. if (third_value->is_length() || (third_value->is_identifier() && third_value->to_identifier() == CSS::ValueID::Content))
  326. style.set_property(CSS::PropertyID::FlexBasis, *third_value);
  327. return;
  328. }
  329. dbgln("Unsure what to do with CSS flex value '{}'", value.to_string());
  330. return;
  331. }
  332. if (property_id == CSS::PropertyID::BorderTop
  333. || property_id == CSS::PropertyID::BorderRight
  334. || property_id == CSS::PropertyID::BorderBottom
  335. || property_id == CSS::PropertyID::BorderLeft) {
  336. Edge edge = Edge::All;
  337. switch (property_id) {
  338. case CSS::PropertyID::BorderTop:
  339. edge = Edge::Top;
  340. break;
  341. case CSS::PropertyID::BorderRight:
  342. edge = Edge::Right;
  343. break;
  344. case CSS::PropertyID::BorderBottom:
  345. edge = Edge::Bottom;
  346. break;
  347. case CSS::PropertyID::BorderLeft:
  348. edge = Edge::Left;
  349. break;
  350. default:
  351. break;
  352. }
  353. auto parts = split_on_whitespace(value.to_string());
  354. if (value.is_length()) {
  355. set_property_border_width(style, value, edge);
  356. return;
  357. }
  358. if (value.is_color()) {
  359. set_property_border_color(style, value, edge);
  360. return;
  361. }
  362. if (value.is_string()) {
  363. auto parts = split_on_whitespace(value.to_string());
  364. if (parts.size() == 1) {
  365. if (auto value = parse_line_style(context, parts[0])) {
  366. set_property_border_style(style, value.release_nonnull(), edge);
  367. set_property_border_color(style, ColorStyleValue::create(Gfx::Color::Black), edge);
  368. set_property_border_width(style, LengthStyleValue::create(Length(3, Length::Type::Px)), edge);
  369. return;
  370. }
  371. }
  372. RefPtr<LengthStyleValue> line_width_value;
  373. RefPtr<ColorStyleValue> color_value;
  374. RefPtr<IdentifierStyleValue> line_style_value;
  375. for (auto& part : parts) {
  376. if (auto value = parse_line_width(context, part)) {
  377. if (line_width_value)
  378. return;
  379. line_width_value = move(value);
  380. continue;
  381. }
  382. if (auto value = parse_color(context, part)) {
  383. if (color_value)
  384. return;
  385. color_value = move(value);
  386. continue;
  387. }
  388. if (auto value = parse_line_style(context, part)) {
  389. if (line_style_value)
  390. return;
  391. line_style_value = move(value);
  392. continue;
  393. }
  394. }
  395. if (line_width_value)
  396. set_property_border_width(style, line_width_value.release_nonnull(), edge);
  397. if (color_value)
  398. set_property_border_color(style, color_value.release_nonnull(), edge);
  399. if (line_style_value)
  400. set_property_border_style(style, line_style_value.release_nonnull(), edge);
  401. return;
  402. }
  403. return;
  404. }
  405. if (property_id == CSS::PropertyID::BorderStyle) {
  406. auto parts = split_on_whitespace(value.to_string());
  407. if (value.is_string() && parts.size() == 4) {
  408. auto top = parse_css_value(context, parts[0]);
  409. auto right = parse_css_value(context, parts[1]);
  410. auto bottom = parse_css_value(context, parts[2]);
  411. auto left = parse_css_value(context, parts[3]);
  412. if (top && right && bottom && left) {
  413. style.set_property(CSS::PropertyID::BorderTopStyle, *top);
  414. style.set_property(CSS::PropertyID::BorderRightStyle, *right);
  415. style.set_property(CSS::PropertyID::BorderBottomStyle, *bottom);
  416. style.set_property(CSS::PropertyID::BorderLeftStyle, *left);
  417. }
  418. } else if (value.is_string() && parts.size() == 3) {
  419. auto top = parse_css_value(context, parts[0]);
  420. auto right = parse_css_value(context, parts[1]);
  421. auto bottom = parse_css_value(context, parts[2]);
  422. auto left = parse_css_value(context, parts[1]);
  423. if (top && right && bottom && left) {
  424. style.set_property(CSS::PropertyID::BorderTopStyle, *top);
  425. style.set_property(CSS::PropertyID::BorderRightStyle, *right);
  426. style.set_property(CSS::PropertyID::BorderBottomStyle, *bottom);
  427. style.set_property(CSS::PropertyID::BorderLeftStyle, *left);
  428. }
  429. } else if (value.is_string() && parts.size() == 2) {
  430. auto vertical = parse_css_value(context, parts[0]);
  431. auto horizontal = parse_css_value(context, parts[1]);
  432. if (vertical && horizontal) {
  433. style.set_property(CSS::PropertyID::BorderTopStyle, *vertical);
  434. style.set_property(CSS::PropertyID::BorderRightStyle, *horizontal);
  435. style.set_property(CSS::PropertyID::BorderBottomStyle, *vertical);
  436. style.set_property(CSS::PropertyID::BorderLeftStyle, *horizontal);
  437. }
  438. } else {
  439. style.set_property(CSS::PropertyID::BorderTopStyle, value);
  440. style.set_property(CSS::PropertyID::BorderRightStyle, value);
  441. style.set_property(CSS::PropertyID::BorderBottomStyle, value);
  442. style.set_property(CSS::PropertyID::BorderLeftStyle, value);
  443. }
  444. return;
  445. }
  446. if (property_id == CSS::PropertyID::BorderWidth) {
  447. auto parts = split_on_whitespace(value.to_string());
  448. if (value.is_string() && parts.size() == 4) {
  449. auto top_border_width = parse_css_value(context, parts[0]);
  450. auto right_border_width = parse_css_value(context, parts[1]);
  451. auto bottom_border_width = parse_css_value(context, parts[2]);
  452. auto left_border_width = parse_css_value(context, parts[3]);
  453. if (top_border_width && right_border_width && bottom_border_width && left_border_width) {
  454. style.set_property(CSS::PropertyID::BorderTopWidth, *top_border_width);
  455. style.set_property(CSS::PropertyID::BorderRightWidth, *right_border_width);
  456. style.set_property(CSS::PropertyID::BorderBottomWidth, *bottom_border_width);
  457. style.set_property(CSS::PropertyID::BorderLeftWidth, *left_border_width);
  458. }
  459. } else if (value.is_string() && parts.size() == 3) {
  460. auto top_border_width = parse_css_value(context, parts[0]);
  461. auto horizontal_border_width = parse_css_value(context, parts[1]);
  462. auto bottom_border_width = parse_css_value(context, parts[2]);
  463. if (top_border_width && horizontal_border_width && bottom_border_width) {
  464. style.set_property(CSS::PropertyID::BorderTopWidth, *top_border_width);
  465. style.set_property(CSS::PropertyID::BorderRightWidth, *horizontal_border_width);
  466. style.set_property(CSS::PropertyID::BorderBottomWidth, *bottom_border_width);
  467. style.set_property(CSS::PropertyID::BorderLeftWidth, *horizontal_border_width);
  468. }
  469. } else if (value.is_string() && parts.size() == 2) {
  470. auto vertical_border_width = parse_css_value(context, parts[0]);
  471. auto horizontal_border_width = parse_css_value(context, parts[1]);
  472. if (vertical_border_width && horizontal_border_width) {
  473. style.set_property(CSS::PropertyID::BorderTopWidth, *vertical_border_width);
  474. style.set_property(CSS::PropertyID::BorderRightWidth, *horizontal_border_width);
  475. style.set_property(CSS::PropertyID::BorderBottomWidth, *vertical_border_width);
  476. style.set_property(CSS::PropertyID::BorderLeftWidth, *horizontal_border_width);
  477. }
  478. } else {
  479. style.set_property(CSS::PropertyID::BorderTopWidth, value);
  480. style.set_property(CSS::PropertyID::BorderRightWidth, value);
  481. style.set_property(CSS::PropertyID::BorderBottomWidth, value);
  482. style.set_property(CSS::PropertyID::BorderLeftWidth, value);
  483. }
  484. return;
  485. }
  486. if (property_id == CSS::PropertyID::BorderColor) {
  487. auto parts = split_on_whitespace(value.to_string());
  488. if (value.is_string() && parts.size() == 4) {
  489. auto top = parse_css_value(context, parts[0]);
  490. auto right = parse_css_value(context, parts[1]);
  491. auto bottom = parse_css_value(context, parts[2]);
  492. auto left = parse_css_value(context, parts[3]);
  493. if (top && right && bottom && left) {
  494. style.set_property(CSS::PropertyID::BorderTopColor, *top);
  495. style.set_property(CSS::PropertyID::BorderRightColor, *right);
  496. style.set_property(CSS::PropertyID::BorderBottomColor, *bottom);
  497. style.set_property(CSS::PropertyID::BorderLeftColor, *left);
  498. }
  499. } else if (value.is_string() && parts.size() == 3) {
  500. auto top = parse_css_value(context, parts[0]);
  501. auto horizontal = parse_css_value(context, parts[1]);
  502. auto bottom = parse_css_value(context, parts[2]);
  503. if (top && horizontal && bottom) {
  504. style.set_property(CSS::PropertyID::BorderTopColor, *top);
  505. style.set_property(CSS::PropertyID::BorderRightColor, *horizontal);
  506. style.set_property(CSS::PropertyID::BorderBottomColor, *bottom);
  507. style.set_property(CSS::PropertyID::BorderLeftColor, *horizontal);
  508. }
  509. } else if (value.is_string() && parts.size() == 2) {
  510. auto vertical = parse_css_value(context, parts[0]);
  511. auto horizontal = parse_css_value(context, parts[1]);
  512. if (vertical && horizontal) {
  513. style.set_property(CSS::PropertyID::BorderTopColor, *vertical);
  514. style.set_property(CSS::PropertyID::BorderRightColor, *horizontal);
  515. style.set_property(CSS::PropertyID::BorderBottomColor, *vertical);
  516. style.set_property(CSS::PropertyID::BorderLeftColor, *horizontal);
  517. }
  518. } else {
  519. style.set_property(CSS::PropertyID::BorderTopColor, value);
  520. style.set_property(CSS::PropertyID::BorderRightColor, value);
  521. style.set_property(CSS::PropertyID::BorderBottomColor, value);
  522. style.set_property(CSS::PropertyID::BorderLeftColor, value);
  523. }
  524. return;
  525. }
  526. if (property_id == CSS::PropertyID::Background) {
  527. if (value.is_identifier() && static_cast<const IdentifierStyleValue&>(value).id() == CSS::ValueID::None) {
  528. style.set_property(CSS::PropertyID::BackgroundColor, ColorStyleValue::create(Color::Transparent));
  529. return;
  530. }
  531. auto parts = split_on_whitespace(value.to_string());
  532. NonnullRefPtrVector<StyleValue> values;
  533. for (auto& part : parts) {
  534. auto value = parse_css_value(context, part);
  535. if (!value)
  536. return;
  537. values.append(value.release_nonnull());
  538. }
  539. // HACK: Disallow more than one color value in a 'background' shorthand
  540. size_t color_value_count = 0;
  541. for (auto& value : values)
  542. color_value_count += value.is_color();
  543. if (values[0].is_color() && color_value_count == 1)
  544. style.set_property(CSS::PropertyID::BackgroundColor, values[0]);
  545. for (auto it = values.begin(); it != values.end(); ++it) {
  546. auto& value = *it;
  547. if (is_background_repeat_property(value)) {
  548. if ((it + 1 != values.end()) && is_background_repeat_property(*(it + 1))) {
  549. ++it;
  550. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundRepeatX, value, document, true);
  551. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundRepeatY, *it, document, true);
  552. } else {
  553. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundRepeat, value, document);
  554. }
  555. }
  556. if (!value.is_string())
  557. continue;
  558. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundImage, value, document);
  559. }
  560. return;
  561. }
  562. if (property_id == CSS::PropertyID::BackgroundImage) {
  563. if (!value.is_string())
  564. return;
  565. auto string = value.to_string();
  566. if (!string.starts_with("url("))
  567. return;
  568. if (!string.ends_with(')'))
  569. return;
  570. auto url = string.substring_view(4, string.length() - 5);
  571. if (url.length() >= 2 && url.starts_with('"') && url.ends_with('"'))
  572. url = url.substring_view(1, url.length() - 2);
  573. else if (url.length() >= 2 && url.starts_with('\'') && url.ends_with('\''))
  574. url = url.substring_view(1, url.length() - 2);
  575. auto background_image_value = ImageStyleValue::create(document.complete_url(url), document);
  576. style.set_property(CSS::PropertyID::BackgroundImage, move(background_image_value));
  577. return;
  578. }
  579. if (property_id == CSS::PropertyID::BackgroundRepeat) {
  580. auto parts = split_on_whitespace(value.to_string());
  581. NonnullRefPtrVector<StyleValue> values;
  582. for (auto& part : parts) {
  583. auto value = parse_css_value(context, part);
  584. if (!value || !is_background_repeat_property(*value))
  585. return;
  586. values.append(value.release_nonnull());
  587. }
  588. if (values.size() == 1) {
  589. auto value_id = values[0].to_identifier();
  590. if (value_id == CSS::ValueID::RepeatX || value_id == CSS::ValueID::RepeatY) {
  591. auto repeat_x = IdentifierStyleValue::create(value_id == CSS::ValueID::RepeatX ? CSS::ValueID::Repeat : CSS::ValueID::NoRepeat);
  592. auto repeat_y = IdentifierStyleValue::create(value_id == CSS::ValueID::RepeatX ? CSS::ValueID::NoRepeat : CSS::ValueID::Repeat);
  593. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundRepeatX, repeat_x, document, true);
  594. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundRepeatY, repeat_y, document, true);
  595. } else {
  596. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundRepeatX, values[0], document, true);
  597. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundRepeatY, values[0], document, true);
  598. }
  599. } else if (values.size() == 2) {
  600. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundRepeatX, values[0], document, true);
  601. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundRepeatY, values[1], document, true);
  602. }
  603. return;
  604. }
  605. if (property_id == CSS::PropertyID::BackgroundRepeatX || property_id == CSS::PropertyID::BackgroundRepeatY) {
  606. auto value_id = value.to_identifier();
  607. if (value_id == CSS::ValueID::RepeatX || value_id == CSS::ValueID::RepeatY)
  608. return;
  609. style.set_property(property_id, value);
  610. return;
  611. }
  612. if (property_id == CSS::PropertyID::Margin) {
  613. if (value.is_length()) {
  614. style.set_property(CSS::PropertyID::MarginTop, value);
  615. style.set_property(CSS::PropertyID::MarginRight, value);
  616. style.set_property(CSS::PropertyID::MarginBottom, value);
  617. style.set_property(CSS::PropertyID::MarginLeft, value);
  618. return;
  619. }
  620. if (value.is_string()) {
  621. auto parts = split_on_whitespace(value.to_string());
  622. if (value.is_string() && parts.size() == 2) {
  623. auto vertical = parse_css_value(context, parts[0]);
  624. auto horizontal = parse_css_value(context, parts[1]);
  625. if (vertical && horizontal) {
  626. style.set_property(CSS::PropertyID::MarginTop, *vertical);
  627. style.set_property(CSS::PropertyID::MarginBottom, *vertical);
  628. style.set_property(CSS::PropertyID::MarginLeft, *horizontal);
  629. style.set_property(CSS::PropertyID::MarginRight, *horizontal);
  630. }
  631. return;
  632. }
  633. if (value.is_string() && parts.size() == 3) {
  634. auto top = parse_css_value(context, parts[0]);
  635. auto horizontal = parse_css_value(context, parts[1]);
  636. auto bottom = parse_css_value(context, parts[2]);
  637. if (top && horizontal && bottom) {
  638. style.set_property(CSS::PropertyID::MarginTop, *top);
  639. style.set_property(CSS::PropertyID::MarginBottom, *bottom);
  640. style.set_property(CSS::PropertyID::MarginLeft, *horizontal);
  641. style.set_property(CSS::PropertyID::MarginRight, *horizontal);
  642. }
  643. return;
  644. }
  645. if (value.is_string() && parts.size() == 4) {
  646. auto top = parse_css_value(context, parts[0]);
  647. auto right = parse_css_value(context, parts[1]);
  648. auto bottom = parse_css_value(context, parts[2]);
  649. auto left = parse_css_value(context, parts[3]);
  650. if (top && right && bottom && left) {
  651. style.set_property(CSS::PropertyID::MarginTop, *top);
  652. style.set_property(CSS::PropertyID::MarginBottom, *bottom);
  653. style.set_property(CSS::PropertyID::MarginLeft, *left);
  654. style.set_property(CSS::PropertyID::MarginRight, *right);
  655. }
  656. return;
  657. }
  658. dbgln("Unsure what to do with CSS margin value '{}'", value.to_string());
  659. return;
  660. }
  661. return;
  662. }
  663. if (property_id == CSS::PropertyID::Padding) {
  664. if (value.is_length()) {
  665. style.set_property(CSS::PropertyID::PaddingTop, value);
  666. style.set_property(CSS::PropertyID::PaddingRight, value);
  667. style.set_property(CSS::PropertyID::PaddingBottom, value);
  668. style.set_property(CSS::PropertyID::PaddingLeft, value);
  669. return;
  670. }
  671. if (value.is_string()) {
  672. auto parts = split_on_whitespace(value.to_string());
  673. if (value.is_string() && parts.size() == 2) {
  674. auto vertical = parse_css_value(context, parts[0]);
  675. auto horizontal = parse_css_value(context, parts[1]);
  676. if (vertical && horizontal) {
  677. style.set_property(CSS::PropertyID::PaddingTop, *vertical);
  678. style.set_property(CSS::PropertyID::PaddingBottom, *vertical);
  679. style.set_property(CSS::PropertyID::PaddingLeft, *horizontal);
  680. style.set_property(CSS::PropertyID::PaddingRight, *horizontal);
  681. }
  682. return;
  683. }
  684. if (value.is_string() && parts.size() == 3) {
  685. auto top = parse_css_value(context, parts[0]);
  686. auto horizontal = parse_css_value(context, parts[1]);
  687. auto bottom = parse_css_value(context, parts[2]);
  688. if (top && bottom && horizontal) {
  689. style.set_property(CSS::PropertyID::PaddingTop, *top);
  690. style.set_property(CSS::PropertyID::PaddingBottom, *bottom);
  691. style.set_property(CSS::PropertyID::PaddingLeft, *horizontal);
  692. style.set_property(CSS::PropertyID::PaddingRight, *horizontal);
  693. }
  694. return;
  695. }
  696. if (value.is_string() && parts.size() == 4) {
  697. auto top = parse_css_value(context, parts[0]);
  698. auto right = parse_css_value(context, parts[1]);
  699. auto bottom = parse_css_value(context, parts[2]);
  700. auto left = parse_css_value(context, parts[3]);
  701. if (top && bottom && left && right) {
  702. style.set_property(CSS::PropertyID::PaddingTop, *top);
  703. style.set_property(CSS::PropertyID::PaddingBottom, *bottom);
  704. style.set_property(CSS::PropertyID::PaddingLeft, *left);
  705. style.set_property(CSS::PropertyID::PaddingRight, *right);
  706. }
  707. return;
  708. }
  709. dbgln("Unsure what to do with CSS padding value '{}'", value.to_string());
  710. return;
  711. }
  712. return;
  713. }
  714. if (property_id == CSS::PropertyID::ListStyle) {
  715. auto parts = split_on_whitespace(value.to_string());
  716. if (!parts.is_empty()) {
  717. auto value = parse_css_value(context, parts[0]);
  718. if (!value)
  719. return;
  720. style.set_property(CSS::PropertyID::ListStyleType, value.release_nonnull());
  721. }
  722. return;
  723. }
  724. // FIXME: parse other values as well
  725. if (property_id == CSS::PropertyID::Font) {
  726. auto parts = split_on_whitespace(value.to_string());
  727. if (parts.size() < 2)
  728. return;
  729. auto size_parts = parts[0].split_view('/');
  730. if (size_parts.size() == 2) {
  731. auto size = parse_css_value(context, size_parts[0]);
  732. auto line_height = parse_css_value(context, size_parts[1]);
  733. if (!size || !line_height)
  734. return;
  735. style.set_property(CSS::PropertyID::FontSize, size.release_nonnull());
  736. style.set_property(CSS::PropertyID::LineHeight, line_height.release_nonnull());
  737. } else if (size_parts.size() == 1) {
  738. auto size = parse_css_value(context, parts[0]);
  739. if (!size)
  740. return;
  741. style.set_property(CSS::PropertyID::FontSize, size.release_nonnull());
  742. }
  743. auto family = parse_css_value(context, parts[1]);
  744. style.set_property(CSS::PropertyID::FontFamily, family.release_nonnull());
  745. return;
  746. }
  747. if (property_id == CSS::PropertyID::FlexFlow) {
  748. auto parts = split_on_whitespace(value.to_string());
  749. if (parts.size() == 0)
  750. return;
  751. auto direction = parse_css_value(context, parts[0]);
  752. style.set_property(CSS::PropertyID::FlexDirection, direction.release_nonnull());
  753. if (parts.size() > 1) {
  754. auto wrap = parse_css_value(context, parts[1]);
  755. style.set_property(CSS::PropertyID::FlexWrap, wrap.release_nonnull());
  756. }
  757. return;
  758. }
  759. style.set_property(property_id, value);
  760. }
  761. StyleResolver::CustomPropertyResolutionTuple StyleResolver::resolve_custom_property_with_specificity(DOM::Element& element, const String& custom_property_name) const
  762. {
  763. if (auto maybe_property = element.resolve_custom_property(custom_property_name); maybe_property.has_value())
  764. return maybe_property.value();
  765. auto parent_element = element.parent_element();
  766. CustomPropertyResolutionTuple parent_resolved {};
  767. if (parent_element)
  768. parent_resolved = resolve_custom_property_with_specificity(*parent_element, custom_property_name);
  769. auto matching_rules = collect_matching_rules(element);
  770. sort_matching_rules(matching_rules);
  771. for (int i = matching_rules.size() - 1; i >= 0; --i) {
  772. auto& match = matching_rules[i];
  773. if (match.specificity < parent_resolved.specificity)
  774. continue;
  775. auto custom_property_style = match.rule->declaration().custom_property(custom_property_name);
  776. if (custom_property_style.has_value()) {
  777. element.add_custom_property(custom_property_name, { custom_property_style.value(), match.specificity });
  778. return { custom_property_style.value(), match.specificity };
  779. }
  780. }
  781. return parent_resolved;
  782. }
  783. Optional<StyleProperty> StyleResolver::resolve_custom_property(DOM::Element& element, const String& custom_property_name) const
  784. {
  785. auto resolved_with_specificity = resolve_custom_property_with_specificity(element, custom_property_name);
  786. return resolved_with_specificity.style;
  787. }
  788. NonnullRefPtr<StyleProperties> StyleResolver::resolve_style(DOM::Element& element) const
  789. {
  790. auto style = StyleProperties::create();
  791. if (auto* parent_style = element.parent_element() ? element.parent_element()->specified_css_values() : nullptr) {
  792. parent_style->for_each_property([&](auto property_id, auto& value) {
  793. if (is_inherited_property(property_id))
  794. set_property_expanding_shorthands(style, property_id, value, m_document);
  795. });
  796. }
  797. element.apply_presentational_hints(*style);
  798. auto matching_rules = collect_matching_rules(element);
  799. sort_matching_rules(matching_rules);
  800. for (auto& match : matching_rules) {
  801. for (auto& property : match.rule->declaration().properties()) {
  802. auto property_value = property.value;
  803. if (property.value->is_custom_property()) {
  804. auto prop = reinterpret_cast<const CSS::CustomStyleValue*>(property.value.ptr());
  805. auto custom_prop_name = prop->custom_property_name();
  806. auto resolved = resolve_custom_property(element, custom_prop_name);
  807. if (resolved.has_value()) {
  808. property_value = resolved.value().value;
  809. }
  810. }
  811. set_property_expanding_shorthands(style, property.property_id, property_value, m_document);
  812. }
  813. }
  814. if (auto* inline_style = element.inline_style()) {
  815. for (auto& property : inline_style->properties()) {
  816. set_property_expanding_shorthands(style, property.property_id, property.value, m_document);
  817. }
  818. }
  819. return style;
  820. }
  821. }