StyleResolver.cpp 39 KB

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